@@ -19,7 +19,6 @@ use tokio_io_timeout::TimeoutStream;
1919use url:: Url ;
2020
2121use crate :: config:: Config ;
22- use crate :: constants:: NON_ALPHANUMERIC_WITHOUT_DOT ;
2322use crate :: context:: Context ;
2423use crate :: net:: connect_tcp;
2524use crate :: net:: session:: SessionStream ;
@@ -93,13 +92,12 @@ impl HttpConfig {
9392 }
9493
9594 fn to_url ( & self , scheme : & str ) -> String {
96- let host = utf8_percent_encode ( & self . host , NON_ALPHANUMERIC_WITHOUT_DOT ) ;
9795 if let Some ( ( user, password) ) = & self . user_password {
9896 let user = utf8_percent_encode ( user, NON_ALPHANUMERIC ) ;
9997 let password = utf8_percent_encode ( password, NON_ALPHANUMERIC ) ;
100- format ! ( "{scheme}://{user}:{password}@{host }:{}" , self . port)
98+ format ! ( "{scheme}://{user}:{password}@{}:{}" , self . host , self . port)
10199 } else {
102- format ! ( "{scheme}://{host }:{}" , self . port)
100+ format ! ( "{scheme}://{}:{}" , self . host , self . port)
103101 }
104102 }
105103}
@@ -143,13 +141,12 @@ impl Socks5Config {
143141 }
144142
145143 fn to_url ( & self ) -> String {
146- let host = utf8_percent_encode ( & self . host , NON_ALPHANUMERIC_WITHOUT_DOT ) ;
147144 if let Some ( ( user, password) ) = & self . user_password {
148145 let user = utf8_percent_encode ( user, NON_ALPHANUMERIC ) ;
149146 let password = utf8_percent_encode ( password, NON_ALPHANUMERIC ) ;
150- format ! ( "socks5://{user}:{password}@{host }:{}" , self . port)
147+ format ! ( "socks5://{user}:{password}@{}:{}" , self . host , self . port)
151148 } else {
152- format ! ( "socks5://{host }:{}" , self . port)
149+ format ! ( "socks5://{}:{}" , self . host , self . port)
153150 }
154151 }
155152}
@@ -565,6 +562,20 @@ mod tests {
565562 user_password: None
566563 } )
567564 ) ;
565+
566+ let proxy_config = ProxyConfig :: from_url ( "socks5://my-proxy.example.org" ) . unwrap ( ) ;
567+ assert_eq ! (
568+ proxy_config,
569+ ProxyConfig :: Socks5 ( Socks5Config {
570+ host: "my-proxy.example.org" . to_string( ) ,
571+ port: 1080 ,
572+ user_password: None
573+ } )
574+ ) ;
575+ assert_eq ! (
576+ proxy_config. to_url( ) ,
577+ "socks5://my-proxy.example.org:1080" . to_string( )
578+ ) ;
568579 }
569580
570581 #[ test]
@@ -598,6 +609,20 @@ mod tests {
598609 user_password: None
599610 } )
600611 ) ;
612+
613+ let proxy_config = ProxyConfig :: from_url ( "http://my-proxy.example.org" ) . unwrap ( ) ;
614+ assert_eq ! (
615+ proxy_config,
616+ ProxyConfig :: Http ( HttpConfig {
617+ host: "my-proxy.example.org" . to_string( ) ,
618+ port: 80 ,
619+ user_password: None
620+ } )
621+ ) ;
622+ assert_eq ! (
623+ proxy_config. to_url( ) ,
624+ "http://my-proxy.example.org:80" . to_string( )
625+ ) ;
601626 }
602627
603628 #[ test]
@@ -631,6 +656,20 @@ mod tests {
631656 user_password: None
632657 } )
633658 ) ;
659+
660+ let proxy_config = ProxyConfig :: from_url ( "https://my-proxy.example.org" ) . unwrap ( ) ;
661+ assert_eq ! (
662+ proxy_config,
663+ ProxyConfig :: Https ( HttpConfig {
664+ host: "my-proxy.example.org" . to_string( ) ,
665+ port: 443 ,
666+ user_password: None
667+ } )
668+ ) ;
669+ assert_eq ! (
670+ proxy_config. to_url( ) ,
671+ "https://my-proxy.example.org:443" . to_string( )
672+ ) ;
634673 }
635674
636675 #[ test]
0 commit comments