From f883bf3eff62f5d27df5ee9ee664edc38a77937f Mon Sep 17 00:00:00 2001 From: Stephan Bosch Date: Sun, 8 May 2016 22:55:25 +0200 Subject: [PATCH] uri-util: Removed have_port and have_host_ip flags. Presence of port can be tested with port != 0. Presence of IP can be tested with ip->family != 0. --- src/imap-login/client-authenticate.c | 4 +--- src/lib-http/http-client-host.c | 2 +- src/lib-http/http-client-request.c | 23 ++++++++++------------ src/lib-http/http-url.c | 25 ++++-------------------- src/lib-http/http-url.h | 2 -- src/lib-http/test-http-client.c | 2 +- src/lib-http/test-http-request-parser.c | 13 ++++++------- src/lib-http/test-http-url.c | 22 +++++++-------------- src/lib-imap-urlauth/imap-urlauth.c | 4 ++-- src/lib-imap/imap-url.c | 12 +++--------- src/lib-imap/imap-url.h | 2 -- src/lib-imap/test-imap-url.c | 26 ++++++++++++------------- src/lib/uri-util.c | 15 ++++++-------- src/lib/uri-util.h | 3 --- 14 files changed, 54 insertions(+), 101 deletions(-) diff --git a/src/imap-login/client-authenticate.c b/src/imap-login/client-authenticate.c index 829ad4a495..328d77833b 100644 --- a/src/imap-login/client-authenticate.c +++ b/src/imap-login/client-authenticate.c @@ -61,10 +61,8 @@ void imap_client_auth_result(struct client *client, url.userid = reply->destuser; url.auth_type = client->auth_mech_name; url.host_name = reply->host; - if (reply->port != 143) { - url.have_port = TRUE; + if (reply->port != 143) url.port = reply->port; - } str_append(referral, "REFERRAL "); str_append(referral, imap_url_create(&url)); diff --git a/src/lib-http/http-client-host.c b/src/lib-http/http-client-host.c index f0a0558e4f..12544b1e04 100644 --- a/src/lib-http/http-client-host.c +++ b/src/lib-http/http-client-host.c @@ -185,7 +185,7 @@ struct http_client_host *http_client_host_get hostname = host->name; hash_table_insert(client->hosts, hostname, host); - if (host_url->have_host_ip) { + if (host_url->host_ip.family != 0) { host->ips_count = 1; host->ips = i_new(struct ip_addr, host->ips_count); host->ips[0] = host_url->host_ip; diff --git a/src/lib-http/http-client-request.c b/src/lib-http/http-client-request.c index 90a1e825b0..930b92e2a1 100644 --- a/src/lib-http/http-client-request.c +++ b/src/lib-http/http-client-request.c @@ -127,7 +127,6 @@ http_client_request_connect(struct http_client *client, req = http_client_request_new(client, "CONNECT", callback, context); req->origin_url.host_name = p_strdup(req->pool, host); req->origin_url.port = port; - req->origin_url.have_port = TRUE; req->connect_tunnel = TRUE; req->target = req->origin_url.host_name; return req; @@ -141,12 +140,14 @@ http_client_request_connect_ip(struct http_client *client, void *context) { struct http_client_request *req; - const char *hostname = net_ip2addr(ip); + const char *hostname; + + i_assert(ip->family != 0); + hostname = net_ip2addr(ip); req = http_client_request_connect (client, hostname, port, callback, context); req->origin_url.host_ip = *ip; - req->origin_url.have_host_ip = TRUE; return req; } @@ -230,7 +231,6 @@ void http_client_request_set_port(struct http_client_request *req, { i_assert(req->state == HTTP_REQUEST_STATE_NEW); req->origin_url.port = port; - req->origin_url.have_port = TRUE; } void http_client_request_set_ssl(struct http_client_request *req, @@ -617,26 +617,23 @@ http_client_request_get_peer_addr(const struct http_client_request *req, addr->a.un.path = host_socket; } else if (req->connect_direct) { addr->type = HTTP_CLIENT_PEER_ADDR_RAW; - if (host_url->have_host_ip) - addr->a.tcp.ip = host_url->host_ip; + addr->a.tcp.ip = host_url->host_ip; addr->a.tcp.port = - (host_url->have_port ? host_url->port : HTTPS_DEFAULT_PORT); + (host_url->port != 0 ? host_url->port : HTTPS_DEFAULT_PORT); } else if (host_url->have_ssl) { if (req->ssl_tunnel) addr->type = HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL; else addr->type = HTTP_CLIENT_PEER_ADDR_HTTPS; - if (host_url->have_host_ip) - addr->a.tcp.ip = host_url->host_ip; + addr->a.tcp.ip = host_url->host_ip; addr->a.tcp.https_name = host_url->host_name; addr->a.tcp.port = - (host_url->have_port ? host_url->port : HTTPS_DEFAULT_PORT); + (host_url->port != 0 ? host_url->port : HTTPS_DEFAULT_PORT); } else { addr->type = HTTP_CLIENT_PEER_ADDR_HTTP; - if (host_url->have_host_ip) - addr->a.tcp.ip = host_url->host_ip; + addr->a.tcp.ip = host_url->host_ip; addr->a.tcp.port = - (host_url->have_port ? host_url->port : HTTP_DEFAULT_PORT); + (host_url->port != 0 ? host_url->port : HTTPS_DEFAULT_PORT); } } diff --git a/src/lib-http/http-url.c b/src/lib-http/http-url.c index 5306d14bc0..9b7050cbae 100644 --- a/src/lib-http/http-url.c +++ b/src/lib-http/http-url.c @@ -81,9 +81,7 @@ static bool http_url_parse_authority(struct http_url_parser *url_parser) if (url != NULL) { url->host_name = p_strdup(parser->pool, auth.host_literal); url->host_ip = auth.host_ip; - url->have_host_ip = auth.have_host_ip; url->port = auth.port; - url->have_port = auth.have_port; url->user = p_strdup(parser->pool, user); url->password = p_strdup(parser->pool, password); } @@ -236,9 +234,7 @@ static bool http_url_do_parse(struct http_url_parser *url_parser) } else if (!have_authority && url != NULL) { url->host_name = p_strdup_empty(parser->pool, base->host_name); url->host_ip = base->host_ip; - url->have_host_ip = base->have_host_ip; url->port = base->port; - url->have_port = base->have_port; url->have_ssl = base->have_ssl; url->user = p_strdup_empty(parser->pool, base->user); url->password = p_strdup_empty(parser->pool, base->password); @@ -388,8 +384,6 @@ int http_url_request_target_parse(const char *request_target, url->host_name = p_strdup(pool, host.host_literal); url->host_ip = host.host_ip; url->port = host.port; - url->have_host_ip = host.have_host_ip; - url->have_port = host.have_port; target->url = url; target->format = HTTP_REQUEST_TARGET_FORMAT_ASTERISK; return 0; @@ -399,8 +393,6 @@ int http_url_request_target_parse(const char *request_target, base.host_name = host.host_literal; base.host_ip = host.host_ip; base.port = host.port; - base.have_host_ip = host.have_host_ip; - base.have_port = host.have_port; memset(parser, '\0', sizeof(*parser)); uri_parser_init(parser, pool, request_target); @@ -430,14 +422,8 @@ void http_url_copy_authority(pool_t pool, struct http_url *dest, { memset(dest, 0, sizeof(*dest)); 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; - } - if (src->have_port) { - dest->port = src->port; - dest->have_port = TRUE; - } + dest->host_ip = src->host_ip; + dest->port = src->port; dest->have_ssl = src->have_ssl; } @@ -516,12 +502,9 @@ http_url_add_authority(string_t *urlstr, const struct http_url *url) str_append(urlstr, url->host_name); else uri_append_host_name(urlstr, url->host_name); - } else if (url->have_host_ip) { - uri_append_host_ip(urlstr, &url->host_ip); } else - i_unreached(); - if (url->have_port) - uri_append_port(urlstr, url->port); + uri_append_host_ip(urlstr, &url->host_ip); + uri_append_port(urlstr, url->port); } static void diff --git a/src/lib-http/http-url.h b/src/lib-http/http-url.h index 54d6140a80..3831000f07 100644 --- a/src/lib-http/http-url.h +++ b/src/lib-http/http-url.h @@ -24,8 +24,6 @@ struct http_url { /* #fragment (still encoded) */ const char *enc_fragment; - unsigned int have_host_ip:1; /* URL uses IP address */ - unsigned int have_port:1; unsigned int have_ssl:1; }; diff --git a/src/lib-http/test-http-client.c b/src/lib-http/test-http-client.c index 2424b0c3f0..d7ea66b5a4 100644 --- a/src/lib-http/test-http-client.c +++ b/src/lib-http/test-http-client.c @@ -298,7 +298,7 @@ test_http_request_init(struct http_client *http_client, method, url->host_name, t_strconcat("/", url->path, url->enc_query, NULL), got_request_response, test_req); - if (url->have_port) + if (url->port != 0) http_client_request_set_port(http_req, url->port); if (url->have_ssl) http_client_request_set_ssl(http_req, TRUE); diff --git a/src/lib-http/test-http-request-parser.c b/src/lib-http/test-http-request-parser.c index 8b4162c8b6..1cab0d9268 100644 --- a/src/lib-http/test-http-request-parser.c +++ b/src/lib-http/test-http-request-parser.c @@ -65,7 +65,7 @@ valid_request_parse_tests[] = { .target_raw = "example.com:443", .target = { .format = HTTP_REQUEST_TARGET_FORMAT_AUTHORITY, - .url = { .host_name = "example.com", .have_port = TRUE, .port = 443 } + .url = { .host_name = "example.com", .port = 443 } }, .version_major = 1, .version_minor = 2, },{ .request = @@ -78,7 +78,7 @@ valid_request_parse_tests[] = { .format = HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE, .url = { .host_name = "www.example.com", - .have_port = TRUE, .port = 443, + .port = 443, .have_ssl = TRUE } }, @@ -93,7 +93,7 @@ valid_request_parse_tests[] = { .target_raw = "http://api.example.com:8080/commit?user=dirk", .target = { .format = HTTP_REQUEST_TARGET_FORMAT_ABSOLUTE, - .url = { .host_name = "api.example.com", .have_port = TRUE, .port = 8080 } + .url = { .host_name = "api.example.com", .port = 8080 } }, .version_major = 1, .version_minor = 1, .payload = "Content!\r\n" @@ -229,7 +229,7 @@ static void test_http_request_parse_valid(void) } if (request.target.url == NULL) { test_out("request->target.url = (null)", - test->target.url.host_name == NULL && !test->target.url.have_port); + test->target.url.host_name == NULL && test->target.url.port == 0); } else { if (request.target.url->host_name == NULL || test->target.url.host_name == NULL) { @@ -242,13 +242,12 @@ static void test_http_request_parse_valid(void) strcmp(request.target.url->host_name, test->target.url.host_name) == 0); } - if (!request.target.url->have_port) { + if (request.target.url->port == 0) { test_out("request->target.url->port = (unspecified)", - request.target.url->have_port == test->target.url.have_port); + request.target.url->port == test->target.url.port); } else { test_out(t_strdup_printf ("request->target.url->port = %u", request.target.url->port), - request.target.url->have_port == test->target.url.have_port && request.target.url->port == test->target.url.port); } test_out(t_strdup_printf("request->target.url->have_ssl = %s", diff --git a/src/lib-http/test-http-url.c b/src/lib-http/test-http-url.c index dcddddcdda..87daf07ae5 100644 --- a/src/lib-http/test-http-url.c +++ b/src/lib-http/test-http-url.c @@ -28,23 +28,23 @@ static struct valid_http_url_test valid_url_tests[] = { .url = "http://www.dovecot.org:8080", .url_parsed = { .host_name = "www.dovecot.org", - .port = 8080, .have_port = TRUE } + .port = 8080 } },{ .url = "http://127.0.0.1", .url_parsed = { .host_name = "127.0.0.1", - .have_host_ip = TRUE } + .host_ip = { .family = AF_INET } } },{ .url = "http://[::1]", .url_parsed = { .host_name = "[::1]", - .have_host_ip = TRUE } + .host_ip = { .family = AF_INET6 } } },{ .url = "http://[::1]:8080", .url_parsed = { .host_name = "[::1]", - .have_host_ip = TRUE, - .port = 8080, .have_port = TRUE } + .host_ip = { .family = AF_INET6 }, + .port = 8080 } },{ .url = "http://user@api.dovecot.org", .flags = HTTP_URL_ALLOW_USERINFO_PART, @@ -315,16 +315,8 @@ static void test_http_url_valid(void) } else { test_assert(strcmp(urlp->host_name, urlt->host_name) == 0); } - if (!urlp->have_port) { - test_assert(urlp->have_port == urlt->have_port); - } else { - test_assert(urlp->have_port == urlt->have_port && urlp->port == urlt->port); - } - if (!urlp->have_host_ip) { - test_assert(urlp->have_host_ip == urlt->have_host_ip); - } else { - test_assert(urlp->have_host_ip == urlt->have_host_ip); - } + test_assert(urlp->port == urlt->port); + test_assert(urlp->host_ip.family == urlt->host_ip.family); if (urlp->user == NULL || urlt->user == NULL) { test_assert(urlp->user == urlt->user); } else { diff --git a/src/lib-imap-urlauth/imap-urlauth.c b/src/lib-imap-urlauth/imap-urlauth.c index d3d50ba8b1..44e906ffe8 100644 --- a/src/lib-imap-urlauth/imap-urlauth.c +++ b/src/lib-imap-urlauth/imap-urlauth.c @@ -211,8 +211,8 @@ imap_urlauth_check_hostport(struct imap_urlauth_context *uctx, } /* validate port */ - if ((!url->have_port && uctx->url_port != 143) || - (url->have_port && uctx->url_port != url->port)) { + if ((url->port == 0 && uctx->url_port != 143) || + (url->port != 0 && uctx->url_port != url->port)) { *error_r = "Invalid URL: Inappropriate server port"; return FALSE; } diff --git a/src/lib-imap/imap-url.c b/src/lib-imap/imap-url.c index 38c923ca50..a0ec9648e2 100644 --- a/src/lib-imap/imap-url.c +++ b/src/lib-imap/imap-url.c @@ -261,9 +261,7 @@ static int imap_url_parse_iserver(struct imap_url_parser *url_parser) if (url != NULL) { url->host_name = auth.host_literal; url->host_ip = auth.host_ip; - url->have_host_ip = auth.have_host_ip; url->port = auth.port; - url->have_port = auth.have_port; } return 1; } @@ -840,8 +838,6 @@ static bool imap_url_do_parse(struct imap_url_parser *url_parser) url->host_name = p_strdup_empty(parser->pool, base->host_name); url->host_ip = base->host_ip; - url->have_host_ip = base->have_host_ip; - url->have_port = base->have_port; url->port = base->port; url->userid = p_strdup_empty(parser->pool, base->userid); url->auth_type = p_strdup_empty(parser->pool, base->auth_type); @@ -999,12 +995,10 @@ const char *imap_url_create(const struct imap_url *url) str_append(urlstr, url->host_name); else uri_append_host_name(urlstr, url->host_name); - } else if (url->have_host_ip) { + } else { uri_append_host_ip(urlstr, &url->host_ip); - } else - i_unreached(); - if (url->have_port) - uri_append_port(urlstr, url->port); + } + uri_append_port(urlstr, url->port); /* Older syntax (RFC 2192) requires this slash at all times */ str_append_c(urlstr, '/'); diff --git a/src/lib-imap/imap-url.h b/src/lib-imap/imap-url.h index ba57f2026a..1c1ab9e70d 100644 --- a/src/lib-imap/imap-url.h +++ b/src/lib-imap/imap-url.h @@ -33,8 +33,6 @@ struct imap_url { size_t uauth_token_size; time_t uauth_expire; /* (time_t)-1 if not set */ - unsigned int have_host_ip:1; /* url uses IP address */ - unsigned int have_port:1; unsigned int have_partial:1; }; diff --git a/src/lib-imap/test-imap-url.c b/src/lib-imap/test-imap-url.c index d49e915035..f3bfdc5668 100644 --- a/src/lib-imap/test-imap-url.c +++ b/src/lib-imap/test-imap-url.c @@ -46,31 +46,31 @@ static const struct valid_imap_url_test valid_url_tests[] = { .url_parsed = { .host_name = "localhost", .userid = "user", - .port = 993, .have_port = TRUE } + .port = 993 } },{ .url = "imap://user@127.0.0.1", .url_parsed = { .host_name = "127.0.0.1", - .userid = "user", - .have_host_ip = TRUE } + .host_ip = { .family = AF_INET }, + .userid = "user" } },{ .url = "imap://user@[::1]", .url_parsed = { .host_name = "[::1]", - .userid = "user", - .have_host_ip = TRUE } + .host_ip = { .family = AF_INET6 }, + .userid = "user" } },{ .url = "imap://user@4example.com:423", .url_parsed = { .host_name = "4example.com", .userid = "user", - .port = 423, .have_port = TRUE } + .port = 423 } },{ .url = "imap://beelzebub@666.4example.com:999", .url_parsed = { .host_name = "666.4example.com", .userid = "beelzebub", - .port = 999, .have_port = TRUE } + .port = 999 } },{ .url = "imap://user@example.com/", .url_parsed = { @@ -648,19 +648,19 @@ static void test_imap_url_valid(void) test_out_quiet(t_strdup_printf("url->auth_type = %s", urlp->auth_type), strcmp(urlp->auth_type, urlt->auth_type) == 0); } - if (!urlp->have_port) { + if (urlp->port == 0) { test_out_quiet("url->port = (unspecified)", - urlp->have_port == urlt->have_port); + urlp->port == urlt->port); } else { test_out_quiet(t_strdup_printf("url->port = %u", urlp->port), - urlp->have_port == urlt->have_port && urlp->port == urlt->port); + urlp->port == urlt->port); } - if (!urlp->have_host_ip) { + if (urlp->host_ip.family == 0) { test_out_quiet("url->host_ip = (unspecified)", - urlp->have_host_ip == urlt->have_host_ip); + urlp->host_ip.family == urlt->host_ip.family); } else { test_out_quiet("url->host_ip = (valid)", - urlp->have_host_ip == urlt->have_host_ip); + urlp->host_ip.family == urlt->host_ip.family); } if (urlp->mailbox == NULL || urlt->mailbox == NULL) { test_out_quiet(t_strdup_printf("url->mailbox = %s", urlp->mailbox), diff --git a/src/lib/uri-util.c b/src/lib/uri-util.c index dad6673c83..c16c2a953c 100644 --- a/src/lib/uri-util.c +++ b/src/lib/uri-util.c @@ -441,7 +441,6 @@ uri_parse_host(struct uri_parser *parser, auth->host_literal = p_strdup(parser->pool, str_c(literal)); auth->host_ip.family = AF_INET6; auth->host_ip.u.ip6 = ip6; - auth->have_host_ip = TRUE; } return 1; } @@ -456,7 +455,6 @@ uri_parse_host(struct uri_parser *parser, auth->host_literal = p_strdup(parser->pool, str_c(literal)); auth->host_ip.family = AF_INET; auth->host_ip.u.ip4 = ip4; - auth->have_host_ip = TRUE; } return ret; } @@ -466,10 +464,8 @@ uri_parse_host(struct uri_parser *parser, /* reg-name */ if (uri_parse_reg_name(parser, literal) < 0) return -1; - if (auth != NULL) { + if (auth != NULL) auth->host_literal = p_strdup(parser->pool, str_c(literal)); - auth->have_host_ip = FALSE; - } return 0; } @@ -496,10 +492,8 @@ uri_parse_port(struct uri_parser *parser, return -1; } - if (auth != NULL) { + if (auth != NULL) auth->port = port; - auth->have_port = TRUE; - } return 1; } @@ -851,6 +845,8 @@ void uri_append_host_ip(string_t *out, const struct ip_addr *host_ip) { const char *addr = net_ip2addr(host_ip); + i_assert(host_ip->family != 0); + if (host_ip->family == AF_INET) { str_append(out, addr); return; @@ -864,7 +860,8 @@ void uri_append_host_ip(string_t *out, const struct ip_addr *host_ip) void uri_append_port(string_t *out, in_port_t port) { - str_printfa(out, ":%u", port); + if (port != 0) + str_printfa(out, ":%u", port); } void uri_append_path_segment_data(string_t *out, const char *esc, diff --git a/src/lib/uri-util.h b/src/lib/uri-util.h index b4bafd0ecb..a716a1a814 100644 --- a/src/lib/uri-util.h +++ b/src/lib/uri-util.h @@ -14,9 +14,6 @@ struct uri_authority { struct ip_addr host_ip; in_port_t port; - - unsigned int have_host_ip:1; - unsigned int have_port:1; }; struct uri_parser { -- 2.47.3