X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fpods.js;h=2647f9ff0b342849007fbca19cfcb552b72ee339;hb=4e284e97b95d1fe93378b90061272e3abc875078;hp=6e0874a7699833c0d8ed7804c90926c8c1aa61f9;hpb=a6375e69668ea42e19531c6bc68dcd37f3f7cbd7;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/pods.js b/server/middlewares/pods.js index 6e0874a76..2647f9ff0 100644 --- a/server/middlewares/pods.js +++ b/server/middlewares/pods.js @@ -1,38 +1,40 @@ 'use strict' -const urlModule = require('url') - -const logger = require('../helpers/logger') +const constants = require('../initializers/constants') const podsMiddleware = { - setBodyUrlsPort, - setBodyUrlPort + setBodyHostsPort, + setBodyHostPort } -function setBodyUrlsPort (req, res, next) { - for (let i = 0; i < req.body.urls.length; i++) { - const urlWithPort = getUrlWithPort(req.body.urls[i]) +function setBodyHostsPort (req, res, next) { + if (!req.body.hosts) return next() + + for (let i = 0; i < req.body.hosts.length; i++) { + const hostWithPort = getHostWithPort(req.body.hosts[i]) // Problem with the url parsing? - if (urlWithPort === null) { + if (hostWithPort === null) { return res.sendStatus(500) } - req.body.urls[i] = urlWithPort + req.body.hosts[i] = hostWithPort } return next() } -function setBodyUrlPort (req, res, next) { - const urlWithPort = getUrlWithPort(req.body.url) +function setBodyHostPort (req, res, next) { + if (!req.body.host) return next() + + const hostWithPort = getHostWithPort(req.body.host) // Problem with the url parsing? - if (urlWithPort === null) { + if (hostWithPort === null) { return res.sendStatus(500) } - req.body.url = urlWithPort + req.body.host = hostWithPort return next() } @@ -43,20 +45,15 @@ 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 - } +function getHostWithPort (host) { + const splitted = host.split(':') + + // The port was not specified + if (splitted.length === 1) { + if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443' + + return host + ':80' } - return url + return host }