diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/middlewares/pods.ts | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/middlewares/pods.ts')
-rw-r--r-- | server/middlewares/pods.ts | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/server/middlewares/pods.ts b/server/middlewares/pods.ts new file mode 100644 index 000000000..e405f265e --- /dev/null +++ b/server/middlewares/pods.ts | |||
@@ -0,0 +1,57 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const constants = require('../initializers/constants') | ||
4 | |||
5 | function setBodyHostsPort (req, res, next) { | ||
6 | if (!req.body.hosts) return next() | ||
7 | |||
8 | for (let i = 0; i < req.body.hosts.length; i++) { | ||
9 | const hostWithPort = getHostWithPort(req.body.hosts[i]) | ||
10 | |||
11 | // Problem with the url parsing? | ||
12 | if (hostWithPort === null) { | ||
13 | return res.sendStatus(500) | ||
14 | } | ||
15 | |||
16 | req.body.hosts[i] = hostWithPort | ||
17 | } | ||
18 | |||
19 | return next() | ||
20 | } | ||
21 | |||
22 | function setBodyHostPort (req, res, next) { | ||
23 | if (!req.body.host) return next() | ||
24 | |||
25 | const hostWithPort = getHostWithPort(req.body.host) | ||
26 | |||
27 | // Problem with the url parsing? | ||
28 | if (hostWithPort === null) { | ||
29 | return res.sendStatus(500) | ||
30 | } | ||
31 | |||
32 | req.body.host = hostWithPort | ||
33 | |||
34 | return next() | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | |||
39 | export { | ||
40 | setBodyHostsPort, | ||
41 | setBodyHostPort | ||
42 | } | ||
43 | |||
44 | // --------------------------------------------------------------------------- | ||
45 | |||
46 | function getHostWithPort (host) { | ||
47 | const splitted = host.split(':') | ||
48 | |||
49 | // The port was not specified | ||
50 | if (splitted.length === 1) { | ||
51 | if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443' | ||
52 | |||
53 | return host + ':80' | ||
54 | } | ||
55 | |||
56 | return host | ||
57 | } | ||