Skip to content

Commit

Permalink
Use etag to generate ETag header
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 5, 2014
1 parent c4fcb5d commit 9afed44
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 27 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Use `etag` to generate `ETag` header

0.8.5 / 2014-09-04
==================

Expand Down
7 changes: 4 additions & 3 deletions lib/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var escapeHtml = require('escape-html')
, normalize = path.normalize
, join = path.join
, utils = require('./utils');
var etag = require('etag')
var EventEmitter = require('events').EventEmitter;
var ms = require('ms');
var onFinished = require('on-finished')
Expand Down Expand Up @@ -718,9 +719,9 @@ SendStream.prototype.setHeader = function setHeader(path, stat){
if (!res.getHeader('Last-Modified')) res.setHeader('Last-Modified', stat.mtime.toUTCString());

if (this._etag && !res.getHeader('ETag')) {
var etag = utils.etag(path, stat);
debug('etag %s', etag);
res.setHeader('ETag', etag);
var val = etag(stat)
debug('etag %s', val)
res.setHeader('ETag', val)
}
};

Expand Down
24 changes: 0 additions & 24 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@

/**
* Module dependencies.
*/

var crypto = require('crypto');

/**
* Return a weak ETag from the given `path` and `stat`.
*
* @param {String} path
* @param {Object} stat
* @return {String}
* @api private
*/

exports.etag = function etag(path, stat) {
var tag = String(stat.mtime.getTime()) + ':' + String(stat.size) + ':' + path;
var str = crypto
.createHash('md5')
.update(tag, 'utf8')
.digest('base64');
return 'W/"' + str + '"';
};

/**
* decodeURIComponent.
*
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"depd": "0.4.4",
"destroy": "1.0.3",
"escape-html": "1.0.1",
"etag": "~1.3.0",
"fresh": "0.2.2",
"mime": "1.2.11",
"ms": "0.6.2",
Expand Down

0 comments on commit 9afed44

Please sign in to comment.