aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server/controllers/api/v1/pods.js8
-rw-r--r--server/middlewares/index.js2
-rw-r--r--server/middlewares/pods.js62
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')
10const middlewares = require('../../../middlewares') 10const middlewares = require('../../../middlewares')
11const admin = middlewares.admin 11const admin = middlewares.admin
12const oAuth = middlewares.oauth 12const oAuth = middlewares.oauth
13const podsMiddleware = middlewares.pods
13const checkSignature = middlewares.secure.checkSignature 14const checkSignature = middlewares.secure.checkSignature
14const validators = middlewares.validators.pods 15const validators = middlewares.validators.pods
15const signatureValidator = middlewares.validators.remote.signature 16const signatureValidator = middlewares.validators.remote.signature
@@ -19,11 +20,16 @@ const Pod = mongoose.model('Pod')
19const Video = mongoose.model('Video') 20const Video = mongoose.model('Video')
20 21
21router.get('/', listPods) 22router.get('/', listPods)
22router.post('/', validators.podsAdd, addPods) 23router.post('/',
24 validators.podsAdd,
25 podsMiddleware.setBodyUrlPort,
26 addPods
27)
23router.post('/makefriends', 28router.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)
29router.get('/quitfriends', 35router.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 @@
3const adminMiddleware = require('./admin') 3const adminMiddleware = require('./admin')
4const oauthMiddleware = require('./oauth') 4const oauthMiddleware = require('./oauth')
5const paginationMiddleware = require('./pagination') 5const paginationMiddleware = require('./pagination')
6const podsMiddleware = require('./pods')
6const validatorsMiddleware = require('./validators') 7const validatorsMiddleware = require('./validators')
7const searchMiddleware = require('./search') 8const searchMiddleware = require('./search')
8const sortMiddleware = require('./sort') 9const 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
3const urlModule = require('url')
4
5const logger = require('../helpers/logger')
6
7const podsMiddleware = {
8 setBodyUrlsPort: setBodyUrlsPort,
9 setBodyUrlPort: setBodyUrlPort
10}
11
12function 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
27function 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
42module.exports = podsMiddleware
43
44// ---------------------------------------------------------------------------
45
46function 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}