@@ -52,69 +52,65 @@ function impl (req, resOrSocket, headOrNil, {
52
52
const incoming = req . stream || req
53
53
incoming . on ( 'error' , errorHandler )
54
54
55
- try {
56
- if ( resOrSocket instanceof net . Socket ) {
57
- if ( req . method !== 'GET' ) {
58
- throw createError ( 'method not allowed' , null , 405 )
59
- }
60
-
61
- if ( ! req . headers [ HTTP2_HEADER_UPGRADE ] ||
62
- req . headers [ HTTP2_HEADER_UPGRADE ] . toLowerCase ( ) !== 'websocket' ) {
63
- throw createError ( 'bad request' , null , 400 )
64
- }
55
+ if ( resOrSocket instanceof net . Socket ) {
56
+ if ( req . method !== 'GET' ) {
57
+ return errorHandler ( createError ( 'method not allowed' , null , 405 ) )
65
58
}
66
59
67
- if ( req . httpVersion !== '1.1' && req . httpVersion !== '2.0' ) {
68
- throw createError ( 'http version not supported' , null , 505 )
60
+ if ( ! req . headers [ HTTP2_HEADER_UPGRADE ] ||
61
+ req . headers [ HTTP2_HEADER_UPGRADE ] . toLowerCase ( ) !== 'websocket' ) {
62
+ return errorHandler ( createError ( 'bad request' , null , 400 ) )
69
63
}
64
+ }
70
65
71
- if ( proxyName && req . headers [ HTTP2_HEADER_VIA ] ) {
72
- for ( const name of req . headers [ HTTP2_HEADER_VIA ] . split ( ',' ) ) {
73
- if ( sanitize ( name ) . endsWith ( proxyName . toLowerCase ( ) ) ) {
74
- throw createError ( 'loop detected' , null , 508 )
75
- }
76
- }
77
- }
66
+ if ( req . httpVersion !== '1.1' && req . httpVersion !== '2.0' ) {
67
+ return errorHandler ( createError ( 'http version not supported' , null , 505 ) )
68
+ }
78
69
79
- if ( timeout ) {
80
- req . setTimeout ( timeout , errorHandler . requestTimeout )
70
+ if ( proxyName && req . headers [ HTTP2_HEADER_VIA ] ) {
71
+ for ( const name of req . headers [ HTTP2_HEADER_VIA ] . split ( ',' ) ) {
72
+ if ( sanitize ( name ) . endsWith ( proxyName . toLowerCase ( ) ) ) {
73
+ return errorHandler ( createError ( 'loop detected' , null , 508 ) )
74
+ }
81
75
}
76
+ }
82
77
83
- if ( resOrSocket instanceof net . Socket ) {
84
- if ( headOrNil && headOrNil . length ) {
85
- resOrSocket . unshift ( headOrNil )
86
- }
78
+ if ( timeout ) {
79
+ req . setTimeout ( timeout , errorHandler . requestTimeout )
80
+ }
87
81
88
- setupSocket ( resOrSocket )
82
+ if ( resOrSocket instanceof net . Socket ) {
83
+ if ( headOrNil && headOrNil . length ) {
84
+ resOrSocket . unshift ( headOrNil )
89
85
}
90
86
91
- const headers = getRequestHeaders ( req )
87
+ setupSocket ( resOrSocket )
88
+ }
92
89
93
- if ( proxyName ) {
94
- if ( headers [ HTTP2_HEADER_VIA ] ) {
95
- headers [ HTTP2_HEADER_VIA ] += `,${ proxyName } `
96
- } else {
97
- headers [ HTTP2_HEADER_VIA ] = proxyName
98
- }
99
- }
90
+ const headers = getRequestHeaders ( req )
100
91
101
- const options = {
102
- method : req . method ,
103
- hostname,
104
- port,
105
- path : req . url ,
106
- headers,
107
- timeout : proxyTimeout
92
+ if ( proxyName ) {
93
+ if ( headers [ HTTP2_HEADER_VIA ] ) {
94
+ headers [ HTTP2_HEADER_VIA ] += `,${ proxyName } `
95
+ } else {
96
+ headers [ HTTP2_HEADER_VIA ] = proxyName
108
97
}
98
+ }
109
99
110
- if ( onReq ) {
111
- onReq ( req , options )
112
- }
100
+ const options = {
101
+ method : req . method ,
102
+ hostname,
103
+ port,
104
+ path : req . url ,
105
+ headers,
106
+ timeout : proxyTimeout
107
+ }
113
108
114
- return proxy ( req , resOrSocket , options , onRes , errorHandler )
115
- } catch ( err ) {
116
- return errorHandler ( err )
109
+ if ( onReq ) {
110
+ onReq ( req , options )
117
111
}
112
+
113
+ proxy ( req , resOrSocket , options , onRes , errorHandler )
118
114
}
119
115
120
116
function proxy ( req , resOrSocket , options , onRes , errorHandler ) {
@@ -365,37 +361,33 @@ class ProxyResponseHandler {
365
361
_handle ( proxyRes ) {
366
362
this . proxyRes = proxyRes
367
363
368
- try {
369
- proxyRes . on ( 'aborted' , this . proxyErrorHandler . socketHangup )
364
+ proxyRes . on ( 'aborted' , this . proxyErrorHandler . socketHangup )
370
365
371
- if ( this . resOrSocket instanceof net . Socket ) {
372
- if ( this . onRes ) {
373
- this . onRes ( this . req , this . resOrSocket )
374
- }
375
-
376
- if ( ! proxyRes . upgrade ) {
377
- this . resOrSocket . end ( )
378
- }
379
- } else {
380
- setupHeaders ( proxyRes . headers )
366
+ if ( this . resOrSocket instanceof net . Socket ) {
367
+ if ( this . onRes ) {
368
+ this . onRes ( this . req , this . resOrSocket )
369
+ }
381
370
382
- this . resOrSocket . statusCode = proxyRes . statusCode
383
- for ( const key of Object . keys ( proxyRes . headers ) ) {
384
- this . resOrSocket . setHeader ( key , proxyRes . headers [ key ] )
385
- }
371
+ if ( ! proxyRes . upgrade ) {
372
+ this . resOrSocket . end ( )
373
+ }
374
+ } else {
375
+ setupHeaders ( proxyRes . headers )
386
376
387
- if ( this . onRes ) {
388
- this . onRes ( this . req , this . resOrSocket )
389
- }
377
+ this . resOrSocket . statusCode = proxyRes . statusCode
378
+ for ( const key of Object . keys ( proxyRes . headers ) ) {
379
+ this . resOrSocket . setHeader ( key , proxyRes . headers [ key ] )
380
+ }
390
381
391
- this . resOrSocket . writeHead ( this . resOrSocket . statusCode )
392
- proxyRes . on ( 'end' , this . _addTrailers )
393
- proxyRes
394
- . on ( 'error' , this . proxyErrorHandler )
395
- . pipe ( this . resOrSocket )
382
+ if ( this . onRes ) {
383
+ this . onRes ( this . req , this . resOrSocket )
396
384
}
397
- } catch ( err ) {
398
- this . proxyErrorHandler ( err )
385
+
386
+ this . resOrSocket . writeHead ( this . resOrSocket . statusCode )
387
+ proxyRes . on ( 'end' , this . _addTrailers )
388
+ proxyRes
389
+ . on ( 'error' , this . proxyErrorHandler )
390
+ . pipe ( this . resOrSocket )
399
391
}
400
392
}
401
393
@@ -442,40 +434,36 @@ class ProxyUpgradeHandler {
442
434
this . proxyRes = proxyRes
443
435
this . proxySocket = proxySocket
444
436
445
- try {
446
- setupSocket ( proxySocket )
437
+ setupSocket ( proxySocket )
447
438
448
- if ( proxyHead && proxyHead . length ) {
449
- proxySocket . unshift ( proxyHead )
450
- }
439
+ if ( proxyHead && proxyHead . length ) {
440
+ proxySocket . unshift ( proxyHead )
441
+ }
451
442
452
- let head = 'HTTP/1.1 101 Switching Protocols'
443
+ let head = 'HTTP/1.1 101 Switching Protocols'
453
444
454
- for ( const key of Object . keys ( proxyRes . headers ) ) {
455
- const value = proxyRes . headers [ key ]
456
-
457
- if ( ! Array . isArray ( value ) ) {
458
- head += '\r\n' + key + ': ' + value
459
- } else {
460
- for ( let i = 0 ; i < value . length ; i ++ ) {
461
- head += '\r\n' + key + ': ' + value [ i ]
462
- }
445
+ for ( const key of Object . keys ( proxyRes . headers ) ) {
446
+ const value = proxyRes . headers [ key ]
447
+
448
+ if ( ! Array . isArray ( value ) ) {
449
+ head += '\r\n' + key + ': ' + value
450
+ } else {
451
+ for ( let i = 0 ; i < value . length ; i ++ ) {
452
+ head += '\r\n' + key + ': ' + value [ i ]
463
453
}
464
454
}
455
+ }
465
456
466
- head += '\r\n\r\n'
457
+ head += '\r\n\r\n'
467
458
468
- this . resOrSocket . write ( head )
459
+ this . resOrSocket . write ( head )
469
460
470
- proxyRes . on ( 'error' , this . proxyErrorHandler )
461
+ proxyRes . on ( 'error' , this . proxyErrorHandler )
471
462
472
- proxySocket
473
- . on ( 'error' , this . proxyErrorHandler )
474
- . pipe ( this . resOrSocket )
475
- . pipe ( proxySocket )
476
- } catch ( err ) {
477
- this . proxyErrorHandler ( err )
478
- }
463
+ proxySocket
464
+ . on ( 'error' , this . proxyErrorHandler )
465
+ . pipe ( this . resOrSocket )
466
+ . pipe ( proxySocket )
479
467
}
480
468
481
469
_release ( ) {
0 commit comments