From 6fa3b312b08818ce9c4ec0e5af0a24dc3cd8242d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= Date: Sun, 13 Feb 2011 12:16:01 +0100 Subject: [PATCH 2/3] Replace make_noauth_url and make_http_proxy_url with mp_asprintf calls. --- stream/network.c | 15 +++++++++++---- stream/url.c | 38 ++++++++------------------------------ stream/url.h | 2 -- 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/stream/network.c b/stream/network.c index 1f1d382..d1d6faf 100644 --- a/stream/network.c +++ b/stream/network.c @@ -42,6 +42,7 @@ #include "libmpdemux/demuxer.h" #include "m_config.h" #include "mpcommon.h" +#include "mp_strings.h" #include "network.h" #include "tcp.h" #include "http.h" @@ -143,7 +144,6 @@ check4proxies( URL_t *url ) { proxy = getenv("http_proxy"); if( proxy!=NULL ) { // We got a proxy, build the URL to use it - int len; char *new_url; URL_t *tmp_url; URL_t *proxy_url = url_new( proxy ); @@ -164,14 +164,21 @@ check4proxies( URL_t *url ) { #endif mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url ); - len = make_http_proxy_url(proxy_url, url->url, NULL, 0) + 1; - new_url = malloc(len); + + if (proxy_url->username) + mp_asprintf(&new_url, "http_proxy://%s:%s@%s:%d/%s", + proxy_url->username, + proxy_url->password ? proxy_url->password : "", + proxy_url->hostname, proxy_url->port, url->url); + else + mp_asprintf(&new_url, "http_proxy://%s:%d/%s", + proxy_url->hostname, proxy_url->port, url->url); + if( new_url==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); url_free(proxy_url); return url_out; } - make_http_proxy_url(proxy_url, url->url, new_url, len); tmp_url = url_new( new_url ); if( tmp_url==NULL ) { free( new_url ); diff --git a/stream/url.c b/stream/url.c index af530f0..7769e91 100644 --- a/stream/url.c +++ b/stream/url.c @@ -28,6 +28,7 @@ #include "url.h" #include "mp_msg.h" +#include "mp_strings.h" #include "help_mp.h" #ifndef SIZE_MAX @@ -58,32 +59,9 @@ URL_t *url_redirect(URL_t **url, const char *redir) { return res; } -static int make_noauth_url(URL_t *url, char *dst, int dst_size) -{ - if (url->port) - return snprintf(dst, dst_size, "%s://%s:%d%s", url->protocol, - url->hostname, url->port, url->file); - else - return snprintf(dst, dst_size, "%s://%s%s", url->protocol, - url->hostname, url->file); -} - -int make_http_proxy_url(URL_t *proxy, const char *host_url, char *dst, - int dst_size) -{ - if (proxy->username) - return snprintf(dst, dst_size, "http_proxy://%s:%s@%s:%d/%s", - proxy->username, - proxy->password ? proxy->password : "", - proxy->hostname, proxy->port, host_url); - else - return snprintf(dst, dst_size, "http_proxy://%s:%d/%s", - proxy->hostname, proxy->port, host_url); -} - URL_t* url_new(const char* url) { - int pos1, pos2,v6addr = 0, noauth_len; + int pos1, pos2,v6addr = 0; URL_t* Curl = NULL; char *escfilename=NULL; char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL; @@ -252,16 +230,16 @@ url_new(const char* url) { strcpy(Curl->file, "/"); } - noauth_len = make_noauth_url(Curl, NULL, 0); - if (noauth_len > 0) { - noauth_len++; - Curl->noauth_url = malloc(noauth_len); + if (Curl->port) + mp_asprintf(&Curl->noauth_url, "%s://%s:%d%s", Curl->protocol, + Curl->hostname, Curl->port, Curl->file); + else + mp_asprintf(&Curl->noauth_url, "%s://%s%s", Curl->protocol, + Curl->hostname, Curl->file); if (!Curl->noauth_url) { mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MemAllocFailed); goto err_out; } - make_noauth_url(Curl, Curl->noauth_url, noauth_len); - } free(escfilename); return Curl; diff --git a/stream/url.h b/stream/url.h index b75978a..1ab67a0 100644 --- a/stream/url.h +++ b/stream/url.h @@ -38,8 +38,6 @@ typedef struct { URL_t *url_redirect(URL_t **url, const char *redir); -int make_http_proxy_url(URL_t *proxy, const char *host_url, char *dst, int dst_size); - URL_t* url_new(const char* url); void url_free(URL_t* url); -- 1.7.4.1