]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-http: Implemented functions for cloning and copying HTTP URLs and for constructin...
authorStephan Bosch <stephan@rename-it.nl>
Sat, 12 Oct 2013 07:58:04 +0000 (10:58 +0300)
committerStephan Bosch <stephan@rename-it.nl>
Sat, 12 Oct 2013 07:58:04 +0000 (10:58 +0300)
src/lib-http/http-url.c
src/lib-http/http-url.h

index 3236720e97cb8ce5ffce35b435609b293b71c1bb..17dba304e6c67e9f5d938baa983153a592195069 100644 (file)
@@ -418,35 +418,61 @@ int http_url_request_target_parse(const char *request_target,
 }
 
 /*
- * HTTP URL construction
+ * HTTP URL manipulation
  */
 
-static void http_url_add_target(string_t *urlstr, const struct http_url *url)
+void http_url_copy_authority(pool_t pool, struct http_url *dest,
+       const struct http_url *src)
 {
-       if (url->path == NULL || *url->path == '\0') {
-               /* Older syntax of RFC 2616 requires this slash at all times for an
-                        absolute URL
-                */
-               str_append_c(urlstr, '/');
-       } else {
-               uri_append_path_data(urlstr, "", url->path);
+       dest->host_name = p_strdup(pool, src->host_name);
+       if (src->have_host_ip) {
+               dest->host_ip = src->host_ip;
+               dest->have_host_ip = TRUE;
        }
-
-       /* query (pre-encoded) */
-       if (url->enc_query != NULL) {
-               str_append_c(urlstr, '?');
-               str_append(urlstr, url->enc_query);
+       if (src->have_port) {
+               dest->port = src->port;
+               dest->have_port = TRUE;
        }
+       dest->have_ssl = src->have_ssl;
 }
 
-const char *http_url_create(const struct http_url *url)
+void http_url_copy(pool_t pool, struct http_url *dest,
+       const struct http_url *src)
 {
-       string_t *urlstr = t_str_new(512);
+       http_url_copy_authority(pool, dest, src);
+       dest->path = p_strdup(pool, src->path);
+       dest->enc_query = p_strdup(pool, src->enc_query);
+       dest->enc_fragment = p_strdup(pool, src->enc_fragment);
+}
 
+struct http_url *http_url_clone(pool_t pool,const struct http_url *src)
+{
+       struct http_url *new_url;
+
+       new_url = p_new(pool, struct http_url, 1);
+       http_url_copy(pool, new_url, src);
+
+       return new_url;
+}
+
+/*
+ * HTTP URL construction
+ */
+
+static void
+http_url_add_scheme(string_t *urlstr, const struct http_url *url)
+{
        /* scheme */
-       uri_append_scheme(urlstr, "http");
+       if (!url->have_ssl)
+               uri_append_scheme(urlstr, "http");
+       else
+               uri_append_scheme(urlstr, "https");
        str_append(urlstr, "//");
+}
 
+static void
+http_url_add_authority(string_t *urlstr, const struct http_url *url)
+{
        /* host:port */
        if (url->host_name != NULL) {
                /* assume IPv6 literal if starts with '['; avoid encoding */
@@ -460,7 +486,33 @@ const char *http_url_create(const struct http_url *url)
                i_unreached();
        if (url->have_port)
                uri_append_port(urlstr, url->port);
+}
+
+static void
+http_url_add_target(string_t *urlstr, const struct http_url *url)
+{
+       if (url->path == NULL || *url->path == '\0') {
+               /* Older syntax of RFC 2616 requires this slash at all times for an
+                        absolute URL
+                */
+               str_append_c(urlstr, '/');
+       } else {
+               uri_append_path_data(urlstr, "", url->path);
+       }
+
+       /* query (pre-encoded) */
+       if (url->enc_query != NULL) {
+               str_append_c(urlstr, '?');
+               str_append(urlstr, url->enc_query);
+       }
+}
+
+const char *http_url_create(const struct http_url *url)
+{
+       string_t *urlstr = t_str_new(512);
 
+       http_url_add_scheme(urlstr, url);
+       http_url_add_authority(urlstr, url);
        http_url_add_target(urlstr, url);
 
        /* fragment */
@@ -472,6 +524,25 @@ const char *http_url_create(const struct http_url *url)
        return str_c(urlstr);
 }
 
+const char *http_url_create_host(const struct http_url *url)
+{
+       string_t *urlstr = t_str_new(512);
+
+       http_url_add_scheme(urlstr, url);
+       http_url_add_authority(urlstr, url);
+
+       return str_c(urlstr);
+}
+
+const char *http_url_create_authority(const struct http_url *url)
+{
+       string_t *urlstr = t_str_new(256);
+
+       http_url_add_authority(urlstr, url);
+
+       return str_c(urlstr);
+}
+
 const char *http_url_create_target(const struct http_url *url)
 {
        string_t *urlstr = t_str_new(256);
index ca058a1a6063e035b2996b3be8834fc01dce286a..34d30ab579d87a0803996948f3da2a354f57b029 100644 (file)
@@ -51,12 +51,24 @@ int http_url_request_target_parse(const char *request_target,
        const char *host_header, pool_t pool,
        struct http_request_target *target, const char **error_r);
 
+/*
+ * HTTP URL manipulation
+ */
+
+void http_url_copy_authority(pool_t pool, struct http_url *dest,
+       const struct http_url *src);
+void http_url_copy(pool_t pool, struct http_url *dest,
+       const struct http_url *src);
+struct http_url *http_url_clone(pool_t pool,const struct http_url *src);
+
 /*
  * HTTP URL construction
  */
 
 const char *http_url_create(const struct http_url *url);
 
+const char *http_url_create_host(const struct http_url *url);
+const char *http_url_create_authority(const struct http_url *url);
 const char *http_url_create_target(const struct http_url *url);
 
 void http_url_escape_param(string_t *out, const char *data);