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