Skip to content

Commit 3a89a59

Browse files
author
deiu
committed
Redirect /foo to /foo/ if missing slash and is container
1 parent 7668f9b commit 3a89a59

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

lib/handlers/get.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = handler
22

33
var fs = require('fs')
44
var glob = require('glob')
5-
var path = require('path')
5+
var _path = require('path')
66
var $rdf = require('rdflib')
77
var S = require('string')
88
var async = require('async')
@@ -60,9 +60,17 @@ function handler (req, res, next) {
6060
return next(err)
6161
}
6262

63+
// Redirect to container with / if missing
64+
if (container && path.lastIndexOf('/') !== path.length - 1) {
65+
debug(contentType)
66+
return res.redirect(301, _path.join(path, '/'))
67+
}
68+
6369
// Till here it must exist
6470
if (!includeBody) {
6571
debug('HEAD only')
72+
// res.set('Content-Type', contentType)
73+
// res.set()
6674
res.sendStatus(200)
6775
return next()
6876
}
@@ -174,7 +182,7 @@ function aclAllow (match, req, res, callback) {
174182
}
175183

176184
var root = ldp.idp ? ldp.root + req.hostname + '/' : ldp.root
177-
var relativePath = '/' + path.relative(root, match)
185+
var relativePath = '/' + _path.relative(root, match)
178186
res.locals.path = relativePath
179187
acl.allow('Read', req, res, function (err) {
180188
callback(err)

test/acl.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,6 @@ describe('ACL HTTP', function () {
462462
done()
463463
})
464464
})
465-
it('user1 should be able to access test directory without trailing slash in the url', function (done) {
466-
var options = createOptions('/acl/read-acl', 'user1')
467-
request.head(options, function (error, response, body) {
468-
assert.equal(error, null)
469-
assert.equal(response.statusCode, 200)
470-
done()
471-
})
472-
})
473465
it('user1 should be able to modify ACL file', function (done) {
474466
var options = createOptions('/acl/read-acl/.acl', 'user1')
475467
options.headers = {

test/http.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,12 @@ describe('HTTP APIs', function () {
173173
})
174174

175175
it('should have Access-Control-Allow-Origin as Origin on containers', function (done) {
176-
server.get('/sampleContainer')
176+
server.get('/sampleContainer/')
177177
.set('Origin', 'http://example.com')
178178
.expect('content-type', /text\/turtle/)
179179
.expect('Access-Control-Allow-Origin', 'http://example.com')
180180
.expect(200, done)
181181
})
182-
183182
it('should have Access-Control-Allow-Origin as Origin on resources',
184183
function (done) {
185184
server.get('/sampleContainer/example1.ttl')
@@ -221,6 +220,10 @@ describe('HTTP APIs', function () {
221220
.expect('content-type', /text\/html/)
222221
.expect(200, done) // Can't check for 303 because of internal redirects
223222
})
223+
it('should redirect to the right container URI if missing /', function (done) {
224+
server.get('/sampleContainer')
225+
.expect(301, done)
226+
})
224227
it('should return 404 for non-existent resource', function (done) {
225228
server.get('/invalidfile.foo')
226229
.expect(404, done)
@@ -486,7 +489,7 @@ describe('HTTP APIs', function () {
486489
})
487490
})
488491
it('should be able to access newly container', function (done) {
489-
server.get('/post-tests/loans')
492+
server.get('/post-tests/loans/')
490493
.expect('content-type', /text\/turtle/)
491494
.expect(200, done)
492495
})

0 commit comments

Comments
 (0)