Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for etag and last-modified headers #258

Merged
merged 3 commits into from
Nov 20, 2012
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/response/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internals.File.prototype._prepare = function (callback) {
self.headers['Content-Type'] = Mime.lookup(self._filePath) || 'application/octet-stream';
self.headers['Content-Length'] = stat.size;
self.headers['Last-Modified'] = new Date(stat.mtime).toUTCString();
self.headers.etag = JSON.stringify([stat.ino, stat.size, Date.parse(stat.mtime)].join('-'));

return callback(self);
});
Expand Down
14 changes: 14 additions & 0 deletions lib/response/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ exports._respond = function (item, request, callback) {
response = new internals.Error(errOptions);
}

// Process ETag and If-Modified-Since headers

var ifModifiedSince = request.raw.req.headers['if-modified-since'] ? Date.parse(request.raw.req.headers['if-modified-since']) : null;
var lastModified = response.headers && response.headers['Last-Modified'] ? Date.parse(response.headers['Last-Modified']) : null;

if (response.headers && response.headers.etag && request.raw.req.headers['if-none-match'] === response.headers.etag) {
response = new internals.Empty();
response._code = 304;
}
else if (ifModifiedSince && lastModified && ifModifiedSince >= lastModified) {
response = new internals.Empty();
response._code = 304;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just one block with || in if?


// Set Cache, CORS, Location headers

Headers.set(response, request);
Expand Down
36 changes: 36 additions & 0 deletions test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,41 @@ describe('Response', function () {
});
});
});

it('returns a 304 when the request has a matching etag', function (done) {

server.start(function () {

Request.post('http://localhost:17082/file', function (err, res1) {

var headers = {
'if-none-match': res1.headers.etag
};

Request.post({ url: 'http://localhost:17082/file', headers: headers }, function (err, res2) {

expect(res2.statusCode).to.equal(304);
done();
});
});
});
});

it('returns a 304 when the request has a future modifed-since', function (done) {

server.start(function () {

var date = new Date(Date.now());
var headers = {
'if-modified-since': new Date(date.setFullYear(date.getFullYear() + 1)).toUTCString()
};

Request.post({ url: 'http://localhost:17082/file', headers: headers }, function (err, res) {

expect(res.statusCode).to.equal(304);
done();
});
});
});
});
});