Skip to content

Commit 29beef3

Browse files
author
nicola
committed
enable requests without trailing /
1 parent 4ef404c commit 29beef3

3 files changed

Lines changed: 55 additions & 17 deletions

File tree

‎lib/handlers/post.js‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,43 @@ function handler (req, res, next) {
2121

2222
// Not a container
2323
if (containerPath[containerPath.length - 1] !== '/') {
24-
debug('POST -- Path is not a container')
25-
res.set('Allow', 'GET,HEAD,PUT,DELETE')
26-
return next(error(405, 'Requested resource is not a container'))
24+
containerPath += '/'
2725
}
2826

2927
debug('POST -- Content Type: ' + contentType)
3028

3129
var linkHeader = header.parseMetadataFromHeader(req.get('Link'))
3230

33-
// TODO: possibly package this in ldp.post
34-
ldp.getAvailablePath(req.hostname, containerPath, req.get('Slug'), function (resourcePath) {
35-
debug('POST -- Will create at: ' + resourcePath)
36-
var meta = ''
37-
if (linkHeader.isBasicContainer) {
38-
resourcePath += '/'
39-
meta = ldp.suffixMeta
31+
ldp.exists(req.hostname, containerPath, function (err, stats) {
32+
if (err) {
33+
return next(error(err, 'Container not valid'))
4034
}
4135

42-
ldp.put(req.hostname, resourcePath + meta, req.text, function (err) {
43-
if (err) {
44-
next(err)
36+
if (!stats.isDirectory()) {
37+
debug('POST -- Path is not a container')
38+
res.set('Allow', 'GET,HEAD,PUT,DELETE')
39+
return next(error(405, 'Requested resource is not a container'))
40+
}
41+
42+
// TODO: possibly package this in ldp.post
43+
ldp.getAvailablePath(req.hostname, containerPath, req.get('Slug'), function (resourcePath) {
44+
debug('POST -- Will create at: ' + resourcePath)
45+
var meta = ''
46+
if (linkHeader.isBasicContainer) {
47+
resourcePath += '/'
48+
meta = ldp.suffixMeta
4549
}
4650

47-
header.addLinks(res, linkHeader)
48-
res.set('Location', resourcePath)
49-
res.sendStatus(201)
51+
ldp.put(req.hostname, resourcePath + meta, req.text, function (err) {
52+
if (err) {
53+
return next(err)
54+
}
55+
56+
header.addLinks(res, linkHeader)
57+
res.set('Location', resourcePath)
58+
res.sendStatus(201)
59+
return next()
60+
})
5061
})
5162
})
5263
}

‎lib/ldp.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ LDP.prototype.get = function (host, reqPath, baseUri, includeBody, contentType,
344344
var ldp = this
345345
var root = !ldp.idp ? ldp.root : ldp.root + host + '/'
346346
var filename = utils.uriToFilename(reqPath, root)
347+
347348
ldp.stat(filename, function (err, stats) {
348349
// File does not exist
349350
if (err) {
@@ -352,7 +353,7 @@ LDP.prototype.get = function (host, reqPath, baseUri, includeBody, contentType,
352353

353354
// Just return, since resource exists
354355
if (!includeBody) {
355-
return callback(null)
356+
return callback(null, stats)
356357
}
357358

358359
// Found a container

‎test/http.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var supertest = require('supertest')
22
var fs = require('fs')
33
var li = require('li')
44
var ldnode = require('../index')
5+
var rm = require('./test-utils').rm
56

67
describe('HTTP APIs', function () {
78
var emptyResponse = function (res) {
@@ -276,6 +277,31 @@ describe('HTTP APIs', function () {
276277
.expect(hasHeader('acl', suffixAcl))
277278
.expect(201, done)
278279
})
280+
it('Should create new resource even if no trailing / is in the target', function (done) {
281+
rm('target.ttl')
282+
server.post('')
283+
.send(postRequest1Body)
284+
.set('content-type', 'text/turtle')
285+
.set('slug', 'target.ttl')
286+
.expect('location', /\/target\.ttl/)
287+
.expect(hasHeader('describedBy', suffixMeta))
288+
.expect(hasHeader('acl', suffixAcl))
289+
.expect(201, function (err) {
290+
rm('target.ttl')
291+
return done(err)
292+
})
293+
})
294+
it('Should fail return 404 if no parent container found', function (done) {
295+
rm('target.ttl')
296+
server.post('/hello.html/')
297+
.send(postRequest1Body)
298+
.set('content-type', 'text/turtle')
299+
.set('slug', 'no- target.ttl')
300+
.expect(404, function (err) {
301+
rm('target.ttl')
302+
return done(err)
303+
})
304+
})
279305
it('Should create a new slug if there is a resource with the same name', function (done) {
280306
server.post('/')
281307
.send(postRequest1Body)

0 commit comments

Comments
 (0)