]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/requests.js
Server: request scheduler refractoring
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.js
index 17b1127c00f2dccb0fd5624c47227b5ec5478f1a..42786411725ce8980bd3d017497567eb1f10ccb9 100644 (file)
 'use strict'
 
-const async = require('async')
-const config = require('config')
-const request = require('request')
 const replay = require('request-replay')
+const request = require('request')
 
 const constants = require('../initializers/constants')
-const logger = require('./logger')
-const peertubeCrypto = require('./peertubeCrypto')
-
-const http = config.get('webserver.https') ? 'https' : 'http'
-const host = config.get('webserver.host')
-const port = config.get('webserver.port')
+const peertubeCrypto = require('./peertube-crypto')
 
 const requests = {
-  makeMultipleRetryRequest: makeMultipleRetryRequest
+  makeRetryRequest,
+  makeSecureRequest
 }
 
-function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) {
-  if (!callback) {
-    callback = callbackEach
-    callbackEach = null
+function makeRetryRequest (params, callback) {
+  replay(
+    request(params, callback),
+    {
+      retries: constants.RETRY_REQUESTS,
+      factor: 3,
+      maxTimeout: Infinity,
+      errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
+    }
+  )
+}
+
+function makeSecureRequest (params, callback) {
+  const requestParams = {
+    url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path
+  }
+
+  if (params.method !== 'POST') {
+    return callback(new Error('Cannot make a secure request with a non POST method.'))
   }
 
-  const url = http + '://' + host + ':' + port
-  let signature
+  requestParams.json = {}
 
   // Add signature if it is specified in the params
-  if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
-    signature = peertubeCrypto.sign(url)
-  }
+  if (params.sign === true) {
+    const host = constants.CONFIG.WEBSERVER.HOST
 
-  // Make a request for each pod
-  async.each(pods, function (pod, callback_each_async) {
-    function callbackEachRetryRequest (err, response, body, url, pod) {
-      if (callbackEach !== null) {
-        callbackEach(err, response, body, url, pod, function () {
-          callback_each_async()
-        })
-      } else {
-        callback_each_async()
-      }
+    let dataToSign
+    if (params.data) {
+      dataToSign = dataToSign = params.data
+    } else {
+      // We do not have data to sign so we just take our host
+      // It is not ideal but the connection should be in HTTPS
+      dataToSign = host
     }
 
-    const params = {
-      url: pod.url + all_data.path,
-      method: all_data.method
+    requestParams.json.signature = {
+      host, // Which host we pretend to be
+      signature: peertubeCrypto.sign(dataToSign)
     }
+  }
 
-    // Add data with POST requst ?
-    if (all_data.method === 'POST' && all_data.data) {
-      // Encrypt data ?
-      if (all_data.encrypt === true) {
-        peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
-          if (err) return callback(err)
+  // If there are data informations
+  if (params.data) {
+    requestParams.json.data = params.data
+  }
 
-          params.json = {
-            data: encrypted.data,
-            key: encrypted.key
-          }
+  console.log(requestParams.json.data)
 
-          makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
-        })
-      } else {
-        params.json = { data: all_data.data }
-        makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
-      }
-    } else {
-      makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
-    }
-  }, callback)
+  request.post(requestParams, callback)
 }
 
 // ---------------------------------------------------------------------------
 
 module.exports = requests
-
-// ---------------------------------------------------------------------------
-
-function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
-  // Append the signature
-  if (signature) {
-    params.json.signature = {
-      url: from_url,
-      signature: signature
-    }
-  }
-
-  logger.debug('Make retry requests to %s.', to_pod.url)
-
-  replay(
-    request.post(params, function (err, response, body) {
-      callbackEach(err, response, body, params.url, to_pod)
-    }),
-    {
-      retries: constants.REQUEST_RETRIES,
-      factor: 3,
-      maxTimeout: Infinity,
-      errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
-    }
-  ).on('replay', function (replay) {
-    logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
-      params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
-  })
-}