]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/pods.ts
Handle express-validator error on the client side and fix #96 (#98)
[github/Chocobozzz/PeerTube.git] / server / middlewares / pods.ts
CommitLineData
69818c93
C
1import 'express-validator'
2import * as express from 'express'
3
e02643f3 4import { REMOTE_SCHEME } from '../initializers'
1ab844d8 5
69818c93 6function setBodyHostsPort (req: express.Request, res: express.Response, next: express.NextFunction) {
b09ce645
C
7 if (!req.body.hosts) return next()
8
49abbbbe
C
9 for (let i = 0; i < req.body.hosts.length; i++) {
10 const hostWithPort = getHostWithPort(req.body.hosts[i])
1ab844d8
C
11
12 // Problem with the url parsing?
49abbbbe 13 if (hostWithPort === null) {
1ab844d8
C
14 return res.sendStatus(500)
15 }
16
49abbbbe 17 req.body.hosts[i] = hostWithPort
1ab844d8
C
18 }
19
20 return next()
21}
22
69818c93 23function setBodyHostPort (req: express.Request, res: express.Response, next: express.NextFunction) {
b09ce645
C
24 if (!req.body.host) return next()
25
49abbbbe 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
65fcc311
C
40export {
41 setBodyHostsPort,
42 setBodyHostPort
43}
1ab844d8
C
44
45// ---------------------------------------------------------------------------
46
69818c93 47function getHostWithPort (host: string) {
49abbbbe
C
48 const splitted = host.split(':')
49
49abbbbe
C
50 // The port was not specified
51 if (splitted.length === 1) {
e02643f3 52 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
49abbbbe
C
53
54 return host + ':80'
1ab844d8
C
55 }
56
49abbbbe 57 return host
1ab844d8 58}