2
2
* Module dependencies.
3
3
*/
4
4
5
- // eslint-disable-next-line node/no-deprecated-api
6
- const { parse, format, resolve } = require ( 'url' ) ;
5
+ const { format } = require ( 'url' ) ;
7
6
const Stream = require ( 'stream' ) ;
8
7
const https = require ( 'https' ) ;
9
8
const http = require ( 'http' ) ;
@@ -503,15 +502,15 @@ Request.prototype._redirect = function (res) {
503
502
debug ( 'redirect %s -> %s' , this . url , url ) ;
504
503
505
504
// location
506
- url = resolve ( this . url , url ) ;
505
+ url = new URL ( url , this . url ) . href ;
507
506
508
507
// ensure the response is being consumed
509
508
// this is required for Node v0.10+
510
509
res . resume ( ) ;
511
510
512
511
let headers = this . req . getHeaders ? this . req . getHeaders ( ) : this . req . _headers ;
513
512
514
- const changesOrigin = parse ( url ) . host !== parse ( this . url ) . host ;
513
+ const changesOrigin = new URL ( url ) . host !== new URL ( this . url ) . host ;
515
514
516
515
// implementation of 302 following defacto standard
517
516
if ( res . statusCode === 301 || res . statusCode === 302 ) {
@@ -695,43 +694,24 @@ Request.prototype.request = function () {
695
694
return this . emit ( 'error' , err ) ;
696
695
}
697
696
698
- let { url } = this ;
697
+ let { url : urlString } = this ;
699
698
const retries = this . _retries ;
700
699
701
- // Capture backticks as-is from the final query string built above.
702
- // Note: this'll only find backticks entered in req.query(String)
703
- // calls, because qs.stringify unconditionally encodes backticks.
704
- let queryStringBackticks ;
705
- if ( url . includes ( '`' ) ) {
706
- const queryStartIndex = url . indexOf ( '?' ) ;
707
-
708
- if ( queryStartIndex !== - 1 ) {
709
- const queryString = url . slice ( queryStartIndex + 1 ) ;
710
- queryStringBackticks = queryString . match ( / ` | % 6 0 / g) ;
711
- }
712
- }
713
-
714
700
// default to http://
715
- if ( url . indexOf ( 'http' ) !== 0 ) url = `http://${ url } ` ;
716
- url = parse ( url ) ;
717
-
718
- // See https://github.com/ladjs/superagent/issues/1367
719
- if ( queryStringBackticks ) {
720
- let i = 0 ;
721
- url . query = url . query . replace ( / % 6 0 / g, ( ) => queryStringBackticks [ i ++ ] ) ;
722
- url . search = `?${ url . query } ` ;
723
- url . path = url . pathname + url . search ;
724
- }
701
+ if ( urlString . indexOf ( 'http' ) !== 0 ) urlString = `http://${ urlString } ` ;
702
+ const url = new URL ( urlString ) ;
703
+ let { protocol } = url ;
704
+ let path = `${ url . pathname } ${ url . search } ` ;
725
705
726
706
// support unix sockets
727
- if ( / ^ h t t p s ? \+ u n i x : / . test ( url . protocol ) === true ) {
707
+ if ( / ^ h t t p s ? \+ u n i x : / . test ( protocol ) === true ) {
728
708
// get the protocol
729
- url . protocol = `${ url . protocol . split ( '+' ) [ 0 ] } :` ;
709
+ protocol = `${ protocol . split ( '+' ) [ 0 ] } :` ;
730
710
731
- // get the socket, path
732
- const unixParts = url . path . match ( / ^ ( [ ^ / ] + ) ( . + ) $ / ) ;
733
- options . socketPath = unixParts [ 1 ] . replace ( / % 2 F / g , '/' ) ;
734
- url . path = unixParts [ 2 ] ;
711
+ // get the socket path
712
+ options . socketPath = url . hostname . replace ( / % 2 F / g , '/' ) ;
713
+ url . host = '' ;
714
+ url . hostname = '' ;
735
715
}
736
716
737
717
// Override IP address of a hostname
@@ -772,7 +752,7 @@ Request.prototype.request = function () {
772
752
// options
773
753
options . method = this . method ;
774
754
options . port = url . port ;
775
- options . path = url . path ;
755
+ options . path = path ;
776
756
options . host = url . hostname ;
777
757
options . ca = this . _ca ;
778
758
options . key = this . _key ;
@@ -800,8 +780,8 @@ Request.prototype.request = function () {
800
780
801
781
// initiate request
802
782
const module_ = this . _enableHttp2
803
- ? exports . protocols [ 'http2:' ] . setProtocol ( url . protocol )
804
- : exports . protocols [ url . protocol ] ;
783
+ ? exports . protocols [ 'http2:' ] . setProtocol ( protocol )
784
+ : exports . protocols [ protocol ] ;
805
785
806
786
// request
807
787
this . req = module_ . request ( options ) ;
@@ -814,7 +794,7 @@ Request.prototype.request = function () {
814
794
req . setHeader ( 'Accept-Encoding' , 'gzip, deflate' ) ;
815
795
}
816
796
817
- this . protocol = url . protocol ;
797
+ this . protocol = protocol ;
818
798
this . host = url . host ;
819
799
820
800
// expose events
@@ -837,9 +817,8 @@ Request.prototype.request = function () {
837
817
} ) ;
838
818
839
819
// auth
840
- if ( url . auth ) {
841
- const auth = url . auth . split ( ':' ) ;
842
- this . auth ( auth [ 0 ] , auth [ 1 ] ) ;
820
+ if ( url . username || url . password ) {
821
+ this . auth ( url . username , url . password ) ;
843
822
}
844
823
845
824
if ( this . username && this . password ) {
0 commit comments