XS.xs

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