Skip to content

Commit

Permalink
Fix handling of modified headers with invalid dates
Browse files Browse the repository at this point in the history
closes #23
  • Loading branch information
benurb authored and dougwilson committed Sep 12, 2017
1 parent 1599530 commit 7a2b460
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Fix handling of modified headers with invalid dates

0.5.0 / 2017-02-21
==================

Expand Down
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function fresh (reqHeaders, resHeaders) {
// if-modified-since
if (modifiedSince) {
var lastModified = resHeaders['last-modified']
var modifiedStale = !lastModified || Date.parse(lastModified) > Date.parse(modifiedSince)
var modifiedStale = !lastModified || !(parseHttpDate(lastModified) <= parseHttpDate(modifiedSince))

if (modifiedStale) {
return false
Expand All @@ -79,3 +79,19 @@ function fresh (reqHeaders, resHeaders) {

return true
}

/**
* Parse an HTTP Date into a number.
*
* @param {string} date
* @private
*/

function parseHttpDate (date) {
var timestamp = date && Date.parse(date)

// istanbul ignore next: guard against date.js Date.parse patching
return typeof timestamp === 'number'
? timestamp
: NaN
}
6 changes: 3 additions & 3 deletions test/fresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ describe('fresh(reqHeaders, resHeaders)', function () {
describe('with invalid If-Modified-Since date', function () {
it('should be stale', function () {
var reqHeaders = { 'if-modified-since': 'foo' }
var resHeaders = { 'modified-since': 'Sat, 01 Jan 2000 00:00:00 GMT' }
var resHeaders = { 'last-modified': 'Sat, 01 Jan 2000 00:00:00 GMT' }
assert.ok(!fresh(reqHeaders, resHeaders))
})
})

describe('with invalid Modified-Since date', function () {
describe('with invalid Last-Modified date', function () {
it('should be stale', function () {
var reqHeaders = { 'if-modified-since': 'Sat, 01 Jan 2000 00:00:00 GMT' }
var resHeaders = { 'modified-since': 'foo' }
var resHeaders = { 'last-modified': 'foo' }
assert.ok(!fresh(reqHeaders, resHeaders))
})
})
Expand Down

0 comments on commit 7a2b460

Please sign in to comment.