@@ -26,6 +26,7 @@ const { NodeServer } = ChromeUtils.import("resource://testing-common/httpd.js");
2626
2727let proxy_port ;
2828let server_port ;
29+ let proxy_id ;
2930let filter ;
3031
3132// See moz-http2
@@ -41,11 +42,7 @@ class ProxyFilter {
4142 this . QueryInterface = ChromeUtils . generateQI ( [ Ci . nsIProtocolProxyFilter ] ) ;
4243 }
4344 applyFilter ( pps , uri , pi , cb ) {
44- if (
45- uri . pathQueryRef . startsWith ( "/execute" ) ||
46- uri . pathQueryRef . startsWith ( "/fork" ) ||
47- uri . pathQueryRef . startsWith ( "/kill" )
48- ) {
45+ if ( uri . pathQueryRef == "/execute" ) {
4946 // So we allow NodeServer.execute to work
5047 cb . onProxyFilterResult ( pi ) ;
5148 return ;
@@ -118,154 +115,13 @@ function get_response(channel, flags = CL_ALLOW_UNKNOWN_CL) {
118115
119116let initial_session_count = 0 ;
120117
121- class http2ProxyCode {
122- static listen ( server , envport ) {
123- if ( ! server ) {
124- return Promise . resolve ( 0 ) ;
125- }
126-
127- let portSelection = 0 ;
128- if ( envport !== undefined ) {
129- try {
130- portSelection = parseInt ( envport , 10 ) ;
131- } catch ( e ) {
132- portSelection = - 1 ;
133- }
134- }
135- return new Promise ( resolve => {
136- server . listen ( portSelection , "0.0.0.0" , 2000 , ( ) => {
137- resolve ( server . address ( ) . port ) ;
138- } ) ;
139- } ) ;
140- }
141-
142- static startNewProxy ( ) {
143- const fs = require ( "fs" ) ;
144- const options = {
145- key : fs . readFileSync ( __dirname + "/http2-cert.key" ) ,
146- cert : fs . readFileSync ( __dirname + "/http2-cert.pem" ) ,
147- } ;
148- const http2 = require ( "http2" ) ;
149- global . proxy = http2 . createSecureServer ( options ) ;
150- this . setupProxy ( ) ;
151- return http2ProxyCode . listen ( proxy ) . then ( port => {
152- return { port, success : true } ;
153- } ) ;
154- }
155-
156- static closeProxy ( ) {
157- proxy . closeSockets ( ) ;
158- return new Promise ( resolve => {
159- proxy . close ( resolve ) ;
160- } ) ;
161- }
162-
163- static proxySessionCount ( ) {
164- if ( ! proxy ) {
165- return 0 ;
166- }
167- return proxy . proxy_session_count ;
168- }
169-
170- static setupProxy ( ) {
171- if ( ! proxy ) {
172- throw new Error ( "proxy is null" ) ;
173- }
174- proxy . proxy_session_count = 0 ;
175- proxy . on ( "session" , ( ) => {
176- ++ proxy . proxy_session_count ;
177- } ) ;
178-
179- // We need to track active connections so we can forcefully close keep-alive
180- // connections when shutting down the proxy.
181- proxy . socketIndex = 0 ;
182- proxy . socketMap = { } ;
183- proxy . on ( "connection" , function ( socket ) {
184- let index = proxy . socketIndex ++ ;
185- proxy . socketMap [ index ] = socket ;
186- socket . on ( "close" , function ( ) {
187- delete proxy . socketMap [ index ] ;
188- } ) ;
189- } ) ;
190- proxy . closeSockets = function ( ) {
191- for ( let i in proxy . socketMap ) {
192- proxy . socketMap [ i ] . destroy ( ) ;
193- }
194- } ;
195-
196- proxy . on ( "stream" , ( stream , headers ) => {
197- if ( headers [ ":method" ] !== "CONNECT" ) {
198- // Only accept CONNECT requests
199- stream . respond ( { ":status" : 405 } ) ;
200- stream . end ( ) ;
201- return ;
202- }
203-
204- const target = headers [ ":authority" ] ;
205-
206- const authorization_token = headers [ "proxy-authorization" ] ;
207- if (
208- "authorization-token" != authorization_token ||
209- target == "407.example.com:443"
210- ) {
211- stream . respond ( { ":status" : 407 } ) ;
212- // Deliberately send no Proxy-Authenticate header
213- stream . end ( ) ;
214- return ;
215- }
216- if ( target == "404.example.com:443" ) {
217- // 404 Not Found, a response code that a proxy should return when the host can't be found
218- stream . respond ( { ":status" : 404 } ) ;
219- stream . end ( ) ;
220- return ;
221- }
222- if ( target == "429.example.com:443" ) {
223- // 429 Too Many Requests, a response code that a proxy should return when receiving too many requests
224- stream . respond ( { ":status" : 429 } ) ;
225- stream . end ( ) ;
226- return ;
227- }
228- if ( target == "502.example.com:443" ) {
229- // 502 Bad Gateway, a response code mostly resembling immediate connection error
230- stream . respond ( { ":status" : 502 } ) ;
231- stream . end ( ) ;
232- return ;
233- }
234- if ( target == "504.example.com:443" ) {
235- // 504 Gateway Timeout, did not receive a timely response from an upstream server
236- stream . respond ( { ":status" : 504 } ) ;
237- stream . end ( ) ;
238- return ;
239- }
240-
241- const net = require ( "net" ) ;
242- const socket = net . connect ( serverPort , "127.0.0.1" , ( ) => {
243- try {
244- stream . respond ( { ":status" : 200 } ) ;
245- socket . pipe ( stream ) ;
246- stream . pipe ( socket ) ;
247- } catch ( exception ) {
248- console . log ( exception ) ;
249- stream . close ( ) ;
250- }
251- } ) ;
252- socket . on ( "error" , error => {
253- throw `Unxpected error when conneting the HTTP/2 server from the HTTP/2 proxy during CONNECT handling: '${ error } '` ;
254- } ) ;
255- } ) ;
256- }
257- }
258-
259118function proxy_session_counter ( ) {
260119 return new Promise ( async resolve => {
261- let data = await NodeServer . execute (
262- processId ,
263- `http2ProxyCode.proxySessionCount()`
264- ) ;
120+ let data = await NodeServer . execute ( `proxySessionCount("${ proxy_id } ")` ) ;
265121 resolve ( parseInt ( data ) - initial_session_count ) ;
266122 } ) ;
267123}
268- let processId ;
124+
269125add_task ( async function setup ( ) {
270126 // Set to allow the cert presented by our H2 server
271127 do_get_profile ( ) ;
@@ -282,14 +138,9 @@ add_task(async function setup() {
282138 ) ;
283139 server_port = env . get ( "MOZHTTP2_PORT" ) ;
284140 Assert . notEqual ( server_port , null ) ;
285- processId = await NodeServer . fork ( ) ;
286- await NodeServer . execute ( processId , `serverPort = ${ server_port } ` ) ;
287- await NodeServer . execute ( processId , http2ProxyCode ) ;
288- let proxy = await NodeServer . execute (
289- processId ,
290- `http2ProxyCode.startNewProxy()`
291- ) ;
141+ let proxy = await NodeServer . execute ( `startNewProxy()` ) ;
292142 proxy_port = proxy . port ;
143+ proxy_id = proxy . name ;
293144 Assert . notEqual ( proxy_port , null ) ;
294145
295146 Services . prefs . setStringPref (
@@ -318,8 +169,7 @@ registerCleanupFunction(async () => {
318169
319170 pps . unregisterFilter ( filter ) ;
320171
321- await NodeServer . execute ( processId , `http2ProxyCode.closeProxy()` ) ;
322- await NodeServer . kill ( processId ) ;
172+ await NodeServer . execute ( `closeProxy("${ proxy_id } ")` ) ;
323173} ) ;
324174
325175/**
0 commit comments