diff options
-rw-r--r-- | server/controllers/api/v1/pods.js | 8 | ||||
-rw-r--r-- | server/middlewares/index.js | 2 | ||||
-rw-r--r-- | server/middlewares/pods.js | 62 |
3 files changed, 71 insertions, 1 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index d509db964..8ffade578 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js | |||
@@ -10,6 +10,7 @@ const friends = require('../../../lib/friends') | |||
10 | const middlewares = require('../../../middlewares') | 10 | const middlewares = require('../../../middlewares') |
11 | const admin = middlewares.admin | 11 | const admin = middlewares.admin |
12 | const oAuth = middlewares.oauth | 12 | const oAuth = middlewares.oauth |
13 | const podsMiddleware = middlewares.pods | ||
13 | const checkSignature = middlewares.secure.checkSignature | 14 | const checkSignature = middlewares.secure.checkSignature |
14 | const validators = middlewares.validators.pods | 15 | const validators = middlewares.validators.pods |
15 | const signatureValidator = middlewares.validators.remote.signature | 16 | const signatureValidator = middlewares.validators.remote.signature |
@@ -19,11 +20,16 @@ const Pod = mongoose.model('Pod') | |||
19 | const Video = mongoose.model('Video') | 20 | const Video = mongoose.model('Video') |
20 | 21 | ||
21 | router.get('/', listPods) | 22 | router.get('/', listPods) |
22 | router.post('/', validators.podsAdd, addPods) | 23 | router.post('/', |
24 | validators.podsAdd, | ||
25 | podsMiddleware.setBodyUrlPort, | ||
26 | addPods | ||
27 | ) | ||
23 | router.post('/makefriends', | 28 | router.post('/makefriends', |
24 | oAuth.authenticate, | 29 | oAuth.authenticate, |
25 | admin.ensureIsAdmin, | 30 | admin.ensureIsAdmin, |
26 | validators.makeFriends, | 31 | validators.makeFriends, |
32 | podsMiddleware.setBodyUrlsPort, | ||
27 | makeFriends | 33 | makeFriends |
28 | ) | 34 | ) |
29 | router.get('/quitfriends', | 35 | router.get('/quitfriends', |
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 | } | ||