[PATCH] Add Proxy-Authorization to authenticate on proxies

Clément Bœsch ubitux at gmail.com
Sun Oct 17 20:25:49 CEST 2010


---
 stream/http.c    |   22 ++++++++++++++++++----
 stream/http.h    |    1 +
 stream/network.c |    2 ++
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/stream/http.c b/stream/http.c
index a700d43..154e093 100644
--- a/stream/http.c
+++ b/stream/http.c
@@ -602,8 +602,8 @@ http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
 	strcpy( http_hdr->uri, uri );
 }
 
-int
-http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
+static int
+http_add_authentication( HTTP_header_t *http_hdr, const char *username, const char *password, const char *auth_str ) {
 	char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL;
 	int encoded_len, pass_len=0, out_len;
 	int res = -1;
@@ -637,16 +637,20 @@ http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, co
 
 	b64_usr_pass[out_len]='\0';
 
-	auth = malloc(encoded_len+22);
+#define AUTH_BASIC_STR ": Basic "
+
+	auth = malloc(encoded_len + strlen(auth_str) + sizeof(AUTH_BASIC_STR));
 	if( auth==NULL ) {
 		mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
 		goto out;
 	}
 
-	sprintf( auth, "Authorization: Basic %s", b64_usr_pass);
+	sprintf( auth, "%s" AUTH_BASIC_STR "%s", auth_str, b64_usr_pass);
 	http_set_field( http_hdr, auth );
 	res = 0;
 
+#undef AUTH_BASIC_STR
+
 out:
 	free( usr_pass );
 	free( b64_usr_pass );
@@ -655,6 +659,16 @@ out:
 	return res;
 }
 
+int
+http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
+	return http_add_authentication(http_hdr, username, password, "Authorization");
+}
+
+int
+http_add_basic_proxy_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
+	return http_add_authentication(http_hdr, username, password, "Proxy-Authorization");
+}
+
 void
 http_debug_hdr( HTTP_header_t *http_hdr ) {
 	HTTP_field_t *field;
diff --git a/stream/http.h b/stream/http.h
index c52b9c8..d24ec45 100644
--- a/stream/http.h
+++ b/stream/http.h
@@ -63,6 +63,7 @@ void		http_set_field( HTTP_header_t *http_hdr, const char *field_name );
 void		http_set_method( HTTP_header_t *http_hdr, const char *method );
 void		http_set_uri( HTTP_header_t *http_hdr, const char *uri );
 int		http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password );
+int		http_add_basic_proxy_authentication( HTTP_header_t *http_hdr, const char *username, const char *password );
 
 void		http_debug_hdr( HTTP_header_t *http_hdr );
 
diff --git a/stream/network.c b/stream/network.c
index c8ec643..02ddcc8 100644
--- a/stream/network.c
+++ b/stream/network.c
@@ -254,6 +254,8 @@ http_send_request( URL_t *url, off_t pos ) {
 	if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
 
 	http_set_field( http_hdr, "Connection: close");
+	if (proxy)
+		http_add_basic_proxy_authentication(http_hdr, url->username, url->password);
 	http_add_basic_authentication(http_hdr, server_url->username, server_url->password);
 	if( http_build_request( http_hdr )==NULL ) {
 		goto err_out;
-- 
1.7.3.2


--tThc/1wpZn/ma/RB--


More information about the MPlayer-dev-eng mailing list