Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/rules/requests/request-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ = require('lodash');
import url = require('url');
import type dns = require('dns');
import net = require('net');
import tls = require('tls');
Expand Down Expand Up @@ -413,7 +412,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {

// Capture raw request data:
let { method, url: reqUrl, rawHeaders } = clientReq as OngoingRequest;
let { protocol, hostname, port, path } = url.parse(reqUrl);
let { protocol, hostname, port, pathname, search } = new URL(reqUrl);

// Check if this request is a request loop:
if (isSocketLoop(this.outgoingSockets, (clientReq as any).socket)) {
Expand All @@ -438,7 +437,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
hostname,
clientReq.remoteIpAddress,
getDnsLookupFunction(this.lookupOptions)
);
) || "";

if (this.forwarding) {
const { targetHost, updateHostHeader } = this.forwarding;
Expand All @@ -447,7 +446,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
[hostname, port] = targetHost.split(':');
} else {
// We're forwarding to a fully specified URL; override the host etc, but never the path.
({ protocol, hostname, port } = url.parse(targetHost));
({ protocol, hostname, port } = new URL(targetHost));
}

const hostHeaderName = isH2Downstream ? ':authority' : 'host';
Expand All @@ -473,7 +472,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
hostHeader[1] = updateHostHeader;
} // Otherwise: falsey means don't touch it.

reqUrl = new URL(`${protocol}//${hostname}${(port ? `:${port}` : '')}${path}`).toString();
reqUrl = new URL(`${protocol}//${hostname}${(port ? `:${port}` : '')}${pathname}${search}`).toString();
}

// Override the request details, if a transform or callback is specified:
Expand Down Expand Up @@ -653,7 +652,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
// Reparse the new URL, if necessary
if (modifiedReq?.url) {
if (!isAbsoluteUrl(modifiedReq?.url)) throw new Error("Overridden request URLs must be absolute");
({ protocol, hostname, port, path } = url.parse(reqUrl));
({ protocol, hostname, port, pathname, search } = new URL(reqUrl));
}

rawHeaders = objectHeadersToRaw(headers);
Expand Down Expand Up @@ -756,7 +755,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
hostname,
port,
family,
path,
path: pathname + search,
headers: shouldTryH2Upstream
? rawHeadersToObjectPreservingCase(rawHeaders)
: flattenPairedRawHeaders(rawHeaders) as any,
Expand Down Expand Up @@ -1169,7 +1168,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
protocol: protocol!.replace(/:$/, ''),
hostname,
port,
path,
path: pathname + search,
rawHeaders
});

Expand Down