]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/pods.js
116b02b3c8116b63db48d5b22baafd51b905cef3
[github/Chocobozzz/PeerTube.git] / server / middlewares / pods.js
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 }