dovecot-2.2: lib-http: Minor improvements.

dovecot at dovecot.org dovecot at dovecot.org
Thu Feb 21 10:23:24 EET 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/b0c4a5ce4933
changeset: 15868:b0c4a5ce4933
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Thu Feb 21 10:23:08 2013 +0200
description:
lib-http: Minor improvements.

diffstat:

 src/lib-http/http-client-peer.c     |   2 +-
 src/lib-http/http-response-parser.c |  30 ++++++++++++++++--------------
 src/lib-http/test-http-client.c     |  11 +++++++++++
 3 files changed, 28 insertions(+), 15 deletions(-)

diffs (144 lines):

diff -r 3b905464ea09 -r b0c4a5ce4933 src/lib-http/http-client-peer.c
--- a/src/lib-http/http-client-peer.c	Thu Feb 21 10:21:31 2013 +0200
+++ b/src/lib-http/http-client-peer.c	Thu Feb 21 10:23:08 2013 +0200
@@ -361,7 +361,7 @@
     struct http_client_connection *const *conn_idx;
     unsigned int idle = 0;
 
-	/* find the least busy connection */
+	/* find idle connections */
     array_foreach(&peer->conns, conn_idx) {
         if (http_client_connection_is_idle(*conn_idx))
 			idle++;
diff -r 3b905464ea09 -r b0c4a5ce4933 src/lib-http/http-response-parser.c
--- a/src/lib-http/http-response-parser.c	Thu Feb 21 10:21:31 2013 +0200
+++ b/src/lib-http/http-response-parser.c	Thu Feb 21 10:23:08 2013 +0200
@@ -98,7 +98,8 @@
 	if (str_len(parser->strbuf) + (parser->cur-first) > 8)
 		return -1;
 
-	str_append_n(parser->strbuf, first, parser->cur-first);
+	if ((parser->cur - first) > 0)
+		str_append_n(parser->strbuf, first, parser->cur-first);
 	if (parser->cur == parser->end)
 		return 0;
 
@@ -116,30 +117,35 @@
 	if (!i_isdigit(*p))
 		return -1;
 	parser->response->version_minor = *p - '0';
+	str_truncate(parser->strbuf, 0);
 	return 1;
 }
 
 static int http_response_parse_status(struct http_response_parser *parser)
 {
 	const unsigned char *first = parser->cur;
-	const char *p;
+	const unsigned char *p;
 
 	/* status-code   = 3DIGIT
 	 */
-	while (parser->cur < parser->end && i_isdigit(*parser->cur))
+	while (parser->cur < parser->end && i_isdigit(*parser->cur)) {
 		parser->cur++;
+		if ((parser->cur - first) > 3)
+			return -1;
+	}
 
-	if (str_len(parser->strbuf) + (parser->cur-first) > 3)
+	if (str_len(parser->strbuf) + (parser->cur - first) > 3)
 		return -1;
-
-	str_append_n(parser->strbuf, first, parser->cur-first);
+	if ((parser->cur - first) > 0)
+		str_append_n(parser->strbuf, first, parser->cur-first);
 	if (parser->cur == parser->end)
 		return 0;
 	if (str_len(parser->strbuf) != 3)
 		return -1;
-	p = str_c(parser->strbuf);
+	p = str_data(parser->strbuf);
 	parser->response->status =
 		(p[0] - '0')*100 + (p[1] - '0')*10 + (p[2] - '0');
+	str_truncate(parser->strbuf, 0);
 	return 1;
 }
 
@@ -152,12 +158,13 @@
 	while (parser->cur < parser->end && http_char_is_text(*parser->cur))
 		parser->cur++;
 
-	str_append_n(parser->strbuf, first, parser->cur-first);
-
+	if ((parser->cur - first) > 0)
+		str_append_n(parser->strbuf, first, parser->cur-first);
 	if (parser->cur == parser->end)
 		return 0;
 	parser->response->reason =
 		p_strdup(parser->response_pool, str_c(parser->strbuf));
+	str_truncate(parser->strbuf, 0);
 	return 1;
 }
 
@@ -189,7 +196,6 @@
 					parser->error = "Invalid HTTP version in response";
 				return ret;
 			}
-			str_truncate(parser->strbuf, 0);
 			parser->state = HTTP_RESPONSE_PARSE_STATE_SP1;
 			if (parser->cur == parser->end)
 				return 0;
@@ -202,7 +208,6 @@
 				return -1;
 			}
 			parser->cur++;
-			str_truncate(parser->strbuf, 0);
 			parser->state = HTTP_RESPONSE_PARSE_STATE_STATUS;
 			if (parser->cur >= parser->end)
 				return 0;
@@ -213,7 +218,6 @@
 					parser->error = "Invalid HTTP status code in response";
 				return ret;
 			}
-			str_truncate(parser->strbuf, 0);
 			parser->state = HTTP_RESPONSE_PARSE_STATE_SP2;
 			if (parser->cur == parser->end)
 				return 0;
@@ -226,7 +230,6 @@
 				return -1;
 			}
 			parser->cur++;
-			str_truncate(parser->strbuf, 0);
 			parser->state = HTTP_RESPONSE_PARSE_STATE_REASON;
 			if (parser->cur >= parser->end)
 				return 0;
@@ -234,7 +237,6 @@
 		case HTTP_RESPONSE_PARSE_STATE_REASON:
 			if ((ret=http_response_parse_reason(parser)) <= 0)
 				return ret;
-			str_truncate(parser->strbuf, 0);
 			parser->state = HTTP_RESPONSE_PARSE_STATE_CR;
 			if (parser->cur == parser->end)
 				return 0;
diff -r 3b905464ea09 -r b0c4a5ce4933 src/lib-http/test-http-client.c
--- a/src/lib-http/test-http-client.c	Thu Feb 21 10:21:31 2013 +0200
+++ b/src/lib-http/test-http-client.c	Thu Feb 21 10:23:08 2013 +0200
@@ -213,6 +213,17 @@
 
 	test_req = i_new(struct http_test_request, 1);
 	http_req = http_client_request(http_client,
+		"POST", "posttestserver.com", "/post.php",
+		got_request_response, test_req);
+	post_payload = i_stream_create_from_data
+		((unsigned char *)test_query1, strlen(test_query1));
+	http_client_request_set_payload(http_req, post_payload, TRUE);
+	i_stream_unref(&post_payload);
+	http_client_request_set_ssl(http_req, TRUE);
+	http_client_request_submit(http_req);
+
+	test_req = i_new(struct http_test_request, 1);
+	http_req = http_client_request(http_client,
 		"GET", "wiki2.dovecot.org", "/Pigeonhole",
 		got_request_response, test_req);
 	http_client_request_submit(http_req);


More information about the dovecot-cvs mailing list