2016-02-07 11:23:23 +01:00
|
|
|
'use strict'
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const replay = require('request-replay')
|
2016-06-18 16:13:54 +02:00
|
|
|
const request = require('request')
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const constants = require('../initializers/constants')
|
2016-07-01 16:22:36 +02:00
|
|
|
const peertubeCrypto = require('./peertube-crypto')
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const requests = {
|
2016-10-02 12:19:02 +02:00
|
|
|
makeRetryRequest,
|
|
|
|
makeSecureRequest
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-06-18 16:13:54 +02:00
|
|
|
function makeRetryRequest (params, callback) {
|
|
|
|
replay(
|
|
|
|
request(params, callback),
|
|
|
|
{
|
|
|
|
retries: constants.RETRY_REQUESTS,
|
|
|
|
factor: 3,
|
|
|
|
maxTimeout: Infinity,
|
|
|
|
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-06-18 16:13:54 +02:00
|
|
|
function makeSecureRequest (params, callback) {
|
|
|
|
const requestParams = {
|
2016-11-14 20:03:04 +01:00
|
|
|
url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-06-18 16:13:54 +02:00
|
|
|
// Add data with POST requst ?
|
|
|
|
if (params.method === 'POST') {
|
|
|
|
requestParams.json = {}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-06-18 16:13:54 +02:00
|
|
|
// Add signature if it is specified in the params
|
|
|
|
if (params.sign === true) {
|
2016-11-14 20:03:04 +01:00
|
|
|
const host = constants.CONFIG.WEBSERVER.HOST
|
|
|
|
|
2016-06-18 16:13:54 +02:00
|
|
|
requestParams.json.signature = {
|
2016-11-14 20:03:04 +01:00
|
|
|
host,
|
|
|
|
signature: peertubeCrypto.sign(host)
|
2016-06-18 16:13:54 +02:00
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-06-18 16:13:54 +02:00
|
|
|
// If there are data informations
|
|
|
|
if (params.data) {
|
2016-11-27 18:25:35 +01:00
|
|
|
requestParams.json.data = params.data
|
|
|
|
request.post(requestParams, callback)
|
2016-02-07 11:23:23 +01:00
|
|
|
} else {
|
2016-06-18 16:13:54 +02:00
|
|
|
// No data
|
|
|
|
request.post(requestParams, callback)
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
2016-06-18 16:13:54 +02:00
|
|
|
} else {
|
|
|
|
request.get(requestParams, callback)
|
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
module.exports = requests
|