@@ -95,9 +95,18 @@ impl Request {
9595
9696impl RequestBuilder {
9797 pub ( super ) fn new ( client : Client , request : :: Result < Request > ) -> RequestBuilder {
98- RequestBuilder {
99- client,
100- request,
98+ let mut builder = RequestBuilder { client, request } ;
99+
100+ let auth = builder
101+ . request
102+ . as_mut ( )
103+ . ok ( )
104+ . and_then ( |req| extract_authority ( & mut req. url ) ) ;
105+
106+ if let Some ( ( username, password) ) = auth {
107+ builder. basic_auth ( username, password)
108+ } else {
109+ builder
101110 }
102111 }
103112
@@ -170,7 +179,7 @@ impl RequestBuilder {
170179 Some ( password) => format ! ( "{}:{}" , username, password) ,
171180 None => format ! ( "{}:" , username)
172181 } ;
173- let header_value = format ! ( "Basic {}" , encode( & auth) ) ;
182+ let header_value = format ! ( "Basic {}" , encode( & dbg! ( auth) ) ) ;
174183 self . header ( :: header:: AUTHORIZATION , & * header_value)
175184 }
176185
@@ -424,6 +433,37 @@ pub(crate) fn replace_headers(dst: &mut HeaderMap, src: HeaderMap) {
424433 }
425434}
426435
436+
437+ /// Check the request URL for a "username:password" type authority, and if
438+ /// found, remove it from the URL and return it.
439+ pub ( crate ) fn extract_authority ( url : & mut Url ) -> Option < ( String , Option < String > ) > {
440+ use url:: percent_encoding:: percent_decode;
441+
442+ if url. has_authority ( ) {
443+ let username: String = percent_decode ( url. username ( ) . as_bytes ( ) )
444+ . decode_utf8 ( )
445+ . ok ( ) ?
446+ . into ( ) ;
447+ let password = url. password ( ) . and_then ( |pass| {
448+ percent_decode ( pass. as_bytes ( ) )
449+ . decode_utf8 ( )
450+ . ok ( )
451+ . map ( String :: from)
452+ } ) ;
453+ if !username. is_empty ( ) || password. is_some ( ) {
454+ url
455+ . set_username ( "" )
456+ . expect ( "has_authority means set_username shouldn't fail" ) ;
457+ url
458+ . set_password ( None )
459+ . expect ( "has_authority means set_password shouldn't fail" ) ;
460+ return Some ( ( username, password) )
461+ }
462+ }
463+
464+ None
465+ }
466+
427467#[ cfg( test) ]
428468mod tests {
429469 use super :: Client ;
@@ -536,6 +576,20 @@ mod tests {
536576 assert_eq ! ( req. url( ) . as_str( ) , "https://google.com/" ) ;
537577 }
538578
579+ #[ test]
580+ fn convert_url_authority_into_basic_auth ( ) {
581+ let client = Client :: new ( ) ;
582+ let some_url = "https://Aladdin:open sesame@localhost/" ;
583+
584+ let req = client
585+ . get ( some_url)
586+ . build ( )
587+ . expect ( "request build" ) ;
588+
589+ assert_eq ! ( req. url( ) . as_str( ) , "https://localhost/" ) ;
590+ assert_eq ! ( req. headers( ) [ "authorization" ] , "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" ) ;
591+ }
592+
539593 /*
540594 use {body, Method};
541595 use super::Client;
0 commit comments