Skip to content

Commit 6432c4e

Browse files
authored
Merge pull request #1338 from solid/dev
Dev (v5.2.2)
2 parents a64f76f + d3a8321 commit 6432c4e

File tree

11 files changed

+237
-170
lines changed

11 files changed

+237
-170
lines changed

lib/create-app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ function initAuthentication (app, argv) {
296296
*/
297297
function sessionSettings (secureCookies, host) {
298298
let sessionSettings = {
299+
name: 'nssidp.sid',
299300
secret: uuid.v1(),
300301
saveUninitialized: false,
301302
resave: false,

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.accepts('html'))
48+
ret = await ldp.get(options, req.accepts(['html', 'turtle', 'rdf+xml', 'n3', 'ld+json']) === 'html')
4949
} catch (err) {
5050
// use globHandler if magic is detected
5151
if (err.status === 404 && glob.hasMagic(path)) {

lib/ldp.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,6 @@ class LDP {
156156
}
157157
// const { url: putUrl } = await this.resourceMapper.mapFileToUrl({ path: resourceUrl, hostname })
158158

159-
// HACK: the middleware in webid-oidc.js uses body-parser, thus ending the stream of data
160-
// for JSON bodies. So, the stream needs to be reset
161-
if (contentType.includes('application/json')) {
162-
stream = intoStream(JSON.stringify(stream.body))
163-
}
164-
165159
await ldp.put(resourceUrl, stream, contentType)
166160
return URL.parse(originalUrl).path
167161
}
@@ -240,6 +234,11 @@ class LDP {
240234

241235
// Directory created, now write the file
242236
return withLock(path, { mustExist: false }, () => new Promise((resolve, reject) => {
237+
// HACK: the middleware in webid-oidc.js uses body-parser, thus ending the stream of data
238+
// for JSON bodies. So, the stream needs to be reset
239+
if (contentType.includes('application/json')) {
240+
stream = intoStream(JSON.stringify(stream.body))
241+
}
243242
const file = stream.pipe(fs.createWriteStream(path))
244243
file.on('error', function () {
245244
reject(error(500, 'Error writing data'))

lib/requests/sharing-request.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class SharingRequest extends AuthRequest {
8282
} else {
8383
request.renderForm(null, req, appOrigin)
8484
}
85+
} else {
86+
request.redirectPostSharing()
8587
}
8688
}
8789

@@ -119,6 +121,8 @@ class SharingRequest extends AuthRequest {
119121

120122
// Redirect once that's all done
121123
request.redirectPostSharing()
124+
} else {
125+
request.redirectPostSharing()
122126
}
123127
}
124128

@@ -144,12 +148,7 @@ class SharingRequest extends AuthRequest {
144148

145149
isUserLoggedIn () {
146150
// Ensure the user arrived here by logging in
147-
if (!this.session.subject || !this.session.subject._id) {
148-
this.response.status(401)
149-
this.response.send('User not logged in 2')
150-
return false
151-
}
152-
return true
151+
return !!(this.session.subject && this.session.subject._id)
153152
}
154153

155154
getAppUrl () {

lib/resource-mapper.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { promisify } = require('util')
44
const { types, extensions } = require('mime-types')
55
const readdir = promisify(fs.readdir)
66
const HTTPError = require('./http-error')
7+
const pathUtil = require('path')
78

89
/*
910
* A ResourceMapper maintains the mapping between HTTP URLs and server filenames,
@@ -33,7 +34,7 @@ class ResourceMapper {
3334
this._defaultContentType = defaultContentType
3435
this._types = { ...types, ...overrideTypes }
3536
this._indexFilename = indexFilename
36-
this._indexContentType = this._getContentTypeByExtension(indexFilename)
37+
this._indexContentType = this._getContentTypeFromExtension(indexFilename)
3738

3839
// If the host needs to be replaced on every call, pre-split the root URL
3940
if (includeHost) {
@@ -78,8 +79,10 @@ class ResourceMapper {
7879

7980
// Determine the URL by chopping off everything after the dollar sign
8081
const pathname = this._removeDollarExtension(path)
81-
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname)}`
82-
return { url, contentType: this._getContentTypeByExtension(path) }
82+
const url = `${this.resolveUrl(hostname)}${
83+
pathname.split(pathUtil.sep).map((component) => encodeURIComponent(component)).join('/')
84+
}`
85+
return { url, contentType: this._getContentTypeFromExtension(path) }
8386
}
8487

8588
// Maps the request for a given resource and representation format to a server file
@@ -94,7 +97,7 @@ class ResourceMapper {
9497
let isFolder = filePath.endsWith('/')
9598
let isIndex = searchIndex && filePath.endsWith('/')
9699

97-
// Create the path for a new ressource
100+
// Create the path for a new resource
98101
let path
99102
if (createIfNotExists) {
100103
path = filePath
@@ -106,8 +109,8 @@ class ResourceMapper {
106109
path += this._indexFilename
107110
}
108111
// If the extension is not correct for the content type, append the correct extension
109-
if (!isFolder && this._getContentTypeByExtension(path) !== contentType) {
110-
path += `$${contentType in extensions ? `.${extensions[contentType][0]}` : '.unknown'}`
112+
if (!isFolder) {
113+
path = this._addContentTypeExtension(path, contentType)
111114
}
112115
// Determine the path of an existing file
113116
} else {
@@ -136,7 +139,7 @@ class ResourceMapper {
136139
}
137140
}
138141
path = `${folder}${match}`
139-
contentType = this._getContentTypeByExtension(match)
142+
contentType = this._getContentTypeFromExtension(match)
140143
}
141144
return { path, contentType: contentType || this._defaultContentType }
142145
}
@@ -157,11 +160,25 @@ class ResourceMapper {
157160
}
158161

159162
// Gets the expected content type based on the extension of the path
160-
_getContentTypeByExtension (path) {
163+
_getContentTypeFromExtension (path) {
161164
const extension = /\.([^/.]+)$/.exec(path)
162165
return extension && this._types[extension[1].toLowerCase()] || this._defaultContentType
163166
}
164167

168+
// Appends an extension for the specific content type, if needed
169+
_addContentTypeExtension (path, contentType) {
170+
// If we would guess the wrong content type from the extension, try appending a better one
171+
const contentTypeFromExtension = this._getContentTypeFromExtension(path)
172+
if (contentTypeFromExtension !== contentType) {
173+
// Some extensions fit multiple content types, so only switch if there's an improvement
174+
const newExtension = contentType in extensions ? extensions[contentType][0] : 'unknown'
175+
if (this._types[newExtension] !== contentTypeFromExtension) {
176+
path += `$.${newExtension}`
177+
}
178+
}
179+
return path
180+
}
181+
165182
// Removes possible trailing slashes from a path
166183
_removeTrailingSlash (path) {
167184
return path.replace(/\/+$/, '')

0 commit comments

Comments
 (0)