summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/web/middleware/checkURiValid.js14
-rw-r--r--lib/web/middleware/redirectWithoutTrailingSlashes.js17
2 files changed, 31 insertions, 0 deletions
diff --git a/lib/web/middleware/checkURiValid.js b/lib/web/middleware/checkURiValid.js
new file mode 100644
index 00000000..88065e79
--- /dev/null
+++ b/lib/web/middleware/checkURiValid.js
@@ -0,0 +1,14 @@
+'use strict'
+
+const logger = require('../../logger')
+const response = require('../../response')
+
+module.exports = function (req, res, next) {
+ try {
+ decodeURIComponent(req.path)
+ } catch (err) {
+ logger.error(err)
+ return response.errorBadRequest(res)
+ }
+ next()
+}
diff --git a/lib/web/middleware/redirectWithoutTrailingSlashes.js b/lib/web/middleware/redirectWithoutTrailingSlashes.js
new file mode 100644
index 00000000..fbaba617
--- /dev/null
+++ b/lib/web/middleware/redirectWithoutTrailingSlashes.js
@@ -0,0 +1,17 @@
+'use strict'
+
+const config = require('../../config')
+
+module.exports = function (req, res, next) {
+ if (req.method === 'GET' && req.path.substr(-1) === '/' && req.path.length > 1) {
+ const queryString = req.url.slice(req.path.length)
+ const urlPath = req.path.slice(0, -1)
+ let serverURL = config.serverurl
+ if (config.urlpath) {
+ serverURL = serverURL.slice(0, -(config.urlpath.length + 1))
+ }
+ res.redirect(301, serverURL + urlPath + queryString)
+ } else {
+ next()
+ }
+}