Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

Commit

Permalink
fix(cache): encode x-local-cache-etc headers to be header-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Jun 6, 2017
1 parent 48386f6 commit dc9fb1b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ function matchDetails (req, cached) {
}

function addCacheHeaders (resHeaders, path, key, hash, time) {
resHeaders.set('X-Local-Cache', path)
resHeaders.set('X-Local-Cache-Key', key)
resHeaders.set('X-Local-Cache-Hash', hash)
resHeaders.set('X-Local-Cache', encodeURIComponent(path))
resHeaders.set('X-Local-Cache-Key', encodeURIComponent(key))
resHeaders.set('X-Local-Cache-Hash', encodeURIComponent(hash))
resHeaders.set('X-Local-Cache-Time', new Date(time).toUTCString())
}
14 changes: 11 additions & 3 deletions test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ test('accepts a local path for caches', t => {
}).then(res => {
t.equal(res.status, 200, 'non-stale cached res has 200 status')
const hs = res.headers
t.equal(hs.get('x-local-cache'), CACHE, 'path added for cached requests')
t.equal(
decodeURIComponent(hs.get('x-local-cache')),
CACHE,
'path added for cached requests'
)
t.match(
hs.get('x-local-cache-key'),
decodeURIComponent(hs.get('x-local-cache-key')),
new RegExp(`${HOST}/test`),
'cache key contains URI'
)
t.equal(hs.get('x-local-cache-hash'), INTEGRITY, 'content hash in header')
t.equal(
decodeURIComponent(hs.get('x-local-cache-hash')),
INTEGRITY,
'content hash in header'
)
t.ok(hs.get('x-local-cache-time'), 'content write time in header')
return res.buffer()
}).then(body => {
Expand Down

0 comments on commit dc9fb1b

Please sign in to comment.