Skip to content

Commit 7a2b460

Browse files
benurbdougwilson
authored andcommitted
Fix handling of modified headers with invalid dates
closes #23
1 parent 1599530 commit 7a2b460

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Fix handling of modified headers with invalid dates
5+
16
0.5.0 / 2017-02-21
27
==================
38

index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function fresh (reqHeaders, resHeaders) {
7070
// if-modified-since
7171
if (modifiedSince) {
7272
var lastModified = resHeaders['last-modified']
73-
var modifiedStale = !lastModified || Date.parse(lastModified) > Date.parse(modifiedSince)
73+
var modifiedStale = !lastModified || !(parseHttpDate(lastModified) <= parseHttpDate(modifiedSince))
7474

7575
if (modifiedStale) {
7676
return false
@@ -79,3 +79,19 @@ function fresh (reqHeaders, resHeaders) {
7979

8080
return true
8181
}
82+
83+
/**
84+
* Parse an HTTP Date into a number.
85+
*
86+
* @param {string} date
87+
* @private
88+
*/
89+
90+
function parseHttpDate (date) {
91+
var timestamp = date && Date.parse(date)
92+
93+
// istanbul ignore next: guard against date.js Date.parse patching
94+
return typeof timestamp === 'number'
95+
? timestamp
96+
: NaN
97+
}

test/fresh.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ describe('fresh(reqHeaders, resHeaders)', function () {
107107
describe('with invalid If-Modified-Since date', function () {
108108
it('should be stale', function () {
109109
var reqHeaders = { 'if-modified-since': 'foo' }
110-
var resHeaders = { 'modified-since': 'Sat, 01 Jan 2000 00:00:00 GMT' }
110+
var resHeaders = { 'last-modified': 'Sat, 01 Jan 2000 00:00:00 GMT' }
111111
assert.ok(!fresh(reqHeaders, resHeaders))
112112
})
113113
})
114114

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

0 commit comments

Comments
 (0)