From d61ea54a3848c5027270d7a456dbf72b329812db 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] Use mp_asprintf in make_noauth_url and make_http_proxy_url. --- stream/network.c | 5 +---- stream/url.c | 35 +++++++++++++++-------------------- stream/url.h | 2 +- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/stream/network.c b/stream/network.c index 1f1d382..fcb58d7 100644 --- a/stream/network.c +++ b/stream/network.c @@ -143,7 +143,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 +163,12 @@ 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); + new_url = get_http_proxy_url(proxy_url, 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..ca7d1e1 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,31 @@ 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) +static char *get_noauth_url(URL_t *url) { if (url->port) - return snprintf(dst, dst_size, "%s://%s:%d%s", url->protocol, - url->hostname, url->port, url->file); + return mp_asprintf("%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); + return mp_asprintf("%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) +char *get_http_proxy_url(URL_t *proxy, const char *host_url) { 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); + return mp_asprintf("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); + return mp_asprintf("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 +252,11 @@ 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); + Curl->noauth_url = get_noauth_url(Curl); 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..f5c75fc 100644 --- a/stream/url.h +++ b/stream/url.h @@ -38,7 +38,7 @@ 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); +char *get_http_proxy_url(URL_t *proxy, const char *host_url); URL_t* url_new(const char* url); void url_free(URL_t* url); -- 1.7.4.1