Skip to content

Commit 44a5540

Browse files
committed
Final fix for index serving
1 parent a1909bd commit 44a5540

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

lib/handlers/get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async function handler (req, res, next) {
4545

4646
let ret
4747
try {
48-
ret = await ldp.get(options, req.headers.accept.includes('text/html'))
48+
ret = await ldp.get(options)
4949
} catch (err) {
5050
// use globHandler if magic is detected
5151
if (err.status === 404 && glob.hasMagic(path)) {

lib/resource-mapper.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ResourceMapper {
4242
}
4343

4444
// Maps the request for a given resource and representation format to a server file
45-
// When searchIndex is true and the URL ends with a '/', indexFilename will be matched.
45+
// When searchIndex is true and the URL ends with a '/', and contentType includes 'text/html' indexFilename will be matched.
4646
async mapUrlToFile ({ url, contentType, createIfNotExists, searchIndex = true }) {
4747
let fullPath = this.getFullPath(url)
4848
let isIndex = searchIndex && fullPath.endsWith('/')
@@ -53,7 +53,9 @@ class ResourceMapper {
5353
if (createIfNotExists && contentType !== this._indexContentType) {
5454
throw new Error(`Index file needs to have ${this._indexContentType} as content type`)
5555
}
56-
fullPath += this._indexFilename
56+
if (contentType && contentType.includes(this._indexContentType)) {
57+
fullPath += this._indexFilename
58+
}
5759
}
5860
// Create the path for a new file
5961
if (createIfNotExists) {
@@ -69,7 +71,7 @@ class ResourceMapper {
6971
const folder = fullPath.substr(0, fullPath.length - filename.length)
7072

7173
// Find a file with the same name (minus the dollar extension)
72-
let match = searchIndex ? await this._getMatchingFile(folder, filename, isIndex) : ''
74+
let match = searchIndex ? await this._getMatchingFile(folder, filename, isIndex, contentType) : ''
7375
if (match === undefined) {
7476
// Error if no match was found,
7577
// unless the URL ends with a '/',
@@ -87,13 +89,13 @@ class ResourceMapper {
8789
return { path, contentType: contentType || this._defaultContentType }
8890
}
8991

90-
async _getMatchingFile (folder, filename, isIndex) {
92+
async _getMatchingFile (folder, filename, isIndex, contentType) {
9193
const files = await this._readdir(folder)
9294
// Search for files with the same name (disregarding a dollar extension)
9395
if (!isIndex) {
9496
return files.find(f => this._removeDollarExtension(f) === filename)
9597
// Check if the index file exists
96-
} else if (files.includes(this._indexFilename)) {
98+
} else if (files.includes(this._indexFilename) && contentType && contentType.includes(this._indexContentType)) {
9799
return this._indexFilename
98100
}
99101
}

test/integration/http-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ describe('HTTP APIs', function () {
165165

166166
it('should have set Link as resource on a implicit index page', function (done) {
167167
server.options('/sampleContainer/')
168-
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
168+
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
169+
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
169170
.end(done)
170171
})
171172

test/unit/resource-mapper-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ describe('ResourceMapper', () => {
239239

240240
itMapsUrl(mapper, 'a URL ending with a slash when index.html is available',
241241
{
242-
url: 'http://localhost/space/'
242+
url: 'http://localhost/space/',
243+
contentType: 'text/html'
243244
},
244245
[
245246
`${rootPath}space/index.html`,

0 commit comments

Comments
 (0)