Klaus Demo bjoern / 195b7ae
Don't use a buffer in unquote_url Jonas Haag 9 years ago
3 changed file(s) with 25 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
44 (c >= 'A' && c <= 'F') ? (c - 'A' + 10) : NOHEX)
55 #define NOHEX -1
66
7 size_t unquote_url(const char* url, const size_t len, char* buf) {
8 size_t n = 0;
9 for(const char* end = url+len; url != end; ++buf, ++url, ++n) {
10 if(*url != '%') {
11 /* Not a hex sequeunce */
12 *buf = *url;
13 continue;
7 size_t unquote_url_inplace(char* url, size_t len) {
8 for(char *p=url, *end=url+len; url != end; ++url, ++p) {
9 if(*url == '%') {
10 if(url >= end-2) {
11 /* Less than two characters left after the '%' */
12 return 0;
13 }
14 char a = UNHEX(url[1]);
15 char b = UNHEX(url[2]);
16 if(a == NOHEX || b == NOHEX) return 0;
17 *p = a*16 + b;
18 url += 2;
19 len -= 2;
20 } else {
21 *p = *url;
1422 }
15 if(url >= end-2) {
16 /* Less than two characters left after the '%' */
17 return 0;
18 }
19 char a = UNHEX(url[1]);
20 char b = UNHEX(url[2]);
21 if(a == NOHEX || b == NOHEX) return 0;
22 *buf = a*16 + b;
23 url += 2;
24 assert(url <= end);
2523 }
26 return n;
24 return len;
2725 }
2826
2927 /* Case insensitive string comparison */
77 #include <string.h>
88
99 void _initialize_static_strings();
10 size_t unquote_url(const char* url, const size_t len, char* buf);
10 size_t unquote_url_inplace(char* url, size_t len);
1111 bool string_iequal(const char* a, const size_t len, const char* b);
1212
1313 typedef enum {
105105 }
106106
107107 static int on_path(http_parser* parser,
108 const char* path_start,
109 const size_t path_len) {
110 char buf[path_len];
111 size_t len = unquote_url(path_start, path_len, buf);
112 if(len) {
113 _set_header_free_value(_PATH_INFO, PyString_FromStringAndSize(buf, len));
114 return 0;
115 } else {
108 char* path_start,
109 size_t path_len) {
110 if(!(path_len = unquote_url_inplace(path_start, path_len)))
116111 return 1;
117 }
112 _set_header_free_value(
113 _PATH_INFO,
114 PyString_FromStringAndSize(path_start, path_len)
115 );
116 return 0;
118117 }
119118
120119 static int on_query_string(http_parser* parser,