aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/requests.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/requests.js')
-rw-r--r--server/helpers/requests.js114
1 files changed, 43 insertions, 71 deletions
diff --git a/server/helpers/requests.js b/server/helpers/requests.js
index 1e1bb4111..871342d60 100644
--- a/server/helpers/requests.js
+++ b/server/helpers/requests.js
@@ -1,12 +1,10 @@
1'use strict' 1'use strict'
2 2
3const async = require('async')
4const config = require('config') 3const config = require('config')
5const request = require('request')
6const replay = require('request-replay') 4const replay = require('request-replay')
5const request = require('request')
7 6
8const constants = require('../initializers/constants') 7const constants = require('../initializers/constants')
9const logger = require('./logger')
10const peertubeCrypto = require('./peertubeCrypto') 8const peertubeCrypto = require('./peertubeCrypto')
11 9
12const http = config.get('webserver.https') ? 'https' : 'http' 10const http = config.get('webserver.https') ? 'https' : 'http'
@@ -14,93 +12,67 @@ const host = config.get('webserver.host')
14const port = config.get('webserver.port') 12const port = config.get('webserver.port')
15 13
16const requests = { 14const requests = {
17 makeMultipleRetryRequest: makeMultipleRetryRequest 15 makeRetryRequest: makeRetryRequest,
16 makeSecureRequest: makeSecureRequest
18} 17}
19 18
20function makeMultipleRetryRequest (allData, pods, callbackEach, callback) { 19function makeRetryRequest (params, callback) {
21 if (!callback) { 20 replay(
22 callback = callbackEach 21 request(params, callback),
23 callbackEach = null 22 {
24 } 23 retries: constants.RETRY_REQUESTS,
24 factor: 3,
25 maxTimeout: Infinity,
26 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
27 }
28 )
29}
25 30
26 const url = http + '://' + host + ':' + port 31function makeSecureRequest (params, callback) {
27 let signature 32 const myUrl = http + '://' + host + ':' + port
28 33
29 // Add signature if it is specified in the params 34 const requestParams = {
30 if (allData.method === 'POST' && allData.data && allData.sign === true) { 35 url: params.toPod.url + params.path
31 signature = peertubeCrypto.sign(url)
32 } 36 }
33 37
34 // Make a request for each pod 38 // Add data with POST requst ?
35 async.each(pods, function (pod, callbackEachAsync) { 39 if (params.method === 'POST') {
36 function callbackEachRetryRequest (err, response, body, url, pod) { 40 requestParams.json = {}
37 if (callbackEach !== null) {
38 callbackEach(err, response, body, url, pod, function () {
39 callbackEachAsync()
40 })
41 } else {
42 callbackEachAsync()
43 }
44 }
45 41
46 const params = { 42 // Add signature if it is specified in the params
47 url: pod.url + allData.path, 43 if (params.sign === true) {
48 method: allData.method 44 requestParams.json.signature = {
45 url: myUrl,
46 signature: peertubeCrypto.sign(myUrl)
47 }
49 } 48 }
50 49
51 // Add data with POST requst ? 50 // If there are data informations
52 if (allData.method === 'POST' && allData.data) { 51 if (params.data) {
53 // Encrypt data ? 52 // Encrypt data
54 if (allData.encrypt === true) { 53 if (params.encrypt === true) {
55 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(allData.data), function (err, encrypted) { 54 peertubeCrypto.encrypt(params.toPod.publicKey, JSON.stringify(params.data), function (err, encrypted) {
56 if (err) return callback(err) 55 if (err) return callback(err)
57 56
58 params.json = { 57 requestParams.json.data = encrypted.data
59 data: encrypted.data, 58 requestParams.json.key = encrypted.key
60 key: encrypted.key
61 }
62 59
63 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) 60 request.post(requestParams, callback)
64 }) 61 })
65 } else { 62 } else {
66 params.json = { data: allData.data } 63 // No encryption
67 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) 64 requestParams.json.data = params.data
65 request.post(requestParams, callback)
68 } 66 }
69 } else { 67 } else {
70 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) 68 // No data
69 request.post(requestParams, callback)
71 } 70 }
72 }, callback) 71 } else {
72 request.get(requestParams, callback)
73 }
73} 74}
74 75
75// --------------------------------------------------------------------------- 76// ---------------------------------------------------------------------------
76 77
77module.exports = requests 78module.exports = requests
78
79// ---------------------------------------------------------------------------
80
81function makeRetryRequest (params, fromUrl, toPod, signature, callbackEach) {
82 // Append the signature
83 if (signature) {
84 params.json.signature = {
85 url: fromUrl,
86 signature: signature
87 }
88 }
89
90 logger.debug('Make retry requests to %s.', toPod.url)
91
92 replay(
93 request.post(params, function (err, response, body) {
94 callbackEach(err, response, body, params.url, toPod)
95 }),
96 {
97 retries: constants.REQUEST_RETRIES,
98 factor: 3,
99 maxTimeout: Infinity,
100 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
101 }
102 ).on('replay', function (replay) {
103 logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
104 params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
105 })
106}