@@ -2182,8 +2182,28 @@ def sanitize_url(url):
2182
2182
return url
2183
2183
2184
2184
2185
+ def extract_basic_auth (url ):
2186
+ parts = compat_urllib_parse .urlsplit (url )
2187
+ if parts .username is None :
2188
+ return url , None
2189
+ url = compat_urllib_parse .urlunsplit (parts ._replace (netloc = (
2190
+ parts .hostname if parts .port is None
2191
+ else '%s:%d' % (parts .hostname , parts .port ))))
2192
+ auth_payload = base64 .b64encode (
2193
+ ('%s:%s' % (parts .username , parts .password or '' )).encode ('utf-8' ))
2194
+ return url , 'Basic {0}' .format (auth_payload .decode ('ascii' ))
2195
+
2196
+
2185
2197
def sanitized_Request (url , * args , ** kwargs ):
2186
- return compat_urllib_request .Request (escape_url (sanitize_url (url )), * args , ** kwargs )
2198
+ url , auth_header = extract_basic_auth (escape_url (sanitize_url (url )))
2199
+ if auth_header is not None :
2200
+ headers = args [1 ] if len (args ) > 1 else kwargs .get ('headers' )
2201
+ headers = headers or {}
2202
+ headers ['Authorization' ] = auth_header
2203
+ if len (args ) <= 1 and kwargs .get ('headers' ) is None :
2204
+ kwargs ['headers' ] = headers
2205
+ kwargs = compat_kwargs (kwargs )
2206
+ return compat_urllib_request .Request (url , * args , ** kwargs )
2187
2207
2188
2208
2189
2209
def expand_path (s ):
0 commit comments