diff options
Diffstat (limited to 'server/middlewares/servers.ts')
-rw-r--r-- | server/middlewares/servers.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/server/middlewares/servers.ts b/server/middlewares/servers.ts new file mode 100644 index 000000000..eaf9aa144 --- /dev/null +++ b/server/middlewares/servers.ts | |||
@@ -0,0 +1,58 @@ | |||
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 | function setBodyHostPort (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
24 | if (!req.body.host) return next() | ||
25 | |||
26 | const hostWithPort = getHostWithPort(req.body.host) | ||
27 | |||
28 | // Problem with the url parsing? | ||
29 | if (hostWithPort === null) { | ||
30 | return res.sendStatus(500) | ||
31 | } | ||
32 | |||
33 | req.body.host = hostWithPort | ||
34 | |||
35 | return next() | ||
36 | } | ||
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | export { | ||
41 | setBodyHostsPort, | ||
42 | setBodyHostPort | ||
43 | } | ||
44 | |||
45 | // --------------------------------------------------------------------------- | ||
46 | |||
47 | function getHostWithPort (host: string) { | ||
48 | const splitted = host.split(':') | ||
49 | |||
50 | // The port was not specified | ||
51 | if (splitted.length === 1) { | ||
52 | if (REMOTE_SCHEME.HTTP === 'https') return host + ':443' | ||
53 | |||
54 | return host + ':80' | ||
55 | } | ||
56 | |||
57 | return host | ||
58 | } | ||