dovecot-2.2: uri-util: Added the possibility of only checking th...

dovecot at dovecot.org dovecot at dovecot.org
Wed Apr 29 09:22:59 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/49bcc3954799
changeset: 18497:49bcc3954799
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Sat Apr 25 11:42:06 2015 +0200
description:
uri-util: Added the possibility of only checking the URI without parsing any of the data.

diffstat:

 src/lib/uri-util.c |  67 ++++++++++++++++++++++++++++++++++++-----------------
 src/lib/uri-util.h |  28 ++++++++++++++--------
 2 files changed, 63 insertions(+), 32 deletions(-)

diffs (210 lines):

diff -r 5ae8cd8e42f7 -r 49bcc3954799 src/lib/uri-util.c
--- a/src/lib/uri-util.c	Sat Apr 25 11:42:06 2015 +0200
+++ b/src/lib/uri-util.c	Sat Apr 25 11:42:06 2015 +0200
@@ -234,7 +234,8 @@
 	if (*p != ':')
 		return -1;
 	
-	*scheme_r = t_strdup_until(*uri_p, p);
+	if (scheme_r != NULL)
+		*scheme_r = t_strdup_until(*uri_p, p);
 	*uri_p = p + 1;
 	return 0;
 }
@@ -256,7 +257,7 @@
 
 static int
 uri_parse_dec_octet(struct uri_parser *parser, string_t *literal,
-		    uint8_t *octet_r)
+		    uint8_t *octet_r) ATTR_NULL(2)
 {
 	unsigned int octet = 0;
 	int count = 0;
@@ -291,7 +292,7 @@
 
 static int
 uri_parse_ipv4address(struct uri_parser *parser, string_t *literal,
-		      struct in_addr *ip4_r)
+		      struct in_addr *ip4_r) ATTR_NULL(2,3)
 {
 	uint8_t octet;
 	uint32_t ip = 0;
@@ -325,7 +326,9 @@
 	return 1;
 }
 
-static int uri_parse_reg_name(struct uri_parser *parser, string_t *reg_name)
+static int
+uri_parse_reg_name(struct uri_parser *parser,
+	string_t *reg_name) ATTR_NULL(2)
 {
 	/* RFC 3986:
 	 *
@@ -362,10 +365,11 @@
 #ifdef HAVE_IPV6
 static int
 uri_parse_ip_literal(struct uri_parser *parser, string_t *literal,
-		     struct in6_addr *ip6_r)
+		     struct in6_addr *ip6_r) ATTR_NULL(2,3)
 {
 	const unsigned char *p;
 	const char *address;
+	struct in6_addr ip6;
 	int ret;
 
 	/* IP-literal    = "[" ( IPv6address / IPvFuture  ) "]"
@@ -400,16 +404,20 @@
 			"Future IP host address '%s' not supported", address);
 		return -1;
 	}
-	if ((ret = inet_pton(AF_INET6, address, ip6_r)) <= 0) {
+	if ((ret = inet_pton(AF_INET6, address, &ip6)) <= 0) {
 		parser->error = t_strdup_printf(
 			"Invalid IPv6 host address '%s'", address);
 		return -1;
 	}
+	if (ip6_r != NULL)
+		*ip6_r = ip6;
 	return 1;
 }
 #endif
 
-static int uri_parse_host(struct uri_parser *parser, struct uri_authority *auth)
+static int 
+uri_parse_host(struct uri_parser *parser,
+	struct uri_authority *auth) ATTR_NULL(2)
 {
 	const unsigned char *preserve;
 	struct in_addr ip4;
@@ -470,7 +478,9 @@
 	return 0;
 }
 
-static int uri_parse_port(struct uri_parser *parser, struct uri_authority *auth)
+static int
+uri_parse_port(struct uri_parser *parser,
+	struct uri_authority *auth) ATTR_NULL(2)
 {
 	unsigned long port = 0;
 	int count = 0;
@@ -612,7 +622,11 @@
 	int relative = 1;
 	int ret;
 
-	t_array_init(&segments, 16);
+	count = 0;
+	if (path_r != NULL)
+		t_array_init(&segments, 16);
+	else
+		memset(&segments, 0, sizeof(segments));
 
 	/* check for a leading '/' and indicate absolute path
 	   when it is present
@@ -636,9 +650,12 @@
 						segment = NULL;
 
 						/* ... pop last segment (if any) */
-						count = array_count(&segments);
 						if (count > 0) {
-							array_delete(&segments, count-1, 1);
+							if (path_r != NULL) {
+								i_assert(count == array_count(&segments));
+								array_delete(&segments, count-1, 1);
+							}
+							count--;
 						} else if ( relative > 0 ) {
 							relative++;
 						}
@@ -652,8 +669,11 @@
 			segment = "";
 		}
 
