3 const urlModule = require('url')
5 const logger = require('../helpers/logger')
7 const podsMiddleware = {
8 setBodyUrlsPort: setBodyUrlsPort,
9 setBodyUrlPort: setBodyUrlPort
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])
16 // Problem with the url parsing?
17 if (urlWithPort === null) {
18 return res.sendStatus(500)
21 req.body.urls[i] = urlWithPort
27 function setBodyUrlPort (req, res, next) {
28 const urlWithPort = getUrlWithPort(req.body.url)
30 // Problem with the url parsing?
31 if (urlWithPort === null) {
32 return res.sendStatus(500)
35 req.body.url = urlWithPort
40 // ---------------------------------------------------------------------------
42 module.exports = podsMiddleware
44 // ---------------------------------------------------------------------------
46 function getUrlWithPort (url) {
47 const urlObj = urlModule.parse(url)
49 // Add the port if it is not specified
50 if (urlObj.port === null) {
51 if (urlObj.protocol === 'http:') {
53 } else if (urlObj.protocol === 'https:') {
56 logger.error('Unknown url protocol: ' + urlObj.protocol)