]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/pods.js
Server: add database field validations
[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
C
10function setBodyHostsPort (req, res, next) {
11 for (let i = 0; i < req.body.hosts.length; i++) {
12 const hostWithPort = getHostWithPort(req.body.hosts[i])
1ab844d8
C
13
14 // Problem with the url parsing?
49abbbbe 15 if (hostWithPort === null) {
1ab844d8
C
16 return res.sendStatus(500)
17 }
18
49abbbbe 19 req.body.hosts[i] = hostWithPort
1ab844d8
C
20 }
21
22 return next()
23}
24
49abbbbe
C
25function setBodyHostPort (req, res, next) {
26 const hostWithPort = getHostWithPort(req.body.host)
1ab844d8
C
27
28 // Problem with the url parsing?
49abbbbe 29 if (hostWithPort === null) {
1ab844d8
C
30 return res.sendStatus(500)
31 }
32
49abbbbe 33 req.body.host = hostWithPort
1ab844d8
C
34
35 return next()
36}
37
38// ---------------------------------------------------------------------------
39
40module.exports = podsMiddleware
41
42// ---------------------------------------------------------------------------
43
49abbbbe
C
44function getHostWithPort (host) {
45 const splitted = host.split(':')
46
49abbbbe
C
47 // The port was not specified
48 if (splitted.length === 1) {
49 if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443'
50
51 return host + ':80'
1ab844d8
C
52 }
53
49abbbbe 54 return host
1ab844d8 55}