Permalink
Browse files

Small refactoring, add edge cases

  • Loading branch information...
1 parent fe31fe8 commit f6f761596ed58525ef17db5a54f80247a00518b7 Bertrand Paquet committed Jul 8, 2012
Showing with 36 additions and 22 deletions.
  1. +17 −13 http_parser.c
  2. +19 −9 test.c
View
30 http_parser.c
@@ -1967,21 +1967,21 @@ http_parse_host_char(enum http_host_state s, const char ch) {
return s_http_host_dead;
}
-int http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
+#include <stdio.h>
+
+static int
+http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
enum http_host_state s;
- int i;
- int start = u->field_data[UF_HOST].off;
- int len = u->field_data[UF_HOST].len;
+ const char *p;
+ size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
u->field_data[UF_HOST].len = 0;
- i = 0;
s = found_at ? s_http_userinfo_start : s_http_host_start;
- for(; i < len; i ++) {
- char ch = buf[start + i];
- enum http_host_state new_s = http_parse_host_char(s, ch);
+ for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) {
+ enum http_host_state new_s = http_parse_host_char(s, *p);
if (new_s == s_http_host_dead) {
return 1;
@@ -1990,21 +1990,21 @@ int http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
switch(new_s) {
case s_http_host:
if (s != s_http_host) {
- u->field_data[UF_HOST].off = start + i;
+ u->field_data[UF_HOST].off = p - buf;
}
u->field_data[UF_HOST].len ++;
break;
case s_http_host_v6:
if (s != s_http_host_v6) {
- u->field_data[UF_HOST].off = start + i;
+ u->field_data[UF_HOST].off = p - buf;
}
u->field_data[UF_HOST].len ++;
break;
case s_http_host_port:
if (s != s_http_host_port) {
- u->field_data[UF_PORT].off = start + i;
+ u->field_data[UF_PORT].off = p - buf;
u->field_data[UF_PORT].len = 0;
u->field_set |= (1 << UF_PORT);
}
@@ -2013,7 +2013,7 @@ int http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
case s_http_userinfo:
if (s != s_http_userinfo) {
- u->field_data[UF_USERINFO].off = start + i;
+ u->field_data[UF_USERINFO].off = p - buf ;
u->field_data[UF_USERINFO].len = 0;
u->field_set |= (1 << UF_USERINFO);
}
@@ -2028,9 +2028,12 @@ int http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
/* Make sure we don't end somewhere unexpected */
switch (s) {
+ case s_http_host_start:
case s_http_host_v6_start:
case s_http_host_v6:
case s_http_host_port_start:
+ case s_http_userinfo:
+ case s_http_userinfo_start:
return 1;
default:
break;
@@ -2106,7 +2109,8 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
old_uf = uf;
}
- if ((u->field_set & (1 << UF_HOST)) != 0) {
+ // host must be present if there is a schema
+ if ((u->field_set & ((1 << UF_SCHEMA) | (1 << UF_HOST))) != 0) {
if (http_parse_host(buf, u, found_at) != 0) {
return 1;
}
View
28 test.c
@@ -2258,7 +2258,7 @@ const struct url_test url_tests[] =
,.rv=1 /* s_dead */
}
-, {.name="basic auth with space url encoded"
+, {.name="proxy basic auth with space url encoded"
,.url="http://a%20:b@host.com/"
,.is_connect=0
,.u=
@@ -2282,12 +2282,12 @@ const struct url_test url_tests[] =
,.rv=1 /* s_dead */
}
-, {.name="double : in URL"
+, {.name="proxy double : in URL"
,.url="http://hostname::443/"
,.rv=1 /* s_dead */
}
-, {.name="basic auth with double :"
+, {.name="proxy basic auth with double :"
,.url="http://a::b@host.com/"
,.is_connect=0
,.u=
@@ -2311,7 +2311,7 @@ const struct url_test url_tests[] =
,.rv=1 /* s_dead */
}
-, {.name="empty basic auth"
+, {.name="proxy empty basic auth"
,.url="http://@hostname/fo"
,.u=
{.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
@@ -2328,22 +2328,22 @@ const struct url_test url_tests[] =
}
,.rv=0
}
-, {.name="line feed in hostname"
+, {.name="proxy line feed in hostname"
,.url="http://host\name/fo"
,.rv=1 /* s_dead */
}
-, {.name="% in hostname"
+, {.name="proxy % in hostname"
,.url="http://host%name/fo"
,.rv=1 /* s_dead */
}
-, {.name="; in hostname"
+, {.name="proxy ; in hostname"
,.url="http://host;ame/fo"
,.rv=1 /* s_dead */
}
-, {.name="basic auth with unreservedchars"
+, {.name="proxy basic auth with unreservedchars"
,.url="http://a!;-_!=+$@host.com/"
,.is_connect=0
,.u=
@@ -2362,7 +2362,17 @@ const struct url_test url_tests[] =
,.rv=0
}
-, {.name="= in URL"
+, {.name="proxy only basic auth"
+ ,.url="http://@/fo"
+ ,.rv=1 /* s_dead */
+ }
+
+, {.name="proxy emtpy hostname"
+ ,.url="http:///fo"
+ ,.rv=1 /* s_dead */
+ }
+
+, {.name="proxy = in URL"
,.url="http://host=ame/fo"
,.rv=1 /* s_dead */
}

0 comments on commit f6f7615

Please sign in to comment.