Skip to content

Commit 2cfa32c

Browse files
committed
Add Last-Modified header for GET and HEAD
1 parent 2caddca commit 2cfa32c

3 files changed

Lines changed: 70 additions & 39 deletions

File tree

lib/handlers/get.mjs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,16 @@ export default async function handler (req, res, next) {
7777
let contentRange
7878
let chunksize
7979

80-
if (ret) {
81-
stream = ret.stream
82-
contentType = ret.contentType
83-
container = ret.container
84-
contentRange = ret.contentRange
85-
chunksize = ret.chunksize
86-
}
80+
if (ret) {
81+
stream = ret.stream
82+
contentType = ret.contentType
83+
container = ret.container
84+
contentRange = ret.contentRange
85+
chunksize = ret.chunksize
86+
if (ret.modified) {
87+
res.header('Last-Modified', ret.modified.toUTCString())
88+
}
89+
}
8790

8891
// Till here it must exist
8992
if (!includeBody) {

lib/ldp.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ class LDP {
451451
}
452452

453453
if (!options.includeBody) {
454-
return { stream: stats, contentType, container: stats.isDirectory() }
454+
return { stream: stats, contentType, container: stats.isDirectory(), modified: stats.mtime }
455455
}
456456

457457
if (stats.isDirectory()) {
@@ -465,7 +465,7 @@ class LDP {
465465
throw err
466466
}
467467
const stream = stringToStream(data)
468-
return { stream, contentType, container: true }
468+
return { stream, contentType, container: true, modified: stats.mtime }
469469
} else {
470470
let chunksize, contentRange, start, end
471471
if (options.range) {
@@ -487,7 +487,7 @@ class LDP {
487487
})
488488
.on('open', function () {
489489
debug.handlers(`GET -- Reading ${pathLocal}`)
490-
return resolve({ stream, contentType, container: false, contentRange, chunksize })
490+
return resolve({ stream, contentType, container: false, contentRange, chunksize, modified: stats.mtime })
491491
})
492492
}))
493493
}

test/integration/http-test.mjs

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { assert, expect } from 'chai'
99
const __filename = fileURLToPath(import.meta.url)
1010
const __dirname = path.dirname(__filename)
1111

12-
const suffixAcl = '.acl'
13-
const suffixMeta = '.meta'
14-
const server = setupSupertestServer({
12+
const suffixAcl = '.acl'
13+
const suffixMeta = '.meta'
14+
const server = setupSupertestServer({
1515
live: true,
1616
dataBrowserPath: 'default',
1717
root: path.join(__dirname, '../resources'),
@@ -253,13 +253,20 @@ describe('HTTP APIs', function () {
253253
.expect('content-type', /text\/turtle/)
254254
.expect('Access-Control-Allow-Origin', 'http://example.com')
255255
.expect(200, done)
256-
})
257-
it('should have set Link as resource', function (done) {
258-
server.get('/sampleContainer2/example1.ttl')
259-
.expect('content-type', /text\/turtle/)
260-
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
261-
.expect(200, done)
262-
})
256+
})
257+
it('should have set Link as resource', function (done) {
258+
server.get('/sampleContainer2/example1.ttl')
259+
.expect('content-type', /text\/turtle/)
260+
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
261+
.expect(200, done)
262+
})
263+
it('should have set Last-Modified for resource', function (done) {
264+
const modified = fs.statSync(path.join(__dirname,
265+
'../resources/sampleContainer2/example1.ttl')).mtime.toUTCString()
266+
server.get('/sampleContainer2/example1.ttl')
267+
.expect('Last-Modified', modified)
268+
.expect(200, done)
269+
})
263270
it('should have set Updates-Via to use WebSockets', function (done) {
264271
server.get('/sampleContainer2/example1.ttl')
265272
.expect('updates-via', /wss?:\/\//)
@@ -273,13 +280,20 @@ describe('HTTP APIs', function () {
273280
.expect(hasHeader('describedBy', 'example1.ttl' + suffixMeta))
274281
.end(done)
275282
})
276-
it('should have set Link as Container/BasicContainer', function (done) {
277-
server.get('/sampleContainer2/')
278-
.expect('content-type', /text\/turtle/)
279-
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
280-
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
281-
.expect(200, done)
282-
})
283+
it('should have set Link as Container/BasicContainer', function (done) {
284+
server.get('/sampleContainer2/')
285+
.expect('content-type', /text\/turtle/)
286+
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
287+
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
288+
.expect(200, done)
289+
})
290+
it('should have set Last-Modified for container', function (done) {
291+
const modified = fs.statSync(path.join(__dirname,
292+
'../resources/sampleContainer2')).mtime.toUTCString()
293+
server.get('/sampleContainer2/')
294+
.expect('Last-Modified', modified)
295+
.expect(200, done)
296+
})
283297
it('should load skin (mashlib) if resource was requested as text/html', function (done) {
284298
server.get('/sampleContainer2/example1.ttl')
285299
.set('Accept', 'text/html')
@@ -505,11 +519,18 @@ describe('HTTP APIs', function () {
505519
.expect('updates-via', /wss?:\/\//)
506520
.expect(200, done)
507521
})
508-
it('should have set Link as Resource', function (done) {
509-
server.head('/sampleContainer2/example1.ttl')
510-
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
511-
.expect(200, done)
512-
})
522+
it('should have set Link as Resource', function (done) {
523+
server.head('/sampleContainer2/example1.ttl')
524+
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
525+
.expect(200, done)
526+
})
527+
it('should have set Last-Modified for resource', function (done) {
528+
const modified = fs.statSync(path.join(__dirname,
529+
'../resources/sampleContainer2/example1.ttl')).mtime.toUTCString()
530+
server.head('/sampleContainer2/example1.ttl')
531+
.expect('Last-Modified', modified)
532+
.expect(200, done)
533+
})
513534
it('should have set acl and describedBy Links for resource',
514535
function (done) {
515536
server.head('/sampleContainer2/example1.ttl')
@@ -523,13 +544,20 @@ describe('HTTP APIs', function () {
523544
.expect('Content-Type', /text\/turtle/)
524545
.expect(200, done)
525546
})
526-
it('should have set Link as Container/BasicContainer',
527-
function (done) {
528-
server.head('/sampleContainer2/')
529-
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
530-
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
531-
.expect(200, done)
532-
})
547+
it('should have set Link as Container/BasicContainer',
548+
function (done) {
549+
server.head('/sampleContainer2/')
550+
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
551+
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
552+
.expect(200, done)
553+
})
554+
it('should have set Last-Modified for container', function (done) {
555+
const modified = fs.statSync(path.join(__dirname,
556+
'../resources/sampleContainer2')).mtime.toUTCString()
557+
server.head('/sampleContainer2/')
558+
.expect('Last-Modified', modified)
559+
.expect(200, done)
560+
})
533561
it('should have set acl and describedBy Links for container',
534562
function (done) {
535563
server.head('/sampleContainer2/')

0 commit comments

Comments
 (0)