diff stream/http.c @ 32547:6a629e6fdb09

Add Proxy-Authorization header to authenticate on proxies
author cboesch
date Thu, 18 Nov 2010 20:20:39 +0000
parents 84bdbf9e9c48
children 683199947822
line wrap: on
line diff
--- a/stream/http.c	Thu Nov 18 20:18:44 2010 +0000
+++ b/stream/http.c	Thu Nov 18 20:20:39 2010 +0000
@@ -602,10 +602,11 @@
 	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;
+	size_t auth_len;
 	int res = -1;
 	if( http_hdr==NULL || username==NULL ) return -1;
 
@@ -637,13 +638,14 @@
 
 	b64_usr_pass[out_len]='\0';
 
-	auth = malloc(encoded_len+22);
+	auth_len = encoded_len + 100;
+	auth = malloc(auth_len);
 	if( auth==NULL ) {
 		mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
 		goto out;
 	}
 
-	sprintf( auth, "Authorization: Basic %s", b64_usr_pass);
+	snprintf(auth, auth_len, "%s: Basic %s", auth_str, b64_usr_pass);
 	http_set_field( http_hdr, auth );
 	res = 0;
 
@@ -655,6 +657,16 @@
 	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;