Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion lib/handlers/get.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,22 @@ export default async function handler (req, res, next) {
let container
let contentRange
let chunksize
let mtime

if (ret) {
stream = ret.stream
contentType = ret.contentType
container = ret.container
contentRange = ret.contentRange
chunksize = ret.chunksize
mtime = ret.mtime
}

// Till here it must exist
if (!includeBody) {
debug('HEAD only')
res.setHeader('Content-Type', ret.contentType)
if (mtime) res.setHeader('Last-Modified', mtime.toUTCString())
return res.status(200).send('OK')
}

Expand Down Expand Up @@ -118,7 +121,8 @@ export default async function handler (req, res, next) {
// If request accepts the content-type we found
if (stream && negotiator.mediaType([contentType])) {
let headers = {
'Content-Type': contentType
'Content-Type': contentType,
...(mtime && { 'Last-Modified': mtime.toUTCString() })
}

if (contentRange) {
Expand Down Expand Up @@ -160,6 +164,9 @@ export default async function handler (req, res, next) {
headers
})) return
res.setHeader('Content-Type', possibleRDFType)
if (mtime) {
res.setHeader('Last-Modified', mtime.toUTCString())
}
res.send(data)
return next()
} catch (err) {
Expand Down
6 changes: 3 additions & 3 deletions lib/ldp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ class LDP {
}

if (!options.includeBody) {
return { stream: stats, contentType, container: stats.isDirectory() }
return { stream: stats, contentType, container: stats.isDirectory(), mtime: stats.mtime }
}

if (stats.isDirectory()) {
Expand All @@ -465,7 +465,7 @@ class LDP {
throw err
}
const stream = stringToStream(data)
return { stream, contentType, container: true }
return { stream, contentType, container: true, mtime: stats.mtime }
} else {
let chunksize, contentRange, start, end
if (options.range) {
Expand All @@ -487,7 +487,7 @@ class LDP {
})
.on('open', function () {
debug.handlers(`GET -- Reading ${pathLocal}`)
return resolve({ stream, contentType, container: false, contentRange, chunksize })
return resolve({ stream, contentType, container: false, contentRange, chunksize, mtime: stats.mtime })
})
}))
}
Expand Down
17 changes: 17 additions & 0 deletions test/integration/header-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ describe('Header handler', () => {
})
})

describe('Last-Modified', () => {
describeHeaderTest('read/append for the public', {
resource: '/public-ra',
headers: {
'Last-Modified': /^\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/
}
})

describe('on a HEAD request', () => {
it('has a Last-Modified header', async () => {
const { headers } = await request.head('/public-ra')
expect(headers).to.have.property('last-modified')
expect(headers['last-modified']).to.match(/^\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/)
})
})
})

function describeHeaderTest (label, { resource, headers }) {
describe(`a resource that is ${label}`, () => {
// Retrieve the response headers
Expand Down