Use etag to generate ETag headers
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 2a0c35a · Browse files · 5 files·+29 added−36 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+29 added−36 removed
- tests:+17 added−15 removed
- other:+12 added−21 removed
Provenance
- Commit
51d33ed- Tree
f965810- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
2a0c35a
5 files changed+29 added−36 removed
modified:lib/utils.js+10 added-21 removed
1 unmodified line2 * Module dependencies.3 */4 5 var mime = require('send').mime;6-var crc32 = require('buffer-crc32');7-var crypto = require('crypto');6 var basename = require('path').basename;7+var etag = require('etag');8 var proxyaddr = require('proxy-addr');9 var qs = require('qs');10 var querystring = require('querystring');11 var typer = require('media-typer');6 unmodified lines18 * @return {String}19 * @api private20 */21 23-exports.etag = function etag(body, encoding){24- if (body.length === 0) {25- // fast-path empty body26- return '"1B2M2Y8AsgTpgAmY7PhCfg=="'27- }22+exports.etag = function (body, encoding) {23+ var buf = !Buffer.isBuffer(body)24+ ? new Buffer(body, encoding)25+ : body26 29- var hash = crypto30- .createHash('md5')31- .update(body, encoding)32- .digest('base64')33- return '"' + hash + '"'27+ return etag(buf, {weak: false})28 };29 30 /**31 * Return weak ETag for `body`.4 unmodified lines36 * @api private37 */38 39 exports.wetag = function wetag(body, encoding){46- if (body.length === 0) {47- // fast-path empty body48- return 'W/"0-0"'49- }40+ var buf = !Buffer.isBuffer(body)41+ ? new Buffer(body, encoding)42+ : body43 51- var buf = Buffer.isBuffer(body)52- ? body53- : new Buffer(body, encoding)54- var len = buf.length55- return 'W/"' + len.toString(16) + '-' + crc32.unsigned(buf) + '"'44+ return etag(buf, {weak: true})45 };46 47 /**48 * Check if `path` looks absolute.243 unmodified linesmodified:test/res.send.js+14 added-12 removed
116 unmodified lines117 it('should set ETag', function(done){118 var app = express();119 120 app.use(function(req, res){121- var str = Array(1024 * 2).join('-');121+ var str = Array(1000).join('-');122 res.send(str);123 });124 125 request(app)126 .get('/')127- .expect('ETag', 'W/"7ff-2796319984"')127+ .expect('ETag', 'W/"3e7-8084ccd1"')128 .end(done);129 })130 131 it('should not set ETag for non-GET/HEAD', function(done){132 var app = express();133 134 app.use(function(req, res){135- var str = Array(1024 * 2).join('-');135+ var str = Array(1000).join('-');136 res.send(str);137 });138 139 request(app)66 unmodified lines206 it('should set ETag', function(done){207 var app = express();208 209 app.use(function(req, res){210- var str = Array(1024 * 2).join('-');210+ var str = Array(1000).join('-');211 res.send(new Buffer(str));212 });213 214 request(app)215 .get('/')216- .expect('ETag', 'W/"7ff-2796319984"')216+ .expect('ETag', 'W/"3e7-8084ccd1"')217 .end(done);218 })219 220 it('should not override Content-Type', function(done){99 unmodified lines320 })321 322 it('should respond with 304 Not Modified when fresh', function(done){323 var app = express();324+ var etag = '"asdf"';325 326 app.use(function(req, res){326- var str = Array(1024 * 2).join('-');327+ var str = Array(1000).join('-');328+ res.set('ETag', etag);329 res.send(str);330 });331 332 request(app)333 .get('/')332- .set('If-None-Match', 'W/"7ff-2796319984"')334+ .set('If-None-Match', etag)335 .expect(304, done);336 })337 338 it('should not perform freshness check unless 2xx or 304', function(done){37 unmodified lines376 app.enable('etag');377 378 request(app)379 .get('/')378- .expect('etag', 'W/"c-1525560792"', done)380+ .expect('etag', 'W/"c-5aee35d8"', done)381 })382 383 it('should send ETag for empty string response', function(done){384 var app = express()12 unmodified lines397 it('should send ETag for long response', function(done){398 var app = express();399 400 app.use(function(req, res){399- var str = Array(1024 * 2).join('-');401+ var str = Array(1000).join('-');402 res.send(str);403 });404 405 app.enable('etag');406 407 request(app)408 .get('/')407- .expect('etag', 'W/"7ff-2796319984"', done)409+ .expect('etag', 'W/"3e7-8084ccd1"', done)410 });411 412 it('should not override ETag when manually set', function(done){413 var app = express();32 unmodified lines446 it('should send no ETag', function(done){447 var app = express();448 449 app.use(function(req, res){448- var str = Array(1024 * 2).join('-');450+ var str = Array(1000).join('-');451 res.send(str);452 });453 454 app.disable('etag');49 unmodified lines504 });505 506 request(app)507 .get('/')506- .expect('etag', 'W/"d-1486392595"', done)508+ .expect('etag', 'W/"d-58988d13"', done)509 })510 })511 512 describe('when a function', function(){41 unmodified linesmodified:test/utils.js+3 added-3 removed
26 unmodified lines272829303131323334353636373839404142424344454627 unmodified lines26 unmodified lines
describe('utils.wetag(body, encoding)', function(){ it('should support strings', function(){ utils.wetag('express!') .should.eql('W/"8-3098196679"') .should.eql('W/"8-b8aabac7"') })
it('should support utf8 strings', function(){ utils.wetag('express❤', 'utf8') .should.eql('W/"a-1751845617"') .should.eql('W/"a-686b0af1"') })
it('should support buffer', function(){ var buf = new Buffer('express!') utils.wetag(buf) .should.eql('W/"8-3098196679"'); .should.eql('W/"8-b8aabac7"'); })
it('should support empty string', function(){ utils.wetag('')27 unmodified linesmodified:History.md+1 added
3 unmodified lines4 * Invoke callback for sendfile when client aborts5 - Applies to `res.sendFile`, `res.sendfile`, and `res.download`6 - `err` will be populated with request aborted error7 * Support IP address host in `req.subdomains`8+ * Use `etag` to generate `ETag` headers9 * deps: accepts@~1.1.010 - update `mime-types`11 * deps: cookie-signature@1.0.512 * deps: debug@~2.0.02210 unmodified linesmodified:package.json+1 added
31 unmodified lines32333435363738394043 unmodified lines31 unmodified lines "cookie-signature": "1.0.5", "debug": "~2.0.0", "depd": "0.4.4", "escape-html": "1.0.1", "etag": "~1.3.0", "finalhandler": "0.2.0", "fresh": "0.2.4", "media-typer": "0.3.0", "methods": "1.1.0",43 unmodified lines