Move content-length calculation to http module

- consolidate http / https functionality to single function
pull/551/head
Jason Fischer 2 years ago
parent ccfafe1b31
commit 9f03d18e49
No known key found for this signature in database

@ -20,7 +20,6 @@ export async function sendJsonRpcRequest(url, method, params, username, password
const client = new JSONRPCClient(async (rpcRequest) => {
const body = JSON.stringify(rpcRequest);
headers['content-length'] = Buffer.byteLength(body);
const httpRequestParams = {
method: "POST",
headers,
@ -49,7 +48,7 @@ export async function sendJsonRpcRequest(url, method, params, username, password
}
catch (e) {
if (e instanceof JSONRPCErrorException) {
logger.warn("Error calling JSONPRC endpoint: %s. %s", url, e.message);
logger.debug("Error calling JSONPRC endpoint: %s. %s", url, e.message);
return [200, "application/json", JSON.stringify({result: null, error: {code: e.code, message: e.message}})];
}

@ -18,10 +18,15 @@ function addCookieHandler(url, params) {
};
}
export function httpsRequest(url, params) {
function handleRequest(requestor, url, params) {
return new Promise((resolve, reject) => {
addCookieHandler(url, params);
const request = https.request(url, params, (response) => {
if (params?.body) {
params.headers = params.headers ?? {};
params.headers['content-length'] = Buffer.byteLength(params.body);
}
const request = requestor.request(url, params, (response) => {
const data = [];
response.on("data", (chunk) => {
@ -38,7 +43,7 @@ export function httpsRequest(url, params) {
reject([500, error]);
});
if (params.body) {
if (params?.body) {
request.write(params.body);
}
@ -46,32 +51,12 @@ export function httpsRequest(url, params) {
});
}
export function httpRequest(url, params) {
return new Promise((resolve, reject) => {
addCookieHandler(url, params);
const request = http.request(url, params, (response) => {
const data = [];
response.on("data", (chunk) => {
data.push(chunk);
});
response.on("end", () => {
addCookieToJar(url, response.headers);
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
});
});
request.on("error", (error) => {
reject([500, error]);
});
if (params.body) {
request.write(params.body);
}
export function httpsRequest(url, params) {
return handleRequest(https, url, params);
}
request.end();
});
export function httpRequest(url, params) {
return handleRequest(http, url, params);
}
export async function httpProxy(url, params = {}) {

Loading…
Cancel
Save