mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-06 03:50:26 +02:00
Server: add port when making friends if it is not specified
This commit is contained in:
parent
825a5d9c5e
commit
1ab844d859
3 changed files with 71 additions and 1 deletions
62
server/middlewares/pods.js
Normal file
62
server/middlewares/pods.js
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue