Check correctly activitypub headers
[oweals/peertube.git] / server / middlewares / servers.ts
1 import 'express-validator'
2 import * as express from 'express'
3
4 import { REMOTE_SCHEME } from '../initializers'
5
6 function setBodyHostsPort (req: express.Request, res: express.Response, next: express.NextFunction) {
7   if (!req.body.hosts) return next()
8
9   for (let i = 0; i < req.body.hosts.length; i++) {
10     const hostWithPort = getHostWithPort(req.body.hosts[i])
11
12     // Problem with the url parsing?
13     if (hostWithPort === null) {
14       return res.sendStatus(500)
15     }
16
17     req.body.hosts[i] = hostWithPort
18   }
19
20   return next()
21 }
22
23 // ---------------------------------------------------------------------------
24
25 export {
26   setBodyHostsPort
27 }
28
29 // ---------------------------------------------------------------------------
30
31 function getHostWithPort (host: string) {
32   const splitted = host.split(':')
33
34   // The port was not specified
35   if (splitted.length === 1) {
36     if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
37
38     return host + ':80'
39   }
40
41   return host
42 }