Skip to content

Commit

Permalink
Move more header methods into header utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Aug 17, 2023
1 parent a5b0fc3 commit 2f41556
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
8 changes: 3 additions & 5 deletions src/rules/requests/request-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ import {
waitForCompletedRequest,
buildBodyReader,
shouldKeepAlive,
dropDefaultHeaders,
isHttp2,
isAbsoluteUrl,
writeHead,
encodeBodyBuffer,
validateHeader,
getEffectivePort
} from '../../util/request-utils';
import {
Expand All @@ -39,13 +37,13 @@ import {
rawHeadersToObject,
rawHeadersToObjectPreservingCase,
flattenPairedRawHeaders,
findRawHeader,
pairFlatRawHeaders,
findRawHeaderIndex
findRawHeaderIndex,
dropDefaultHeaders,
validateHeader
} from '../../util/header-utils';
import { streamToBuffer, asBuffer } from '../../util/buffer-utils';
import {
isLocalhostAddress,
isLocalPortActive,
isSocketLoop,
requireSocketResetSupport,
Expand Down
27 changes: 27 additions & 0 deletions src/util/header-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as http from 'http';

import {
Headers,
OngoingResponse,
RawHeaders
} from "../types";

Expand Down Expand Up @@ -190,4 +193,28 @@ export function h1HeadersToH2(headers: RawHeaders): RawHeaders {
return headers.filter(([key]) =>
!HTTP2_ILLEGAL_HEADERS.includes(key.toLowerCase())
);
}

// If the user explicitly specifies headers, we tell Node not to handle them,
// so the user-defined headers are the full set.
export function dropDefaultHeaders(response: OngoingResponse) {
// Drop the default headers, so only the headers we explicitly configure are included
[
'connection',
'content-length',
'transfer-encoding',
'date'
].forEach((defaultHeader) =>
response.removeHeader(defaultHeader)
);
}

export function validateHeader(name: string, value: string | string[]): boolean {
try {
http.validateHeaderName(name);
http.validateHeaderValue(name, value);
return true;
} catch (e) {
return false;
}
}
24 changes: 0 additions & 24 deletions src/util/request-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,6 @@ export const writeHead = (
}
};

// If the user explicitly specifies headers, we tell Node not to handle them,
// so the user-defined headers are the full set.
export function dropDefaultHeaders(response: OngoingResponse) {
// Drop the default headers, so only the headers we explicitly configure are included
[
'connection',
'content-length',
'transfer-encoding',
'date'
].forEach((defaultHeader) =>
response.removeHeader(defaultHeader)
);
}

export function validateHeader(name: string, value: string | string[]): boolean {
try {
http.validateHeaderName(name);
http.validateHeaderValue(name, value);
return true;
} catch (e) {
return false;
}
}

export function isHttp2(
message: | http.IncomingMessage
| http.ServerResponse
Expand Down

0 comments on commit 2f41556

Please sign in to comment.