]> git.ozlabs.org Git - petitboot/blobdiff - lib/url/url.c
lib/url: Include port in pb_url_to_string()
[petitboot] / lib / url / url.c
index 6d1004f7f806d2ac6e94cfd9e265be4446eee852..a2ca3a58d4a4273016012b351d228b18154208a3 100644 (file)
@@ -20,9 +20,7 @@
 #include "config.h"
 #endif
 
-#define _GNU_SOURCE
 #include <assert.h>
-#include <stdbool.h>
 #include <string.h>
 
 #include "log/log.h"
@@ -228,7 +226,7 @@ fail:
        return NULL;
 }
 
-static bool is_url(const char *str)
+bool is_url(const char *str)
 {
        return strstr(str, "://") != NULL;
 }
@@ -236,10 +234,17 @@ static bool is_url(const char *str)
 char *pb_url_to_string(struct pb_url *url)
 {
        const struct pb_scheme_info *scheme = pb_url_scheme_info(url->scheme);
+       char *str, *port;
        assert(scheme);
 
-       return talloc_asprintf(url, "%s://%s%s", scheme->str,
-                       scheme->has_host ? url->host : "", url->path);
+       port = url->port ? talloc_asprintf(url, ":%s", url->port) : NULL;
+
+       str = talloc_asprintf(url, "%s://%s%s%s", scheme->str,
+                       scheme->has_host ? url->host : "",
+                       port ?: "", url->path);
+
+       talloc_free(port);
+       return str;
 }
 
 static void pb_url_update_full(struct pb_url *url)
@@ -248,19 +253,19 @@ static void pb_url_update_full(struct pb_url *url)
        url->full = pb_url_to_string(url);
 }
 
-static struct pb_url *pb_url_copy(void *ctx, const struct pb_url *url)
+struct pb_url *pb_url_copy(void *ctx, const struct pb_url *url)
 {
        struct pb_url *new_url;
 
        new_url = talloc(ctx, struct pb_url);
        new_url->scheme = url->scheme;
-       new_url->full = talloc_strdup(url, url->full);
+       new_url->full = talloc_strdup(new_url, url->full);
 
-       new_url->host = url->host ? talloc_strdup(url, url->host) : NULL;
-       new_url->port = url->port ? talloc_strdup(url, url->port) : NULL;
-       new_url->path = url->path ? talloc_strdup(url, url->path) : NULL;
-       new_url->dir  = url->dir  ? talloc_strdup(url, url->dir)  : NULL;
-       new_url->file = url->file ? talloc_strdup(url, url->file) : NULL;
+       new_url->host = url->host ? talloc_strdup(new_url, url->host) : NULL;
+       new_url->port = url->port ? talloc_strdup(new_url, url->port) : NULL;
+       new_url->path = url->path ? talloc_strdup(new_url, url->path) : NULL;
+       new_url->dir  = url->dir  ? talloc_strdup(new_url, url->dir)  : NULL;
+       new_url->file = url->file ? talloc_strdup(new_url, url->file) : NULL;
 
        return new_url;
 }