Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merges fix-not-modified-conditions
  • Loading branch information
Pablo Cantero committed Oct 13, 2012
2 parents ddfe906 + f592d8a commit 8de8fa9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/node-static.js
Expand Up @@ -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] }
Expand All @@ -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();
Expand Down
22 changes: 22 additions & 0 deletions test/integration/node-static-test.js
Expand Up @@ -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': {
Expand Down

0 comments on commit 8de8fa9

Please sign in to comment.