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
Provenance
- Commit
8ab96ab- Tree
3ef1b25- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
1f2e00e
4 files changed+17 added−14 removed
modified:lib/utils.js+7 added-2 removed
45 unmodified lines46 /**47 * Return ETag for `body`.48 *49 * @param {String|Buffer} body50+ * @param {String} [encoding]51 * @return {String}52 * @api private53 */54 54-exports.etag = function(body){55- return '"' + crc32.signed(body) + '"';55+exports.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 };62 63 /**64 * Make `locals()` bound to the given `obj`.346 unmodified linesmodified:test/res.send.js+5 added-8 removed
103 unmodified lines104 });105 106 request(app)107 .get('/')108- .expect('ETag', '"-1498647312"')108+ .expect('ETag', 'W/"7ff-2796319984"')109 .end(done);110 })111 112 it('should not set ETag for non-GET/HEAD', function(done){80 unmodified lines193 });194 195 request(app)196 .get('/')197- .expect('ETag', '"-1498647312"')197+ .expect('ETag', 'W/"7ff-2796319984"')198 .end(done);199 })200 201 it('should not override Content-Type', function(done){106 unmodified lines308 });309 310 request(app)311 .get('/')312- .set('If-None-Match', '"-1498647312"')312+ .set('If-None-Match', 'W/"7ff-2796319984"')313 .expect(304, done);314 })315 316 it('should not perform freshness check unless 2xx or 304', function(done){40 unmodified lines357 done();358 });359 })360 361- it('should send ETag ', function(done){361+ it('should send ETag', function(done){362 var app = express();363 364 app.use(function(req, res){365 var str = Array(1024 * 2).join('-');1 unmodified line367 });368 369 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 });374 375 describe('when disabled', function(){38 unmodified linesmodified:test/utils.js+4 added-4 removed
6 unmodified lines7 var str = 'Hello CRC';8 var strUTF8 = '<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body><p>自動販売</p></body></html>';9 10 it('should support strings', function(){11- utils.etag(str).should.eql('"-2034458343"');11+ utils.etag(str).should.eql('W/"9-2260508953"');12 })13 14 it('should support utf8 strings', function(){15- utils.etag(strUTF8).should.eql('"1395090196"');15+ utils.etag(strUTF8, 'utf8').should.eql('W/"4d-1395090196"');16 })17 18 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 })22 23 })24 203 unmodified linesmodified:History.md+1 added
1 3.x2 ===3 4 * 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.01363 unmodified lines