Skip to content

Commit c124d8e

Browse files
committed
Improve echo data handling in HTTP/1 as well
1 parent 778be15 commit c124d8e

2 files changed

Lines changed: 16 additions & 26 deletions

File tree

src/http-handler.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import * as http from 'http';
33
import * as http2 from 'http2';
44
import { MaybePromise } from '@httptoolkit/util';
55

6-
import { clearArray } from './util.js';
7-
86
import { httpEndpoints } from './endpoints/endpoint-index.js';
97
import { HttpRequest, HttpResponse } from './endpoints/http-index.js';
108

@@ -70,9 +68,7 @@ function createHttpRequestHandler(options: {
7068
res.end('Unrecognized ACME challenge request');
7169
}
7270

73-
// We have to clear this, as we might get multiple requests on the same
74-
// socket with keep-alive etc.
75-
clearArray(req.socket.receivedData);
71+
req.socket.receivedData = [];
7672
return;
7773
}
7874

@@ -106,15 +102,19 @@ function createHttpRequestHandler(options: {
106102
endpoint.matchPath(path, hostnamePrefix)
107103
);
108104

109-
// For HTTP/2, stop data capturing for this stream unless the endpoint needs it
110-
// This prevents unbounded buffering of large request bodies
111-
if (req.httpVersion === '2.0' && (!matchingEndpoint || !matchingEndpoint.needsRawData)) {
112-
const stream = (req as any).stream;
113-
const session = stream?.session;
114-
const capturingStream = session?.socket?.stream;
115-
const streamId = stream?.id as number | undefined;
116-
if (streamId !== undefined) {
117-
capturingStream?.stopCapturingStream?.(streamId);
105+
// Stop data capturing unless the endpoint needs it
106+
if (!matchingEndpoint || !matchingEndpoint.needsRawData) {
107+
if (req.httpVersion === '2.0') {
108+
const stream = (req as any).stream;
109+
const session = stream?.session;
110+
const capturingStream = session?.socket?.stream;
111+
const streamId = stream?.id as number | undefined;
112+
if (streamId !== undefined) {
113+
capturingStream?.stopCapturingStream?.(streamId);
114+
}
115+
} else {
116+
// HTTP/1: stop capturing and clear buffer
117+
req.socket.receivedData = undefined;
118118
}
119119
}
120120

@@ -157,9 +157,8 @@ export function createHttp1Handler(options: {
157157
res.end('HTTP handler failed');
158158
}
159159
} finally {
160-
// We have to clear this, as we might get multiple requests on the same
161-
// socket with keep-alive etc.
162-
clearArray(req.socket.receivedData);
160+
// Reset for next request on keep-alive connections
161+
req.socket.receivedData = [];
163162
}
164163
});
165164

@@ -186,10 +185,6 @@ export function createHttp2Handler(options: {
186185
res.writeHead(500);
187186
res.end('HTTP handler failed');
188187
}
189-
} finally {
190-
// We have to clear this, as we might get multiple requests on the same
191-
// socket with keep-alive etc.
192-
clearArray(req.socket.receivedData);
193188
}
194189
});
195190

src/util.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
export const clearArray = (array: Array<unknown> | undefined) => {
2-
if (!array) return;
3-
array.length = 0;
4-
}
5-
61
export const serializeJson = (json: any) =>
72
JSON.stringify(json, null, 2) + '\n';

0 commit comments

Comments
 (0)