XS.xs

filter: Statementstmt 94.0% 174 / 185 Branchbran 83.8% 104 / 124 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.9% 278 / 309 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/HTTP/Parser/XS.pm ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #define NEED_newRV_noinc
6 #define NEED_newSVpvn_flags
7 #define NEED_sv_2pv_flags
8 #include "ppport.h"
9 #include "picohttpparser/picohttpparser.c"
10  
11 #ifndef STATIC_INLINE /* a public perl API from 5.13.4 */
12 # if defined(__GNUC__) || defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))
13 # define STATIC_INLINE static inline
14 # else
15 # define STATIC_INLINE static
16 # endif
17 #endif /* STATIC_INLINE */
18  
19 #define MAX_HEADER_NAME_LEN 1024
20 #define MAX_HEADERS 128
21  
22 #define HEADERS_NONE 0
23 #define HEADERS_AS_HASHREF 1
24 #define HEADERS_AS_ARRAYREF 2
25  
26 STATIC_INLINE
27 1072 char tou(char ch)
28 {
29 1072 ▶ if ('a' <= ch && ch <= 'z')
Branch: 2 branches
BranchTrueFalseTotal
if ('a' <= ch && ch <= 'z') 1060 12 1072
if ('a' <= ch && ch <= 'z') 1060 0 1060
30 1060 ch -= 'a' - 'A';
31 1072 return ch;
32 }
33  
34 1919 STATIC_INLINE char tol(char const ch)
35 {
36 1595 ▶ return ('A' <= ch && ch <= 'Z')
Branch: 1 branch
BranchTrueFalseTotal
return ('A' <= ch && ch <= 'Z') 244 1351 1595
37 ? ch - ('A' - 'a')
38 3514 ▶ : ch;
Branch: 1 branch
BranchTrueFalseTotal
: ch; 1595 324 1919
39 }
40  
41 /* copy src to dest with normalization.
42 dest must have enough size for src */
43 STATIC_INLINE
44 180 void normalize_response_header_name(pTHX_
45 char* const dest,
46 const char* const src, STRLEN const len) {
47 STRLEN i;
48 2099 ▶ for(i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < len; i++) { 1919 180 2099
49 1919 dest[i] = tol(src[i]);
50 }
51 180 }
52  
53 STATIC_INLINE
54 4 void concat_multiline_header(pTHX_ SV * val, const char * const cont, size_t const cont_len) {
55 4 sv_catpvs(val, "\n"); /* XXX: is it collect? */
56 4 sv_catpvn(val, cont, cont_len);
57 4 }
58  
59 static
60 17 int header_is(const struct phr_header* header, const char* name,
61 size_t len)
62 {
63 const char* x, * y;
64 17 ▶ if (header->name_len != len)
Branch: 1 branch
BranchTrueFalseTotal
if (header->name_len != len) 15 2 17
65 15 return 0;
66 28 ▶ for (x = header->name, y = name; len != 0; --len, ++x, ++y)
Branch: 1 branch
BranchTrueFalseTotal
for (x = header->name, y = name; len != 0; --len, ++x, ++y) 26 2 28
67 26 ▶ if (tou(*x) != *y)
Branch: 1 branch
BranchTrueFalseTotal
if (tou(*x) != *y) 0 26 26
68 0 return 0;
69 2 return 1;
70 }
71  
72 static
73 34 size_t find_ch(const char* s, size_t len, char ch)
74 {
75 size_t i;
76 246 ▶ for (i = 0; i != len; ++i, ++s)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i != len; ++i, ++s) 217 29 246
77 217 ▶ if (*s == ch)
Branch: 1 branch
BranchTrueFalseTotal
if (*s == ch) 5 212 217
78 5 break;
79 34 return i;
80 }
81  
82 STATIC_INLINE
83 26 int hex_decode(const char ch)
84 {
85 int r;
86 26 ▶ if ('0' <= ch && ch <= '9')
Branch: 2 branches
BranchTrueFalseTotal
if ('0' <= ch && ch <= '9') 25 1 26
if ('0' <= ch && ch <= '9') 22 3 25
87 22 r = ch - '0';
88 4 ▶ else if ('A' <= ch && ch <= 'F')
Branch: 2 branches
BranchTrueFalseTotal
else if ('A' <= ch && ch <= 'F') 3 1 4
else if ('A' <= ch && ch <= 'F') 2 1 3
89 2 r = ch - 'A' + 0xa;
90 2 ▶ else if ('a' <= ch && ch <= 'f')
Branch: 2 branches
BranchTrueFalseTotal
else if ('a' <= ch && ch <= 'f') 1 1 2
else if ('a' <= ch && ch <= 'f') 0 1 1
91 0 r = ch - 'a' + 0xa;
92 else
93 2 r = -1;
94 26 return r;
95 }
96  
97 static
98 17 char* url_decode(const char* s, size_t len, size_t* decoded_len)
99 {
100 dTHX;
101 char* dbuf, * d;
102 size_t i;
103
104 64 ▶ for (i = 0; i < len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) 56 8 64
105 56 ▶ if (s[i] == '%')
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '%') 9 47 56
106 9 goto NEEDS_DECODE;
107 8 *decoded_len = len;
108 8 return (char*)s;
109
110 9 NEEDS_DECODE:
111 9 dbuf = (char*)malloc(len - 1);
112 assert(dbuf != NULL);
113 9 memcpy(dbuf, s, i);
114 9 d = dbuf + i;
115 31 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 24 7 31
116 24 ▶ if (s[i] == '%') {
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '%') { 13 11 24
117 int hi, lo;
118 13 ▶ if ((hi = hex_decode(s[i + 1])) == -1
Branch: 1 branch
BranchTrueFalseTotal
if ((hi = hex_decode(s[i + 1])) == -1 13 0 13
119 13 ▶ || (lo = hex_decode(s[i + 2])) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
|| (lo = hex_decode(s[i + 2])) == -1) { 2 11 13
120 2 free(dbuf);
121 2 return NULL;
122 }
123 11 *d++ = hi * 16 + lo;
124 11 i += 3;
125 } else
126 11 *d++ = s[i++];
127 }
128 7 *d = '\0';
129 7 *decoded_len = d - dbuf;
130 7 return dbuf;
131 }
132  
133 STATIC_INLINE
134 17 int store_url_decoded(HV* env, const char* name, size_t name_len,
135 const char* value, size_t value_len)
136 {
137 dTHX;
138 size_t decoded_len;
139 17 char* decoded = url_decode(value, value_len, &decoded_len);
140 17 ▶ if (decoded == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (decoded == NULL) 2 15 17
141 2 return -1;
142
143 15 ▶ if (decoded == value)
Branch: 1 branch
BranchTrueFalseTotal
if (decoded == value) 8 7 15
144 8 hv_store(env, name, name_len, newSVpvn(value, value_len), 0);
145 else {
146 7 hv_store(env, name, name_len, newSVpvn(decoded, decoded_len), 0);
147 7 free(decoded);
148 }
149 15 return 0;
150 }
151  
152 MODULE = HTTP::Parser::XS PACKAGE = HTTP::Parser::XS
153  
154 int parse_http_request(SV* buf, SV* envref)
155 PROTOTYPE: $$
156 CODE:
157 {
158 const char* buf_str;
159 STRLEN buf_len;
160 const char* method;
161 size_t method_len;
162 const char* path;
163 size_t path_len;
164 int minor_version;
165 struct phr_header headers[MAX_HEADERS];
166 size_t num_headers, question_at;
167 size_t i;
168 int ret;
169 HV* env;
170 SV* last_value;
171 char tmp[MAX_HEADER_NAME_LEN + sizeof("HTTP_") - 1];
172
173 19 buf_str = SvPV(buf, buf_len);
174 19 num_headers = MAX_HEADERS;
175 19 ret = phr_parse_request(buf_str, buf_len, &method, &method_len, &path,
176 &path_len, &minor_version, headers, &num_headers, 0);
177 19 ▶ if (ret < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ret < 0) 1 18 19
178 1 goto done;
179
180 18 ▶ if (!SvROK(envref))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(envref)) 1 17 18
181 1 Perl_croak(aTHX_ "second param to parse_http_request should be a hashref");
182  
183 17 env = (HV*)SvRV(envref);
184 17 ▶ if (SvTYPE(env) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(env) != SVt_PVHV) 0 17 17
185 0 Perl_croak(aTHX_ "second param to parse_http_request should be a hashref");
186
187 17 hv_store(env, "REQUEST_METHOD", sizeof("REQUEST_METHOD") - 1,
188 newSVpvn(method, method_len), 0);
189 17 hv_store(env, "REQUEST_URI", sizeof("REQUEST_URI") - 1,
190 newSVpvn(path, path_len), 0);
191 17 hv_store(env, "SCRIPT_NAME", sizeof("SCRIPT_NAME") - 1, newSVpvn("", 0), 0);
192 17 path_len = find_ch(path, path_len, '#'); /* strip off all text after # after storing request_uri */
193 17 question_at = find_ch(path, path_len, '?');
194 17 ▶ if (store_url_decoded(env, "PATH_INFO", sizeof("PATH_INFO") - 1, path,
Branch: 1 branch
BranchTrueFalseTotal
if (store_url_decoded(env, "PATH_INFO", sizeof("PATH_INFO") - 1, path, 2 15 17
195 question_at)
196 != 0) {
197 2 hv_clear(env);
198 2 ret = -1;
199 2 goto done;
200 }
201 15 ▶ if (question_at != path_len)
Branch: 1 branch
BranchTrueFalseTotal
if (question_at != path_len) 3 12 15
202 3 ++question_at;
203 15 hv_store(env, "QUERY_STRING", sizeof("QUERY_STRING") - 1,
204 newSVpvn(path + question_at, path_len - question_at), 0);
205 15 sprintf(tmp, "HTTP/1.%d", minor_version);
206 15 hv_store(env, "SERVER_PROTOCOL", sizeof("SERVER_PROTOCOL") - 1,
207 newSVpv(tmp, 0), 0);
208 15 last_value = NULL;
209 25 ▶ for (i = 0; i < num_headers; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_headers; ++i) { 11 14 25
210 11 ▶ if (headers[i].name != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (headers[i].name != NULL) { 9 2 11
211 const char* name;
212 size_t name_len;
213 SV** slot;
214 9 ▶ if (header_is(headers + i, "CONTENT-TYPE", sizeof("CONTENT-TYPE") - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (header_is(headers + i, "CONTENT-TYPE", sizeof("CONTENT-TYPE") - 1)) { 1 8 9
215 1 name = "CONTENT_TYPE";
216 1 name_len = sizeof("CONTENT_TYPE") - 1;
217 8 ▶ } else if (header_is(headers + i, "CONTENT-LENGTH",
Branch: 1 branch
BranchTrueFalseTotal
} else if (header_is(headers + i, "CONTENT-LENGTH", 1 7 8
218 sizeof("CONTENT-LENGTH") - 1)) {
219 1 name = "CONTENT_LENGTH";
220 1 name_len = sizeof("CONTENT_LENGTH") - 1;
221 } else {
222 const char* s;
223 char* d;
224 size_t n;
225 7 ▶ if (sizeof(tmp) - 5 < headers[i].name_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (sizeof(tmp) - 5 < headers[i].name_len) { 1 6 7
226 1 hv_clear(env);
227 1 ret = -1;
228 1 goto done;
229 }
230 6 strcpy(tmp, "HTTP_");
231 6 for (s = headers[i].name, n = headers[i].name_len, d = tmp + 5;
232 1053 ▶ n != 0;
Branch: 1 branch
BranchTrueFalseTotal
n != 0; 1047 6 1053
233 1047 s++, --n, d++)
234 1047 ▶ *d = *s == '-' ? '_' : tou(*s);
Branch: 1 branch
BranchTrueFalseTotal
*d = *s == '-' ? '_' : tou(*s); 1046 1 1047
235 6 name = tmp;
236 6 name_len = headers[i].name_len + 5;
237 }
238 8 slot = hv_fetch(env, name, name_len, 1);
239 8 ▶ if ( !slot )
Branch: 1 branch
BranchTrueFalseTotal
if ( !slot ) 0 8 8
240 0 croak("failed to create hash entry");
241 8 ▶ if (SvOK(*slot)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(*slot)) { 2 6 8
242 2 sv_catpvn(*slot, ", ", 2);
243 2 sv_catpvn(*slot, headers[i].value, headers[i].value_len);
244 } else
245 6 sv_setpvn(*slot, headers[i].value, headers[i].value_len);
246 8 last_value = *slot;
247 } else {
248 /* continuing lines of a mulitiline header */
249 2 sv_catpvn(last_value, headers[i].value, headers[i].value_len);
250 }
251 }
252
253 14 done:
254 18 ▶ RETVAL = ret;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ret; 5 13 18
255 }
256 OUTPUT:
257 RETVAL
258  
259 void
260 parse_http_response(SV* buf, int header_format, HV* special_headers = NULL)
261 PPCODE:
262 {
263 int minor_version, status;
264 const char* msg;
265 size_t msg_len;
266 struct phr_header headers[MAX_HEADERS];
267 40 size_t num_headers = MAX_HEADERS;
268 STRLEN buf_len;
269 40 const char* const buf_str = SvPV_const(buf, buf_len);
270 40 size_t last_len = 0;
271 40 int const ret = phr_parse_response(buf_str, buf_len,
272 &minor_version, &status, &msg, &msg_len, headers, &num_headers, last_len);
273 40 SV* last_special_headers_value_sv = NULL;
274 40 SV* last_element_value_sv = NULL;
275 size_t i;
276 SV *res_headers;
277 char name[MAX_HEADER_NAME_LEN]; /* temp buffer for normalized names */
278  
279 40 ▶ if (header_format == HEADERS_AS_HASHREF) {
Branch: 1 branch
BranchTrueFalseTotal
if (header_format == HEADERS_AS_HASHREF) { 24 16 40
280 24 res_headers = sv_2mortal((SV*)newHV());
281 16 ▶ } else if (header_format == HEADERS_AS_ARRAYREF) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (header_format == HEADERS_AS_ARRAYREF) { 8 8 16
282 8 res_headers = sv_2mortal((SV*)newAV());
283 8 av_extend((AV*)res_headers, (num_headers * 2) - 1);
284 8 ▶ } else if (header_format == HEADERS_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (header_format == HEADERS_NONE) { 8 0 8
285 8 res_headers = NULL;
286 }
287  
288 226 ▶ for (i = 0; i < num_headers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_headers; i++) { 186 40 226
289 186 struct phr_header const h = headers[i];
290 186 ▶ if (h.name != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (h.name != NULL) { 181 5 186
291 SV* namesv;
292 SV* valuesv;
293 181 ▶ if(h.name_len > sizeof(name)) {
Branch: 1 branch
BranchTrueFalseTotal
if(h.name_len > sizeof(name)) { 1 180 181
294 /* skip if name_len is too long */
295 12 continue;
296 }
297  
298 180 normalize_response_header_name(aTHX_
299 180 name, h.name, h.name_len);
300  
301 180 ▶ if(special_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if(special_headers) { 0 180 180
302 0 SV** const slot = hv_fetch(special_headers,
303 name, h.name_len, FALSE);
304 0 ▶ if (slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot) { 0 0 0
305 0 SV* const hash_value = *slot;
306 0 sv_setpvn_mg(hash_value, h.value, h.value_len);
307 0 last_special_headers_value_sv = hash_value;
308 }
309 else {
310 0 last_special_headers_value_sv = NULL;
311 }
312 }
313  
314 180 ▶ if(header_format == HEADERS_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if(header_format == HEADERS_NONE) { 11 169 180
315 11 continue;
316 }
317  
318 169 namesv = sv_2mortal(newSVpvn_share(name, h.name_len, 0U));
319 169 valuesv = newSVpvn_flags(
320 h.value, h.value_len, SVs_TEMP);
321  
322 169 ▶ if (header_format == HEADERS_AS_HASHREF) {
Branch: 1 branch
BranchTrueFalseTotal
if (header_format == HEADERS_AS_HASHREF) { 158 11 169
323 158 HE* const slot = hv_fetch_ent((HV*)res_headers, namesv, FALSE, 0U);
324 158 ▶ if(!slot) { /* first time */
Branch: 1 branch
BranchTrueFalseTotal
if(!slot) { /* first time */ 155 3 158
325 155 (void)hv_store_ent((HV*)res_headers, namesv,
326 SvREFCNT_inc_simple_NN(valuesv), 0U);
327 }
328 else { /* second time; the header has multiple values */
329 3 SV* sv = hv_iterval((HV*)res_headers, slot);
330 3 ▶ if(!( SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV )) {
Branch: 2 branches
BranchTrueFalseTotal
if(!( SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV )) { 0 3 3
if(!( SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV )) { 0 0 0
331 /* make $value to [$value] and restore it to $res_header */
332 3 AV* const av = newAV();
333 3 SV* const avref = newRV_noinc((SV*)av);
334 3 (void)av_store(av, 0, SvREFCNT_inc_simple_NN(sv));
335 3 (void)hv_store_ent((HV*)res_headers, namesv, avref, 0U);
336 3 sv = avref;
337 }
338 3 av_push((AV*)SvRV(sv), SvREFCNT_inc_simple_NN(valuesv));
339 }
340 158 last_element_value_sv = valuesv;
341 11 ▶ } else if (header_format == HEADERS_AS_ARRAYREF) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (header_format == HEADERS_AS_ARRAYREF) { 11 0 11
342 11 av_push((AV*)res_headers, SvREFCNT_inc_simple_NN(namesv));
343 11 av_push((AV*)res_headers, SvREFCNT_inc_simple_NN(valuesv));
344 11 last_element_value_sv = valuesv;
345 }
346 } else {
347 /* continuing lines of a mulitiline header */
348 5 ▶ if (special_headers && last_special_headers_value_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (special_headers && last_special_headers_value_sv) { 0 5 5
if (special_headers && last_special_headers_value_sv) { 0 0 0
349 0 concat_multiline_header(aTHX_ last_special_headers_value_sv, h.value, h.value_len);
350 }
351 5 ▶ if ((header_format == HEADERS_AS_HASHREF || header_format == HEADERS_AS_ARRAYREF) && last_element_value_sv) {
Branch: 3 branches
BranchTrueFalseTotal
if ((header_format == HEADERS_AS_HASHREF || header_format == HEADERS_AS_ARRAYREF) && last_element_value_sv) { 2 3 5
if ((header_format == HEADERS_AS_HASHREF || header_format == HEADERS_AS_ARRAYREF) && last_element_value_sv) { 1 1 2
if ((header_format == HEADERS_AS_HASHREF || header_format == HEADERS_AS_ARRAYREF) && last_element_value_sv) { 4 0 4
352 4 concat_multiline_header(aTHX_ last_element_value_sv, h.value, h.value_len);
353 }
354 }
355 }
356
357 40 ▶ if(ret > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(ret > 0) { 36 4 40
358 36 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 36 36
359 36 mPUSHi(ret);
360 36 mPUSHi(minor_version);
361 36 mPUSHi(status);
362 36 mPUSHp(msg, msg_len);
363 36 ▶ if (res_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (res_headers) { 28 8 36
364 28 mPUSHs(newRV_inc(res_headers));
365 } else {
366 8 mPUSHs(&PL_sv_undef);
367 }
368 }
369 else {
370 4 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 4 4
371 4 mPUSHi(ret);
372 }
373 }
374  
« blib/lib/HTTP/Parser/XS.pm ↑ Summary