Skip to content
Merged
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
9 changes: 7 additions & 2 deletions lib/handlers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = handler

var fs = require('fs')
var glob = require('glob')
var path = require('path')
var _path = require('path')
var $rdf = require('rdflib')
var S = require('string')
var async = require('async')
Expand Down Expand Up @@ -60,6 +60,11 @@ function handler (req, res, next) {
return next(err)
}

// Redirect to container with / if missing
if (container && path.lastIndexOf('/') !== path.length - 1) {
return res.redirect(301, _path.join(path, '/'))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we really want to redirect every time? I am not sure I understand why this should always happen

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, because /foo and /foo/ are not the same thing. For example, this breaks relative URIs you get from dereferencing /foo.

}

// Till here it must exist
if (!includeBody) {
debug('HEAD only')
Expand Down Expand Up @@ -174,7 +179,7 @@ function aclAllow (match, req, res, callback) {
}

var root = ldp.idp ? ldp.root + req.hostname + '/' : ldp.root
var relativePath = '/' + path.relative(root, match)
var relativePath = '/' + _path.relative(root, match)
res.locals.path = relativePath
acl.allow('Read', req, res, function (err) {
callback(err)
Expand Down
2 changes: 1 addition & 1 deletion lib/ldp.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ LDP.prototype.get = function (host, reqPath, baseUri, includeBody, contentType,

// Just return, since resource exists
if (!includeBody) {
return callback(null, stats)
return callback(null, stats, contentType, stats.isDirectory())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if we pass stats there is no need to pass stats.isDirectory()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also when is the contentType used?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The problem occurs later on, since otherwise container will be undefined. This is the relevant line: https://github.com/linkeddata/ldnode/blob/fix-redirect/lib/handlers/get.js#L64

}

// Found a container
Expand Down
8 changes: 0 additions & 8 deletions test/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,6 @@ describe('ACL HTTP', function () {
done()
})
})
it('user1 should be able to access test directory without trailing slash in the url', function (done) {
var options = createOptions('/acl/read-acl', 'user1')
request.head(options, function (error, response, body) {
assert.equal(error, null)
assert.equal(response.statusCode, 200)
done()
})
})
it('user1 should be able to modify ACL file', function (done) {
var options = createOptions('/acl/read-acl/.acl', 'user1')
options.headers = {
Expand Down
9 changes: 6 additions & 3 deletions test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,12 @@ describe('HTTP APIs', function () {
})

it('should have Access-Control-Allow-Origin as Origin on containers', function (done) {
server.get('/sampleContainer')
server.get('/sampleContainer/')
.set('Origin', 'http://example.com')
.expect('content-type', /text\/turtle/)
.expect('Access-Control-Allow-Origin', 'http://example.com')
.expect(200, done)
})

it('should have Access-Control-Allow-Origin as Origin on resources',
function (done) {
server.get('/sampleContainer/example1.ttl')
Expand Down Expand Up @@ -221,6 +220,10 @@ describe('HTTP APIs', function () {
.expect('content-type', /text\/html/)
.expect(200, done) // Can't check for 303 because of internal redirects
})
it('should redirect to the right container URI if missing /', function (done) {
server.get('/sampleContainer')
.expect(301, done)
})
it('should return 404 for non-existent resource', function (done) {
server.get('/invalidfile.foo')
.expect(404, done)
Expand Down Expand Up @@ -486,7 +489,7 @@ describe('HTTP APIs', function () {
})
})
it('should be able to access newly container', function (done) {
server.get('/post-tests/loans')
server.get('/post-tests/loans/')
.expect('content-type', /text\/turtle/)
.expect(200, done)
})
Expand Down