@@ -3,8 +3,6 @@ import * as http from 'http';
33import * as http2 from 'http2' ;
44import { MaybePromise } from '@httptoolkit/util' ;
55
6- import { clearArray } from './util.js' ;
7-
86import { httpEndpoints } from './endpoints/endpoint-index.js' ;
97import { 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
0 commit comments