aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2023-02-16 13:54:19 +0100
committerFlorian Dold <florian@dold.me>2023-02-16 13:54:19 +0100
commiteca3819bcdf8f2a715c1f5e212ab4d81e3bbfd45 (patch)
tree64dd1288d9865176b6a47ea0d1c37b0af02316c6 /packages/taler-util
parentfedc45144ff25d61631015b8fb3c9e27a6c695a3 (diff)
taler-util: node http fixes
Diffstat (limited to 'packages/taler-util')
-rw-r--r--packages/taler-util/src/http-impl.node.ts33
1 files changed, 28 insertions, 5 deletions
diff --git a/packages/taler-util/src/http-impl.node.ts b/packages/taler-util/src/http-impl.node.ts
index da7a759aa..8f215e596 100644
--- a/packages/taler-util/src/http-impl.node.ts
+++ b/packages/taler-util/src/http-impl.node.ts
@@ -20,6 +20,7 @@
* Imports.
*/
import * as http from "node:http";
+import * as https from "node:https";
import { RequestOptions } from "node:http";
import { TalerError } from "./errors.js";
import { encodeBody, HttpLibArgs } from "./http-common.js";
@@ -62,7 +63,6 @@ export class HttpLibImpl implements HttpRequestLibrary {
async fetch(url: string, opt?: HttpRequestOptions): Promise<HttpResponse> {
const method = opt?.method?.toUpperCase() ?? "GET";
- let body = opt?.body;
logger.trace(`Requesting ${method} ${url}`);
@@ -94,19 +94,33 @@ export class HttpLibImpl implements HttpRequestLibrary {
reqBody = encodeBody(opt.body);
}
+ let path = parsedUrl.pathname;
+ if (parsedUrl.search != null) {
+ path += parsedUrl.search;
+ }
+
+ let protocol: string;
+ if (parsedUrl.protocol === "https:") {
+ protocol = "https:";
+ } else if (parsedUrl.protocol === "http:") {
+ protocol = "http:";
+ } else {
+ throw Error(`unsupported protocol (${parsedUrl.protocol})`);
+ }
+
const options: RequestOptions = {
- protocol: parsedUrl.protocol,
+ protocol,
port: parsedUrl.port,
host: parsedUrl.hostname,
method: method,
- path: parsedUrl.pathname,
+ path,
headers: opt?.headers,
};
const chunks: Uint8Array[] = [];
return new Promise((resolve, reject) => {
- const req = http.request(options, (res) => {
+ const handler = (res: http.IncomingMessage) => {
res.on("data", (d) => {
chunks.push(d);
});
@@ -145,7 +159,16 @@ export class HttpLibImpl implements HttpRequestLibrary {
res.on("error", (e) => {
reject(e);
});
- });
+ };
+
+ let req: http.ClientRequest;
+ if (options.protocol === "http:") {
+ req = http.request(options, handler);
+ } else if (options.protocol === "https:") {
+ req = https.request(options, handler);
+ } else {
+ throw new Error(`unsupported protocol ${options.protocol}`);
+ }
if (reqBody) {
req.write(new Uint8Array(reqBody));