Skip to content
Myr

mark res.send ETag as weak and reduce collisions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 1f2e00e · Browse files · 4 files+17 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+17 added−14 removed

  • tests:+9 added−12 removed
  • other:+8 added−2 removed

4 files+17 added−14 removed

modified:lib/utils.js+7 added-2 removed
45 unmodified lines
46/**47 * Return ETag for `body`.48 *49 * @param {String|Buffer} body50 * @param {String} [encoding]51 * @return {String}52 * @api private53 */5454exports.etag = function(body){55 return '"' + crc32.signed(body) + '"';55exports.etag = function(body, encoding){56 var buf = Buffer.isBuffer(body)57 ? body58 : new Buffer(body, encoding)59 var len = buf.length60 return 'W/"' + len.toString(16) + '-' + crc32.unsigned(buf) + '"'61};6263/**64 * Make `locals()` bound to the given `obj`.
346 unmodified lines
modified:test/res.send.js+5 added-8 removed
103 unmodified lines
104 });105106 request(app)107 .get('/')108 .expect('ETag', '"-1498647312"')108 .expect('ETag', 'W/"7ff-2796319984"')109 .end(done);110 })111112 it('should not set ETag for non-GET/HEAD', function(done){
80 unmodified lines
193 });194195 request(app)196 .get('/')197 .expect('ETag', '"-1498647312"')197 .expect('ETag', 'W/"7ff-2796319984"')198 .end(done);199 })200201 it('should not override Content-Type', function(done){
106 unmodified lines
308 });309310 request(app)311 .get('/')312 .set('If-None-Match', '"-1498647312"')312 .set('If-None-Match', 'W/"7ff-2796319984"')313 .expect(304, done);314 })315316 it('should not perform freshness check unless 2xx or 304', function(done){
40 unmodified lines
357 done();358 });359 })360361 it('should send ETag ', function(done){361 it('should send ETag', function(done){362 var app = express();363364 app.use(function(req, res){365 var str = Array(1024 * 2).join('-');
1 unmodified line
367 });368369 request(app)370 .get('/')371 .end(function(err, res){372 res.headers.should.have.property('etag', '"-1498647312"');373 done();374 });371 .expect('etag', 'W/"7ff-2796319984"', done)372 });373 });374375 describe('when disabled', function(){
38 unmodified lines
modified:test/utils.js+4 added-4 removed
6 unmodified lines
7 var str = 'Hello CRC';8 var strUTF8 = '<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body><p>自動販売</p></body></html>';910 it('should support strings', function(){11 utils.etag(str).should.eql('"-2034458343"');11 utils.etag(str).should.eql('W/"9-2260508953"');12 })1314 it('should support utf8 strings', function(){15 utils.etag(strUTF8).should.eql('"1395090196"');15 utils.etag(strUTF8, 'utf8').should.eql('W/"4d-1395090196"');16 })1718 it('should support buffer', function(){19 utils.etag(new Buffer(strUTF8)).should.eql('"1395090196"');20 utils.etag(new Buffer(str)).should.eql('"-2034458343"');19 utils.etag(new Buffer(strUTF8, 'utf8')).should.eql('W/"4d-1395090196"');20 utils.etag(new Buffer(str)).should.eql('W/"9-2260508953"');21 })2223})24
203 unmodified lines
modified:History.md+1 added
13.x2===34 * Include ETag in HEAD requests5 * mark `res.send` ETag as weak and reduce collisions6 * update connect to 2.18.07 - deps: compression@1.0.38 - deps: serve-index@1.1.09 - deps: serve-static@1.2.0
1363 unmodified lines