From 5e712316d83c379645076ff868337981fcf82567 Mon Sep 17 00:00:00 2001 From: Jeff Zucker <44732708+jeff-zucker@users.noreply.github.com> Date: Fri, 24 May 2024 12:32:04 -0700 Subject: [PATCH 1/5] Update put.js send 200 or 201 depending on prexistance of resource The HTTP spec says that the Server MUST return a 200 or 204 if PUT replaces and existing resource. See second paragraph of https://httpwg.org/specs/rfc9110#PUT. Brought up by @CxRes. --- lib/handlers/put.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/handlers/put.js b/lib/handlers/put.js index 740bf346..681eb78b 100644 --- a/lib/handlers/put.js +++ b/lib/handlers/put.js @@ -62,6 +62,7 @@ async function putStream (req, res, next, stream = req) { // try { // Obtain details of the target resource let resourceExists = true + let returnStatus = 201; try { // First check if the file already exists await ldp.resourceMapper.mapUrlToFile({ url: req }) @@ -70,6 +71,7 @@ async function putStream (req, res, next, stream = req) { res.sendStatus(412) return next() } + returnStatus = 200; } catch (err) { resourceExists = false } @@ -77,7 +79,7 @@ async function putStream (req, res, next, stream = req) { // 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) + res.sendStatus(returnStatus) return next() } catch (err) { err.message = 'Can\'t write file/folder: ' + err.message From d602ad81dbc65cbb95fd1292ab79e071439d9008 Mon Sep 17 00:00:00 2001 From: Jeff Zucker <44732708+jeff-zucker@users.noreply.github.com> Date: Fri, 24 May 2024 12:45:03 -0700 Subject: [PATCH 2/5] Update put.js --- lib/handlers/put.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/handlers/put.js b/lib/handlers/put.js index 681eb78b..fb029624 100644 --- a/lib/handlers/put.js +++ b/lib/handlers/put.js @@ -62,7 +62,7 @@ async function putStream (req, res, next, stream = req) { // try { // Obtain details of the target resource let resourceExists = true - let returnStatus = 201; + let returnStatus = 201 try { // First check if the file already exists await ldp.resourceMapper.mapUrlToFile({ url: req }) @@ -71,7 +71,7 @@ async function putStream (req, res, next, stream = req) { res.sendStatus(412) return next() } - returnStatus = 200; + returnStatus = 200 } catch (err) { resourceExists = false } From 12d34052711186e503b63e7efcbd3ebd46b1b8fa Mon Sep 17 00:00:00 2001 From: Jeff Zucker <44732708+jeff-zucker@users.noreply.github.com> Date: Sat, 25 May 2024 11:29:36 -0700 Subject: [PATCH 3/5] Update put.js 200->204 when replacing --- lib/handlers/put.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/handlers/put.js b/lib/handlers/put.js index fb029624..76bc3b61 100644 --- a/lib/handlers/put.js +++ b/lib/handlers/put.js @@ -71,7 +71,7 @@ async function putStream (req, res, next, stream = req) { res.sendStatus(412) return next() } - returnStatus = 200 + returnStatus = 204 // if content is sent back change to 200 } catch (err) { resourceExists = false } From df9beabfb89449d443362bf0c75c5f19c69aee9d Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Wed, 29 May 2024 23:21:24 +0530 Subject: [PATCH 4/5] Update lib/handlers/put.js Co-authored-by: Alain Bourgeois --- lib/handlers/put.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/handlers/put.js b/lib/handlers/put.js index 76bc3b61..38ba1369 100644 --- a/lib/handlers/put.js +++ b/lib/handlers/put.js @@ -79,7 +79,7 @@ async function putStream (req, res, next, stream = req) { // 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(returnStatus) + res.sendStatus(resourceExists ? 204 : 201) return next() } catch (err) { err.message = 'Can\'t write file/folder: ' + err.message From 43bf813de45cca568bd3a6aa8cd2574506d1247a Mon Sep 17 00:00:00 2001 From: bourgeoa Date: Thu, 30 May 2024 18:26:56 +0200 Subject: [PATCH 5/5] tests and cleaning --- lib/handlers/put.js | 2 -- test/integration/acl-oidc-test.js | 18 +++++++++--------- test/integration/http-test.js | 14 +++++++------- test/integration/validate-tts.js | 2 +- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/handlers/put.js b/lib/handlers/put.js index 38ba1369..ba698ff9 100644 --- a/lib/handlers/put.js +++ b/lib/handlers/put.js @@ -62,7 +62,6 @@ async function putStream (req, res, next, stream = req) { // try { // Obtain details of the target resource let resourceExists = true - let returnStatus = 201 try { // First check if the file already exists await ldp.resourceMapper.mapUrlToFile({ url: req }) @@ -71,7 +70,6 @@ async function putStream (req, res, next, stream = req) { res.sendStatus(412) return next() } - returnStatus = 204 // if content is sent back change to 200 } catch (err) { resourceExists = false } diff --git a/test/integration/acl-oidc-test.js b/test/integration/acl-oidc-test.js index 1de13539..77919a35 100644 --- a/test/integration/acl-oidc-test.js +++ b/test/integration/acl-oidc-test.js @@ -143,7 +143,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.body = '' request.put(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 201) + assert.equal(response.statusCode, 204) done() }) }) @@ -214,7 +214,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.body = '' request.put(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 201) // 403) is this a must ? + assert.equal(response.statusCode, 204) // 403) is this a must ? done() }) }) @@ -240,7 +240,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.body = ' .' request.put(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 201) + assert.equal(response.statusCode, 204) done() }) }) @@ -283,7 +283,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { '\n .' request.put(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 201) + assert.equal(response.statusCode, 204) done() }) }) @@ -456,7 +456,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.body = body request.put(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 201) + assert.equal(response.statusCode, 204) done() }) }) @@ -606,7 +606,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.body = ' .\n' request.put(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 201) + assert.equal(response.statusCode, 204) done() }) }) @@ -809,7 +809,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.body = body request.put(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 201) + assert.equal(response.statusCode, 204) done() }) }) @@ -834,7 +834,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.body = ' .\n' request.put(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 201) + assert.equal(response.statusCode, 204) done() }) }) @@ -860,7 +860,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.body = ' .\n' request.put(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 201) + assert.equal(response.statusCode, 204) done() }) }) diff --git a/test/integration/http-test.js b/test/integration/http-test.js index 4e5d599f..58c9f714 100644 --- a/test/integration/http-test.js +++ b/test/integration/http-test.js @@ -565,7 +565,7 @@ describe('HTTP APIs', function () { server.put('/put-resource-1.ttl') .send(putRequestBody) .set('content-type', 'text/turtle') - .expect(201) + .expect(204) .end(function (err) { if (err) return done(err) if (fs.existsSync(path.join(__dirname, '../resources/put-resource-1.ttl$.txt'))) { @@ -646,28 +646,28 @@ describe('HTTP APIs', function () { .expect(201, done) } ) - it('should return 201 code when trying to put to a container', + it('should return 204 code when trying to put to a container', function (done) { server.put('/foo/bar/test/') .set('content-type', 'text/turtle') .set('link', '; rel="type"') - .expect(201, done) + .expect(204, done) } ) - it('should return 201 when trying to put to a container without content-type', + it('should return 204 when trying to put to a container without content-type', function (done) { server.put('/foo/bar/test/') // .set('content-type', 'text/turtle') .set('link', '; rel="type"') - .expect(201, done) + .expect(204, done) } ) - it('should return 201 code when trying to put to a container', + it('should return 204 code when trying to put to a container', function (done) { server.put('/foo/bar/test/') .set('content-type', 'text/turtle') .set('link', '; rel="type"') - .expect(201, done) + .expect(204, done) } ) it('should return a 400 error when trying to PUT a container with a name that contains a reserved suffix', diff --git a/test/integration/validate-tts.js b/test/integration/validate-tts.js index ae4d5e2d..cd24c38c 100644 --- a/test/integration/validate-tts.js +++ b/test/integration/validate-tts.js @@ -20,7 +20,7 @@ describe('HTTP requests with invalid Turtle syntax', () => { server.put('/invalid1.ttl') .send(invalidTurtleBody) .set('content-type', 'text/turtle') - .expect(201, done) + .expect(204, done) }) it('is not allowed with invalid ACL files', (done) => {