diff --git a/src/rules/requests/request-handlers.ts b/src/rules/requests/request-handlers.ts index efecb5b9c..72f3aa2a3 100644 --- a/src/rules/requests/request-handlers.ts +++ b/src/rules/requests/request-handlers.ts @@ -1,5 +1,4 @@ import _ = require('lodash'); -import url = require('url'); import type dns = require('dns'); import net = require('net'); import tls = require('tls'); @@ -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)) { @@ -438,7 +437,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { hostname, clientReq.remoteIpAddress, getDnsLookupFunction(this.lookupOptions) - ); + ) || ""; if (this.forwarding) { const { targetHost, updateHostHeader } = this.forwarding; @@ -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'; @@ -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: @@ -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); @@ -756,7 +755,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { hostname, port, family, - path, + path: pathname + search, headers: shouldTryH2Upstream ? rawHeadersToObjectPreservingCase(rawHeaders) : flattenPairedRawHeaders(rawHeaders) as any, @@ -1169,7 +1168,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { protocol: protocol!.replace(/:$/, ''), hostname, port, - path, + path: pathname + search, rawHeaders });