1
0
Fork 0

Server: add port when making friends if it is not specified

This commit is contained in:
Chocobozzz 2016-10-01 14:23:50 +02:00
parent 825a5d9c5e
commit 1ab844d859
3 changed files with 71 additions and 1 deletions

View file

@ -10,6 +10,7 @@ const friends = require('../../../lib/friends')
const middlewares = require('../../../middlewares') const middlewares = require('../../../middlewares')
const admin = middlewares.admin const admin = middlewares.admin
const oAuth = middlewares.oauth const oAuth = middlewares.oauth
const podsMiddleware = middlewares.pods
const checkSignature = middlewares.secure.checkSignature const checkSignature = middlewares.secure.checkSignature
const validators = middlewares.validators.pods const validators = middlewares.validators.pods
const signatureValidator = middlewares.validators.remote.signature const signatureValidator = middlewares.validators.remote.signature
@ -19,11 +20,16 @@ const Pod = mongoose.model('Pod')
const Video = mongoose.model('Video') const Video = mongoose.model('Video')
router.get('/', listPods) router.get('/', listPods)
router.post('/', validators.podsAdd, addPods) router.post('/',
validators.podsAdd,
podsMiddleware.setBodyUrlPort,
addPods
)
router.post('/makefriends', router.post('/makefriends',
oAuth.authenticate, oAuth.authenticate,
admin.ensureIsAdmin, admin.ensureIsAdmin,
validators.makeFriends, validators.makeFriends,
podsMiddleware.setBodyUrlsPort,
makeFriends makeFriends
) )
router.get('/quitfriends', router.get('/quitfriends',

View file

@ -3,6 +3,7 @@
const adminMiddleware = require('./admin') const adminMiddleware = require('./admin')
const oauthMiddleware = require('./oauth') const oauthMiddleware = require('./oauth')
const paginationMiddleware = require('./pagination') const paginationMiddleware = require('./pagination')
const podsMiddleware = require('./pods')
const validatorsMiddleware = require('./validators') const validatorsMiddleware = require('./validators')
const searchMiddleware = require('./search') const searchMiddleware = require('./search')
const sortMiddleware = require('./sort') const sortMiddleware = require('./sort')
@ -12,6 +13,7 @@ const middlewares = {
admin: adminMiddleware, admin: adminMiddleware,
oauth: oauthMiddleware, oauth: oauthMiddleware,
pagination: paginationMiddleware, pagination: paginationMiddleware,
pods: podsMiddleware,
search: searchMiddleware, search: searchMiddleware,
secure: secureMiddleware, secure: secureMiddleware,
sort: sortMiddleware, sort: sortMiddleware,

View file

@ -0,0 +1,62 @@
'use strict'
const urlModule = require('url')
const logger = require('../helpers/logger')
const podsMiddleware = {
setBodyUrlsPort: setBodyUrlsPort,
setBodyUrlPort: setBodyUrlPort
}
function setBodyUrlsPort (req, res, next) {
for (let i = 0; i < req.body.urls.length; i++) {
const urlWithPort = getUrlWithPort(req.body.urls[i])
// Problem with the url parsing?
if (urlWithPort === null) {
return res.sendStatus(500)
}
req.body.urls[i] = urlWithPort
}
return next()
}
function setBodyUrlPort (req, res, next) {
const urlWithPort = getUrlWithPort(req.body.url)
// Problem with the url parsing?
if (urlWithPort === null) {
return res.sendStatus(500)
}
req.body.url = urlWithPort
return next()
}
// ---------------------------------------------------------------------------
module.exports = podsMiddleware
// ---------------------------------------------------------------------------
function getUrlWithPort (url) {
const urlObj = urlModule.parse(url)
// Add the port if it is not specified
if (urlObj.port === null) {
if (urlObj.protocol === 'http:') {
return url + ':80'
} else if (urlObj.protocol === 'https:') {
return url + ':443'
} else {
logger.error('Unknown url protocol: ' + urlObj.protocol)
return null
}
}
return url
}