diff utils.c @ 5755:dbc61b2840eb libavformat

Add a function ff_url_join for assembling URLs
author mstorsjo
date Fri, 05 Mar 2010 22:31:45 +0000
parents 2e5aecabfb1e
children 8ba33938e06f
line wrap: on
line diff
--- a/utils.c	Fri Mar 05 22:30:21 2010 +0000
+++ b/utils.c	Fri Mar 05 22:31:45 2010 +0000
@@ -27,6 +27,10 @@
 #include <sys/time.h>
 #include <time.h>
 #include <strings.h>
+#include <stdarg.h>
+#if CONFIG_NETWORK
+#include "network.h"
+#endif
 
 #undef NDEBUG
 #include <assert.h>
@@ -3446,3 +3450,48 @@
     if(!s->time_base.num || !s->time_base.den)
         s->time_base.num= s->time_base.den= 0;
 }
+
+int ff_url_join(char *str, int size, const char *proto,
+                const char *authorization, const char *hostname,
+                int port, const char *fmt, ...)
+{
+#if CONFIG_NETWORK
+    struct addrinfo hints, *ai;
+#endif
+
+    str[0] = '\0';
+    if (proto)
+        av_strlcatf(str, size, "%s://", proto);
+    if (authorization)
+        av_strlcatf(str, size, "%s@", authorization);
+#if CONFIG_NETWORK && defined(AF_INET6)
+    /* Determine if hostname is a numerical IPv6 address,
+     * properly escape it within [] in that case. */
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_flags = AI_NUMERICHOST;
+    if (!getaddrinfo(hostname, NULL, &hints, &ai)) {
+        if (ai->ai_family == AF_INET6) {
+            av_strlcat(str, "[", size);
+            av_strlcat(str, hostname, size);
+            av_strlcat(str, "]", size);
+        } else {
+            av_strlcat(str, hostname, size);
+        }
+        freeaddrinfo(ai);
+    } else
+#endif
+        /* Not an IPv6 address, just output the plain string. */
+        av_strlcat(str, hostname, size);
+
+    if (port >= 0)
+        av_strlcatf(str, size, ":%d", port);
+    if (fmt) {
+        va_list vl;
+        int len = strlen(str);
+
+        va_start(vl, fmt);
+        vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
+        va_end(vl);
+    }
+    return strlen(str);
+}