@@ -12,11 +12,13 @@ import * as http from "node:http";
1212import * as https from "node:https" ;
1313import * as common from "../common" ;
1414import type { Request , ProxyResponse } from "./web-incoming" ;
15+ import { OUTGOING_PASSES , EditableResponse } from "./web-outgoing" ;
1516import type { Socket } from "node:net" ;
1617import debug from "debug" ;
17- import type { NormalizedServerOptions , ProxyServer } from ".." ;
18+ import type { NormalizedServerOptions , NormalizeProxyTarget , ProxyServer , ProxyTarget } from ".." ;
1819
1920const log = debug ( "http-proxy-3:ws-incoming" ) ;
21+ const web_o = Object . values ( OUTGOING_PASSES ) ;
2022
2123function createSocketCounter ( name : string ) {
2224 let sockets = new Set < number > ( ) ;
@@ -55,6 +57,26 @@ function createSocketCounter(name: string) {
5557const socketCounter = createSocketCounter ( "socket" ) ;
5658const proxySocketCounter = createSocketCounter ( "proxySocket" ) ;
5759
60+ /* MockResponse
61+ when a websocket gets a regular HTTP Response,
62+ apply proxied headers
63+ */
64+ class MockResponse implements EditableResponse {
65+ constructor ( ) {
66+ this . headers = { } ;
67+ this . statusCode = 200
68+ this . statusMessage = "" ;
69+ }
70+ public headers : { [ key : string ] : string } ;
71+ public statusCode : number ;
72+ public statusMessage : string ;
73+
74+ setHeader ( key : string , value : string ) {
75+ this . headers [ key ] = value ;
76+ return this ;
77+ } ;
78+ }
79+
5880export function numOpenSockets ( ) : number {
5981 return socketCounter ( ) + proxySocketCounter ( ) ;
6082}
@@ -219,7 +241,7 @@ export function stream(
219241 // which may be another leak type situation and definitely doesn't work for unit testing.
220242 socket . destroySoon ( ) ;
221243 }
222-
244+
223245 // if we get a response, backend is not a websocket endpoint,
224246 // relay HTTP response and close the socket
225247 proxyReq . on ( "response" , ( proxyRes : ProxyResponse ) => {
@@ -229,9 +251,18 @@ export function stream(
229251 statusMessage : proxyRes . statusMessage ,
230252 }
231253 ) ;
254+
255+ const res = new MockResponse ( ) ;
256+ for ( const pass of web_o ) {
257+ // note: none of these return anything
258+ pass ( req , res as EditableResponse , proxyRes , options as NormalizedServerOptions & { target : NormalizeProxyTarget < ProxyTarget > } ) ;
259+ }
260+ // avoid Invalid character error in chunk size
261+ delete res . headers [ 'transfer-encoding' ] ;
262+
232263 const proxyHead = createHttpHeader (
233- `HTTP/1.1 ${ proxyRes . statusCode } ${ proxyRes . statusMessage } ` ,
234- { "Connection" : "close" } ,
264+ `HTTP/${ req . httpVersion } ${ proxyRes . statusCode } ${ proxyRes . statusMessage } ` ,
265+ res . headers ,
235266 ) ;
236267 if ( ! socket . destroyed ) {
237268 socket . write ( proxyHead ) ;
0 commit comments