@@ -13,12 +13,10 @@ var http = require("http"),
1313 fs = require ( "fs" ) ,
1414 path = require ( "path" ) ,
1515 EventEmitter = require ( "events" ) . EventEmitter ,
16- httpProxy = require ( "http-proxy-node16 " ) ,
16+ httpProxy = require ( "http-proxy-3 " ) ,
1717 winston = require ( "winston" ) ,
1818 util = require ( "util" ) ,
19- URL = require ( "url" ) ,
2019 defaultLogger = require ( "./log" ) . defaultLogger ,
21- querystring = require ( "querystring" ) ,
2220 metrics = require ( "./metrics" ) ;
2321
2422function bound ( that , method ) {
@@ -311,23 +309,20 @@ class ConfigurableProxy extends EventEmitter {
311309 }
312310 // GET returns routing table as JSON dict
313311 var that = this ;
314- var parsed = URL . parse ( req . url ) ;
312+ var parsed = new URL ( req . url , "https://example.com" ) ;
315313 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 ;
331326 }
332327 }
333328 res . writeHead ( 200 , { "Content-Type" : "application/json" } ) ;
@@ -393,7 +388,7 @@ class ConfigurableProxy extends EventEmitter {
393388 var metricsTimerEnd = this . metrics . findTargetForReqSummary . startTimer ( ) ;
394389 // return proxy target for a given url path
395390 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 ) ;
397392 var route = await this . _routes . getTarget ( path ) ;
398393 metricsTimerEnd ( ) ;
399394 if ( route ) {
@@ -460,26 +455,27 @@ class ConfigurableProxy extends EventEmitter {
460455 return ;
461456 }
462457 if ( this . errorTarget ) {
463- var urlSpec = URL . parse ( this . errorTarget ) ;
458+ var urlSpec = new URL ( this . errorTarget ) ;
464459 // error request is $errorTarget/$code?url=$requestUrl
465- urlSpec . search = "?" + querystring . encode ( { url : req . url } ) ;
460+ urlSpec . searchParams . set ( " url" , req . url ) ;
466461 urlSpec . pathname = urlSpec . pathname + code . toString ( ) ;
467462 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 ) ;
470465
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+ } ;
474470
475471 // add client SSL config if error target is using https
476472 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 ;
480476 }
481477
482- var errorRequest = ( secure ? https : http ) . request ( target , function ( upstream ) {
478+ var errorRequest = ( secure ? https : http ) . request ( url , options , function ( upstream ) {
483479 if ( res . writableEnded ) return ; // response already done
484480 [ "content-type" , "content-encoding" ] . map ( function ( key ) {
485481 if ( ! upstream . headers [ key ] ) return ;
@@ -556,15 +552,15 @@ class ConfigurableProxy extends EventEmitter {
556552 req . url = req . url . slice ( prefix . length ) ;
557553 }
558554
559- target = URL . parse ( target ) ;
555+ target = new URL ( target ) ;
556+ var proxyOptions = { target : target } ;
560557 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 ;
564561 }
565562
566563 // add config argument
567- var proxyOptions = { target : target } ;
568564 if ( target . protocol . slice ( - 2 ) === "s:" ) {
569565 proxyOptions . agent = that . httpsAgent ;
570566 } else {
@@ -657,7 +653,7 @@ class ConfigurableProxy extends EventEmitter {
657653 function pushPathArg ( arg ) {
658654 args . push ( arg === undefined ? arg : decodeURIComponent ( arg ) ) ;
659655 }
660- var path = URL . parse ( req . url ) . pathname ;
656+ var path = new URL ( req . url , "https://example.com" ) . pathname ;
661657 for ( var i = 0 ; i < this . apiHandlers . length ; i ++ ) {
662658 var pat = this . apiHandlers [ i ] [ 0 ] ;
663659 var match = pat . exec ( path ) ;
0 commit comments