Permalink
Browse files

Merges fix-not-modified-conditions

  • Loading branch information...
2 parents ddfe906 + f592d8a commit 8de8fa96f7ec3975a19b4be993fdde06af1eef82 @phstc phstc committed Oct 13, 2012
Showing with 28 additions and 3 deletions.
  1. +6 −3 lib/node-static.js
  2. +22 −0 test/integration/node-static-test.js
View
9 lib/node-static.js
@@ -187,7 +187,9 @@ exports.Server.prototype.serve = function (req, res, callback) {
exports.Server.prototype.respond = function (pathname, status, _headers, files, stat, req, res, finish) {
var mtime = Date.parse(stat.mtime),
key = pathname || files[0],
- headers = {};
+ headers = {},
+ clientETag = req.headers['if-none-match'],
+ clientMTime = Date.parse(req.headers['if-modified-since']);
// Copy default headers
for (var k in this.options.headers) { headers[k] = this.options.headers[k] }
@@ -199,8 +201,9 @@ exports.Server.prototype.respond = function (pathname, status, _headers, files,
// Conditional GET
// If the "If-Modified-Since" or "If-None-Match" headers
// match the conditions, send a 304 Not Modified.
- if (req.headers['if-none-match'] === headers['ETag'] ||
- Date.parse(req.headers['if-modified-since']) >= mtime) {
+ if ((clientMTime || clientETag) &&
+ (!clientETag || clientETag === headers['ETag']) &&
+ (!clientMTime || clientMTime >= mtime)) {
finish(304, headers);
} else {
var fileExtension = path.extname(files[0]).slice(1).toLowerCase();
View
22 test/integration/node-static-test.js
@@ -90,6 +90,28 @@ suite.addBatch({
'should respond with 304' : function(error, response, body){
assert.equal(response.statusCode, 304);
}
+ },
+ 'requesting with If-None-Match and If-Modified-Since': {
+ topic : function(){
+ var _this = this;
+ request.get(TEST_SERVER + '/index.html', function(error, response, body){
+ var modified = Date.parse(response.headers['last-modified']);
+ var oneDayLater = new Date(modified + (24 * 60 * 60 * 1000)).toUTCString();
+ var nonMatchingEtag = '1111222233334444';
+ request({
+ method: 'GET',
+ uri: TEST_SERVER + '/index.html',
+ headers: {
+ 'if-none-match': nonMatchingEtag,
+ 'if-modified-since': oneDayLater
+ }
+ },
+ _this.callback);
+ });
+ },
+ 'should respond with a 200': function(error, response, body){
+ assert.equal(response.statusCode, 200);
+ }
}
}).addBatch({
'requesting HEAD': {

0 comments on commit 8de8fa9

Please sign in to comment.