aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/pods.js
blob: 6e0874a7699833c0d8ed7804c90926c8c1aa61f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict'

const urlModule = require('url')

const logger = require('../helpers/logger')

const podsMiddleware = {
  setBodyUrlsPort,
  setBodyUrlPort
}

function setBodyUrlsPort (req, res, next) {
  for (let i = 0; i < req.body.urls.length; i++) {
    const urlWithPort = getUrlWithPort(req.body.urls[i])

    // Problem with the url parsing?
    if (urlWithPort === null) {
      return res.sendStatus(500)
    }

    req.body.urls[i] = urlWithPort
  }

  return next()
}

function setBodyUrlPort (req, res, next) {
  const urlWithPort = getUrlWithPort(req.body.url)

  // Problem with the url parsing?
  if (urlWithPort === null) {
    return res.sendStatus(500)
  }

  req.body.url = urlWithPort

  return next()
}

// ---------------------------------------------------------------------------

module.exports = podsMiddleware

// ---------------------------------------------------------------------------

function getUrlWithPort (url) {
  const urlObj = urlModule.parse(url)

  // Add the port if it is not specified
  if (urlObj.port === null) {
    if (urlObj.protocol === 'http:') {
      return url + ':80'
    } else if (urlObj.protocol === 'https:') {
      return url + ':443'
    } else {
      logger.error('Unknown url protocol: ' + urlObj.protocol)
      return null
    }
  }

  return url
}