From: Stephan Bosch Date: Sat, 12 Oct 2013 07:58:04 +0000 (+0300) Subject: lib-http: Implemented functions for cloning and copying HTTP URLs and for constructin... X-Git-Tag: 2.2.7~76 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d82ad7143c057c565e1fd5f3580645556ed0bcc9;p=thirdparty%2Fdovecot%2Fcore.git lib-http: Implemented functions for cloning and copying HTTP URLs and for constructing partial URLs. --- diff --git a/src/lib-http/http-url.c b/src/lib-http/http-url.c index 3236720e97..17dba304e6 100644 --- a/src/lib-http/http-url.c +++ b/src/lib-http/http-url.c @@ -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); diff --git a/src/lib-http/http-url.h b/src/lib-http/http-url.h index ca058a1a60..34d30ab579 100644 --- a/src/lib-http/http-url.h +++ b/src/lib-http/http-url.h @@ -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);