Skip to content

Commit b2b7328

Browse files
committed
Build a reusable getEffectivePort helper
1 parent 6b71e8b commit b2b7328

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

src/rules/requests/request-handlers.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import {
2929
isAbsoluteUrl,
3030
writeHead,
3131
encodeBodyBuffer,
32-
validateHeader
32+
validateHeader,
33+
getEffectivePort
3334
} from '../../util/request-utils';
3435
import {
3536
h1HeadersToH2,
@@ -639,13 +640,11 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
639640
rawHeaders = objectHeadersToRaw(headers);
640641
}
641642

642-
const effectivePort = !!port
643-
? parseInt(port, 10)
644-
: (protocol === 'https:' ? 443 : 80);
643+
const effectivePort = getEffectivePort({ protocol, port });
645644

646645
const strictHttpsChecks = shouldUseStrictHttps(
647646
hostname!,
648-
effectivePort!,
647+
effectivePort,
649648
this.ignoreHostHttpsErrors
650649
);
651650

src/rules/websockets/websocket-handlers.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
TimeoutHandler
2020
} from '../requests/request-handlers';
2121
import {
22+
getEffectivePort,
2223
isHttp2
2324
} from '../../util/request-utils';
2425
import {
@@ -293,9 +294,7 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
293294
) {
294295
const parsedUrl = url.parse(wsUrl);
295296

296-
const effectivePort = !!parsedUrl.port
297-
? parseInt(parsedUrl.port, 10)
298-
: parsedUrl.protocol == 'wss:' ? 443 : 80;
297+
const effectivePort = getEffectivePort(parsedUrl);
299298

300299
const checkServerCertificate = shouldUseStrictHttps(
301300
parsedUrl.hostname!,

src/util/request-utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ export const getPathFromAbsoluteUrl = (url: string) => {
6464
}
6565
}
6666

67+
export const getEffectivePort = (url: { protocol: string | null, port: string | null }) => {
68+
if (url.port) {
69+
return parseInt(url.port, 10);
70+
} else if (url.protocol === 'https:' || url.protocol === 'wss:') {
71+
return 443;
72+
} else {
73+
return 80;
74+
}
75+
}
76+
6777
export const shouldKeepAlive = (req: OngoingRequest): boolean =>
6878
req.httpVersion !== '1.0' &&
6979
req.headers['connection'] !== 'close' &&

test/test-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { Mockttp } from "..";
2929
export { getDeferred, Deferred } from '../src/util/promise';
3030
import { makeDestroyable, DestroyableServer } from "destroyable-server";
3131
import { isNode, isWeb, delay } from '../src/util/util';
32+
import { getEffectivePort } from '../src/util/request-utils';
3233
export { isNode, isWeb, delay, makeDestroyable, DestroyableServer };
3334

3435
if (isNode) {
@@ -363,7 +364,7 @@ export async function http2ProxyRequest(
363364
const isTLS = parsedUrl.protocol === 'https:';
364365

365366
const targetHost = parsedUrl.hostname!;
366-
const targetPort = parsedUrl.port! ?? (isTLS ? 443 : 80);
367+
const targetPort = getEffectivePort(parsedUrl);
367368

368369
const proxyClient = http2.connect(proxyServer.url);
369370
return await new Promise<Http2TestRequestResult>(async (resolve, reject) => {

0 commit comments

Comments
 (0)