mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 12:19:51 +01:00
Use etag to generate ETag headers
This commit is contained in:
parent
2a0c35a108
commit
51d33edb79
|
|
@ -5,6 +5,7 @@ unreleased
|
|||
- Applies to `res.sendFile`, `res.sendfile`, and `res.download`
|
||||
- `err` will be populated with request aborted error
|
||||
* Support IP address host in `req.subdomains`
|
||||
* Use `etag` to generate `ETag` headers
|
||||
* deps: accepts@~1.1.0
|
||||
- update `mime-types`
|
||||
* deps: cookie-signature@1.0.5
|
||||
|
|
|
|||
31
lib/utils.js
31
lib/utils.js
|
|
@ -3,9 +3,8 @@
|
|||
*/
|
||||
|
||||
var mime = require('send').mime;
|
||||
var crc32 = require('buffer-crc32');
|
||||
var crypto = require('crypto');
|
||||
var basename = require('path').basename;
|
||||
var etag = require('etag');
|
||||
var proxyaddr = require('proxy-addr');
|
||||
var qs = require('qs');
|
||||
var querystring = require('querystring');
|
||||
|
|
@ -20,17 +19,12 @@ var typer = require('media-typer');
|
|||
* @api private
|
||||
*/
|
||||
|
||||
exports.etag = function etag(body, encoding){
|
||||
if (body.length === 0) {
|
||||
// fast-path empty body
|
||||
return '"1B2M2Y8AsgTpgAmY7PhCfg=="'
|
||||
}
|
||||
exports.etag = function (body, encoding) {
|
||||
var buf = !Buffer.isBuffer(body)
|
||||
? new Buffer(body, encoding)
|
||||
: body
|
||||
|
||||
var hash = crypto
|
||||
.createHash('md5')
|
||||
.update(body, encoding)
|
||||
.digest('base64')
|
||||
return '"' + hash + '"'
|
||||
return etag(buf, {weak: false})
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -43,16 +37,11 @@ exports.etag = function etag(body, encoding){
|
|||
*/
|
||||
|
||||
exports.wetag = function wetag(body, encoding){
|
||||
if (body.length === 0) {
|
||||
// fast-path empty body
|
||||
return 'W/"0-0"'
|
||||
}
|
||||
var buf = !Buffer.isBuffer(body)
|
||||
? new Buffer(body, encoding)
|
||||
: body
|
||||
|
||||
var buf = Buffer.isBuffer(body)
|
||||
? body
|
||||
: new Buffer(body, encoding)
|
||||
var len = buf.length
|
||||
return 'W/"' + len.toString(16) + '-' + crc32.unsigned(buf) + '"'
|
||||
return etag(buf, {weak: true})
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@
|
|||
"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",
|
||||
|
|
|
|||
|
|
@ -118,13 +118,13 @@ describe('res', function(){
|
|||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
var str = Array(1024 * 2).join('-');
|
||||
var str = Array(1000).join('-');
|
||||
res.send(str);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('ETag', 'W/"7ff-2796319984"')
|
||||
.expect('ETag', 'W/"3e7-8084ccd1"')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
|
|
@ -132,7 +132,7 @@ describe('res', function(){
|
|||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
var str = Array(1024 * 2).join('-');
|
||||
var str = Array(1000).join('-');
|
||||
res.send(str);
|
||||
});
|
||||
|
||||
|
|
@ -207,13 +207,13 @@ describe('res', function(){
|
|||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
var str = Array(1024 * 2).join('-');
|
||||
var str = Array(1000).join('-');
|
||||
res.send(new Buffer(str));
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('ETag', 'W/"7ff-2796319984"')
|
||||
.expect('ETag', 'W/"3e7-8084ccd1"')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
|
|
@ -321,15 +321,17 @@ describe('res', function(){
|
|||
|
||||
it('should respond with 304 Not Modified when fresh', function(done){
|
||||
var app = express();
|
||||
var etag = '"asdf"';
|
||||
|
||||
app.use(function(req, res){
|
||||
var str = Array(1024 * 2).join('-');
|
||||
var str = Array(1000).join('-');
|
||||
res.set('ETag', etag);
|
||||
res.send(str);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('If-None-Match', 'W/"7ff-2796319984"')
|
||||
.set('If-None-Match', etag)
|
||||
.expect(304, done);
|
||||
})
|
||||
|
||||
|
|
@ -375,7 +377,7 @@ describe('res', function(){
|
|||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('etag', 'W/"c-1525560792"', done)
|
||||
.expect('etag', 'W/"c-5aee35d8"', done)
|
||||
})
|
||||
|
||||
it('should send ETag for empty string response', function(done){
|
||||
|
|
@ -396,7 +398,7 @@ describe('res', function(){
|
|||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
var str = Array(1024 * 2).join('-');
|
||||
var str = Array(1000).join('-');
|
||||
res.send(str);
|
||||
});
|
||||
|
||||
|
|
@ -404,7 +406,7 @@ describe('res', function(){
|
|||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('etag', 'W/"7ff-2796319984"', done)
|
||||
.expect('etag', 'W/"3e7-8084ccd1"', done)
|
||||
});
|
||||
|
||||
it('should not override ETag when manually set', function(done){
|
||||
|
|
@ -445,7 +447,7 @@ describe('res', function(){
|
|||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
var str = Array(1024 * 2).join('-');
|
||||
var str = Array(1000).join('-');
|
||||
res.send(str);
|
||||
});
|
||||
|
||||
|
|
@ -503,7 +505,7 @@ describe('res', function(){
|
|||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('etag', 'W/"d-1486392595"', done)
|
||||
.expect('etag', 'W/"d-58988d13"', done)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -28,18 +28,18 @@ describe('utils.etag(body, encoding)', function(){
|
|||
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(){
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user