]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
uri-util: Changed URI host/authority parsing API to not use a boolean.
authorStephan Bosch <stephan@rename-it.nl>
Mon, 16 May 2016 16:43:52 +0000 (18:43 +0200)
committerGitLab <gitlab@git.dovecot.net>
Mon, 16 May 2016 18:32:53 +0000 (21:32 +0300)
The boolean indicated whether the basic reg-name syntax or the more limited Internet host name syntax was to be expected.
This change creates separate functions for this.

src/lib-http/http-url.c
src/lib-imap/imap-url.c
src/lib/uri-util.c
src/lib/uri-util.h

index e4b76619b9b02b9b2229e45cd330824e80fc8be0..df7a209ba6b3801dacbf63dcdfa40c3112f3d32d 100644 (file)
@@ -35,7 +35,7 @@ static bool http_url_parse_authority(struct http_url_parser *url_parser)
        const char *user = NULL, *password = NULL;
        int ret;
 
-       if ((ret = uri_parse_authority(parser, &auth, TRUE)) < 0)
+       if ((ret = uri_parse_host_authority(parser, &auth)) < 0)
                return FALSE;
        if (auth.host.name == NULL || *auth.host.name == '\0') {
                /* RFC 7230, Section 2.7.1: http URI Scheme
@@ -367,7 +367,7 @@ int http_url_request_target_parse(const char *request_target,
        parser = &url_parser.parser;
        uri_parser_init(parser, pool, host_header);
 
-       if (uri_parse_authority(parser, &auth, TRUE) <= 0) {
+       if (uri_parse_host_authority(parser, &auth) <= 0) {
                *error_r = t_strdup_printf("Invalid Host header: %s", parser->error);
                return -1;
        }
index 670b6020091d5d6b573ce62031a56a00282aa917..6ce2318e05f43880021dbf7d5831d1c1e89796d6 100644 (file)
@@ -191,8 +191,8 @@ static int imap_url_parse_iserver(struct imap_url_parser *url_parser)
         */
 
        /* "//" iserver */
-       if ((ret = uri_parse_slashslash_authority
-               (parser, &auth, TRUE)) <= 0)
+       if ((ret = uri_parse_slashslash_host_authority
+               (parser, &auth)) <= 0)
                return ret;
        if (auth.host.name == NULL || *auth.host.name == '\0') {
                /* This situation is not documented anywhere, but it is not
index 5e6c2e6cc0782a09a4f1c174ce02b68a3eea6e39..a3e170fbb9a500dbf6b3c5ba15cbf125e7c4e5ea 100644 (file)
@@ -393,7 +393,7 @@ uri_parse_ipv4address(struct uri_parser *parser, string_t *literal,
 }
 
 static int
-uri_parse_reg_name(struct uri_parser *parser,
+uri_do_parse_reg_name(struct uri_parser *parser,
        string_t *reg_name) ATTR_NULL(2)
 {
        /* RFC 3986:
@@ -431,7 +431,24 @@ uri_parse_reg_name(struct uri_parser *parser,
        return 0;
 }
 
-static int uri_do_parse_host_name_dns(struct uri_parser *parser,
+int uri_parse_reg_name(struct uri_parser *parser,
+       const char **reg_name_r)
+{
+       string_t *reg_name = NULL;
+       int ret;
+
+       if (reg_name_r != NULL)
+               reg_name = uri_parser_get_tmpbuf(parser, 256);
+
+       if ((ret=uri_do_parse_reg_name(parser, reg_name)) <= 0)
+               return ret;
+
+       if (reg_name_r != NULL)
+               *reg_name_r = str_c(reg_name);
+       return 1;
+}
+
+static int uri_do_parse_host_name(struct uri_parser *parser,
        string_t *host_name) ATTR_NULL(2)
 {
        const unsigned char *first, *part;
@@ -547,7 +564,7 @@ static int uri_do_parse_host_name_dns(struct uri_parser *parser,
        return 1;
 }
 
-int uri_parse_host_name_dns(struct uri_parser *parser,
+int uri_parse_host_name(struct uri_parser *parser,
        const char **host_name_r)
 {
        string_t *host_name = NULL;
@@ -556,7 +573,7 @@ int uri_parse_host_name_dns(struct uri_parser *parser,
        if (host_name_r != NULL)
                host_name = uri_parser_get_tmpbuf(parser, 256);
 
-       if ((ret=uri_do_parse_host_name_dns(parser, host_name)) <= 0)
+       if ((ret=uri_do_parse_host_name(parser, host_name)) <= 0)
                return ret;
 
        if (host_name_r != NULL)
@@ -615,8 +632,10 @@ uri_parse_ip_literal(struct uri_parser *parser, string_t *literal,
        return 1;
 }
 
-int uri_parse_host(struct uri_parser *parser,
-       struct uri_host *host, bool dns_name)
+static int
+uri_do_parse_host(struct uri_parser *parser,
+       struct uri_host *host, bool host_name)
+       ATTR_NULL(2)
 {
        const unsigned char *preserve;
        struct in_addr ip4;
@@ -664,16 +683,22 @@ int uri_parse_host(struct uri_parser *parser,
        str_truncate(literal, 0);
 
        /* reg-name */
-       if (dns_name) {
-               if (uri_do_parse_host_name_dns(parser, literal) < 0)
+       if (host_name) {
+               if (uri_do_parse_host_name(parser, literal) < 0)
                        return -1;
-       } else  if (uri_parse_reg_name(parser, literal) < 0)
+       } else  if (uri_do_parse_reg_name(parser, literal) < 0)
                return -1;
        if (host != NULL)
                host->name = p_strdup(parser->pool, str_c(literal));
        return 0;
 }
 
+int uri_parse_host(struct uri_parser *parser,
+       struct uri_host *host)
+{
+       return uri_do_parse_host(parser, host, TRUE);
+}
+
 static int
 uri_parse_port(struct uri_parser *parser,
        struct uri_authority *auth) ATTR_NULL(2)
@@ -702,8 +727,9 @@ uri_parse_port(struct uri_parser *parser,
        return 1;
 }
 
-int uri_parse_authority(struct uri_parser *parser,
-       struct uri_authority *auth, bool dns_name)
+static int
+uri_do_parse_authority(struct uri_parser *parser,
+       struct uri_authority *auth, bool host_name) ATTR_NULL(2)
 {
        const unsigned char *p;
        int ret;
@@ -734,8 +760,8 @@ int uri_parse_authority(struct uri_parser *parser,
        }
 
        /* host */
-       if (uri_parse_host(parser,
-               (auth == NULL ? NULL : &auth->host), dns_name) < 0)
+       if (uri_do_parse_host(parser,
+               (auth == NULL ? NULL : &auth->host), host_name) < 0)
                return -1;
        if (parser->cur == parser->end)
                return 1;
@@ -767,8 +793,10 @@ int uri_parse_authority(struct uri_parser *parser,
        return 1;
 }
 
-int uri_parse_slashslash_authority(struct uri_parser *parser,
-       struct uri_authority *auth, bool dns_name)
+static int
+uri_do_parse_slashslash_authority(struct uri_parser *parser,
+       struct uri_authority *auth, bool host_name)
+       ATTR_NULL(2)
 {
        /* "//" authority */
 
@@ -777,7 +805,31 @@ int uri_parse_slashslash_authority(struct uri_parser *parser,
                return 0;
 
        parser->cur += 2;
-       return uri_parse_authority(parser, auth, dns_name);
+       return uri_do_parse_authority(parser, auth, host_name);
+}
+
+int uri_parse_authority(struct uri_parser *parser,
+       struct uri_authority *auth)
+{
+       return uri_do_parse_authority(parser, auth, FALSE);
+}
+
+int uri_parse_slashslash_authority(struct uri_parser *parser,
+       struct uri_authority *auth)
+{
+       return uri_do_parse_slashslash_authority(parser, auth, FALSE);
+}
+
+int uri_parse_host_authority(struct uri_parser *parser,
+       struct uri_authority *auth)
+{
+       return uri_do_parse_authority(parser, auth, TRUE);
+}
+
+int uri_parse_slashslash_host_authority(struct uri_parser *parser,
+       struct uri_authority *auth)
+{
+       return uri_do_parse_slashslash_authority(parser, auth, TRUE);
 }
 
 int uri_parse_path_segment(struct uri_parser *parser, const char **segment_r)
index 7a7e4bf7e137aa5f90b2071454876ec5d5fbb39e..4db1f0f379cafc77414ab58819e0dce6ecd33a37 100644 (file)
@@ -66,46 +66,63 @@ int uri_cut_scheme(const char **uri_p, const char **scheme_r)
 int uri_parse_scheme(struct uri_parser *parser, const char **scheme_r)
        ATTR_NULL(2);
 
-/* parse a DNS host name. A host name is a sequence of domain name labels
-   separated by '.', as defined in Section 3.5 of RFC 1034 and Section
-   2.1 of RFC 1123. Returns 1 if successful, 0 if the first character is
-   not valid for a host name, and -1 in case of error. The result parameter
-   host_name_r can be NULL to use this function for merely checking the
-   presence of a valid host name. The result is allocated from the data
-   stack.
- */
-int uri_parse_host_name_dns(struct uri_parser *parser,
+/* parse the URI 'reg-name' syntax. Returns 1 if successful, 0 if the first
+   character is not valid for a host name, and -1 in case of error. The
+   result parameter reg_name_r can be NULL to use this function for merely
+   checking the presence of a valid host name. The result is allocated from
+   the data stack.
+ */
+int uri_parse_reg_name(struct uri_parser *parser,
+       const char **reg_name_r) ATTR_NULL(2);
+/* parse the URI 'reg-name' part as an Internet host name, which is a
+   sequence of domain name labels separated by '.', as defined in
+   Section 3.5 of RFC 1034 and Section 2.1 of RFC 1123. Returns 1 if
+   successful, 0 if the first character is not valid for a host name,
+   and -1 in case of error. The result parameter host_name_r can be NULL
+   to use this function for merely checking the presence of a valid host
+   name. The result is allocated from the data stack.
+ */
+int uri_parse_host_name(struct uri_parser *parser,
        const char **host_name_r) ATTR_NULL(2);
 /* parse the URI 'host' syntax, which is either an IP address literal or
-   a registered (host) name. If dns_name is TRUE, this function expects
-   a host name, as defined in Section 3.5 of RFC 1034 and Section
-   2.1 of RFC 1123. Otherwise, a generic registered name syntax is allowed.
-   An IP address literal is always allowed. Returns 1 if successful, 0 if
-   the first character is not valid for a host name, and -1 in case of
-   error. The provided host struct is filled in with the parsed data, all
-   allocated from the parser pool. The host parameter can be NULL to use
-   this function for merely checking for valid 'host' syntax.
+   a an Internet host name, as defined in Section 3.5 of RFC 1034 and
+   Section 2.1 of RFC 1123. An IP address literal is always allowed.
+   Returns 1 if successful, 0 if the first character is not valid for a
+   host name, and -1 in case of error. The provided host struct is filled
+   in with the parsed data, all allocated from the parser pool. The host
+   parameter can be NULL to use this function for merely checking for
+   valid 'host' syntax.
  */
 int uri_parse_host(struct uri_parser *parser,
-       struct uri_host *host, bool dns_name) ATTR_NULL(2);
-
-/* parse the URI 'authority' syntax. If dns_name is TRUE, this function
-   expects a host name for the 'host' part, as defined in Section 3.5 of
-   RFC 1034 and Section 2.1 of RFC 1123. Otherwise, a generic registered
-   name syntax is allowed. Returns 1 if successful, 0 if the first
-   character is not valid for the 'authority' syntax and -1 in case of
-   error. The provided uri_authority struct is filled in with the parsed
+       struct uri_host *host) ATTR_NULL(2);
+
+/* parse the URI 'authority' syntax. Returns 1 if successful, 0 if the
+   first character is not valid for the 'authority' syntax and -1 in case
+   of error. The provided uri_authority struct is filled in with the parsed
    data, all allocated from the parser pool. The auth parameter can be
    NULL to use this function for merely checking for valid 'authority'
    syntax.
  */
 int uri_parse_authority(struct uri_parser *parser,
-       struct uri_authority *auth, bool dns_name) ATTR_NULL(2);
+       struct uri_authority *auth) ATTR_NULL(2);
 /* identical to uri_parse_authority(), except that this function parses
    '"//" authority', rather than 'authority'.
  */
 int uri_parse_slashslash_authority(struct uri_parser *parser,
-       struct uri_authority *auth, bool dns_name) ATTR_NULL(2);
+       struct uri_authority *auth) ATTR_NULL(2);
+/* identical to uri_parse_authority(), except that this function parses
+   the registered name ('reg-name' syntax) as an Internet host name, as
+   defined in Section 3.5 of RFC 1034 and Section 2.1 of RFC 1123.
+ */
+int uri_parse_host_authority(struct uri_parser *parser,
+       struct uri_authority *auth) ATTR_NULL(2);
+/* identical to uri_parse_slashslash_authority(), except that this
+   function parses the registered name ('reg-name' syntax) as an Internet
+   host name, as defined in Section 3.5 of RFC 1034 and Section 2.1 of
+   RFC 1123.
+ */
+int uri_parse_slashslash_host_authority(struct uri_parser *parser,
+       struct uri_authority *auth) ATTR_NULL(2);
 
 /* parse the URI 'segment' syntax. Returns 1 if successful, 0 if the first
    character is not valid for the 'segment' syntax and -1 in case of