-		if (segment != NULL)
-			array_append(&segments, &segment, 1);
+		if (segment != NULL) {
+			if (path_r != NULL)
+				array_append(&segments, &segment, 1);
+			count++;
+		}
 
 		if (parser->cur >= parser->end || *parser->cur != '/')
 			break;
@@ -664,22 +684,25 @@
 			return -1;
 	}
 
-	*path_r = NULL;
-	*relative_r = relative;
+	if (relative_r != NULL)
+		*relative_r = relative;
+	if (path_r != NULL)
+		*path_r = NULL;
 
 	if (parser->cur == pbegin) {
 		/* path part of URI is empty */
 		return 0;
 	}
 
-	/* special treatment for a trailing '..' or '.' */
-	if (segment == NULL) {
-		segment = "";
-		array_append(&segments, &segment, 1);
+	if (path_r != NULL) {
+		/* special treatment for a trailing '..' or '.' */
+		if (segment == NULL) {
+			segment = "";
+			array_append(&segments, &segment, 1);
+		}
+		array_append_zero(&segments);
+		*path_r = array_get(&segments, &count);
 	}
-	array_append_zero(&segments);
-	*path_r = array_get(&segments, &count);
-
 	if (parser->cur < parser->end &&
 		*parser->cur != '?' && *parser->cur != '#') {
 		parser->error = "Path component contains invalid character";
diff -r 5ae8cd8e42f7 -r 49bcc3954799 src/lib/uri-util.h
--- a/src/lib/uri-util.h	Sat Apr 25 11:42:06 2015 +0200
+++ b/src/lib/uri-util.h	Sat Apr 25 11:42:06 2015 +0200
@@ -33,22 +33,30 @@
 bool uri_data_decode(struct uri_parser *parser, const char *data,
 		     const char *until, const char **decoded_r) ATTR_NULL(3);
 
-int uri_cut_scheme(const char **uri_p, const char **scheme_r);
-int uri_parse_scheme(struct uri_parser *parser, const char **scheme_r);
+int uri_cut_scheme(const char **uri_p, const char **scheme_r)
+	ATTR_NULL(2);
+int uri_parse_scheme(struct uri_parser *parser, const char **scheme_r)
+	ATTR_NULL(2);
 int uri_parse_authority(struct uri_parser *parser,
-	struct uri_authority *auth);
+	struct uri_authority *auth)	 ATTR_NULL(2);
 int uri_parse_slashslash_authority(struct uri_parser *parser,
-	struct uri_authority *auth);
+	struct uri_authority *auth) ATTR_NULL(2);
 
-int uri_parse_path_segment(struct uri_parser *parser, const char **segment_r);
+int uri_parse_path_segment(struct uri_parser *parser,
+	const char **segment_r) ATTR_NULL(2);
 int uri_parse_path(struct uri_parser *parser, int *relative_r,
-		   const char *const **path_r);
+		   const char *const **path_r) ATTR_NULL(2,3);
 
-int uri_parse_query(struct uri_parser *parser, const char **query_r);
-int uri_parse_fragment(struct uri_parser *parser, const char **fragment_r);
+int uri_parse_query(struct uri_parser *parser,
+	const char **query_r) ATTR_NULL(2);
+int uri_parse_fragment(struct uri_parser *parser,
+	const char **fragment_r) ATTR_NULL(2);
 
-void uri_parser_init(struct uri_parser *parser, pool_t pool, const char *data);
-string_t *uri_parser_get_tmpbuf(struct uri_parser *parser, size_t size);
+void uri_parser_init(struct uri_parser *parser, pool_t pool,
+	const char *data);
+string_t *uri_parser_get_tmpbuf(struct uri_parser *parser,
+	size_t size);
+
 
 /*
  * Generic URI construction


More information about the dovecot-cvs mailing list