diff options
Diffstat (limited to 'server/middlewares/pods.js')
-rw-r--r-- | server/middlewares/pods.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/server/middlewares/pods.js b/server/middlewares/pods.js new file mode 100644 index 000000000..6e0874a76 --- /dev/null +++ b/server/middlewares/pods.js | |||
@@ -0,0 +1,62 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const urlModule = require('url') | ||
4 | |||
5 | const logger = require('../helpers/logger') | ||
6 | |||
7 | const podsMiddleware = { | ||
8 | setBodyUrlsPort, | ||
9 | setBodyUrlPort | ||
10 | } | ||
11 | |||
12 | function 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 | |||
27 | function 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 | |||
42 | module.exports = podsMiddleware | ||
43 | |||
44 | // --------------------------------------------------------------------------- | ||
45 | |||
46 | function 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 | } | ||