From a39691c97ee296757466bd5079111e756586d6c6 Mon Sep 17 00:00:00 2001 From: Georges Haidar Date: Mon, 18 Nov 2019 00:23:10 +0000 Subject: [PATCH 1/2] fix: handle missing trailing slash on directories --- index.js | 10 +++--- test/static.test.js | 75 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index b0a3dd06..ad1f9fde 100644 --- a/index.js +++ b/index.js @@ -77,12 +77,14 @@ function fastifyStatic (fastify, opts, next) { stream.on('headers', setHeaders) } - if (opts.redirect === true) { - stream.on('directory', function (res, path) { + stream.on('directory', function (res, path) { + if (opts.redirect === true) { const parsed = url.parse(request.raw.url) reply.redirect(301, parsed.pathname + '/' + (parsed.search || '')) - }) - } + } else { + reply.callNotFound() + } + }) stream.on('error', function (err) { if (err) { diff --git a/test/static.test.js b/test/static.test.js index 12b92457..5aa2811d 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -1850,3 +1850,78 @@ t.test('register /static with redirect true and wildcard false', t => { }) }) }) + +t.test('trailing slash behavior with redirect = false', t => { + t.plan(6) + + const fastify = Fastify() + fastify.register(fastifyStatic, { + root: path.join(__dirname, '/static'), + prefix: '/static', + redirect: false + }) + fastify.server.unref() + + t.tearDown(fastify.close.bind(fastify)) + + fastify.listen(0, err => { + t.error(err) + + const host = 'http://localhost:' + fastify.server.address().port + + t.test('prefix with no trailing slash => 404', t => { + t.plan(2) + simple.concat({ + method: 'GET', + url: host + '/static' + }, (err, response) => { + t.error(err) + t.strictEqual(response.statusCode, 404) + }) + }) + + t.test('prefix with trailing trailing slash => 200', t => { + t.plan(2) + simple.concat({ + method: 'GET', + url: host + '/static/' + }, (err, response) => { + t.error(err) + t.strictEqual(response.statusCode, 200) + }) + }) + + t.test('deep path with no index.html or trailing slash => 404', t => { + t.plan(2) + simple.concat({ + method: 'GET', + url: host + '/static/deep/path' + }, (err, response) => { + t.error(err) + t.strictEqual(response.statusCode, 404) + }) + }) + + t.test('deep path with index.html but no trailing slash => 404', t => { + t.plan(2) + simple.concat({ + method: 'GET', + url: host + '/static/deep/path/for/test' + }, (err, response) => { + t.error(err) + t.strictEqual(response.statusCode, 404) + }) + }) + + t.test('deep path with index.html and trailing slash => 200', t => { + t.plan(2) + simple.concat({ + method: 'GET', + url: host + '/static/deep/path/for/test/' + }, (err, response) => { + t.error(err) + t.strictEqual(response.statusCode, 200) + }) + }) + }) +}) From afda86cd6ae5c3cf0093512969755584640b5eb9 Mon Sep 17 00:00:00 2001 From: Georges Haidar Date: Mon, 18 Nov 2019 00:26:14 +0000 Subject: [PATCH 2/2] docs: update docs --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index e3a0d827..8ef24cd2 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,9 @@ If set to `true`, `fastify-static` redirects to the directory with a trailing sl This option cannot be set to `true` with `wildcard` set to `false` on a server with `ignoreTrailingSlash` set to `true`. +If this option is set to `false`, then requesting directories without trailing +slash will trigger your app's 404 handler using `reply.callNotFound()`. + #### `wildcard` Default: `true`