From 5a6dde54af8ce7208585a9e7ad0ef3ede26f24e0 Mon Sep 17 00:00:00 2001 From: cboesch Date: Sat, 26 Feb 2011 11:57:18 +0000 Subject: url: simplify Use mp_asprintf in make_noauth_url and make_http_proxy_url. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@32971 b3059339-0415-0410-9bf9-f77b7e298cf2 Author: cboesch Conflicts: stream/url.c Note: the mp_asprintf() function was introduced in r32970, and put into its own files. We just put that directly into url.c, as we use talloc_asprintf() in general, and mp_asprintf() is for url.c code only. (Making url.c use talloc would probably result in a big mess.) Make proxy and url parameter const in get_noauth_url and get_http_proxy_url. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@32972 b3059339-0415-0410-9bf9-f77b7e298cf2 Author: cboesch --- stream/network.c | 5 +---- stream/url.c | 61 +++++++++++++++++++++++++++++++++++++------------------- stream/url.h | 2 +- 3 files changed, 43 insertions(+), 25 deletions(-) (limited to 'stream') diff --git a/stream/network.c b/stream/network.c index 7d45ca71d9..572114a806 100644 --- a/stream/network.c +++ b/stream/network.c @@ -121,7 +121,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 ); @@ -142,14 +141,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_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n"); 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 2c241ee7ee..a8fbab5fb4 100644 --- a/stream/url.c +++ b/stream/url.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "url.h" @@ -33,6 +34,32 @@ #define SIZE_MAX ((size_t)-1) #endif +static char *mp_asprintf(const char *fmt, ...) +{ + char *p = NULL; + va_list va, va_bak; + int len; + + va_start(va, fmt); + va_copy(va_bak, va); + + len = vsnprintf(NULL, 0, fmt, va); + if (len < 0) + goto end; + + p = malloc(len + 1); + if (!p) + goto end; + + len = vsnprintf(p, len + 1, fmt, va_bak); + if (len < 0) + free(p), p = NULL; + +end: + va_end(va); + return p; +} + URL_t *url_redirect(URL_t **url, const char *redir) { URL_t *u = *url; URL_t *res; @@ -57,32 +84,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(const 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(const 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; @@ -251,16 +277,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, "Memory allocation failed.\n"); 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 b75978af2c..5954367c8a 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(const URL_t *proxy, const char *host_url); URL_t* url_new(const char* url); void url_free(URL_t* url); -- cgit v1.2.3