diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-01 14:23:50 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-01 14:23:50 +0200 |
commit | 1ab844d8593fa84dfe65cad6b5bbf0e45efd7886 (patch) | |
tree | 2687c7f66caff9ed57b4adde2e6ae804016295d3 /server/middlewares | |
parent | 825a5d9c5e6d8cbe005b44499aba4ce4ba069fe3 (diff) | |
download | PeerTube-1ab844d8593fa84dfe65cad6b5bbf0e45efd7886.tar.gz PeerTube-1ab844d8593fa84dfe65cad6b5bbf0e45efd7886.tar.zst PeerTube-1ab844d8593fa84dfe65cad6b5bbf0e45efd7886.zip |
Server: add port when making friends if it is not specified
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/index.js | 2 | ||||
-rw-r--r-- | server/middlewares/pods.js | 62 |
2 files changed, 64 insertions, 0 deletions
diff --git a/server/middlewares/index.js b/server/middlewares/index.js index 1e294de5f..3f253e31b 100644 --- a/server/middlewares/index.js +++ b/server/middlewares/index.js | |||
@@ -3,6 +3,7 @@ | |||
3 | const adminMiddleware = require('./admin') | 3 | const adminMiddleware = require('./admin') |
4 | const oauthMiddleware = require('./oauth') | 4 | const oauthMiddleware = require('./oauth') |
5 | const paginationMiddleware = require('./pagination') | 5 | const paginationMiddleware = require('./pagination') |
6 | const podsMiddleware = require('./pods') | ||
6 | const validatorsMiddleware = require('./validators') | 7 | const validatorsMiddleware = require('./validators') |
7 | const searchMiddleware = require('./search') | 8 | const searchMiddleware = require('./search') |
8 | const sortMiddleware = require('./sort') | 9 | const sortMiddleware = require('./sort') |
@@ -12,6 +13,7 @@ const middlewares = { | |||
12 | admin: adminMiddleware, | 13 | admin: adminMiddleware, |
13 | oauth: oauthMiddleware, | 14 | oauth: oauthMiddleware, |
14 | pagination: paginationMiddleware, | 15 | pagination: paginationMiddleware, |
16 | pods: podsMiddleware, | ||
15 | search: searchMiddleware, | 17 | search: searchMiddleware, |
16 | secure: secureMiddleware, | 18 | secure: secureMiddleware, |
17 | sort: sortMiddleware, | 19 | sort: sortMiddleware, |
diff --git a/server/middlewares/pods.js b/server/middlewares/pods.js new file mode 100644 index 000000000..116b02b3c --- /dev/null +++ b/server/middlewares/pods.js | |||
@@ -0,0 +1,62 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const urlModule = require('url') | ||
4 | |||
5 | const logger = require('../helpers/logger') | ||
6 | |||
7 | const podsMiddleware = { | ||
8 | setBodyUrlsPort: setBodyUrlsPort, | ||
9 | setBodyUrlPort: setBodyUrlPort | ||
10 | } | ||
11 | |||
12 | function setBodyUrlsPort (req, res, next) { | ||
13 | for (let i = 0; i < req.body.urls.length; i++) { | ||
14 | const urlWithPort = getUrlWithPort(req.body.urls[i]) | ||
15 | |||
16 | // Problem with the url parsing? | ||
17 | if (urlWithPort === null) { | ||
18 | return res.sendStatus(500) | ||
19 | } | ||
20 | |||
21 | req.body.urls[i] = urlWithPort | ||
22 | } | ||
23 | |||
24 | return next() | ||
25 | } | ||
26 | |||
27 | function setBodyUrlPort (req, res, next) { | ||
28 | const urlWithPort = getUrlWithPort(req.body.url) | ||
29 | |||
30 | // Problem with the url parsing? | ||
31 | if (urlWithPort === null) { | ||
32 | return res.sendStatus(500) | ||
33 | } | ||
34 | |||
35 | req.body.url = urlWithPort | ||
36 | |||
37 | return next() | ||
38 | } | ||
39 | |||
40 | // --------------------------------------------------------------------------- | ||
41 | |||
42 | module.exports = podsMiddleware | ||
43 | |||
44 | // --------------------------------------------------------------------------- | ||
45 | |||
46 | function getUrlWithPort (url) { | ||
47 | const urlObj = urlModule.parse(url) | ||
48 | |||
49 | // Add the port if it is not specified | ||
50 | if (urlObj.port === null) { | ||
51 | if (urlObj.protocol === 'http:') { | ||
52 | return url + ':80' | ||
53 | } else if (urlObj.protocol === 'https:') { | ||
54 | return url + ':443' | ||
55 | } else { | ||
56 | logger.error('Unknown url protocol: ' + urlObj.protocol) | ||
57 | return null | ||
58 | } | ||
59 | } | ||
60 | |||
61 | return url | ||
62 | } | ||