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