| 1 |
|
| |
| 2 |
|
| #include <parser2.h> |
| 3 |
|
| |
| 4 |
|
| #define NEED_newRV_noinc |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| |
| 7 |
|
| /* XXX based on HTTP-Parser-XS-0.13 */ |
| 8 |
|
| |
| 9 |
|
| |
| 10 |
|
| #if __GNUC__ >= 3 |
| 11 |
|
| # define likely(x) __builtin_expect(!!(x), 1) |
| 12 |
|
| # define unlikely(x) __builtin_expect(!!(x), 0) |
| 13 |
|
| #else |
| 14 |
|
| # define likely(x) (x) |
| 15 |
|
| # define unlikely(x) (x) |
| 16 |
|
| #endif |
| 17 |
|
| |
| 18 |
|
| #define CHECK_EOF() \ |
| 19 |
|
| if (buf == buf_end) { \ |
| 20 |
|
| *ret = -2; \ |
| 21 |
|
| return NULL; \ |
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
|
| #define EXPECT_CHAR(ch) \ |
| 25 |
|
| CHECK_EOF(); \ |
| 26 |
|
| if (*buf++ != ch) { \ |
| 27 |
|
| *ret = -1; \ |
| 28 |
|
| return NULL; \ |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
|
| #define ADVANCE_TOKEN(tok, toklen) do { \ |
| 32 |
|
| const char* tok_start = buf; \ |
| 33 |
|
| for (; ; ++buf) { \ |
| 34 |
|
| CHECK_EOF(); \ |
| 35 |
|
| if (*buf == ' ') { \ |
| 36 |
|
| break; \ |
| 37 |
|
| } else if (*buf == '\015' || *buf == '\012') { \ |
| 38 |
|
| *ret = -1; \ |
| 39 |
|
| return NULL; \ |
| 40 |
|
| } \ |
| 41 |
|
| } \ |
| 42 |
|
| tok = tok_start; \ |
| 43 |
|
| toklen = buf - tok_start; \ |
| 44 |
|
| } while (0) |
| 45 |
|
| |
| 46 |
40 |
| static const char* get_token_to_eol(const char* buf, const char* buf_end, |
| 47 |
|
| const char** token, size_t* token_len, |
| 48 |
|
| int* ret) |
| 49 |
|
| { |
| 50 |
40 |
| const char* token_start = buf; |
| 51 |
|
| |
| 52 |
|
| while (1) { |
| 53 |
42 |
▶ | if (likely(buf_end - buf >= 16)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(buf_end - buf >= 16)) { |
28 |
14 |
42 |
|
| 54 |
|
| unsigned i; |
| 55 |
261 |
▶ | for (i = 0; i < 16; i++, ++buf) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; i++, ++buf) { |
259 |
2 |
261 |
|
| 56 |
259 |
▶ | if (unlikely((unsigned char)*buf <= '\015') |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely((unsigned char)*buf <= '\015') |
26 |
233 |
259 |
|
| 57 |
26 |
▶ | && (*buf == '\015' || *buf == '\012')) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (*buf == '\015' || *buf == '\012')) { |
0 |
26 |
26 |
| && (*buf == '\015' || *buf == '\012')) { |
0 |
0 |
0 |
|
| 58 |
26 |
| goto EOL_FOUND; |
| 59 |
|
| } |
| 60 |
|
| } |
| 61 |
|
| } else { |
| 62 |
101 |
| for (; ; ++buf) { |
| 63 |
115 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
2 |
113 |
115 |
|
| 64 |
113 |
▶ | if (unlikely((unsigned char)*buf <= '\015') |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely((unsigned char)*buf <= '\015') |
12 |
101 |
113 |
|
| 65 |
12 |
▶ | && (*buf == '\015' || *buf == '\012')) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (*buf == '\015' || *buf == '\012')) { |
0 |
12 |
12 |
| && (*buf == '\015' || *buf == '\012')) { |
0 |
0 |
0 |
|
| 66 |
12 |
| goto EOL_FOUND; |
| 67 |
|
| } |
| 68 |
|
| } |
| 69 |
|
| } |
| 70 |
|
| } |
| 71 |
38 |
| EOL_FOUND: |
| 72 |
38 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
38 |
0 |
38 |
|
| 73 |
38 |
| ++buf; |
| 74 |
38 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
38 |
38 |
| EXPECT_CHAR('\012'); |
0 |
38 |
38 |
|
| 75 |
38 |
| *token_len = buf - 2 - token_start; |
| 76 |
|
| } else { /* should be: *buf == '\012' */ |
| 77 |
0 |
| *token_len = buf - token_start; |
| 78 |
0 |
| ++buf; |
| 79 |
|
| } |
| 80 |
38 |
| *token = token_start; |
| 81 |
|
| |
| 82 |
38 |
| return buf; |
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
0 |
| static const char* is_complete(const char* buf, const char* buf_end, |
| 86 |
|
| size_t last_len, int* ret) |
| 87 |
|
| { |
| 88 |
0 |
| int ret_cnt = 0; |
| 89 |
0 |
▶ | buf = last_len < 3 ? buf : buf + last_len - 3; |
Branch: 1 branch
| Branch | True | False | Total |
| buf = last_len < 3 ? buf : buf + last_len - 3; |
0 |
0 |
0 |
|
| 90 |
|
| |
| 91 |
|
| while (1) { |
| 92 |
0 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
0 |
0 |
|
| 93 |
0 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
0 |
0 |
0 |
|
| 94 |
0 |
| ++buf; |
| 95 |
0 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
0 |
0 |
|
| 96 |
0 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
0 |
0 |
| EXPECT_CHAR('\012'); |
0 |
0 |
0 |
|
| 97 |
0 |
| ++ret_cnt; |
| 98 |
0 |
▶ | } else if (*buf == '\012') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf == '\012') { |
0 |
0 |
0 |
|
| 99 |
0 |
| ++buf; |
| 100 |
0 |
| ++ret_cnt; |
| 101 |
|
| } else { |
| 102 |
0 |
| ++buf; |
| 103 |
0 |
| ret_cnt = 0; |
| 104 |
|
| } |
| 105 |
0 |
▶ | if (ret_cnt == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret_cnt == 2) { |
0 |
0 |
0 |
|
| 106 |
0 |
| return buf; |
| 107 |
|
| } |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| *ret = -2; |
| 111 |
|
| return NULL; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
|
| /* *_buf is always within [buf, buf_end) upon success */ |
| 115 |
27 |
| static const char* parse_int(const char* buf, const char* buf_end, int* value, |
| 116 |
|
| int* ret) |
| 117 |
|
| { |
| 118 |
|
| int v; |
| 119 |
27 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
27 |
27 |
|
| 120 |
27 |
▶ | if (! ('0' <= *buf && *buf <= '9')) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (! ('0' <= *buf && *buf <= '9')) { |
27 |
0 |
27 |
| if (! ('0' <= *buf && *buf <= '9')) { |
0 |
27 |
27 |
|
| 121 |
0 |
| *ret = -1; |
| 122 |
0 |
| return NULL; |
| 123 |
|
| } |
| 124 |
27 |
| v = 0; |
| 125 |
45 |
| for (; ; ++buf) { |
| 126 |
72 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
72 |
72 |
|
| 127 |
72 |
▶ | if ('0' <= *buf && *buf <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| if ('0' <= *buf && *buf <= '9') { |
45 |
27 |
72 |
| if ('0' <= *buf && *buf <= '9') { |
45 |
0 |
45 |
|
| 128 |
45 |
| v = v * 10 + *buf - '0'; |
| 129 |
|
| } else { |
| 130 |
|
| break; |
| 131 |
|
| } |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
27 |
| *value = v; |
| 135 |
27 |
| return buf; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| /* returned pointer is always within [buf, buf_end), or null */ |
| 139 |
19 |
| static const char* parse_http_version(const char* buf, const char* buf_end, |
| 140 |
|
| int* minor_version, int* ret) |
| 141 |
|
| { |
| 142 |
19 |
▶ | EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
Branch: 8 branches
| Branch | True | False | Total |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
19 |
19 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
1 |
18 |
19 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
18 |
18 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
18 |
18 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
18 |
18 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
18 |
18 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
18 |
18 |
| EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P'); |
0 |
18 |
18 |
|
| 143 |
18 |
▶ | EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
Branch: 6 branches
| Branch | True | False | Total |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
18 |
18 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
18 |
18 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
18 |
18 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
18 |
18 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
18 |
18 |
| EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.'); |
0 |
18 |
18 |
|
| 144 |
18 |
| return parse_int(buf, buf_end, minor_version, ret); |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
18 |
| static const char* parse_headers(const char* buf, const char* buf_end, |
| 148 |
|
| struct phr_header* headers, |
| 149 |
|
| size_t* num_headers, size_t max_headers, |
| 150 |
|
| int* ret) |
| 151 |
|
| { |
| 152 |
29 |
| for (; ; ++*num_headers) { |
| 153 |
47 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
47 |
47 |
|
| 154 |
47 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
16 |
31 |
47 |
|
| 155 |
16 |
| ++buf; |
| 156 |
16 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
16 |
16 |
| EXPECT_CHAR('\012'); |
0 |
16 |
16 |
|
| 157 |
16 |
| break; |
| 158 |
31 |
▶ | } else if (*buf == '\012') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf == '\012') { |
0 |
31 |
31 |
|
| 159 |
0 |
| ++buf; |
| 160 |
0 |
| break; |
| 161 |
|
| } |
| 162 |
31 |
▶ | if (*num_headers == max_headers) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*num_headers == max_headers) { |
0 |
31 |
31 |
|
| 163 |
0 |
| *ret = -1; |
| 164 |
0 |
| return NULL; |
| 165 |
|
| } |
| 166 |
31 |
▶ | if (*num_headers == 0 || ! (*buf == ' ' || *buf == '\t')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (*num_headers == 0 || ! (*buf == ' ' || *buf == '\t')) { |
13 |
18 |
31 |
| if (*num_headers == 0 || ! (*buf == ' ' || *buf == '\t')) { |
13 |
0 |
13 |
| if (*num_headers == 0 || ! (*buf == ' ' || *buf == '\t')) { |
13 |
0 |
13 |
|
| 167 |
|
| /* parsing name, but do not discard SP before colon, see |
| 168 |
|
| * http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */ |
| 169 |
31 |
| headers[*num_headers].name = buf; |
| 170 |
2268 |
| for (; ; ++buf) { |
| 171 |
2299 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
2299 |
2299 |
|
| 172 |
2299 |
▶ | if (*buf == ':') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == ':') { |
31 |
2268 |
2299 |
|
| 173 |
31 |
| break; |
| 174 |
2268 |
▶ | } else if (*buf < ' ') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf < ' ') { |
0 |
2268 |
2268 |
|
| 175 |
0 |
| *ret = -1; |
| 176 |
0 |
| return NULL; |
| 177 |
|
| } |
| 178 |
|
| } |
| 179 |
31 |
| headers[*num_headers].name_len = buf - headers[*num_headers].name; |
| 180 |
31 |
| ++buf; |
| 181 |
31 |
| for (; ; ++buf) { |
| 182 |
62 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
62 |
62 |
|
| 183 |
62 |
▶ | if (! (*buf == ' ' || *buf == '\t')) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (! (*buf == ' ' || *buf == '\t')) { |
31 |
31 |
62 |
| if (! (*buf == ' ' || *buf == '\t')) { |
31 |
0 |
31 |
|
| 184 |
31 |
| break; |
| 185 |
|
| } |
| 186 |
|
| } |
| 187 |
|
| } else { |
| 188 |
0 |
| headers[*num_headers].name = NULL; |
| 189 |
0 |
| headers[*num_headers].name_len = 0; |
| 190 |
|
| } |
| 191 |
31 |
▶ | if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value, |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value, |
2 |
29 |
31 |
|
| 192 |
31 |
| &headers[*num_headers].value_len, ret)) |
| 193 |
|
| == NULL) { |
| 194 |
2 |
| return NULL; |
| 195 |
|
| } |
| 196 |
|
| } |
| 197 |
16 |
| return buf; |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
10 |
| const char* parse_request(const char* buf, const char* buf_end, |
| 201 |
|
| const char** method, size_t* method_len, |
| 202 |
|
| const char** path, size_t* path_len, |
| 203 |
|
| int* minor_version, struct phr_header* headers, |
| 204 |
|
| size_t* num_headers, size_t max_headers, int* ret) |
| 205 |
|
| { |
| 206 |
|
| /* skip first empty line (some clients add CRLF after POST content) */ |
| 207 |
10 |
▶ | CHECK_EOF(); |
Branch: 1 branch
| Branch | True | False | Total |
| CHECK_EOF(); |
0 |
10 |
10 |
|
| 208 |
10 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
0 |
10 |
10 |
|
| 209 |
0 |
| ++buf; |
| 210 |
0 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
0 |
0 |
| EXPECT_CHAR('\012'); |
0 |
0 |
0 |
|
| 211 |
10 |
▶ | } else if (*buf == '\012') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf == '\012') { |
0 |
10 |
10 |
|
| 212 |
0 |
| ++buf; |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
|
| /* parse request line */ |
| 216 |
56 |
▶ | ADVANCE_TOKEN(*method, *method_len); |
Branch: 4 branches
| Branch | True | False | Total |
| ADVANCE_TOKEN(*method, *method_len); |
0 |
56 |
56 |
| ADVANCE_TOKEN(*method, *method_len); |
9 |
47 |
56 |
| ADVANCE_TOKEN(*method, *method_len); |
46 |
1 |
47 |
| ADVANCE_TOKEN(*method, *method_len); |
0 |
46 |
46 |
|
| 217 |
9 |
| ++buf; |
| 218 |
23 |
▶ | ADVANCE_TOKEN(*path, *path_len); |
Branch: 4 branches
| Branch | True | False | Total |
| ADVANCE_TOKEN(*path, *path_len); |
0 |
23 |
23 |
| ADVANCE_TOKEN(*path, *path_len); |
9 |
14 |
23 |
| ADVANCE_TOKEN(*path, *path_len); |
14 |
0 |
14 |
| ADVANCE_TOKEN(*path, *path_len); |
0 |
14 |
14 |
|
| 219 |
9 |
| ++buf; |
| 220 |
9 |
▶ | if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
0 |
9 |
9 |
|
| 221 |
0 |
| return NULL; |
| 222 |
|
| } |
| 223 |
9 |
▶ | if (*buf == '\015') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf == '\015') { |
9 |
0 |
9 |
|
| 224 |
9 |
| ++buf; |
| 225 |
9 |
▶ | EXPECT_CHAR('\012'); |
Branch: 2 branches
| Branch | True | False | Total |
| EXPECT_CHAR('\012'); |
0 |
9 |
9 |
| EXPECT_CHAR('\012'); |
0 |
9 |
9 |
|
| 226 |
0 |
▶ | } else if (*buf == '\012') { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*buf == '\012') { |
0 |
0 |
0 |
|
| 227 |
0 |
| ++buf; |
| 228 |
|
| } else { |
| 229 |
0 |
| *ret = -1; |
| 230 |
0 |
| return NULL; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
9 |
| return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
10 |
| int phr_parse_request(const char* buf_start, size_t len, const char** method, |
| 237 |
|
| size_t* method_len, const char** path, size_t* path_len, |
| 238 |
|
| int* minor_version, struct phr_header* headers, |
| 239 |
|
| size_t* num_headers, size_t last_len) |
| 240 |
|
| { |
| 241 |
10 |
| const char * buf = buf_start, * buf_end = buf_start + len; |
| 242 |
10 |
| size_t max_headers = *num_headers; |
| 243 |
|
| int r; |
| 244 |
|
| |
| 245 |
10 |
| *method = NULL; |
| 246 |
10 |
| *method_len = 0; |
| 247 |
10 |
| *path = NULL; |
| 248 |
10 |
| *path_len = 0; |
| 249 |
10 |
| *minor_version = -1; |
| 250 |
10 |
| *num_headers = 0; |
| 251 |
|
| |
| 252 |
|
| /* if last_len != 0, check if the request is complete (a fast countermeasure |
| 253 |
|
| againt slowloris */ |
| 254 |
10 |
▶ | if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
0 |
10 |
10 |
| if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
0 |
0 |
0 |
|
| 255 |
0 |
| return r; |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
10 |
▶ | if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len, |
2 |
8 |
10 |
|
| 259 |
|
| minor_version, headers, num_headers, max_headers, |
| 260 |
|
| &r)) |
| 261 |
|
| == NULL) { |
| 262 |
2 |
| return r; |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
8 |
| return buf - buf_start; |
| 266 |
|
| } |
| 267 |
|
| |
| 268 |
10 |
| static const char* parse_response(const char* buf, const char* buf_end, |
| 269 |
|
| int* minor_version, int* status, |
| 270 |
|
| const char** msg, size_t* msg_len, |
| 271 |
|
| struct phr_header* headers, |
| 272 |
|
| size_t* num_headers, size_t max_headers, |
| 273 |
|
| int* ret) |
| 274 |
|
| { |
| 275 |
|
| /* parse "HTTP/1.x" */ |
| 276 |
10 |
▶ | if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) { |
1 |
9 |
10 |
|
| 277 |
1 |
| return NULL; |
| 278 |
|
| } |
| 279 |
|
| /* skip space */ |
| 280 |
9 |
▶ | if (*buf++ != ' ') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf++ != ' ') { |
0 |
9 |
9 |
|
| 281 |
0 |
| *ret = -1; |
| 282 |
0 |
| return NULL; |
| 283 |
|
| } |
| 284 |
|
| /* parse status code */ |
| 285 |
9 |
▶ | if ((buf = parse_int(buf, buf_end, status, ret)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_int(buf, buf_end, status, ret)) == NULL) { |
0 |
9 |
9 |
|
| 286 |
0 |
| return NULL; |
| 287 |
|
| } |
| 288 |
|
| /* skip space */ |
| 289 |
9 |
▶ | if (*buf++ != ' ') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*buf++ != ' ') { |
0 |
9 |
9 |
|
| 290 |
0 |
| *ret = -1; |
| 291 |
0 |
| return NULL; |
| 292 |
|
| } |
| 293 |
|
| /* get message */ |
| 294 |
9 |
▶ | if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) { |
0 |
9 |
9 |
|
| 295 |
0 |
| return NULL; |
| 296 |
|
| } |
| 297 |
|
| |
| 298 |
9 |
| return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret); |
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
10 |
| int phr_parse_response(const char* buf_start, size_t len, int* minor_version, |
| 302 |
|
| int* status, const char** msg, size_t* msg_len, |
| 303 |
|
| struct phr_header* headers, size_t* num_headers, |
| 304 |
|
| size_t last_len) |
| 305 |
|
| { |
| 306 |
10 |
| const char * buf = buf_start, * buf_end = buf + len; |
| 307 |
10 |
| size_t max_headers = *num_headers; |
| 308 |
|
| int r; |
| 309 |
|
| |
| 310 |
10 |
| *minor_version = -1; |
| 311 |
10 |
| *status = 0; |
| 312 |
10 |
| *msg = NULL; |
| 313 |
10 |
| *msg_len = 0; |
| 314 |
10 |
| *num_headers = 0; |
| 315 |
|
| |
| 316 |
|
| /* if last_len != 0, check if the response is complete (a fast countermeasure |
| 317 |
|
| against slowloris */ |
| 318 |
10 |
▶ | if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
0 |
10 |
10 |
| if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) { |
0 |
0 |
0 |
|
| 319 |
0 |
| return r; |
| 320 |
|
| } |
| 321 |
|
| |
| 322 |
10 |
▶ | if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, |
Branch: 1 branch
| Branch | True | False | Total |
| if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len, |
2 |
8 |
10 |
|
| 323 |
|
| headers, num_headers, max_headers, &r)) |
| 324 |
|
| == NULL) { |
| 325 |
2 |
| return r; |
| 326 |
|
| } |
| 327 |
|
| |
| 328 |
8 |
| return buf - buf_start; |
| 329 |
|
| } |
| 330 |
|
| |
| 331 |
|
| #undef CHECK_EOF |
| 332 |
|
| #undef EXPECT_CHAR |
| 333 |
|
| #undef ADVANCE_TOKEN |
| 334 |
|
| |
| 335 |
|
| |
| 336 |
|
| |
| 337 |
|
| #ifndef STATIC_INLINE /* a public perl API from 5.13.4 */ |
| 338 |
|
| # if defined(__GNUC__) || defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) |
| 339 |
|
| # define STATIC_INLINE static inline |
| 340 |
|
| # else |
| 341 |
|
| # define STATIC_INLINE static |
| 342 |
|
| # endif |
| 343 |
|
| #endif /* STATIC_INLINE */ |
| 344 |
|
| |
| 345 |
|
| #define MAX_HEADER_NAME_LEN 1024 |
| 346 |
|
| #define MAX_HEADERS 128 |
| 347 |
|
| |
| 348 |
|
| #define HEADERS_NONE 0 |
| 349 |
|
| #define HEADERS_AS_HASHREF 1 |
| 350 |
|
| #define HEADERS_AS_ARRAYREF 2 |
| 351 |
|
| |
| 352 |
|
| STATIC_INLINE |
| 353 |
0 |
| char tou(char ch) |
| 354 |
|
| { |
| 355 |
0 |
▶ | if ('a' <= ch && ch <= 'z') |
Branch: 2 branches
| Branch | True | False | Total |
| if ('a' <= ch && ch <= 'z') |
0 |
0 |
0 |
| if ('a' <= ch && ch <= 'z') |
0 |
0 |
0 |
|
| 356 |
0 |
| ch -= 'a' - 'A'; |
| 357 |
0 |
| return ch; |
| 358 |
|
| } |
| 359 |
|
| |
| 360 |
266 |
| STATIC_INLINE char tol(char const ch) |
| 361 |
|
| { |
| 362 |
257 |
▶ | return ('A' <= ch && ch <= 'Z') |
Branch: 1 branch
| Branch | True | False | Total |
| return ('A' <= ch && ch <= 'Z') |
33 |
224 |
257 |
|
| 363 |
|
| ? ch - ('A' - 'a') |
| 364 |
523 |
▶ | : ch; |
Branch: 1 branch
| Branch | True | False | Total |
| : ch; |
257 |
9 |
266 |
|
| 365 |
|
| } |
| 366 |
|
| |
| 367 |
|
| /* copy src to dest with normalization. |
| 368 |
|
| dest must have enough size for src */ |
| 369 |
|
| STATIC_INLINE |
| 370 |
|
| void normalize_response_header_name(pTHX_ |
| 371 |
|
| char* const dest, |
| 372 |
|
| const char* const src, STRLEN const len) { |
| 373 |
|
| STRLEN i; |
| 374 |
|
| for(i = 0; i < len; i++) { |
| 375 |
|
| dest[i] = tol(src[i]); |
| 376 |
|
| } |
| 377 |
|
| } |
| 378 |
|
| |
| 379 |
|
| STATIC_INLINE |
| 380 |
|
| void concat_multiline_header(pTHX_ SV * val, const char * const cont, size_t const cont_len) { |
| 381 |
|
| sv_catpvs(val, "\n"); /* XXX: is it collect? */ |
| 382 |
|
| sv_catpvn(val, cont, cont_len); |
| 383 |
|
| } |
| 384 |
|
| |
| 385 |
|
| static |
| 386 |
0 |
| int header_is(const struct phr_header* header, const char* name, |
| 387 |
|
| size_t len) |
| 388 |
|
| { |
| 389 |
|
| const char* x, * y; |
| 390 |
0 |
▶ | if (header->name_len != len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (header->name_len != len) |
0 |
0 |
0 |
|
| 391 |
0 |
| return 0; |
| 392 |
0 |
▶ | for (x = header->name, y = name; len != 0; --len, ++x, ++y) |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = header->name, y = name; len != 0; --len, ++x, ++y) |
0 |
0 |
0 |
|
| 393 |
0 |
▶ | if (tou(*x) != *y) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tou(*x) != *y) |
0 |
0 |
0 |
|
| 394 |
0 |
| return 0; |
| 395 |
0 |
| return 1; |
| 396 |
|
| } |
| 397 |
|
| |
| 398 |
|
| static |
| 399 |
16 |
| size_t find_ch(const char* s, size_t len, char ch) |
| 400 |
|
| { |
| 401 |
|
| size_t i; |
| 402 |
42 |
▶ | for (i = 0; i != len; ++i, ++s) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i != len; ++i, ++s) |
26 |
16 |
42 |
|
| 403 |
26 |
▶ | if (*s == ch) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == ch) |
0 |
26 |
26 |
|
| 404 |
0 |
| break; |
| 405 |
16 |
| return i; |
| 406 |
|
| } |
| 407 |
|
| |
| 408 |
|
| STATIC_INLINE |
| 409 |
2 |
| int hex_decode(const char ch) |
| 410 |
|
| { |
| 411 |
|
| int r; |
| 412 |
2 |
▶ | if ('0' <= ch && ch <= '9') |
Branch: 2 branches
| Branch | True | False | Total |
| if ('0' <= ch && ch <= '9') |
2 |
0 |
2 |
| if ('0' <= ch && ch <= '9') |
2 |
0 |
2 |
|
| 413 |
2 |
| r = ch - '0'; |
| 414 |
0 |
▶ | else if ('A' <= ch && ch <= 'F') |
Branch: 2 branches
| Branch | True | False | Total |
| else if ('A' <= ch && ch <= 'F') |
0 |
0 |
0 |
| else if ('A' <= ch && ch <= 'F') |
0 |
0 |
0 |
|
| 415 |
0 |
| r = ch - 'A' + 0xa; |
| 416 |
0 |
▶ | else if ('a' <= ch && ch <= 'f') |
Branch: 2 branches
| Branch | True | False | Total |
| else if ('a' <= ch && ch <= 'f') |
0 |
0 |
0 |
| else if ('a' <= ch && ch <= 'f') |
0 |
0 |
0 |
|
| 417 |
0 |
| r = ch - 'a' + 0xa; |
| 418 |
|
| else |
| 419 |
0 |
| r = -1; |
| 420 |
2 |
| return r; |
| 421 |
|
| } |
| 422 |
|
| |
| 423 |
|
| static |
| 424 |
8 |
| char* url_decode(const char* s, size_t len) |
| 425 |
|
| { |
| 426 |
|
| dTHX; |
| 427 |
|
| char* dbuf, * d; |
| 428 |
|
| size_t i; |
| 429 |
|
| |
| 430 |
17 |
▶ | for (i = 0; i < len; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; ++i) |
10 |
7 |
17 |
|
| 431 |
10 |
▶ | if (s[i] == '%') |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[i] == '%') |
1 |
9 |
10 |
|
| 432 |
1 |
| goto NEEDS_DECODE; |
| 433 |
7 |
| return (char*)s; |
| 434 |
|
| |
| 435 |
1 |
| NEEDS_DECODE: |
| 436 |
|
| /* dbuf = (char*)malloc(len - 1); */ |
| 437 |
1 |
| Newz(0, dbuf, len - 1, char); |
| 438 |
|
| |
| 439 |
|
| assert(dbuf != NULL); |
| 440 |
1 |
| memcpy(dbuf, s, i); |
| 441 |
1 |
| d = dbuf + i; |
| 442 |
3 |
▶ | while (i < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < len) { |
2 |
1 |
3 |
|
| 443 |
2 |
▶ | if (s[i] == '%') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[i] == '%') { |
1 |
1 |
2 |
|
| 444 |
|
| int hi, lo; |
| 445 |
1 |
▶ | if ((hi = hex_decode(s[i + 1])) == -1 |
Branch: 1 branch
| Branch | True | False | Total |
| if ((hi = hex_decode(s[i + 1])) == -1 |
1 |
0 |
1 |
|
| 446 |
1 |
▶ | || (lo = hex_decode(s[i + 2])) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| || (lo = hex_decode(s[i + 2])) == -1) { |
0 |
1 |
1 |
|
| 447 |
|
| /* free(dbuf); */ |
| 448 |
0 |
| safefree(dbuf); |
| 449 |
0 |
| return NULL; |
| 450 |
|
| } |
| 451 |
1 |
| *d++ = hi * 16 + lo; |
| 452 |
1 |
| i += 3; |
| 453 |
|
| } else |
| 454 |
1 |
| *d++ = s[i++]; |
| 455 |
|
| } |
| 456 |
1 |
| *d = '\0'; |
| 457 |
1 |
| return dbuf; |
| 458 |
|
| } |
| 459 |
|
| |
| 460 |
|
| STATIC_INLINE |
| 461 |
8 |
| int store_url_decoded(HV* env, const char* name, size_t name_len, |
| 462 |
|
| const char* value, size_t value_len) |
| 463 |
|
| { |
| 464 |
|
| dTHX; |
| 465 |
8 |
| char* decoded = url_decode(value, value_len); |
| 466 |
8 |
▶ | if (decoded == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (decoded == NULL) |
0 |
8 |
8 |
|
| 467 |
0 |
| return -1; |
| 468 |
|
| |
| 469 |
8 |
▶ | if (decoded == value) |
Branch: 1 branch
| Branch | True | False | Total |
| if (decoded == value) |
7 |
1 |
8 |
|
| 470 |
7 |
| hv_store(env, name, name_len, newSVpvn(value, value_len), 0); |
| 471 |
|
| else { |
| 472 |
1 |
| hv_store(env, name, name_len, newSVpv(decoded, 0), 0); |
| 473 |
|
| /* free(decoded); */ |
| 474 |
1 |
| safefree(decoded); |
| 475 |
|
| } |
| 476 |
8 |
| return 0; |
| 477 |
|
| } |
| 478 |
|
| |
| 479 |
|
| |
| 480 |
|
| |
| 481 |
|
| int |
| 482 |
0 |
| parse_http_request_psgi(SV* buf, SV* envref) |
| 483 |
|
| { |
| 484 |
|
| const char* buf_str; |
| 485 |
|
| STRLEN buf_len; |
| 486 |
|
| const char* method; |
| 487 |
|
| size_t method_len; |
| 488 |
|
| const char* path; |
| 489 |
|
| size_t path_len; |
| 490 |
|
| int minor_version; |
| 491 |
|
| struct phr_header headers[MAX_HEADERS]; |
| 492 |
|
| size_t num_headers, question_at; |
| 493 |
|
| size_t i; |
| 494 |
|
| int ret; |
| 495 |
|
| HV* env; |
| 496 |
|
| SV* last_value; |
| 497 |
|
| char tmp[MAX_HEADER_NAME_LEN + sizeof("HTTP_") - 1]; |
| 498 |
|
| |
| 499 |
0 |
| buf_str = SvPV(buf, buf_len); |
| 500 |
0 |
| num_headers = MAX_HEADERS; |
| 501 |
0 |
| ret = phr_parse_request(buf_str, buf_len, &method, &method_len, &path, |
| 502 |
|
| &path_len, &minor_version, headers, &num_headers, 0); |
| 503 |
0 |
▶ | if (ret < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) |
0 |
0 |
0 |
|
| 504 |
0 |
| goto done; |
| 505 |
|
| |
| 506 |
0 |
| env = (HV*)SvRV(envref); |
| 507 |
|
| |
| 508 |
0 |
| hv_store(env, "REQUEST_METHOD", sizeof("REQUEST_METHOD") - 1, |
| 509 |
|
| newSVpvn(method, method_len), 0); |
| 510 |
0 |
| hv_store(env, "REQUEST_URI", sizeof("REQUEST_URI") - 1, |
| 511 |
|
| newSVpvn(path, path_len), 0); |
| 512 |
0 |
| hv_store(env, "SCRIPT_NAME", sizeof("SCRIPT_NAME") - 1, newSVpvn("", 0), 0); |
| 513 |
0 |
| path_len = find_ch(path, path_len, '#'); /* backported from |
| 514 |
|
| HTTP-Parser-XS-0.14 */ |
| 515 |
0 |
| question_at = find_ch(path, path_len, '?'); |
| 516 |
0 |
▶ | if (store_url_decoded(env, "PATH_INFO", sizeof("PATH_INFO") - 1, path, |
Branch: 1 branch
| Branch | True | False | Total |
| if (store_url_decoded(env, "PATH_INFO", sizeof("PATH_INFO") - 1, path, |
0 |
0 |
0 |
|
| 517 |
|
| question_at) |
| 518 |
|
| != 0) { |
| 519 |
0 |
| hv_clear(env); |
| 520 |
0 |
| ret = -1; |
| 521 |
0 |
| goto done; |
| 522 |
|
| } |
| 523 |
0 |
▶ | if (question_at != path_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (question_at != path_len) |
0 |
0 |
0 |
|
| 524 |
0 |
| ++question_at; |
| 525 |
0 |
| hv_store(env, "QUERY_STRING", sizeof("QUERY_STRING") - 1, |
| 526 |
|
| newSVpvn(path + question_at, path_len - question_at), 0); |
| 527 |
0 |
| sprintf(tmp, "HTTP/1.%d", minor_version); |
| 528 |
0 |
| hv_store(env, "SERVER_PROTOCOL", sizeof("SERVER_PROTOCOL") - 1, |
| 529 |
|
| newSVpv(tmp, 0), 0); |
| 530 |
0 |
| last_value = NULL; |
| 531 |
0 |
▶ | for (i = 0; i < num_headers; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < num_headers; ++i) { |
0 |
0 |
0 |
|
| 532 |
0 |
▶ | if (headers[i].name != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (headers[i].name != NULL) { |
0 |
0 |
0 |
|
| 533 |
|
| const char* name; |
| 534 |
|
| size_t name_len; |
| 535 |
|
| SV** slot; |
| 536 |
0 |
▶ | if (header_is(headers + i, "CONTENT-TYPE", sizeof("CONTENT-TYPE") - 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (header_is(headers + i, "CONTENT-TYPE", sizeof("CONTENT-TYPE") - 1)) { |
0 |
0 |
0 |
|
| 537 |
0 |
| name = "CONTENT_TYPE"; |
| 538 |
0 |
| name_len = sizeof("CONTENT_TYPE") - 1; |
| 539 |
0 |
▶ | } else if (header_is(headers + i, "CONTENT-LENGTH", |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (header_is(headers + i, "CONTENT-LENGTH", |
0 |
0 |
0 |
|
| 540 |
|
| sizeof("CONTENT-LENGTH") - 1)) { |
| 541 |
0 |
| name = "CONTENT_LENGTH"; |
| 542 |
0 |
| name_len = sizeof("CONTENT_LENGTH") - 1; |
| 543 |
|
| } else { |
| 544 |
|
| const char* s; |
| 545 |
|
| char* d; |
| 546 |
|
| size_t n; |
| 547 |
0 |
▶ | if (sizeof(tmp) - 5 < headers[i].name_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sizeof(tmp) - 5 < headers[i].name_len) { |
0 |
0 |
0 |
|
| 548 |
0 |
| hv_clear(env); |
| 549 |
0 |
| ret = -1; |
| 550 |
0 |
| goto done; |
| 551 |
|
| } |
| 552 |
0 |
| strcpy(tmp, "HTTP_"); |
| 553 |
0 |
| for (s = headers[i].name, n = headers[i].name_len, d = tmp + 5; |
| 554 |
0 |
▶ | n != 0; |
Branch: 1 branch
| Branch | True | False | Total |
| n != 0; |
0 |
0 |
0 |
|
| 555 |
0 |
| s++, --n, d++) |
| 556 |
0 |
▶ | *d = *s == '-' ? '_' : tou(*s); |
Branch: 1 branch
| Branch | True | False | Total |
| *d = *s == '-' ? '_' : tou(*s); |
0 |
0 |
0 |
|
| 557 |
0 |
| name = tmp; |
| 558 |
0 |
| name_len = headers[i].name_len + 5; |
| 559 |
|
| } |
| 560 |
0 |
| slot = hv_fetch(env, name, name_len, 1); |
| 561 |
0 |
▶ | if ( !slot ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !slot ) { |
0 |
0 |
0 |
|
| 562 |
0 |
| warn("failed to create hash entry"); |
| 563 |
0 |
| ret = -2; |
| 564 |
0 |
| goto done; |
| 565 |
|
| } |
| 566 |
0 |
▶ | if (SvOK(*slot)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(*slot)) { |
0 |
0 |
0 |
|
| 567 |
0 |
| sv_catpvn(*slot, ", ", 2); |
| 568 |
0 |
| sv_catpvn(*slot, headers[i].value, headers[i].value_len); |
| 569 |
|
| } else |
| 570 |
0 |
| sv_setpvn(*slot, headers[i].value, headers[i].value_len); |
| 571 |
0 |
| last_value = *slot; |
| 572 |
|
| } else { |
| 573 |
|
| /* continuing lines of a mulitiline header */ |
| 574 |
0 |
| sv_catpvn(last_value, headers[i].value, headers[i].value_len); |
| 575 |
|
| } |
| 576 |
|
| } |
| 577 |
|
| |
| 578 |
0 |
| done: |
| 579 |
|
| |
| 580 |
0 |
| return ret; |
| 581 |
|
| } |
| 582 |
|
| |
| 583 |
|
| |
| 584 |
|
| static int |
| 585 |
54 |
| str_eq(const char* str1, size_t len1, const char* str2, size_t len2) |
| 586 |
|
| { |
| 587 |
|
| const char *x, *y; |
| 588 |
54 |
▶ | if (len1 != len2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len1 != len2) |
42 |
12 |
54 |
|
| 589 |
42 |
| return 0; |
| 590 |
136 |
▶ | for (x = str1, y = str2; len1 != 0; --len1, ++x, ++y) |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = str1, y = str2; len1 != 0; --len1, ++x, ++y) |
124 |
12 |
136 |
|
| 591 |
124 |
▶ | if (*x != *y) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*x != *y) |
0 |
124 |
124 |
|
| 592 |
0 |
| return 0; |
| 593 |
12 |
| return 1; |
| 594 |
|
| } |
| 595 |
|
| |
| 596 |
|
| static int |
| 597 |
11 |
| str_lc_eq(const char* str1, size_t len1, const char* str2, size_t len2) |
| 598 |
|
| { |
| 599 |
|
| const char *x, *y; |
| 600 |
11 |
▶ | if (len1 != len2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len1 != len2) |
0 |
11 |
11 |
|
| 601 |
0 |
| return 0; |
| 602 |
81 |
▶ | for (x = str1, y = str2; len1 != 0; --len1, ++x, ++y) |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = str1, y = str2; len1 != 0; --len1, ++x, ++y) |
74 |
7 |
81 |
|
| 603 |
74 |
▶ | if (tol(*x) != *y) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tol(*x) != *y) |
4 |
70 |
74 |
|
| 604 |
4 |
| return 0; |
| 605 |
7 |
| return 1; |
| 606 |
|
| } |
| 607 |
|
| |
| 608 |
|
| static SV* |
| 609 |
14 |
| str_svnv(const char *buf, size_t len) |
| 610 |
|
| { |
| 611 |
|
| SV *sv; |
| 612 |
|
| const char *c; |
| 613 |
|
| size_t i; |
| 614 |
|
| |
| 615 |
14 |
▶ | if (buf == NULL || len == 0 || len > 15) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (buf == NULL || len == 0 || len > 15) { |
1 |
13 |
14 |
| if (buf == NULL || len == 0 || len > 15) { |
1 |
0 |
1 |
| if (buf == NULL || len == 0 || len > 15) { |
0 |
1 |
1 |
|
| 616 |
13 |
| return &PL_sv_undef; |
| 617 |
|
| } |
| 618 |
|
| |
| 619 |
1 |
| sv = newSVnv(0); |
| 620 |
1 |
▶ | if (sv == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv == NULL) { |
0 |
1 |
1 |
|
| 621 |
0 |
| return &PL_sv_undef; |
| 622 |
|
| } |
| 623 |
|
| |
| 624 |
1 |
| c = buf; |
| 625 |
12 |
▶ | for (i = 0; i < len; i++, c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++, c++) { |
11 |
1 |
12 |
|
| 626 |
11 |
▶ | if (*c >= '0' && *c <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*c >= '0' && *c <= '9') { |
11 |
0 |
11 |
| if (*c >= '0' && *c <= '9') { |
11 |
0 |
11 |
|
| 627 |
11 |
| SvNV_set(sv, SvNVX(sv) * 10 + (*c - '0')); |
| 628 |
0 |
▶ | } else if (SvNVX(sv) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (SvNVX(sv) > 0) { |
0 |
0 |
0 |
|
| 629 |
0 |
| break; |
| 630 |
|
| } else { |
| 631 |
0 |
| goto failed; |
| 632 |
|
| } |
| 633 |
|
| } |
| 634 |
|
| |
| 635 |
1 |
| return sv; |
| 636 |
|
| |
| 637 |
0 |
| failed: |
| 638 |
|
| |
| 639 |
0 |
| SvREFCNT_dec(sv); |
| 640 |
0 |
| return &PL_sv_undef; |
| 641 |
|
| } |
| 642 |
|
| |
| 643 |
|
| |
| 644 |
|
| int |
| 645 |
10 |
| parse_http_request(SV* buf, SV* envref) |
| 646 |
|
| { |
| 647 |
|
| const char* buf_str; |
| 648 |
|
| STRLEN buf_len; |
| 649 |
|
| const char* method; |
| 650 |
|
| size_t method_len; |
| 651 |
|
| const char* path; |
| 652 |
|
| size_t path_len; |
| 653 |
|
| int minor_version; |
| 654 |
|
| struct phr_header headers[MAX_HEADERS]; |
| 655 |
|
| size_t num_headers, question_at; |
| 656 |
|
| size_t i; |
| 657 |
|
| int ret; |
| 658 |
10 |
| int keepalive = 0; |
| 659 |
|
| HV* env; |
| 660 |
|
| SV* last_value; |
| 661 |
10 |
| char *content_length_str = NULL; |
| 662 |
10 |
| size_t content_length_strlen = 0; |
| 663 |
|
| char tmp[MAX_HEADER_NAME_LEN + sizeof("HTTP_") - 1]; |
| 664 |
|
| |
| 665 |
10 |
| buf_str = SvPV(buf, buf_len); |
| 666 |
10 |
| num_headers = MAX_HEADERS; |
| 667 |
10 |
| ret = phr_parse_request(buf_str, buf_len, &method, &method_len, &path, |
| 668 |
|
| &path_len, &minor_version, headers, &num_headers, 0); |
| 669 |
10 |
▶ | if (ret < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) |
2 |
8 |
10 |
|
| 670 |
2 |
| goto failed; |
| 671 |
|
| |
| 672 |
8 |
| env = (HV*)SvRV(envref); |
| 673 |
|
| |
| 674 |
8 |
| hv_store(env, "_method", sizeof("_method") - 1, |
| 675 |
|
| newSVpvn(method, method_len), 0); |
| 676 |
8 |
| hv_store(env, "_request_uri", sizeof("_request_uri") - 1, |
| 677 |
|
| newSVpvn(path, path_len), 0); |
| 678 |
|
| |
| 679 |
8 |
| path_len = find_ch(path, path_len, '#'); /* backported from |
| 680 |
|
| HTTP-Parser-XS-0.14 */ |
| 681 |
8 |
| question_at = find_ch(path, path_len, '?'); |
| 682 |
8 |
▶ | if (store_url_decoded(env, "_uri", sizeof("_uri") - 1, path, |
Branch: 1 branch
| Branch | True | False | Total |
| if (store_url_decoded(env, "_uri", sizeof("_uri") - 1, path, |
0 |
8 |
8 |
|
| 683 |
|
| question_at) |
| 684 |
|
| != 0) { |
| 685 |
0 |
| hv_clear(env); |
| 686 |
0 |
| ret = -1; |
| 687 |
0 |
| goto failed; |
| 688 |
|
| } |
| 689 |
8 |
▶ | if (question_at != path_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (question_at != path_len) |
0 |
8 |
8 |
|
| 690 |
0 |
| ++question_at; |
| 691 |
8 |
| hv_store(env, "_query_string", sizeof("_query_string") - 1, |
| 692 |
|
| newSVpvn(path + question_at, path_len - question_at), 0); |
| 693 |
|
| |
| 694 |
8 |
| sprintf(tmp, "HTTP/1.%d", minor_version); |
| 695 |
8 |
| hv_store(env, "_protocol", sizeof("_protocol") - 1, |
| 696 |
|
| newSVpv(tmp, 0), 0); |
| 697 |
|
| |
| 698 |
8 |
▶ | if (minor_version == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (minor_version == 1) { |
2 |
6 |
8 |
|
| 699 |
2 |
| keepalive = 1; |
| 700 |
|
| } |
| 701 |
|
| |
| 702 |
8 |
| last_value = NULL; |
| 703 |
23 |
▶ | for (i = 0; i < num_headers; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < num_headers; ++i) { |
16 |
7 |
23 |
|
| 704 |
16 |
▶ | if (headers[i].name != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (headers[i].name != NULL) { |
16 |
0 |
16 |
|
| 705 |
|
| const char* name; |
| 706 |
|
| size_t name_len; |
| 707 |
|
| SV** slot; |
| 708 |
|
| |
| 709 |
|
| AV *av; /* for arrayref */ |
| 710 |
|
| SV *rv, *sv; |
| 711 |
|
| |
| 712 |
16 |
▶ | if (*(headers[i].name) == '_') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*(headers[i].name) == '_') { |
0 |
16 |
16 |
|
| 713 |
0 |
| ret = -1; |
| 714 |
0 |
| goto failed; |
| 715 |
|
| } else { |
| 716 |
|
| const char* s; |
| 717 |
|
| char* d; |
| 718 |
|
| size_t n; |
| 719 |
16 |
▶ | if (sizeof(tmp) < headers[i].name_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sizeof(tmp) < headers[i].name_len) { |
1 |
15 |
16 |
|
| 720 |
1 |
| hv_clear(env); |
| 721 |
1 |
| ret = -1; |
| 722 |
1 |
| goto failed; |
| 723 |
|
| } |
| 724 |
15 |
| for (s = headers[i].name, n = headers[i].name_len, d = tmp; |
| 725 |
121 |
▶ | n != 0; |
Branch: 1 branch
| Branch | True | False | Total |
| n != 0; |
106 |
15 |
121 |
|
| 726 |
106 |
| s++, --n, d++) |
| 727 |
106 |
| *d = tol(*s); |
| 728 |
15 |
| name = tmp; |
| 729 |
15 |
| name_len = headers[i].name_len; |
| 730 |
|
| |
| 731 |
15 |
▶ | if (str_eq(name, name_len, "connection", sizeof("connection") - 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_eq(name, name_len, "connection", sizeof("connection") - 1)) { |
6 |
9 |
15 |
|
| 732 |
6 |
▶ | if (str_lc_eq(headers[i].value, |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_lc_eq(headers[i].value, |
4 |
2 |
6 |
|
| 733 |
6 |
| headers[i].value_len > (sizeof("keep-alive") - 1) |
| 734 |
|
| ? (sizeof("keep-alive") - 1) |
| 735 |
6 |
| : headers[i].value_len, |
| 736 |
|
| "keep-alive", sizeof("keep-alive") - 1)) { |
| 737 |
4 |
| keepalive = 1; |
| 738 |
|
| } else { |
| 739 |
2 |
| keepalive = 0; |
| 740 |
|
| } |
| 741 |
|
| } |
| 742 |
|
| |
| 743 |
15 |
▶ | if (str_eq(name, name_len, "content-length", |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_eq(name, name_len, "content-length", |
1 |
14 |
15 |
|
| 744 |
|
| sizeof("content-length") - 1)) { |
| 745 |
1 |
| content_length_str = (char *) headers[i].value; |
| 746 |
1 |
| content_length_strlen = headers[i].value_len; |
| 747 |
|
| } |
| 748 |
|
| } |
| 749 |
15 |
| slot = hv_fetch(env, name, name_len, 1); |
| 750 |
15 |
▶ | if ( !slot ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !slot ) { |
0 |
15 |
15 |
|
| 751 |
0 |
| warn("failed to create hash entry"); |
| 752 |
0 |
| ret = -1; |
| 753 |
0 |
| goto failed; |
| 754 |
|
| } |
| 755 |
|
| |
| 756 |
15 |
▶ | if (SvOK(*slot)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(*slot)) { |
0 |
15 |
15 |
|
| 757 |
0 |
| av = (AV *) SvRV(*slot); |
| 758 |
0 |
| sv = newSV(128); |
| 759 |
|
| |
| 760 |
0 |
| SvPOK_only(sv); |
| 761 |
|
| |
| 762 |
0 |
▶ | SvGROW(sv, headers[i].value_len + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW(sv, headers[i].value_len + 1); |
0 |
0 |
0 |
| SvGROW(sv, headers[i].value_len + 1); |
0 |
0 |
0 |
|
| 763 |
0 |
| sv_setpvn(sv, headers[i].value, headers[i].value_len); |
| 764 |
|
| |
| 765 |
0 |
| av_push(av, sv); |
| 766 |
|
| } else { |
| 767 |
15 |
| av = newAV(); |
| 768 |
15 |
| rv = sv_2mortal(newRV_noinc((SV *) av)); |
| 769 |
15 |
| sv = newSV(128); |
| 770 |
|
| |
| 771 |
15 |
| SvPOK_only(sv); |
| 772 |
|
| |
| 773 |
15 |
▶ | SvGROW(sv, headers[i].value_len + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW(sv, headers[i].value_len + 1); |
15 |
0 |
15 |
| SvGROW(sv, headers[i].value_len + 1); |
0 |
15 |
15 |
|
| 774 |
15 |
| sv_setpvn(sv, headers[i].value, headers[i].value_len); |
| 775 |
|
| |
| 776 |
15 |
| av_push(av, sv); |
| 777 |
15 |
▶ | SvSetSV(*slot, rv); |
Branch: 1 branch
| Branch | True | False | Total |
| SvSetSV(*slot, rv); |
15 |
0 |
15 |
|
| 778 |
|
| } |
| 779 |
|
| |
| 780 |
15 |
| last_value = sv; |
| 781 |
|
| } else { |
| 782 |
|
| /* continuing lines of a mulitiline header */ |
| 783 |
0 |
| sv_catpvn(last_value, headers[i].value, headers[i].value_len); |
| 784 |
|
| } |
| 785 |
|
| } |
| 786 |
|
| |
| 787 |
7 |
▶ | if (ret > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret > 0) { |
0 |
7 |
7 |
|
| 788 |
7 |
| hv_store(env, "_keepalive", sizeof("_keepalive") - 1, |
| 789 |
|
| newSViv(keepalive), 0); |
| 790 |
7 |
| hv_store(env, "_content_length", sizeof("_content_length") - 1, |
| 791 |
|
| str_svnv(content_length_str, content_length_strlen), 0); |
| 792 |
|
| } |
| 793 |
|
| |
| 794 |
0 |
| failed: |
| 795 |
|
| |
| 796 |
10 |
| return ret; |
| 797 |
|
| } |
| 798 |
|
| |
| 799 |
|
| |
| 800 |
|
| |
| 801 |
|
| int |
| 802 |
10 |
| parse_http_response(SV* buf, SV* envref) |
| 803 |
|
| { |
| 804 |
|
| int minor_version, status; |
| 805 |
|
| const char* msg; |
| 806 |
|
| size_t msg_len; |
| 807 |
|
| struct phr_header headers[MAX_HEADERS]; |
| 808 |
10 |
| size_t num_headers = MAX_HEADERS; |
| 809 |
|
| STRLEN buf_len; |
| 810 |
|
| const char *buf_str; |
| 811 |
10 |
| size_t last_len = 0; |
| 812 |
|
| size_t i; |
| 813 |
|
| char name[MAX_HEADER_NAME_LEN]; /* temp buffer for normalized names */ |
| 814 |
|
| int ret; |
| 815 |
10 |
| int keepalive = 0; |
| 816 |
|
| HV* env; |
| 817 |
|
| SV* last_value; |
| 818 |
10 |
| char *content_length_str = NULL; |
| 819 |
10 |
| size_t content_length_strlen = 0; |
| 820 |
|
| char tmp[MAX_HEADER_NAME_LEN + sizeof("HTTP_") - 1]; |
| 821 |
|
| |
| 822 |
10 |
| buf_str = SvPV(buf, buf_len); |
| 823 |
10 |
| num_headers = MAX_HEADERS; |
| 824 |
|
| |
| 825 |
10 |
| ret = phr_parse_response(buf_str, buf_len, &minor_version, &status, &msg, |
| 826 |
|
| &msg_len, headers, &num_headers, last_len); |
| 827 |
10 |
▶ | if (ret < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) |
2 |
8 |
10 |
|
| 828 |
2 |
| goto done; |
| 829 |
|
| |
| 830 |
8 |
| env = (HV*)SvRV(envref); |
| 831 |
|
| |
| 832 |
8 |
| hv_store(env, "_message", sizeof("_message") - 1, |
| 833 |
|
| newSVpvn(msg, msg_len), 0); |
| 834 |
|
| |
| 835 |
8 |
| sprintf(tmp, "HTTP/1.%d", minor_version); |
| 836 |
8 |
| hv_store(env, "_protocol", sizeof("_protocol") - 1, |
| 837 |
|
| newSVpv(tmp, 0), 0); |
| 838 |
|
| |
| 839 |
8 |
| sprintf(tmp, "%d", status); |
| 840 |
8 |
| hv_store(env, "_status", sizeof("_status") - 1, |
| 841 |
|
| newSVpv(tmp, 0), 0); |
| 842 |
|
| |
| 843 |
8 |
▶ | if (minor_version == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (minor_version == 1) { |
2 |
6 |
8 |
|
| 844 |
2 |
| keepalive = 1; |
| 845 |
|
| } |
| 846 |
|
| |
| 847 |
8 |
| last_value = NULL; |
| 848 |
20 |
▶ | for (i = 0; i < num_headers; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < num_headers; ++i) { |
13 |
7 |
20 |
|
| 849 |
13 |
▶ | if (headers[i].name != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (headers[i].name != NULL) { |
13 |
0 |
13 |
|
| 850 |
|
| const char* name; |
| 851 |
|
| size_t name_len; |
| 852 |
|
| SV** slot; |
| 853 |
|
| |
| 854 |
|
| AV *av; /* for arrayref */ |
| 855 |
|
| SV *rv, *sv; |
| 856 |
|
| |
| 857 |
13 |
▶ | if (*(headers[i].name) == '_') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*(headers[i].name) == '_') { |
0 |
13 |
13 |
|
| 858 |
0 |
| ret = -1; |
| 859 |
0 |
| goto done; |
| 860 |
|
| } else { |
| 861 |
|
| const char* s; |
| 862 |
|
| char* d; |
| 863 |
|
| size_t n; |
| 864 |
13 |
▶ | if (sizeof(tmp) < headers[i].name_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sizeof(tmp) < headers[i].name_len) { |
1 |
12 |
13 |
|
| 865 |
1 |
| hv_clear(env); |
| 866 |
1 |
| ret = -1; |
| 867 |
1 |
| goto done; |
| 868 |
|
| } |
| 869 |
12 |
| for (s = headers[i].name, n = headers[i].name_len, d = tmp; |
| 870 |
98 |
▶ | n != 0; |
Branch: 1 branch
| Branch | True | False | Total |
| n != 0; |
86 |
12 |
98 |
|
| 871 |
86 |
| s++, --n, d++) |
| 872 |
86 |
| *d = tol(*s); |
| 873 |
12 |
| name = tmp; |
| 874 |
12 |
| name_len = headers[i].name_len; |
| 875 |
|
| |
| 876 |
12 |
▶ | if (str_eq(name, name_len, "connection", sizeof("connection") - 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_eq(name, name_len, "connection", sizeof("connection") - 1)) { |
5 |
7 |
12 |
|
| 877 |
5 |
▶ | if (str_lc_eq(headers[i].value, |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_lc_eq(headers[i].value, |
3 |
2 |
5 |
|
| 878 |
5 |
| headers[i].value_len > (sizeof("keep-alive") - 1) |
| 879 |
|
| ? (sizeof("keep-alive") - 1) |
| 880 |
5 |
| : headers[i].value_len, |
| 881 |
|
| "keep-alive", sizeof("keep-alive") - 1)) { |
| 882 |
3 |
| keepalive = 1; |
| 883 |
|
| } else { |
| 884 |
2 |
| keepalive = 0; |
| 885 |
|
| } |
| 886 |
|
| } |
| 887 |
|
| |
| 888 |
12 |
▶ | if (str_eq(name, name_len, "content-length", |
Branch: 1 branch
| Branch | True | False | Total |
| if (str_eq(name, name_len, "content-length", |
0 |
12 |
12 |
|
| 889 |
|
| sizeof("content-length") - 1)) { |
| 890 |
0 |
| content_length_str = (char *) headers[i].value; |
| 891 |
0 |
| content_length_strlen = headers[i].value_len; |
| 892 |
|
| } |
| 893 |
|
| |
| 894 |
|
| } |
| 895 |
12 |
| slot = hv_fetch(env, name, name_len, 1); |
| 896 |
12 |
▶ | if ( !slot ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !slot ) { |
0 |
12 |
12 |
|
| 897 |
0 |
| warn("failed to create hash entry"); |
| 898 |
0 |
| ret = -2; |
| 899 |
0 |
| goto done; |
| 900 |
|
| } |
| 901 |
|
| |
| 902 |
12 |
▶ | if (SvOK(*slot)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(*slot)) { |
0 |
12 |
12 |
|
| 903 |
0 |
| av = (AV *) SvRV(*slot); |
| 904 |
0 |
| sv = newSV(128); |
| 905 |
|
| |
| 906 |
0 |
| SvPOK_only(sv); |
| 907 |
|
| |
| 908 |
0 |
▶ | SvGROW(sv, headers[i].value_len + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW(sv, headers[i].value_len + 1); |
0 |
0 |
0 |
| SvGROW(sv, headers[i].value_len + 1); |
0 |
0 |
0 |
|
| 909 |
0 |
| sv_setpvn(sv, headers[i].value, headers[i].value_len); |
| 910 |
|
| |
| 911 |
0 |
| av_push(av, sv); |
| 912 |
|
| } else { |
| 913 |
12 |
| av = newAV(); |
| 914 |
12 |
| rv = sv_2mortal(newRV_noinc((SV *) av)); |
| 915 |
12 |
| sv = newSV(128); |
| 916 |
|
| |
| 917 |
12 |
| SvPOK_only(sv); |
| 918 |
|
| |
| 919 |
12 |
▶ | SvGROW(sv, headers[i].value_len + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW(sv, headers[i].value_len + 1); |
12 |
0 |
12 |
| SvGROW(sv, headers[i].value_len + 1); |
0 |
12 |
12 |
|
| 920 |
12 |
| sv_setpvn(sv, headers[i].value, headers[i].value_len); |
| 921 |
|
| |
| 922 |
12 |
| av_push(av, sv); |
| 923 |
12 |
▶ | SvSetSV(*slot, rv); |
Branch: 1 branch
| Branch | True | False | Total |
| SvSetSV(*slot, rv); |
12 |
0 |
12 |
|
| 924 |
|
| } |
| 925 |
|
| |
| 926 |
12 |
| last_value = sv; |
| 927 |
|
| } else { |
| 928 |
|
| /* continuing lines of a mulitiline header */ |
| 929 |
0 |
| sv_catpvn(last_value, headers[i].value, headers[i].value_len); |
| 930 |
|
| } |
| 931 |
|
| } |
| 932 |
|
| |
| 933 |
7 |
▶ | if (ret > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret > 0) { |
0 |
7 |
7 |
|
| 934 |
7 |
| hv_store(env, "_keepalive", sizeof("_keepalive") - 1, |
| 935 |
|
| newSViv(keepalive), 0); |
| 936 |
7 |
| hv_store(env, "_content_length", sizeof("_content_length") - 1, |
| 937 |
|
| str_svnv(content_length_str, content_length_strlen), 0); |
| 938 |
|
| } |
| 939 |
|
| |
| 940 |
0 |
| done: |
| 941 |
|
| |
| 942 |
10 |
| return ret; |
| 943 |
|
| } |
| 944 |
|
| |
| 945 |
|
| |
| 946 |
|
| |