@@ -388,6 +388,34 @@ export class ConfigurableProxy extends EventEmitter {
388
388
} ) ;
389
389
}
390
390
391
+ proxyOptsForTarget ( target , reqUrl ) {
392
+ var proxyOptions = { target } ;
393
+
394
+ if ( target . protocol . startsWith ( "unix" ) ) {
395
+ proxyOptions . secure = false ;
396
+ proxyOptions . target . socketPath = decodeURIComponent ( target . host ) ;
397
+ proxyOptions . target . pathname = ( target . pathname ? target . pathname + "/" : "" ) + reqUrl ;
398
+ } else {
399
+ // No need for agents for unix sockets
400
+ // No support for https for unix sockets
401
+ proxyOptions . secure = target . protocol . slice ( - 2 ) === "s:" ;
402
+
403
+ if ( proxyOptions . secure ) {
404
+ proxyOptions . agent = this . httpsAgent ;
405
+ } else {
406
+ proxyOptions . agent = this . httpAgent ;
407
+ }
408
+ }
409
+
410
+ if ( proxyOptions . secure && this . options . clientSsl ) {
411
+ proxyOptions . target . key = this . options . clientSsl . key ;
412
+ proxyOptions . target . cert = this . options . clientSsl . cert ;
413
+ proxyOptions . target . ca = this . options . clientSsl . ca ;
414
+ }
415
+
416
+ return proxyOptions ;
417
+ }
418
+
391
419
async targetForReq ( req ) {
392
420
var metricsTimerEnd = this . metrics . findTargetForReqSummary . startTimer ( ) ;
393
421
// return proxy target for a given url path
@@ -463,23 +491,13 @@ export class ConfigurableProxy extends EventEmitter {
463
491
// error request is $errorTarget/$code?url=$requestUrl
464
492
urlSpec . searchParams . set ( "url" , req . url ) ;
465
493
urlSpec . pathname = urlSpec . pathname + code . toString ( ) ;
466
- var secure = / h t t p s / gi. test ( urlSpec . protocol ) ? true : false ;
467
494
var url = urlSpec . toString ( ) ;
468
495
this . log . debug ( "Requesting custom error page: %s" , url ) ;
469
496
470
- // construct request options
471
- var options = {
472
- method : "GET" ,
473
- } ;
474
-
475
- // add client SSL config if error target is using https
476
- if ( secure && this . options . clientSsl ) {
477
- options . key = this . options . clientSsl . key ;
478
- options . cert = this . options . clientSsl . cert ;
479
- options . ca = this . options . clientSsl . ca ;
480
- }
497
+ var options = this . proxyOptsForTarget ( urlSpec , req . url ) ;
498
+ options . method = "GET" ;
481
499
482
- var errorRequest = ( secure ? https : http ) . request ( url , options , function ( upstream ) {
500
+ var errorRequest = ( options . secure ? https : http ) . request ( url , options , function ( upstream ) {
483
501
if ( res . writableEnded ) return ; // response already done
484
502
[ "content-type" , "content-encoding" ] . map ( function ( key ) {
485
503
if ( ! upstream . headers [ key ] ) return ;
@@ -557,19 +575,8 @@ export class ConfigurableProxy extends EventEmitter {
557
575
}
558
576
559
577
target = new URL ( target ) ;
560
- var proxyOptions = { target } ;
561
- if ( that . options . clientSsl ) {
562
- target . key = that . options . clientSsl . key ;
563
- target . cert = that . options . clientSsl . cert ;
564
- target . ca = that . options . clientSsl . ca ;
565
- }
578
+ var proxyOptions = this . proxyOptsForTarget ( target , req . url ) ;
566
579
567
- // add config argument
568
- if ( target . protocol . slice ( - 2 ) === "s:" ) {
569
- proxyOptions . agent = that . httpsAgent ;
570
- } else {
571
- proxyOptions . agent = that . httpAgent ;
572
- }
573
580
args . push ( proxyOptions ) ;
574
581
575
582
// add error handling
0 commit comments