]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
url: add urlrecompose()
authorJaroslav Kysela <perex@perex.cz>
Sat, 2 Dec 2017 17:32:28 +0000 (18:32 +0100)
committerJaroslav Kysela <perex@perex.cz>
Sat, 2 Dec 2017 17:45:49 +0000 (18:45 +0100)
src/input/mpegts/iptv/iptv_auto.c
src/input/mpegts/iptv/iptv_mux.c
src/url.c
src/url.h

index 1ac69bae60354848cb203f5c2665b415270ee18b..1cad3a601f7d34916944db755907d37dec934e65 100644 (file)
@@ -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:
index d993e5d48dc8e49548e6f554cf189f65720c18aa..5b42a663190407e56287cb13f6362a7f3849f4ea 100644 (file)
@@ -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 {
index 43b1163efc1856caec3ef2ca501bce1acc31a977..dd275e8d43d4d9b3c86bebc1de07743cc84b8ec8 100644 (file)
--- 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 <uriparser/Uri.h>
index d0b7d80445a0b0c2abafc1c7a9ad188c3ceaa172..f4859c1a961c7c626aefb94b641d3f06dad8191d 100644 (file)
--- 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