Skip to content

Commit

Permalink
Refactoring etag support
Browse files Browse the repository at this point in the history
  • Loading branch information
geek committed Nov 20, 2012
1 parent a3bed4a commit ace9888
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
22 changes: 12 additions & 10 deletions lib/response/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +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 (request.method === 'get' || request.method === 'head') {
// Process ETag and If-Modified-Since headers

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;
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;
var etag = response.headers ? response.headers.etag : null;

if ((etag && request.raw.req.headers['if-none-match'] === etag) ||
(ifModifiedSince && lastModified && ifModifiedSince >= lastModified)) {

response = new internals.Empty();
response._code = 304;
}
}

// Set Cache, CORS, Location headers
Expand Down
24 changes: 12 additions & 12 deletions test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ describe('Response', function () {
{ method: 'GET', path: '/direct', config: { handler: directHandler, cache: { mode: 'client', expiresIn: 9999 } } },
{ method: 'POST', path: '/exp', handler: expHandler },
{ method: 'POST', path: '/stream/{issue?}', handler: streamHandler },
{ method: 'POST', path: '/file', handler: fileHandler },
{ method: 'POST', path: '/relativefile', handler: relativeFileHandler },
{ method: 'POST', path: '/filenotfound', handler: fileNotFoundHandler },
{ method: 'POST', path: '/staticfile', handler: { file: __dirname + '/../../package.json' } },
{ method: 'POST', path: '/relativestaticfile', handler: { file: './package.json' } },
{ method: 'GET', path: '/file', handler: fileHandler },
{ method: 'GET', path: '/relativefile', handler: relativeFileHandler },
{ method: 'GET', path: '/filenotfound', handler: fileNotFoundHandler },
{ method: 'GET', path: '/staticfile', handler: { file: __dirname + '/../../package.json' } },
{ method: 'GET', path: '/relativestaticfile', handler: { file: './package.json' } },
{ method: 'GET', path: '/cache', config: { handler: cacheHandler, cache: { expiresIn: 5000 } } }
]);

Expand Down Expand Up @@ -280,7 +280,7 @@ describe('Response', function () {

server.start(function () {

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

expect(err).to.not.exist;
expect(body).to.contain('hapi');
Expand All @@ -306,7 +306,7 @@ describe('Response', function () {

it('returns a file using the built-in handler config', function (done) {

Request.post('http://localhost:17082/staticfile', function (err, res, body) {
Request.get('http://localhost:17082/staticfile', function (err, res, body) {

expect(err).to.not.exist;
expect(body).to.contain('hapi');
Expand All @@ -322,7 +322,7 @@ describe('Response', function () {

server.start(function () {

Request.post('http://localhost:17082/relativefile', function (err, res, body) {
Request.get('http://localhost:17082/relativefile', function (err, res, body) {

expect(err).to.not.exist;
expect(body).to.contain('hapi');
Expand All @@ -335,7 +335,7 @@ describe('Response', function () {

it('returns a file using the built-in handler config', function (done) {

Request.post('http://localhost:17082/relativestaticfile', function (err, res, body) {
Request.get('http://localhost:17082/relativestaticfile', function (err, res, body) {

expect(err).to.not.exist;
expect(body).to.contain('hapi');
Expand All @@ -350,13 +350,13 @@ describe('Response', function () {

server.start(function () {

Request.post('http://localhost:17082/file', function (err, res1) {
Request.get('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) {
Request.get({ url: 'http://localhost:17082/file', headers: headers }, function (err, res2) {

expect(res2.statusCode).to.equal(304);
done();
Expand All @@ -374,7 +374,7 @@ describe('Response', function () {
'if-modified-since': new Date(date.setFullYear(date.getFullYear() + 1)).toUTCString()
};

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

expect(res.statusCode).to.equal(304);
done();
Expand Down

0 comments on commit ace9888

Please sign in to comment.