]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/pods.js
Server: error if we add a pod that already exists
[github/Chocobozzz/PeerTube.git] / server / middlewares / pods.js
CommitLineData
1ab844d8
C
1'use strict'
2
49abbbbe 3const constants = require('../initializers/constants')
1ab844d8
C
4
5const podsMiddleware = {
49abbbbe
C
6 setBodyHostsPort,
7 setBodyHostPort
1ab844d8
C
8}
9
49abbbbe 10function setBodyHostsPort (req, res, next) {
b09ce645
C
11 if (!req.body.hosts) return next()
12
49abbbbe
C
13 for (let i = 0; i < req.body.hosts.length; i++) {
14 const hostWithPort = getHostWithPort(req.body.hosts[i])
1ab844d8
C
15
16 // Problem with the url parsing?
49abbbbe 17 if (hostWithPort === null) {
1ab844d8
C
18 return res.sendStatus(500)
19 }
20
49abbbbe 21 req.body.hosts[i] = hostWithPort
1ab844d8
C
22 }
23
24 return next()
25}
26
49abbbbe 27function setBodyHostPort (req, res, next) {
b09ce645
C
28 if (!req.body.host) return next()
29
49abbbbe 30 const hostWithPort = getHostWithPort(req.body.host)
1ab844d8
C
31
32 // Problem with the url parsing?
49abbbbe 33 if (hostWithPort === null) {
1ab844d8
C
34 return res.sendStatus(500)
35 }
36
49abbbbe 37 req.body.host = hostWithPort
1ab844d8
C
38
39 return next()
40}
41
42// ---------------------------------------------------------------------------
43
44module.exports = podsMiddleware
45
46// ---------------------------------------------------------------------------
47
49abbbbe
C
48function getHostWithPort (host) {
49 const splitted = host.split(':')
50
49abbbbe
C
51 // The port was not specified
52 if (splitted.length === 1) {
53 if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443'
54
55 return host + ':80'
1ab844d8
C
56 }
57
49abbbbe 58 return host
1ab844d8 59}