Small refactoring, add edge cases

v0.10
Bertrand Paquet 13 years ago
parent fe31fe8bf0
commit f6f761596e

@ -1967,21 +1967,21 @@ http_parse_host_char(enum http_host_state s, const char ch) {
return s_http_host_dead; 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; enum http_host_state s;
int i; const char *p;
int start = u->field_data[UF_HOST].off; size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
int len = u->field_data[UF_HOST].len;
u->field_data[UF_HOST].len = 0; u->field_data[UF_HOST].len = 0;
i = 0;
s = found_at ? s_http_userinfo_start : s_http_host_start; s = found_at ? s_http_userinfo_start : s_http_host_start;
for(; i < len; i ++) { for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) {
char ch = buf[start + i]; enum http_host_state new_s = http_parse_host_char(s, *p);
enum http_host_state new_s = http_parse_host_char(s, ch);
if (new_s == s_http_host_dead) { if (new_s == s_http_host_dead) {
return 1; return 1;
@ -1990,21 +1990,21 @@ int http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
switch(new_s) { switch(new_s) {
case s_http_host: case s_http_host:
if (s != 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 ++; u->field_data[UF_HOST].len ++;
break; break;
case s_http_host_v6: case s_http_host_v6:
if (s != 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 ++; u->field_data[UF_HOST].len ++;
break; break;
case s_http_host_port: case s_http_host_port:
if (s != 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_data[UF_PORT].len = 0;
u->field_set |= (1 << UF_PORT); 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: case s_http_userinfo:
if (s != 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_data[UF_USERINFO].len = 0;
u->field_set |= (1 << UF_USERINFO); 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 */ /* Make sure we don't end somewhere unexpected */
switch (s) { switch (s) {
case s_http_host_start:
case s_http_host_v6_start: case s_http_host_v6_start:
case s_http_host_v6: case s_http_host_v6:
case s_http_host_port_start: case s_http_host_port_start:
case s_http_userinfo:
case s_http_userinfo_start:
return 1; return 1;
default: default:
break; break;
@ -2106,7 +2109,8 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
old_uf = uf; 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) { if (http_parse_host(buf, u, found_at) != 0) {
return 1; return 1;
} }

@ -2258,7 +2258,7 @@ const struct url_test url_tests[] =
,.rv=1 /* s_dead */ ,.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/" ,.url="http://a%20:b@host.com/"
,.is_connect=0 ,.is_connect=0
,.u= ,.u=
@ -2282,12 +2282,12 @@ const struct url_test url_tests[] =
,.rv=1 /* s_dead */ ,.rv=1 /* s_dead */
} }
, {.name="double : in URL" , {.name="proxy double : in URL"
,.url="http://hostname::443/" ,.url="http://hostname::443/"
,.rv=1 /* s_dead */ ,.rv=1 /* s_dead */
} }
, {.name="basic auth with double :" , {.name="proxy basic auth with double :"
,.url="http://a::b@host.com/" ,.url="http://a::b@host.com/"
,.is_connect=0 ,.is_connect=0
,.u= ,.u=
@ -2311,7 +2311,7 @@ const struct url_test url_tests[] =
,.rv=1 /* s_dead */ ,.rv=1 /* s_dead */
} }
, {.name="empty basic auth" , {.name="proxy empty basic auth"
,.url="http://@hostname/fo" ,.url="http://@hostname/fo"
,.u= ,.u=
{.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH) {.field_set= (1<<UF_SCHEMA) | (1<<UF_HOST) | (1<<UF_PATH)
@ -2328,22 +2328,22 @@ const struct url_test url_tests[] =
} }
,.rv=0 ,.rv=0
} }
, {.name="line feed in hostname" , {.name="proxy line feed in hostname"
,.url="http://host\name/fo" ,.url="http://host\name/fo"
,.rv=1 /* s_dead */ ,.rv=1 /* s_dead */
} }
, {.name="% in hostname" , {.name="proxy % in hostname"
,.url="http://host%name/fo" ,.url="http://host%name/fo"
,.rv=1 /* s_dead */ ,.rv=1 /* s_dead */
} }
, {.name="; in hostname" , {.name="proxy ; in hostname"
,.url="http://host;ame/fo" ,.url="http://host;ame/fo"
,.rv=1 /* s_dead */ ,.rv=1 /* s_dead */
} }
, {.name="basic auth with unreservedchars" , {.name="proxy basic auth with unreservedchars"
,.url="http://a!;-_!=+$@host.com/" ,.url="http://a!;-_!=+$@host.com/"
,.is_connect=0 ,.is_connect=0
,.u= ,.u=
@ -2362,7 +2362,17 @@ const struct url_test url_tests[] =
,.rv=0 ,.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" ,.url="http://host=ame/fo"
,.rv=1 /* s_dead */ ,.rv=1 /* s_dead */
} }

Loading…
Cancel
Save