Skip to content
Myr

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

5 files+29 added−36 removed

modified:lib/utils.js+10 added-21 removed
1 unmodified line
2 * Module dependencies.3 */45var mime = require('send').mime;6var crc32 = require('buffer-crc32');7var crypto = require('crypto');6var basename = require('path').basename;7var etag = require('etag');8var proxyaddr = require('proxy-addr');9var qs = require('qs');10var querystring = require('querystring');11var typer = require('media-typer');
6 unmodified lines
18 * @return {String}19 * @api private20 */2123exports.etag = function etag(body, encoding){24 if (body.length === 0) {25 // fast-path empty body26 return '"1B2M2Y8AsgTpgAmY7PhCfg=="'27 }22exports.etag = function (body, encoding) {23 var buf = !Buffer.isBuffer(body)24 ? new Buffer(body, encoding)25 : body2629 var hash = crypto30 .createHash('md5')31 .update(body, encoding)32 .digest('base64')33 return '"' + hash + '"'27 return etag(buf, {weak: false})28};2930/**31 * Return weak ETag for `body`.
4 unmodified lines
36 * @api private37 */3839exports.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 : body4351 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};4647/**48 * Check if `path` looks absolute.
243 unmodified lines
modified:test/res.send.js+14 added-12 removed
116 unmodified lines
117 it('should set ETag', function(done){118 var app = express();119120 app.use(function(req, res){121 var str = Array(1024 * 2).join('-');121 var str = Array(1000).join('-');122 res.send(str);123 });124125 request(app)126 .get('/')127 .expect('ETag', 'W/"7ff-2796319984"')127 .expect('ETag', 'W/"3e7-8084ccd1"')128 .end(done);129 })130131 it('should not set ETag for non-GET/HEAD', function(done){132 var app = express();133134 app.use(function(req, res){135 var str = Array(1024 * 2).join('-');135 var str = Array(1000).join('-');136 res.send(str);137 });138139 request(app)
66 unmodified lines
206 it('should set ETag', function(done){207 var app = express();208209 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 });213214 request(app)215 .get('/')216 .expect('ETag', 'W/"7ff-2796319984"')216 .expect('ETag', 'W/"3e7-8084ccd1"')217 .end(done);218 })219220 it('should not override Content-Type', function(done){
99 unmodified lines
320 })321322 it('should respond with 304 Not Modified when fresh', function(done){323 var app = express();324 var etag = '"asdf"';325326 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 });331332 request(app)333 .get('/')332 .set('If-None-Match', 'W/"7ff-2796319984"')334 .set('If-None-Match', etag)335 .expect(304, done);336 })337338 it('should not perform freshness check unless 2xx or 304', function(done){
37 unmodified lines
376 app.enable('etag');377378 request(app)379 .get('/')378 .expect('etag', 'W/"c-1525560792"', done)380 .expect('etag', 'W/"c-5aee35d8"', done)381 })382383 it('should send ETag for empty string response', function(done){384 var app = express()
12 unmodified lines
397 it('should send ETag for long response', function(done){398 var app = express();399400 app.use(function(req, res){399 var str = Array(1024 * 2).join('-');401 var str = Array(1000).join('-');402 res.send(str);403 });404405 app.enable('etag');406407 request(app)408 .get('/')407 .expect('etag', 'W/"7ff-2796319984"', done)409 .expect('etag', 'W/"3e7-8084ccd1"', done)410 });411412 it('should not override ETag when manually set', function(done){413 var app = express();
32 unmodified lines
446 it('should send no ETag', function(done){447 var app = express();448449 app.use(function(req, res){448 var str = Array(1024 * 2).join('-');450 var str = Array(1000).join('-');451 res.send(str);452 });453454 app.disable('etag');
49 unmodified lines
504 });505506 request(app)507 .get('/')506 .expect('etag', 'W/"d-1486392595"', done)508 .expect('etag', 'W/"d-58988d13"', done)509 })510 })511512 describe('when a function', function(){
41 unmodified lines
modified:test/utils.js+3 added-3 removed
modified:History.md+1 added
3 unmodified lines
4 * 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.0
2210 unmodified lines
modified:package.json+1 added