فهرست منبع

Reqrite request parsing (Step 17/?)

bel2125 8 سال پیش
والد
کامیت
c0c5212e70
2فایلهای تغییر یافته به همراه23 افزوده شده و 11 حذف شده
  1. 18 8
      src/civetweb.c
  2. 5 3
      test/private.c

+ 18 - 8
src/civetweb.c

@@ -8760,13 +8760,20 @@ parse_http_request(char *buf, int len, struct mg_request_info *ri)
 	    NULL;
 	ri->num_headers = 0;
 
-	/* Ignore leading \r and \n */
-	while ((len > 0) && ((*buf == '\r') || (*buf == '\n'))) {
+	/* RFC says that all initial whitespaces should be ingored */
+	/* This included all leading \r and \n (isspace) */
+    /* See table: http://www.cplusplus.com/reference/cctype/ */
+	while ((len > 0) && isspace(*(unsigned char *)buf)) {
 		buf++;
 		len--;
 		init_skip++;
 	}
 
+	/* Control characters are not allowed, including zero */
+	if (iscntrl(*(unsigned char *)buf)) {
+		return -1;
+	}
+
 	/* Find end of HTTP header */
 	request_length = get_http_header_len(buf, len);
 	if (request_length <= 0) {
@@ -8774,10 +8781,6 @@ parse_http_request(char *buf, int len, struct mg_request_info *ri)
 	}
 	buf[request_length - 1] = '\0';
 
-	/* RFC says that all initial whitespaces should be ingored */
-	while ((*buf != '\0') && isspace(*(unsigned char *)buf)) {
-		buf++;
-	}
 	if ((*buf == 0) || (*buf == '\r') || (*buf == '\n')) {
 		return -1;
 	}
@@ -8839,13 +8842,20 @@ parse_http_response(char *buf, int len, struct mg_response_info *ri)
 	ri->http_version = ri->status_text = NULL;
 	ri->num_headers = ri->status_code = 0;
 
-	/* Ignore leading \r and \n */
-	while ((len > 0) && ((*buf == '\r') || (*buf == '\n'))) {
+	/* RFC says that all initial whitespaces should be ingored */
+	/* This included all leading \r and \n (isspace) */
+    /* See table: http://www.cplusplus.com/reference/cctype/ */
+	while ((len > 0) && isspace(*(unsigned char *)buf)) {
 		buf++;
 		len--;
 		init_skip++;
 	}
 
+	/* Control characters are not allowed, including zero */
+	if (iscntrl(*(unsigned char *)buf)) {
+		return -1;
+	}
+
 	/* Find end of HTTP header */
 	response_length = get_http_header_len(buf, len);
 	if (response_length <= 0) {

+ 5 - 3
test/private.c

@@ -107,6 +107,7 @@ START_TEST(test_parse_http_message)
 
 	ck_assert_int_eq(lenreq3, get_http_header_len(req3, lenreq3));
 	ck_assert_int_eq(lenreq3, parse_http_request(req3, lenreq3, &ri));
+	ck_assert_int_eq(-1, parse_http_response(req3, lenreq3, &respi));
 
 
 	/* Multiline header are obsolete, so return an error
@@ -118,9 +119,10 @@ START_TEST(test_parse_http_message)
 	ck_assert_int_eq(lenreq5, parse_http_request(req5, lenreq5, &ri));
 	ck_assert_str_eq("GET", ri.request_method);
 	ck_assert_str_eq("1.1", ri.http_version);
+	ck_assert_int_eq(-1, parse_http_response(req5, lenreq5, &respi));
 
 
-        ck_assert_int_eq(0, get_http_header_len(req6, lenreq6));
+	ck_assert_int_eq(0, get_http_header_len(req6, lenreq6));
 	ck_assert_int_eq(0, parse_http_request(req6, lenreq6, &ri));
 
 
@@ -129,8 +131,8 @@ START_TEST(test_parse_http_message)
 
 
 	ck_assert_int_eq(lenreq8, get_http_header_len(req8, lenreq8));
-	ck_assert_int_eq(-1, parse_http_request(req8, lenreq8, &ri));
-	ck_assert_int_eq(lenreq8, parse_http_response(req8, lenreq8, &respi));
+	ck_assert_int_eq(lenreq8, parse_http_request(req8, lenreq8, &ri));
+	ck_assert_int_eq(-1, parse_http_response(req8, lenreq8, &respi));
 
 
 	ck_assert_int_eq(lenreq9, get_http_header_len(req9, lenreq9));