Skip to content

Commit

Permalink
precondition if-none-match asterisk
Browse files Browse the repository at this point in the history
  • Loading branch information
bourgeoa committed Jan 28, 2024
1 parent 5a42a14 commit 59f94fb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const { routeResolvedFile } = require('./utils')
const ResourceMapper = require('./resource-mapper')
const aclCheck = require('@solid/acl-check')
const { version } = require('../package.json')
// const preconditions = require('express-preconditions')

const corsSettings = cors({
methods: [
Expand Down
6 changes: 6 additions & 0 deletions lib/handlers/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@ async function putStream (req, res, next, stream = req) {
try {
// First check if the file already exists
await ldp.resourceMapper.mapUrlToFile({ url: req })
// Fails on if-none-match asterisk precondition
if ((req.headers['if-none-match'] === '*') && !req.path.endsWith('/')) {
res.sendStatus(412)
return next()
}
} catch (err) {
resourceExists = false
}
try {
// Fails with Append on existing resource
if (!req.originalUrl.endsWith('.acl')) await checkPermission(req, resourceExists)
await ldp.put(req, stream, getContentType(req.headers))
res.sendStatus(201)
Expand Down
10 changes: 9 additions & 1 deletion test/integration/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,20 @@ describe('HTTP APIs', function () {
'../resources/sampleContainer/put1.ttl'), {
encoding: 'utf8'
})
it('should create new resource', function (done) {
it('should create new resource with if-none-match on non existing resource', function (done) {
server.put('/put-resource-1.ttl')
.send(putRequestBody)
.set('if-none-match', '*')
.set('content-type', 'text/plain')
.expect(201, done)
})
it('should fail with 412 with precondition on existing resource', function (done) {
server.put('/put-resource-1.ttl')
.send(putRequestBody)
.set('if-none-match', '*')
.set('content-type', 'text/plain')
.expect(412, done)
})
it('should fail with 400 if not content-type', function (done) {
server.put('/put-resource-1.ttl')
.send(putRequestBody)
Expand Down

0 comments on commit 59f94fb

Please sign in to comment.