Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues #1120 #1256 from NSS5.1.6 ressource-mapper #1282

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/handlers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function handler (req, res, next) {

let ret
try {
ret = await ldp.get(options)
ret = await ldp.get(options, req.accepts('html'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be better handled as

req.accepts(['text/turtle', 'application/ld+json', 'html']) === 'html'

so that we can handle requests where rdf has a higher weighted q value.

Copy link
Member Author

@bourgeoa bourgeoa Aug 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall I push your proposal (might be better ?) or do we keep it like it is ?
__
Just tried your proposal locally and npm run test, it fails on :

  1. formats
    turtle
    should return turtle when listing container with an index page:
    Error: expected "content-type" matching /text/html/, got "text/turtle"

So I propose to leave it like it is for node-solid-server EOL

} catch (err) {
// use globHandler if magic is detected
if (err.status === 404 && glob.hasMagic(path)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ldp.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ class LDP {
requestUrl = requestUrl.replace(/\/*$/, '/')

const { path: containerFilePath } = await this.resourceMapper.mapUrlToFile({ url: requestUrl })
let fileName = slug + extension
let fileName = slug.endsWith(extension) || slug.endsWith(this.suffixAcl) || slug.endsWith(this.suffixMeta) ? slug : slug + extension
if (await promisify(fs.exists)(utilPath.join(containerFilePath, fileName))) {
fileName = `${uuid.v1()}-${fileName}`
}
Expand Down
40 changes: 17 additions & 23 deletions lib/resource-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const HTTPError = require('./http-error')

/*
* A ResourceMapper maintains the mapping between HTTP URLs and server filenames,
* following the principles of the sweet spot discussed in
* following the principles of the "sweet spot" discussed in
* https://www.w3.org/DesignIssues/HTTPFilenameMapping.html
*
* This class implements this mapping in a single place
Expand Down Expand Up @@ -91,22 +91,24 @@ class ResourceMapper {
if (filePath.indexOf('/..') >= 0) {
throw new Error('Disallowed /.. segment in URL')
}
let isIndex = searchIndex && filePath.endsWith('/')
let isFolder = filePath.endsWith('/')

// Create the path for a new file
// Create the path for a new ressource
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

let path
if (createIfNotExists) {
path = filePath
// Append index filename if needed
if (isIndex) {
if (searchIndex && isFolder) {
if (contentType !== this._indexContentType) {
throw new Error(`Index file needs to have ${this._indexContentType} as content type`)
}
path += this._indexFilename
}
// If the extension is not correct for the content type, append the correct extension
if (searchIndex && this._getContentTypeByExtension(path) !== contentType) {
path += `$${contentType in extensions ? `.${extensions[contentType][0]}` : '.unknown'}`
if (!isFolder) {
if (this._getContentTypeByExtension(path) !== contentType) {
path += `$${contentType in extensions ? `.${extensions[contentType][0]}` : '.unknown'}`
}
}
// Determine the path of an existing file
} else {
Expand All @@ -116,23 +118,15 @@ class ResourceMapper {

// Find a file with the same name (minus the dollar extension)
let match = ''
if (searchIndex) {
const files = await this._readdir(folder)
// Search for files with the same name (disregarding a dollar extension)
if (!isIndex) {
match = files.find(f => this._removeDollarExtension(f) === filename)
// Check if the index file exists
} else if (files.includes(this._indexFilename)) {
match = this._indexFilename
}
}
// Error if no match was found (unless URL ends with '/', then fall back to the folder)
if (match === undefined) {
if (isIndex) {
match = ''
} else {
throw new HTTPError(404, `Resource not found: ${pathname}`)
}
const files = await this._readdir(folder)
// Search for files with the same name (disregarding a dollar extension)
if (!isFolder) {
match = files.find(f => this._removeDollarExtension(f) === filename)
if (match === undefined) { throw new HTTPError(404, `Resource not found: ${pathname}`) }

// Check if the index file exists
} else if (searchIndex && files.includes(this._indexFilename)) {
match = this._indexFilename
}
path = `${folder}${match}`
contentType = this._getContentTypeByExtension(match)
Expand Down
Loading