aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares')
-rw-r--r--server/middlewares/index.js2
-rw-r--r--server/middlewares/pods.js62
2 files changed, 64 insertions, 0 deletions
diff --git a/server/middlewares/index.js b/server/middlewares/index.js
index 1e294de5f..3f253e31b 100644
--- a/server/middlewares/index.js
+++ b/server/middlewares/index.js
@@ -3,6 +3,7 @@
3const adminMiddleware = require('./admin') 3const adminMiddleware = require('./admin')
4const oauthMiddleware = require('./oauth') 4const oauthMiddleware = require('./oauth')
5const paginationMiddleware = require('./pagination') 5const paginationMiddleware = require('./pagination')
6const podsMiddleware = require('./pods')
6const validatorsMiddleware = require('./validators') 7const validatorsMiddleware = require('./validators')
7const searchMiddleware = require('./search') 8const searchMiddleware = require('./search')
8const sortMiddleware = require('./sort') 9const sortMiddleware = require('./sort')
@@ -12,6 +13,7 @@ const middlewares = {
12 admin: adminMiddleware, 13 admin: adminMiddleware,
13 oauth: oauthMiddleware, 14 oauth: oauthMiddleware,
14 pagination: paginationMiddleware, 15 pagination: paginationMiddleware,
16 pods: podsMiddleware,
15 search: searchMiddleware, 17 search: searchMiddleware,
16 secure: secureMiddleware, 18 secure: secureMiddleware,
17 sort: sortMiddleware, 19 sort: sortMiddleware,
diff --git a/server/middlewares/pods.js b/server/middlewares/pods.js
new file mode 100644
index 000000000..116b02b3c
--- /dev/null
+++ b/server/middlewares/pods.js
@@ -0,0 +1,62 @@
1'use strict'
2
3const urlModule = require('url')
4
5const logger = require('../helpers/logger')
6
7const podsMiddleware = {
8 setBodyUrlsPort: setBodyUrlsPort,
9 setBodyUrlPort: setBodyUrlPort
10}
11
12function setBodyUrlsPort (req, res, next) {
13 for (let i = 0; i < req.body.urls.length; i++) {
14 const urlWithPort = getUrlWithPort(req.body.urls[i])
15
16 // Problem with the url parsing?
17 if (urlWithPort === null) {
18 return res.sendStatus(500)
19 }
20
21 req.body.urls[i] = urlWithPort
22 }
23
24 return next()
25}
26
27function setBodyUrlPort (req, res, next) {
28 const urlWithPort = getUrlWithPort(req.body.url)
29
30 // Problem with the url parsing?
31 if (urlWithPort === null) {
32 return res.sendStatus(500)
33 }
34
35 req.body.url = urlWithPort
36
37 return next()
38}
39
40// ---------------------------------------------------------------------------
41
42module.exports = podsMiddleware
43
44// ---------------------------------------------------------------------------
45
46function getUrlWithPort (url) {
47 const urlObj = urlModule.parse(url)
48
49 // Add the port if it is not specified
50 if (urlObj.port === null) {
51 if (urlObj.protocol === 'http:') {
52 return url + ':80'
53 } else if (urlObj.protocol === 'https:') {
54 return url + ':443'
55 } else {
56 logger.error('Unknown url protocol: ' + urlObj.protocol)
57 return null
58 }
59 }
60
61 return url
62}