Server: forbid to make friends with a non https server
[oweals/peertube.git] / server / middlewares / pods.js
1 'use strict'
2
3 const constants = require('../initializers/constants')
4
5 const podsMiddleware = {
6   setBodyHostsPort,
7   setBodyHostPort
8 }
9
10 function setBodyHostsPort (req, res, next) {
11   for (let i = 0; i < req.body.hosts.length; i++) {
12     const hostWithPort = getHostWithPort(req.body.hosts[i])
13
14     // Problem with the url parsing?
15     if (hostWithPort === null) {
16       return res.sendStatus(500)
17     }
18
19     req.body.hosts[i] = hostWithPort
20   }
21
22   return next()
23 }
24
25 function setBodyHostPort (req, res, next) {
26   const hostWithPort = getHostWithPort(req.body.host)
27
28   // Problem with the url parsing?
29   if (hostWithPort === null) {
30     return res.sendStatus(500)
31   }
32
33   req.body.host = hostWithPort
34
35   return next()
36 }
37
38 // ---------------------------------------------------------------------------
39
40 module.exports = podsMiddleware
41
42 // ---------------------------------------------------------------------------
43
44 function getHostWithPort (host) {
45   const splitted = host.split(':')
46
47   console.log(splitted)
48   // The port was not specified
49   if (splitted.length === 1) {
50     if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443'
51
52     return host + ':80'
53   }
54
55   return host
56 }