]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/requests.js
Server: Add NSFW in user profile
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.js
CommitLineData
9f10b292 1'use strict'
dac0a531 2
f0f5567b 3const replay = require('request-replay')
528a9efa 4const request = require('request')
dac0a531 5
f0f5567b 6const constants = require('../initializers/constants')
5f698b82 7const peertubeCrypto = require('./peertube-crypto')
dac0a531 8
f0f5567b 9const requests = {
c4403b29
C
10 makeRetryRequest,
11 makeSecureRequest
9f10b292 12}
dac0a531 13
528a9efa
C
14function makeRetryRequest (params, callback) {
15 replay(
16 request(params, callback),
17 {
18 retries: constants.RETRY_REQUESTS,
19 factor: 3,
20 maxTimeout: Infinity,
21 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
22 }
23 )
24}
dac0a531 25
528a9efa 26function makeSecureRequest (params, callback) {
528a9efa 27 const requestParams = {
49abbbbe 28 url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path
9f10b292 29 }
dac0a531 30
bdfbd4f1
C
31 if (params.method !== 'POST') {
32 return callback(new Error('Cannot make a secure request with a non POST method.'))
33 }
34
35 requestParams.json = {}
dac0a531 36
bdfbd4f1
C
37 // Add signature if it is specified in the params
38 if (params.sign === true) {
39 const host = constants.CONFIG.WEBSERVER.HOST
40
41 let dataToSign
528a9efa 42 if (params.data) {
bdfbd4f1 43 dataToSign = dataToSign = params.data
9f10b292 44 } else {
bdfbd4f1
C
45 // We do not have data to sign so we just take our host
46 // It is not ideal but the connection should be in HTTPS
47 dataToSign = host
9f10b292 48 }
bdfbd4f1
C
49
50 requestParams.json.signature = {
51 host, // Which host we pretend to be
52 signature: peertubeCrypto.sign(dataToSign)
53 }
54 }
55
56 // If there are data informations
57 if (params.data) {
58 requestParams.json.data = params.data
528a9efa 59 }
bdfbd4f1 60
9e167724
C
61 console.log(requestParams.json.data)
62
bdfbd4f1 63 request.post(requestParams, callback)
9f10b292 64}
dac0a531 65
9f10b292 66// ---------------------------------------------------------------------------
dac0a531 67
9f10b292 68module.exports = requests