]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
uri-util: Removed have_port and have_host_ip flags.
authorStephan Bosch <stephan@rename-it.nl>
Sun, 8 May 2016 20:55:25 +0000 (22:55 +0200)
committerGitLab <gitlab@git.dovecot.net>
Mon, 16 May 2016 07:36:48 +0000 (10:36 +0300)
Presence of port can be tested with port != 0.
Presence of IP can be tested with ip->family != 0.

14 files changed:
src/imap-login/client-authenticate.c
src/lib-http/http-client-host.c
src/lib-http/http-client-request.c
src/lib-http/http-url.c
src/lib-http/http-url.h
src/lib-http/test-http-client.c
src/lib-http/test-http-request-parser.c
src/lib-http/test-http-url.c
src/lib-imap-urlauth/imap-urlauth.c
src/lib-imap/imap-url.c
src/lib-imap/imap-url.h
src/lib-imap/test-imap-url.c
src/lib/uri-util.c
src/lib/uri-util.h

index 829ad4a495dd93b681283d8f9325068f1a0e44e4..328d77833b0edf51ea643618027661c573824e5a 100644 (file)
@@ -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));
 
index f0a0558e4fafe15734ec8286a86f745abaec5c2e..12544b1e0499c82e1e93c73222b30ee4868d2d42 100644 (file)
@@ -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;
index 90a1e825b070287c1286cb54802a09554e07e002..930b92e2a14e1c6957b2069748c3f562498280e7 100644 (file)
@@ -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);
        }
 }
 
index 5306d14bc0dd4a7a7abab73082aceb7eb4d63059..9b7050cbae55c0235dba368fb2f68da8eefb9718 100644 (file)
@@ -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
index 54d6140a80ab0de1e022ddfc9fe66971bf2a0a99..3831000f078cb9592ce7e0e7c3d64639073dd9a3 100644 (file)
@@ -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;
 };
 
index 2424b0c3f0b31c7752ce05ee2dbe6336e02148bc..d7ea66b5a4a9e96d10ef2df21e114c8224aa660d 100644 (file)
@@ -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);
index 8b4162c8b63a5ebeb5af8f3ee734ae99c3d9f05b..1cab0d92684449bebe0750ac619858d3f3c4c6f8 100644 (file)
@@ -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",
index dcddddcddaa632a184e686e8ac7e9a00603edd68..87daf07ae59259689ba030cad56e36e1126fb118 100644 (file)
@@ -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 {
index d3d50ba8b14049000796450264117dc7bf0f11b7..44e906ffe8e94142ad573f735ee15433b7dd3545 100644 (file)
@@ -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;
        }
index 38c923ca50834ad8847686f7a2888dd64ddcbc06..a0ec9648e2bdb5495066b2182202fe57ef3edc6a 100644 (file)
@@ -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, '/');
index ba57f2026adf907ccbe08284967514adea9b527c..1c1ab9e70d01c06db439271728b2c14ee427e895 100644 (file)
@@ -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;
 };
 
index d49e915035b557941b6297c36475b67835c37bdd..f3bfdc5668f853f0043566968deb878fc83946a6 100644 (file)
@@ -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),
index dad6673c83ed3dca88ad91507c4d3a33f95b1e79..c16c2a953c9d6429a8c0ec6f015b4001ef29b6a1 100644 (file)
@@ -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,
index b4bafd0ecb2c5e0fa85373b7b78187df9bca4215..a716a1a814e4256403df4969136af5471d17e499 100644 (file)
@@ -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 {