mark res.send ETag as weak and reduce collisions

This commit is contained in:
Douglas Christopher Wilson 2014-05-29 23:10:25 -04:00
parent 1f2e00ef8d
commit 8ab96ab80d
4 changed files with 17 additions and 14 deletions

View File

@ -2,6 +2,7 @@
===
* Include ETag in HEAD requests
* mark `res.send` ETag as weak and reduce collisions
* update connect to 2.18.0
- deps: compression@1.0.3
- deps: serve-index@1.1.0

View File

@ -47,12 +47,17 @@ exports.deprecate = function(fn, msg){
* Return ETag for `body`.
*
* @param {String|Buffer} body
* @param {String} [encoding]
* @return {String}
* @api private
*/
exports.etag = function(body){
return '"' + crc32.signed(body) + '"';
exports.etag = function(body, encoding){
var buf = Buffer.isBuffer(body)
? body
: new Buffer(body, encoding)
var len = buf.length
return 'W/"' + len.toString(16) + '-' + crc32.unsigned(buf) + '"'
};
/**

View File

@ -105,7 +105,7 @@ describe('res', function(){
request(app)
.get('/')
.expect('ETag', '"-1498647312"')
.expect('ETag', 'W/"7ff-2796319984"')
.end(done);
})
@ -194,7 +194,7 @@ describe('res', function(){
request(app)
.get('/')
.expect('ETag', '"-1498647312"')
.expect('ETag', 'W/"7ff-2796319984"')
.end(done);
})
@ -309,7 +309,7 @@ describe('res', function(){
request(app)
.get('/')
.set('If-None-Match', '"-1498647312"')
.set('If-None-Match', 'W/"7ff-2796319984"')
.expect(304, done);
})
@ -358,7 +358,7 @@ describe('res', function(){
});
})
it('should send ETag ', function(done){
it('should send ETag', function(done){
var app = express();
app.use(function(req, res){
@ -368,10 +368,7 @@ describe('res', function(){
request(app)
.get('/')
.end(function(err, res){
res.headers.should.have.property('etag', '"-1498647312"');
done();
});
.expect('etag', 'W/"7ff-2796319984"', done)
});
});

View File

@ -8,16 +8,16 @@ describe('utils.etag(body)', function(){
var strUTF8 = '<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body><p>自動販売</p></body></html>';
it('should support strings', function(){
utils.etag(str).should.eql('"-2034458343"');
utils.etag(str).should.eql('W/"9-2260508953"');
})
it('should support utf8 strings', function(){
utils.etag(strUTF8).should.eql('"1395090196"');
utils.etag(strUTF8, 'utf8').should.eql('W/"4d-1395090196"');
})
it('should support buffer', function(){
utils.etag(new Buffer(strUTF8)).should.eql('"1395090196"');
utils.etag(new Buffer(str)).should.eql('"-2034458343"');
utils.etag(new Buffer(strUTF8, 'utf8')).should.eql('W/"4d-1395090196"');
utils.etag(new Buffer(str)).should.eql('W/"9-2260508953"');
})
})