@@ -16,9 +16,7 @@ var http = require("http"),
16
16
httpProxy = require ( "http-proxy-3" ) ,
17
17
winston = require ( "winston" ) ,
18
18
util = require ( "util" ) ,
19
- URL = require ( "url" ) ,
20
19
defaultLogger = require ( "./log" ) . defaultLogger ,
21
- querystring = require ( "querystring" ) ,
22
20
metrics = require ( "./metrics" ) ;
23
21
24
22
function bound ( that , method ) {
@@ -311,23 +309,20 @@ class ConfigurableProxy extends EventEmitter {
311
309
}
312
310
// GET returns routing table as JSON dict
313
311
var that = this ;
314
- var parsed = URL . parse ( req . url ) ;
312
+ var parsed = new URL ( req . url , "https://example.com" ) ;
315
313
var inactiveSince = null ;
316
- if ( parsed . query ) {
317
- var query = querystring . parse ( parsed . query ) ;
318
- if ( query . inactive_since !== undefined ) {
319
- // camelCaseify
320
- query . inactiveSince = query . inactive_since ;
321
- }
322
-
323
- if ( query . inactiveSince !== undefined ) {
324
- var timestamp = Date . parse ( query . inactiveSince ) ;
325
- if ( isFinite ( timestamp ) ) {
326
- inactiveSince = new Date ( timestamp ) ;
327
- } else {
328
- fail ( req , res , 400 , "Invalid datestamp '" + query . inactiveSince + "' must be ISO8601." ) ;
329
- return ;
330
- }
314
+ var inactiveSinceParam = parsed . searchParams . get ( "inactiveSince" ) ;
315
+ if ( ! inactiveSinceParam ) {
316
+ // camelCaseify old inactive_since
317
+ inactiveSinceParam = parsed . searchParams . get ( "inactive_since" ) ;
318
+ }
319
+ if ( inactiveSinceParam ) {
320
+ var timestamp = Date . parse ( inactiveSinceParam ) ;
321
+ if ( isFinite ( timestamp ) ) {
322
+ inactiveSince = new Date ( timestamp ) ;
323
+ } else {
324
+ fail ( req , res , 400 , "Invalid datestamp '" + inactiveSinceParam + "' must be ISO8601." ) ;
325
+ return ;
331
326
}
332
327
}
333
328
res . writeHead ( 200 , { "Content-Type" : "application/json" } ) ;
@@ -393,7 +388,7 @@ class ConfigurableProxy extends EventEmitter {
393
388
var metricsTimerEnd = this . metrics . findTargetForReqSummary . startTimer ( ) ;
394
389
// return proxy target for a given url path
395
390
var basePath = this . hostRouting ? "/" + parseHost ( req ) : "" ;
396
- var path = basePath + decodeURIComponent ( URL . parse ( req . url ) . pathname ) ;
391
+ var path = basePath + decodeURIComponent ( new URL ( req . url , "http://example.com" ) . pathname ) ;
397
392
var route = await this . _routes . getTarget ( path ) ;
398
393
metricsTimerEnd ( ) ;
399
394
if ( route ) {
@@ -460,26 +455,27 @@ class ConfigurableProxy extends EventEmitter {
460
455
return ;
461
456
}
462
457
if ( this . errorTarget ) {
463
- var urlSpec = URL . parse ( this . errorTarget ) ;
458
+ var urlSpec = new URL ( this . errorTarget ) ;
464
459
// error request is $errorTarget/$code?url=$requestUrl
465
- urlSpec . search = "?" + querystring . encode ( { url : req . url } ) ;
460
+ urlSpec . searchParams . set ( " url" , req . url ) ;
466
461
urlSpec . pathname = urlSpec . pathname + code . toString ( ) ;
467
462
var secure = / h t t p s / gi. test ( urlSpec . protocol ) ? true : false ;
468
- var url = URL . format ( urlSpec ) ;
469
- this . log . debug ( "Requesting custom error page: %s" , urlSpec . format ( ) ) ;
463
+ var url = urlSpec . toString ( ) ;
464
+ this . log . debug ( "Requesting custom error page: %s" , url ) ;
470
465
471
- // construct request target from urlSpec
472
- var target = URL . parse ( url ) ;
473
- target . method = "GET" ;
466
+ // construct request options
467
+ var options = {
468
+ method : "GET" ,
469
+ } ;
474
470
475
471
// add client SSL config if error target is using https
476
472
if ( secure && this . options . clientSsl ) {
477
- target . key = this . options . clientSsl . key ;
478
- target . cert = this . options . clientSsl . cert ;
479
- target . ca = this . options . clientSsl . ca ;
473
+ options . key = this . options . clientSsl . key ;
474
+ options . cert = this . options . clientSsl . cert ;
475
+ options . ca = this . options . clientSsl . ca ;
480
476
}
481
477
482
- var errorRequest = ( secure ? https : http ) . request ( target , function ( upstream ) {
478
+ var errorRequest = ( secure ? https : http ) . request ( url , options , function ( upstream ) {
483
479
if ( res . writableEnded ) return ; // response already done
484
480
[ "content-type" , "content-encoding" ] . map ( function ( key ) {
485
481
if ( ! upstream . headers [ key ] ) return ;
@@ -556,15 +552,15 @@ class ConfigurableProxy extends EventEmitter {
556
552
req . url = req . url . slice ( prefix . length ) ;
557
553
}
558
554
559
- target = URL . parse ( target ) ;
555
+ target = new URL ( target ) ;
556
+ var proxyOptions = { target : target } ;
560
557
if ( that . options . clientSsl ) {
561
- target . key = that . options . clientSsl . key ;
562
- target . cert = that . options . clientSsl . cert ;
563
- target . ca = that . options . clientSsl . ca ;
558
+ proxyOptions . key = that . options . clientSsl . key ;
559
+ proxyOptions . cert = that . options . clientSsl . cert ;
560
+ proxyOptions . ca = that . options . clientSsl . ca ;
564
561
}
565
562
566
563
// add config argument
567
- var proxyOptions = { target : target } ;
568
564
if ( target . protocol . slice ( - 2 ) === "s:" ) {
569
565
proxyOptions . agent = that . httpsAgent ;
570
566
} else {
@@ -657,7 +653,7 @@ class ConfigurableProxy extends EventEmitter {
657
653
function pushPathArg ( arg ) {
658
654
args . push ( arg === undefined ? arg : decodeURIComponent ( arg ) ) ;
659
655
}
660
- var path = URL . parse ( req . url ) . pathname ;
656
+ var path = new URL ( req . url , "https://example.com" ) . pathname ;
661
657
for ( var i = 0 ; i < this . apiHandlers . length ; i ++ ) {
662
658
var pat = this . apiHandlers [ i ] [ 0 ] ;
663
659
var match = pat . exec ( path ) ;
0 commit comments