From: Jaroslav Kysela Date: Sat, 2 Dec 2017 17:32:28 +0000 (+0100) Subject: url: add urlrecompose() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b45a0f05c84129443976ed35ee4cbb0d18b45ead;p=thirdparty%2Ftvheadend.git url: add urlrecompose() --- diff --git a/src/input/mpegts/iptv/iptv_auto.c b/src/input/mpegts/iptv/iptv_auto.c index 1ac69bae6..1cad3a601 100644 --- a/src/input/mpegts/iptv/iptv_auto.c +++ b/src/input/mpegts/iptv/iptv_auto.c @@ -84,7 +84,7 @@ iptv_auto_network_process_m3u_item(iptv_network_t *in, size_t l; int64_t chnum2, vlcprog; const char *url, *name, *logo, *epgid, *tags; - char url2[512], custom[512], name2[128], buf[32], *n; + char custom[512], name2[128], buf[32], *n; url = htsmsg_get_str(item, "m3u-url"); @@ -181,18 +181,8 @@ iptv_auto_network_process_m3u_item(iptv_network_t *in, htsbuf_queue_flush(&q); } http_arg_flush(&args); - l = 0; - tvh_strlcatf(url2, sizeof(url2), l, "%s://", u.scheme); - if (u.user && u.user[0] && u.pass && u.pass[0]) - tvh_strlcatf(url2, sizeof(url2), l, "%s:%s@", u.user, u.pass); - tvh_strlcatf(url2, sizeof(url2), l, "%s", u.host); - if (u.port > 0) - tvh_strlcatf(url2, sizeof(url2), l, ":%d", u.port); - if (u.path) - tvh_strlcatf(url2, sizeof(url2), l, "%s", u.path); - if (u.query) - tvh_strlcatf(url2, sizeof(url2), l, "?%s", u.query); - url = url2; + if (!urlrecompose(&u)) + url = u.raw; } skip_url: diff --git a/src/input/mpegts/iptv/iptv_mux.c b/src/input/mpegts/iptv/iptv_mux.c index d993e5d48..5b42a6631 100644 --- a/src/input/mpegts/iptv/iptv_mux.c +++ b/src/input/mpegts/iptv/iptv_mux.c @@ -41,8 +41,6 @@ int iptv_url_set ( char **url, char **sane_url, const char *str, int allow_file, int allow_pipe ) { const char *x; - char *buf, port[16] = ""; - size_t len; url_t u; if (strcmp(str ?: "", *url ?: "") == 0) @@ -65,21 +63,8 @@ iptv_url_set ( char **url, char **sane_url, const char *str, int allow_file, int return 1; } urlinit(&u); - if (!urlparse(str, &u)) { - len = (u.scheme ? strlen(u.scheme) + 3 : 0) + - (u.host ? strlen(u.host) + 1 : 0) + - /* port */ 16 + - (u.path ? strlen(u.path) + 1 : 0) + - (u.query ? strlen(u.query) + 2 : 0); - buf = alloca(len); - if (u.port > 0 && u.port <= 65535) - snprintf(port, sizeof(port), ":%d", u.port); - snprintf(buf, len, "%s%s%s%s%s%s%s", - u.scheme ?: "", u.scheme ? "://" : "", - u.host ?: "", port, - u.path ?: "", (u.query && u.query[0]) ? "?" : "", - u.query ?: ""); - iptv_url_set0(url, sane_url, str, buf); + if (!urlparse(str, &u) && !urlrecompose(&u)) { + iptv_url_set0(url, sane_url, u.raw, str); urlreset(&u); return 1; } else { diff --git a/src/url.c b/src/url.c index 43b1163ef..dd275e8d4 100644 --- a/src/url.c +++ b/src/url.c @@ -54,6 +54,36 @@ urlcopy ( url_t *dst, const url_t *src ) dst->raw = strdup(src->raw); } +int +urlrecompose( url_t *url ) +{ + size_t len; + char *raw, port[16]; + + len = strlen(url->scheme) + 4 + + (url->user && url->pass ? + ((url->user ? strlen(url->user) + 2 : 0) + + (url->pass ? strlen(url->pass) : 0)) : 0 + ) + + strlen(url->host) + + (url->port > 0 ? 6 : 0) + + (url->path ? strlen(url->path) : 0) + + (url->query ? strlen(url->query) : 0); + raw = malloc(len); + if (raw == NULL) + return -ENOMEM; + if (url->port > 0 && url->port <= 65535) + snprintf(port, sizeof(port), ":%d", url->port); + snprintf(raw, len, "%s%s%s%s%s%s%s", + url->scheme ?: "", url->scheme ? "://" : "", + url->host ?: "", port, + url->path ?: "", + (url->query && url->query[0]) ? "?" : "", + url->query ?: ""); + url->raw = raw; + return 0; +} + /* Use liburiparser if available */ #if ENABLE_URIPARSER #include diff --git a/src/url.h b/src/url.h index d0b7d8044..f4859c1a9 100644 --- a/src/url.h +++ b/src/url.h @@ -42,5 +42,6 @@ void urlreset ( url_t *url ); int urlparse ( const char *str, url_t *url ); void urlparse_done ( void ); void urlcopy ( url_t *dst, const url_t *src ); +int urlrecompose ( url_t *url ); #endif