HTTP1.xs

filter: Statementstmt 82.9% 860 / 1037 Branchbran 64.0% 692 / 1080 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.3% 1552 / 2117 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« vendor/picohttpparser/picohttpparser.c ↑ Summary blib/lib/Unblock/HTTP1.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "uniform_http_fastpath.h"
6 #include "unblock_http1_native_abi.h"
7  
8 #include "vendor/picohttpparser/picohttpparser.c"
9  
10 #define UB_HTTP1_MAX_HEADERS 256
11 #define UB_BODY_NONE 0
12 #define UB_BODY_CONTENT_LENGTH 1
13 #define UB_BODY_CHUNKED 2
14  
15 typedef struct {
16 uhttp_native_api uniform_api;
17 int initialized;
18 int compatible;
19 } my_cxt_t;
20 START_MY_CXT
21  
22 static int
23 689 ascii_equal_ci(const char *left, size_t left_len, const char *right, size_t right_len)
24 {
25 size_t i;
26 689 ▶ if (left_len != right_len)
Branch: 1 branch
BranchTrueFalseTotal
if (left_len != right_len) 398 291 689
27 398 return 0;
28 2619 ▶ for (i = 0; i < left_len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < left_len; ++i) { 2330 289 2619
29 2330 unsigned char a = (unsigned char)left[i];
30 2330 unsigned char b = (unsigned char)right[i];
31 2330 ▶ if (a >= 'A' && a <= 'Z') a = (unsigned char)(a + ('a' - 'A'));
Branch: 2 branches
BranchTrueFalseTotal
if (a >= 'A' && a <= 'Z') a = (unsigned char)(a + ('a' - 'A')); 2223 107 2330
if (a >= 'A' && a <= 'Z') a = (unsigned char)(a + ('a' - 'A')); 314 1909 2223
32 2330 ▶ if (b >= 'A' && b <= 'Z') b = (unsigned char)(b + ('a' - 'A'));
Branch: 2 branches
BranchTrueFalseTotal
if (b >= 'A' && b <= 'Z') b = (unsigned char)(b + ('a' - 'A')); 2223 107 2330
if (b >= 'A' && b <= 'Z') b = (unsigned char)(b + ('a' - 'A')); 310 1913 2223
33 2330 ▶ if (a != b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a != b) return 0; 2 2328 2330
34 }
35 289 return 1;
36 }
37  
38 static int
39 704 is_ows(unsigned char c)
40 {
41 704 ▶ return c == ' ' || c == '\t';
Branch: 2 branches
BranchTrueFalseTotal
return c == ' ' || c == '\t'; 692 12 704
return c == ' ' || c == '\t'; 0 692 692
42 }
43  
44 static int
45 3191 is_tchar(unsigned char c)
46 {
47 3191 ▶ if ((c >= '0' && c <= '9') ||
Branch: 3 branches
BranchTrueFalseTotal
if ((c >= '0' && c <= '9') || 3044 147 3191
if ((c >= '0' && c <= '9') || 3044 0 3044
if ((c >= '0' && c <= '9') || 3043 148 3191
48 3191 ▶ (c >= 'A' && c <= 'Z') ||
Branch: 2 branches
BranchTrueFalseTotal
(c >= 'A' && c <= 'Z') || 2557 486 3043
(c >= 'A' && c <= 'Z') || 2557 148 2705
49 2557 ▶ (c >= 'a' && c <= 'z'))
Branch: 1 branch
BranchTrueFalseTotal
(c >= 'a' && c <= 'z')) 2557 0 2557
50 3043 return 1;
51 148 ▶ switch (c) {
Branch: 1 branch
BranchTrueFalseTotal
switch (c) { 143 5 148
52 143 case '!': case '#': case '$': case '%': case '&': case '\'':
53 case '*': case '+': case '-': case '.': case '^': case '_':
54 case 0x60: case '|': case '~':
55 143 return 1;
56 5 default:
57 5 return 0;
58 }
59 }
60  
61 static int
62 330 valid_field_name(const char *name, size_t len)
63 {
64 size_t i;
65 330 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 330 330
66 3193 ▶ for (i = 0; i < len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) 2863 330 3193
67 2863 ▶ if (!is_tchar((unsigned char)name[i])) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_tchar((unsigned char)name[i])) return 0; 0 2863 2863
68 330 return 1;
69 }
70  
71 static int
72 395 valid_field_value(const char *value, size_t len)
73 {
74 size_t i;
75 3482 ▶ for (i = 0; i < len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) { 3087 395 3482
76 3087 unsigned char c = (unsigned char)value[i];
77 3087 ▶ if (c == '\t') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\t') continue; 0 3087 3087
78 3087 ▶ if (c < 0x20 || c == 0x7f) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c < 0x20 || c == 0x7f) return 0; 3087 0 3087
if (c < 0x20 || c == 0x7f) return 0; 0 3087 3087
79 }
80 395 return 1;
81 }
82  
83 static int
84 129 valid_host_value(const char *value, size_t len)
85 {
86 size_t i;
87 129 size_t close_bracket = (size_t)-1;
88 129 size_t colon_count = 0;
89 129 ▶ if (len == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 1; 0 129 129
90 1459 ▶ for (i = 0; i < len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) { 1331 128 1459
91 1331 unsigned char c = (unsigned char)value[i];
92 1331 ▶ if (c <= 0x20 || c >= 0x7f) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c <= 0x20 || c >= 0x7f) return 0; 1330 1 1331
if (c <= 0x20 || c >= 0x7f) return 0; 0 1330 1330
93 1330 ▶ if (c == '/' || c == '?' || c == '#' || c == '@') return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (c == '/' || c == '?' || c == '#' || c == '@') return 0; 1330 0 1330
if (c == '/' || c == '?' || c == '#' || c == '@') return 0; 1330 0 1330
if (c == '/' || c == '?' || c == '#' || c == '@') return 0; 1330 0 1330
if (c == '/' || c == '?' || c == '#' || c == '@') return 0; 0 1330 1330
94 }
95 128 ▶ if (value[0] == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (value[0] == '[') { 2 126 128
96 8 ▶ for (i = 1; i < len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; ++i) { 8 0 8
97 8 ▶ if (value[i] == ']') { close_bracket = i; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (value[i] == ']') { close_bracket = i; break; } 2 6 8
98 }
99 2 ▶ if (close_bracket == (size_t)-1 || close_bracket == 1) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (close_bracket == (size_t)-1 || close_bracket == 1) return 0; 2 0 2
if (close_bracket == (size_t)-1 || close_bracket == 1) return 0; 0 2 2
100 2 ▶ if (close_bracket + 1 == len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (close_bracket + 1 == len) return 1; 0 2 2
101 2 ▶ if (value[close_bracket + 1] != ':') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value[close_bracket + 1] != ':') return 0; 0 2 2
102 2 ▶ if (close_bracket + 2 == len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (close_bracket + 2 == len) return 1; 0 2 2
103 9 ▶ for (i = close_bracket + 2; i < len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = close_bracket + 2; i < len; ++i) 7 2 9
104 7 ▶ if (value[i] < '0' || value[i] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (value[i] < '0' || value[i] > '9') return 0; 7 0 7
if (value[i] < '0' || value[i] > '9') return 0; 0 7 7
105 2 return 1;
106 }
107 1405 ▶ for (i = 0; i < len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) { 1287 118 1405
108 1287 ▶ if (value[i] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (value[i] == ':') { 8 1279 1287
109 size_t j;
110 8 ++colon_count;
111 8 ▶ if (colon_count > 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (colon_count > 1) return 0; 0 8 8
112 29 ▶ for (j = i + 1; j < len; ++j)
Branch: 1 branch
BranchTrueFalseTotal
for (j = i + 1; j < len; ++j) 21 8 29
113 21 ▶ if (value[j] < '0' || value[j] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (value[j] < '0' || value[j] > '9') return 0; 21 0 21
if (value[j] < '0' || value[j] > '9') return 0; 0 21 21
114 8 break;
115 }
116 }
117 126 return 1;
118 }
119  
120 static int
121 604 ascii_equal_cs(const char *left, size_t left_len, const char *right, size_t right_len)
122 {
123 604 ▶ return left_len == right_len && memEQ(left, right, right_len);
Branch: 2 branches
BranchTrueFalseTotal
return left_len == right_len && memEQ(left, right, right_len); 170 434 604
return left_len == right_len && memEQ(left, right, right_len); 130 40 170
124 }
125  
126 static int
127 12 valid_port_number(const char *value, size_t len)
128 {
129 size_t i;
130 12 unsigned int port = 0;
131 12 ▶ if (len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return 0; 0 12 12
132 47 ▶ for (i = 0; i < len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) { 36 11 47
133 unsigned int digit;
134 36 ▶ if (value[i] < '0' || value[i] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (value[i] < '0' || value[i] > '9') return 0; 36 0 36
if (value[i] < '0' || value[i] > '9') return 0; 0 36 36
135 36 digit = (unsigned int)(value[i] - '0');
136 36 ▶ if (port > 6553 || (port == 6553 && digit > 5)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (port > 6553 || (port == 6553 && digit > 5)) return 0; 36 0 36
if (port > 6553 || (port == 6553 && digit > 5)) return 0; 1 35 36
if (port > 6553 || (port == 6553 && digit > 5)) return 0; 1 0 1
137 35 port = port * 10 + digit;
138 }
139 11 return port != 0;
140 }
141  
142 static int
143 14 valid_connect_authority(const char *target, size_t len)
144 {
145 size_t i;
146 14 ▶ if (len < 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 3) return 0; 0 14 14
147  
148 14 ▶ if (target[0] == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (target[0] == '[') { 1 13 14
149 1 size_t close_bracket = (size_t)-1;
150 4 ▶ for (i = 1; i < len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; ++i) { 4 0 4
151 4 ▶ if (target[i] == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (target[i] == ']') { 1 3 4
152 1 close_bracket = i;
153 1 break;
154 }
155 }
156 1 ▶ if (close_bracket == (size_t)-1 || close_bracket == 1) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (close_bracket == (size_t)-1 || close_bracket == 1) return 0; 1 0 1
if (close_bracket == (size_t)-1 || close_bracket == 1) return 0; 0 1 1
157 1 ▶ if (close_bracket + 2 >= len || target[close_bracket + 1] != ':')
Branch: 2 branches
BranchTrueFalseTotal
if (close_bracket + 2 >= len || target[close_bracket + 1] != ':') 1 0 1
if (close_bracket + 2 >= len || target[close_bracket + 1] != ':') 0 1 1
158 0 return 0;
159 4 ▶ for (i = 1; i < close_bracket; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < close_bracket; ++i) { 3 1 4
160 3 unsigned char ch = (unsigned char)target[i];
161 3 ▶ if (ch <= 0x20 || ch == 0x7f ||
Branch: 3 branches
BranchTrueFalseTotal
if (ch <= 0x20 || ch == 0x7f || 3 0 3
if (ch <= 0x20 || ch == 0x7f || 3 0 3
if (ch <= 0x20 || ch == 0x7f || 3 0 3
162 3 ▶ ch == '/' || ch == '?' || ch == '#' || ch == '@')
Branch: 3 branches
BranchTrueFalseTotal
ch == '/' || ch == '?' || ch == '#' || ch == '@') 3 0 3
ch == '/' || ch == '?' || ch == '#' || ch == '@') 3 0 3
ch == '/' || ch == '?' || ch == '#' || ch == '@') 0 3 3
163 0 return 0;
164 }
165 1 return valid_port_number(
166 1 target + close_bracket + 2,
167 1 len - close_bracket - 2
168 );
169 }
170  
171 {
172 13 size_t colon = (size_t)-1;
173 201 ▶ for (i = 0; i < len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; ++i) { 189 12 201
174 189 unsigned char ch = (unsigned char)target[i];
175 189 ▶ if (ch <= 0x20 || ch == 0x7f ||
Branch: 3 branches
BranchTrueFalseTotal
if (ch <= 0x20 || ch == 0x7f || 189 0 189
if (ch <= 0x20 || ch == 0x7f || 189 0 189
if (ch <= 0x20 || ch == 0x7f || 188 1 189
176 188 ▶ ch == '/' || ch == '?' || ch == '#' || ch == '@')
Branch: 3 branches
BranchTrueFalseTotal
ch == '/' || ch == '?' || ch == '#' || ch == '@') 188 0 188
ch == '/' || ch == '?' || ch == '#' || ch == '@') 188 0 188
ch == '/' || ch == '?' || ch == '#' || ch == '@') 0 188 188
177 1 return 0;
178 188 ▶ if (target[i] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (target[i] == ':') { 11 177 188
179 11 ▶ if (colon != (size_t)-1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (colon != (size_t)-1) return 0; 0 11 11
180 11 colon = i;
181 }
182 }
183 12 ▶ if (colon == (size_t)-1 || colon == 0 || colon + 1 == len) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (colon == (size_t)-1 || colon == 0 || colon + 1 == len) return 0; 11 1 12
if (colon == (size_t)-1 || colon == 0 || colon + 1 == len) return 0; 11 0 11
if (colon == (size_t)-1 || colon == 0 || colon + 1 == len) return 0; 0 11 11
184 11 return valid_port_number(target + colon + 1, len - colon - 1);
185 }
186 }
187  
188 static int
189 7 connect_host_matches_target(const char *host, size_t host_len,
190 const char *target, size_t target_len)
191 {
192 7 size_t target_host_len = 0;
193 7 size_t compare_host_len = host_len;
194 size_t i;
195  
196 7 ▶ if (ascii_equal_ci(host, host_len, target, target_len))
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_ci(host, host_len, target, target_len)) 5 2 7
197 5 return 1;
198  
199 2 ▶ if (host_len && host[host_len - 1] == ':')
Branch: 2 branches
BranchTrueFalseTotal
if (host_len && host[host_len - 1] == ':') 2 0 2
if (host_len && host[host_len - 1] == ':') 0 2 2
200 0 --compare_host_len;
201  
202 2 ▶ if (target_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (target_len == 0) 0 2 2
203 0 return 0;
204  
205 2 ▶ if (target[0] == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (target[0] == '[') { 0 2 2
206 0 ▶ for (i = 1; i < target_len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < target_len; ++i) { 0 0 0
207 0 ▶ if (target[i] == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (target[i] == ']') { 0 0 0
208 0 target_host_len = i + 1;
209 0 break;
210 }
211 }
212 } else {
213 26 ▶ for (i = 0; i < target_len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < target_len; ++i) { 26 0 26
214 26 ▶ if (target[i] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (target[i] == ':') { 2 24 26
215 2 target_host_len = i;
216 2 break;
217 }
218 }
219 }
220  
221 return target_host_len != 0
222 2 ▶ && ascii_equal_ci(host, compare_host_len, target, target_host_len);
Branch: 2 branches
BranchTrueFalseTotal
&& ascii_equal_ci(host, compare_host_len, target, target_host_len); 2 0 2
&& ascii_equal_ci(host, compare_host_len, target, target_host_len); 1 1 2
223 }
224  
225 static int
226 149 valid_request_target(const char *method, size_t method_len,
227 const char *target, size_t target_len)
228 {
229 size_t i;
230 149 ▶ if (target_len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (target_len == 0) return 0; 0 149 149
231  
232 752 ▶ for (i = 0; i < target_len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < target_len; ++i) 604 148 752
233 604 ▶ if (target[i] == '#') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (target[i] == '#') return 0; 1 603 604
234  
235 148 ▶ if (target_len == 1 && target[0] == '*')
Branch: 2 branches
BranchTrueFalseTotal
if (target_len == 1 && target[0] == '*') 90 58 148
if (target_len == 1 && target[0] == '*') 2 88 90
236 2 return ascii_equal_cs(method, method_len, "OPTIONS", 7);
237  
238 146 ▶ if (ascii_equal_cs(method, method_len, "CONNECT", 7))
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_cs(method, method_len, "CONNECT", 7)) 14 132 146
239 14 return valid_connect_authority(target, target_len);
240  
241 132 ▶ if (target[0] == '/') return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (target[0] == '/') return 1; 127 5 132
242  
243 5 ▶ if (!((target[0] >= 'A' && target[0] <= 'Z') ||
Branch: 2 branches
BranchTrueFalseTotal
if (!((target[0] >= 'A' && target[0] <= 'Z') || 5 0 5
if (!((target[0] >= 'A' && target[0] <= 'Z') || 5 0 5
244 5 ▶ (target[0] >= 'a' && target[0] <= 'z')))
Branch: 2 branches
BranchTrueFalseTotal
(target[0] >= 'a' && target[0] <= 'z'))) 5 0 5
(target[0] >= 'a' && target[0] <= 'z'))) 0 5 5
245 0 return 0;
246  
247 24 ▶ for (i = 1; i < target_len; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < target_len; ++i) { 24 0 24
248 24 unsigned char ch = (unsigned char)target[i];
249 24 ▶ if (ch == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == ':') { 4 20 24
250 4 if (ascii_equal_ci(target, i, "http", 4) ||
251 0 ascii_equal_ci(target, i, "https", 5)) {
252 4 size_t pos = i + 1;
253 size_t authority_start;
254  
255 4 ▶ if (pos + 1 >= target_len ||
Branch: 1 branch
BranchTrueFalseTotal
if (pos + 1 >= target_len || 4 0 4
256 4 ▶ target[pos] != '/' || target[pos + 1] != '/')
Branch: 2 branches
BranchTrueFalseTotal
target[pos] != '/' || target[pos + 1] != '/') 3 1 4
target[pos] != '/' || target[pos + 1] != '/') 0 3 3
257 1 return 0;
258  
259 3 pos += 2;
260 3 authority_start = pos;
261 3 while (pos < target_len &&
262 22 ▶ target[pos] != '/' && target[pos] != '?') {
Branch: 3 branches
BranchTrueFalseTotal
target[pos] != '/' && target[pos] != '?') { 22 0 22
target[pos] != '/' && target[pos] != '?') { 20 2 22
target[pos] != '/' && target[pos] != '?') { 20 0 20
263 20 ▶ if (target[pos] == '@')
Branch: 1 branch
BranchTrueFalseTotal
if (target[pos] == '@') 1 19 20
264 1 return 0;
265 19 ++pos;
266 }
267 2 ▶ if (pos == authority_start || target[authority_start] == ':')
Branch: 2 branches
BranchTrueFalseTotal
if (pos == authority_start || target[authority_start] == ':') 2 0 2
if (pos == authority_start || target[authority_start] == ':') 1 1 2
268 1 return 0;
269 }
270 1 return 1;
271 }
272 21 ▶ if (!((ch >= 'A' && ch <= 'Z') ||
Branch: 4 branches
BranchTrueFalseTotal
if (!((ch >= 'A' && ch <= 'Z') || 19 1 20
if (!((ch >= 'A' && ch <= 'Z') || 19 0 19
if (!((ch >= 'A' && ch <= 'Z') || 19 1 20
if (!((ch >= 'A' && ch <= 'Z') || 1 0 1
273 20 ▶ (ch >= 'a' && ch <= 'z') ||
Branch: 2 branches
BranchTrueFalseTotal
(ch >= 'a' && ch <= 'z') || 0 19 19
(ch >= 'a' && ch <= 'z') || 0 1 1
274 1 ▶ (ch >= '0' && ch <= '9') ||
Branch: 2 branches
BranchTrueFalseTotal
(ch >= '0' && ch <= '9') || 0 0 0
(ch >= '0' && ch <= '9') || 1 0 1
275 1 ▶ ch == '+' || ch == '-' || ch == '.'))
Branch: 1 branch
BranchTrueFalseTotal
ch == '+' || ch == '-' || ch == '.')) 1 0 1
276 1 return 0;
277 }
278 0 return 0;
279 }
280  
281 static int
282 41 parse_content_length(const char *value, size_t len, UV *out, int *seen)
283 {
284 41 size_t pos = 0;
285 41 int members = 0;
286 41 const UV uv_max = ~(UV)0;
287 2 while (1) {
288 43 UV parsed = 0;
289 43 int digits = 0;
290 45 ▶ while (pos < len && is_ows((unsigned char)value[pos])) ++pos;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 45 0 45
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 2 43 45
291 43 ▶ if (pos == len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pos == len) return 0; 0 43 43
292 103 ▶ while (pos < len && value[pos] >= '0' && value[pos] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (pos < len && value[pos] >= '0' && value[pos] <= '9') { 64 39 103
while (pos < len && value[pos] >= '0' && value[pos] <= '9') { 61 3 64
while (pos < len && value[pos] >= '0' && value[pos] <= '9') { 61 0 61
293 61 UV digit = (UV)(value[pos] - '0');
294 61 ▶ if (parsed > uv_max / 10 ||
Branch: 1 branch
BranchTrueFalseTotal
if (parsed > uv_max / 10 || 61 0 61
295 61 ▶ (parsed == uv_max / 10 && digit > uv_max % 10))
Branch: 2 branches
BranchTrueFalseTotal
(parsed == uv_max / 10 && digit > uv_max % 10)) 1 60 61
(parsed == uv_max / 10 && digit > uv_max % 10)) 1 0 1
296 1 return 0;
297 60 parsed = parsed * 10 + digit;
298 60 ++pos;
299 60 ++digits;
300 }
301 42 ▶ if (!digits) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!digits) return 0; 1 41 42
302 41 ▶ while (pos < len && is_ows((unsigned char)value[pos])) ++pos;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 2 39 41
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 0 2 2
303 41 ▶ if (*seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (*seen) { 4 37 41
304 4 ▶ if (*out != parsed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*out != parsed) return 0; 2 2 4
305 } else {
306 37 *out = parsed;
307 37 *seen = 1;
308 }
309 39 ++members;
310 39 ▶ if (pos == len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (pos == len) break; 37 2 39
311 2 ▶ if (value[pos] != ',') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (value[pos] != ',') return 0; 0 2 2
312 2 ++pos;
313 }
314 37 return members != 0;
315 }
316  
317 static int
318 22 parse_connection(const char *value, size_t len, int *close_seen, int *keep_seen)
319 {
320 22 size_t pos = 0;
321 50 ▶ while (pos < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < len) { 29 21 50
322 size_t start, end, i;
323 36 ▶ while (pos < len && is_ows((unsigned char)value[pos])) ++pos;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 36 0 36
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 7 29 36
324 29 start = pos;
325 203 ▶ while (pos < len && value[pos] != ',') ++pos;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && value[pos] != ',') ++pos; 182 21 203
while (pos < len && value[pos] != ',') ++pos; 174 8 182
326 29 end = pos;
327 29 ▶ while (end > start && is_ows((unsigned char)value[end - 1])) --end;
Branch: 2 branches
BranchTrueFalseTotal
while (end > start && is_ows((unsigned char)value[end - 1])) --end; 27 2 29
while (end > start && is_ows((unsigned char)value[end - 1])) --end; 0 27 27
328 29 ▶ if (end > start) {
Branch: 1 branch
BranchTrueFalseTotal
if (end > start) { 27 2 29
329 195 ▶ for (i = start; i < end; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = start; i < end; ++i) 169 26 195
330 169 ▶ if (!is_tchar((unsigned char)value[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (!is_tchar((unsigned char)value[i])) 1 168 169
331 1 return 0;
332 26 ▶ if (ascii_equal_ci(value + start, end - start, "close", 5))
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_ci(value + start, end - start, "close", 5)) 9 17 26
333 9 *close_seen = 1;
334 17 ▶ else if (ascii_equal_ci(value + start, end - start, "keep-alive", 10))
Branch: 1 branch
BranchTrueFalseTotal
else if (ascii_equal_ci(value + start, end - start, "keep-alive", 10)) 5 12 17
335 5 *keep_seen = 1;
336 }
337 28 ▶ if (pos < len) ++pos;
Branch: 1 branch
BranchTrueFalseTotal
if (pos < len) ++pos; 8 20 28
338 }
339 21 return 1;
340 }
341  
342 static int
343 20 parse_transfer_encoding(const char *value, size_t len,
344 int *count, int *chunked_count,
345 int *final_chunked, int *unsupported)
346 {
347 20 size_t pos = 0;
348 3 while (1) {
349 size_t start, token_end, member_end, tail;
350 int chunked;
351 26 ▶ while (pos < len && is_ows((unsigned char)value[pos])) ++pos;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 26 0 26
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 3 23 26
352 23 ▶ if (pos == len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pos == len) return 0; 0 23 23
353 23 start = pos;
354 178 ▶ while (pos < len && is_tchar((unsigned char)value[pos])) ++pos;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && is_tchar((unsigned char)value[pos])) ++pos; 159 19 178
while (pos < len && is_tchar((unsigned char)value[pos])) ++pos; 155 4 159
355 23 token_end = pos;
356 23 ▶ if (token_end == start) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (token_end == start) return 0; 0 23 23
357 31 ▶ while (pos < len && value[pos] != ',') ++pos;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && value[pos] != ',') ++pos; 11 20 31
while (pos < len && value[pos] != ',') ++pos; 8 3 11
358 23 member_end = pos;
359 23 ▶ while (member_end > token_end && is_ows((unsigned char)value[member_end - 1])) --member_end;
Branch: 2 branches
BranchTrueFalseTotal
while (member_end > token_end && is_ows((unsigned char)value[member_end - 1])) --member_end; 1 22 23
while (member_end > token_end && is_ows((unsigned char)value[member_end - 1])) --member_end; 0 1 1
360 23 chunked = ascii_equal_ci(value + start, token_end - start, "chunked", 7);
361 23 ▶ if (chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunked) { 21 2 23
362 21 tail = token_end;
363 21 ▶ while (tail < member_end && is_ows((unsigned char)value[tail])) ++tail;
Branch: 2 branches
BranchTrueFalseTotal
while (tail < member_end && is_ows((unsigned char)value[tail])) ++tail; 1 20 21
while (tail < member_end && is_ows((unsigned char)value[tail])) ++tail; 0 1 1
364 21 ▶ if (tail != member_end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (tail != member_end) return 0; 1 20 21
365 20 ++*chunked_count;
366 } else {
367 2 *unsupported = 1;
368 }
369 22 ++*count;
370 22 *final_chunked = chunked;
371 22 ▶ if (pos == len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (pos == len) break; 19 3 22
372 3 ++pos;
373 }
374 19 return *count != 0;
375 }
376  
377 static int
378 3 parse_expect(const char *value, size_t len)
379 {
380 3 size_t pos = 0;
381 3 int members = 0;
382 0 while (1) {
383 size_t start, end;
384 3 ▶ while (pos < len && is_ows((unsigned char)value[pos])) ++pos;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 3 0 3
while (pos < len && is_ows((unsigned char)value[pos])) ++pos; 0 3 3
385 3 start = pos;
386 41 ▶ while (pos < len && value[pos] != ',') ++pos;
Branch: 2 branches
BranchTrueFalseTotal
while (pos < len && value[pos] != ',') ++pos; 38 3 41
while (pos < len && value[pos] != ',') ++pos; 38 0 38
387 3 end = pos;
388 3 ▶ while (end > start && is_ows((unsigned char)value[end - 1])) --end;
Branch: 2 branches
BranchTrueFalseTotal
while (end > start && is_ows((unsigned char)value[end - 1])) --end; 3 0 3
while (end > start && is_ows((unsigned char)value[end - 1])) --end; 0 3 3
389 3 ▶ if (end == start || !ascii_equal_ci(value + start, end - start, "100-continue", 12))
Branch: 2 branches
BranchTrueFalseTotal
if (end == start || !ascii_equal_ci(value + start, end - start, "100-continue", 12)) 3 0 3
if (end == start || !ascii_equal_ci(value + start, end - start, "100-continue", 12)) 1 2 3
390 1 return -1;
391 2 ++members;
392 2 ▶ if (pos == len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (pos == len) break; 2 0 2
393 0 ++pos;
394 }
395 2 ▶ return members ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
return members ? 1 : -1; 2 0 2
396 }
397  
398 static SV *
399 33 new_error_result(pTHX_ int status, const char *message)
400 {
401 33 HV *hv = newHV();
402 33 hv_store(hv, "ok", 2, newSViv(0), 0);
403 33 ▶ if (status) hv_store(hv, "status", 6, newSViv(status), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (status) hv_store(hv, "status", 6, newSViv(status), 0); 33 0 33
404 33 hv_store(hv, "error", 5, newSVpv(message, 0), 0);
405 33 return newRV_noinc((SV *)hv);
406 }
407  
408 static AV *
409 177 headers_to_av(pTHX_ const struct phr_header *headers, size_t count)
410 {
411 177 AV *out = newAV();
412 size_t i;
413 421 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 244 177 421
414 244 const char *value = headers[i].value;
415 244 size_t value_len = headers[i].value_len;
416 AV *pair;
417 244 ▶ while (value_len && is_ows((unsigned char)*value)) { ++value; --value_len; }
Branch: 2 branches
BranchTrueFalseTotal
while (value_len && is_ows((unsigned char)*value)) { ++value; --value_len; } 244 0 244
while (value_len && is_ows((unsigned char)*value)) { ++value; --value_len; } 0 244 244
418 244 ▶ while (value_len && is_ows((unsigned char)value[value_len - 1])) --value_len;
Branch: 2 branches
BranchTrueFalseTotal
while (value_len && is_ows((unsigned char)value[value_len - 1])) --value_len; 244 0 244
while (value_len && is_ows((unsigned char)value[value_len - 1])) --value_len; 0 244 244
419 244 pair = newAV();
420 244 av_push(pair, newSVpvn(headers[i].name, (STRLEN)headers[i].name_len));
421 244 av_push(pair, newSVpvn(value, (STRLEN)value_len));
422 244 av_push(out, newRV_noinc((SV *)pair));
423 }
424 177 return out;
425 }
426  
427 static int
428 231 strict_headers(const struct phr_header *headers, size_t count)
429 {
430 size_t i;
431 561 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 331 230 561
432 661 if (headers[i].name == NULL ||
433 660 ▶ !valid_field_name(headers[i].name, headers[i].name_len) ||
Branch: 1 branch
BranchTrueFalseTotal
!valid_field_name(headers[i].name, headers[i].name_len) || 0 330 330
434 330 !valid_field_value(headers[i].value, headers[i].value_len))
435 1 return 0;
436 }
437 230 return 1;
438 }
439  
440  
441 static SV *
442 322 ub_http1_parse_request_head_result(
443 pTHX_
444 const char *buf,
445 size_t buffer_len,
446 size_t last_len,
447 size_t max_headers,
448 const uhttp_native_api *uniform_api,
449 SV **request_out
450 )
451 {
452 const char *method;
453 size_t method_len;
454 const char *target;
455 size_t target_len;
456 int minor;
457 struct phr_header headers[UB_HTTP1_MAX_HEADERS];
458 size_t count;
459 int consumed;
460 size_t i;
461 322 int host_count = 0;
462 322 int has_cl = 0;
463 322 UV content_length = 0;
464 322 int te_present = 0;
465 322 int te_count = 0;
466 322 int chunked_count = 0;
467 322 int final_chunked = 0;
468 322 int unsupported = 0;
469 322 int close_seen = 0;
470 322 int keep_seen = 0;
471 322 int expect_mode = 0;
472 322 int body_mode = UB_BODY_NONE;
473 322 int is_connect = 0;
474 322 const char *host_value = NULL;
475 322 size_t host_value_len = 0;
476 uhttp_native_field native_headers[UB_HTTP1_MAX_HEADERS];
477 char version_bytes[3];
478 322 SV *native_request = NULL;
479 HV *hv;
480 AV *list;
481  
482 322 ▶ if (request_out != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (request_out != NULL) 18 304 322
483 18 *request_out = NULL;
484  
485 322 ▶ if (last_len > buffer_len)
Branch: 1 branch
BranchTrueFalseTotal
if (last_len > buffer_len) 0 322 322
486 0 croak("last_len exceeds input window length");
487 322 ▶ if (max_headers == 0 || max_headers > UB_HTTP1_MAX_HEADERS)
Branch: 2 branches
BranchTrueFalseTotal
if (max_headers == 0 || max_headers > UB_HTTP1_MAX_HEADERS) 322 0 322
if (max_headers == 0 || max_headers > UB_HTTP1_MAX_HEADERS) 0 322 322
488 0 croak("max_headers must be between 1 and %d", UB_HTTP1_MAX_HEADERS);
489  
490 322 count = max_headers;
491 322 consumed = phr_parse_request(
492 buf, buffer_len,
493 &method, &method_len, &target, &target_len, &minor,
494 headers, &count, last_len
495 );
496  
497 322 ▶ if (consumed == -2)
Branch: 1 branch
BranchTrueFalseTotal
if (consumed == -2) 171 151 322
498 171 return NULL;
499  
500 151 ▶ if (consumed == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (consumed == -1) { 1 150 151
501 1 ▶ if (count == max_headers)
Branch: 1 branch
BranchTrueFalseTotal
if (count == max_headers) 1 0 1
502 1 return new_error_result(
503 aTHX_ 431, "too many HTTP/1 request header fields"
504 );
505 0 return new_error_result(aTHX_ 400, "malformed HTTP/1 request");
506 }
507  
508 150 ▶ if (minor < 0 || minor > 9)
Branch: 2 branches
BranchTrueFalseTotal
if (minor < 0 || minor > 9) 150 0 150
if (minor < 0 || minor > 9) 0 150 150
509 0 return new_error_result(aTHX_ 505, "unsupported HTTP/1 version");
510  
511 150 ▶ if (!strict_headers(headers, count))
Branch: 1 branch
BranchTrueFalseTotal
if (!strict_headers(headers, count)) 1 149 150
512 1 return new_error_result(
513 aTHX_ 400, "invalid or folded HTTP/1 header field"
514 );
515  
516 {
517 149 const char *error = NULL;
518 149 int error_status = 0;
519  
520 149 is_connect = ascii_equal_cs(method, method_len, "CONNECT", 7);
521 149 ▶ if (!valid_request_target(method, method_len, target, target_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_request_target(method, method_len, target, target_len)) { 10 139 149
522 10 error = "invalid HTTP/1 request target";
523 10 error_status = 400;
524 }
525 149 ▶ if (!error && is_connect && minor == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (!error && is_connect && minor == 0) { 139 10 149
if (!error && is_connect && minor == 0) { 10 129 139
if (!error && is_connect && minor == 0) { 1 9 10
526 1 error = "CONNECT requires HTTP/1.1 semantics";
527 1 error_status = 400;
528 }
529  
530 380 ▶ for (i = 0; i < count && !error; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < count && !error; ++i) { 242 138 380
for (i = 0; i < count && !error; ++i) { 231 11 242
531 231 const char *name = headers[i].name;
532 231 size_t name_len = headers[i].name_len;
533 231 const char *value = headers[i].value;
534 231 size_t value_len = headers[i].value_len;
535  
536 231 ▶ if (ascii_equal_ci(name, name_len, "Host", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_ci(name, name_len, "Host", 4)) { 129 102 231
537 129 ++host_count;
538 129 ▶ if (host_count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (host_count == 1) { 128 1 129
539 128 host_value = value;
540 128 host_value_len = value_len;
541 }
542 129 ▶ if (!valid_host_value(value, value_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_host_value(value, value_len)) { 1 128 129
543 1 error = "invalid Host field";
544 1 error_status = 400;
545 }
546 102 ▶ } else if (ascii_equal_ci(
Branch: 1 branch
BranchTrueFalseTotal
} else if (ascii_equal_ci( 41 61 102
547 name, name_len, "Content-Length", 14)) {
548 41 ▶ if (!parse_content_length(
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_content_length( 4 37 41
549 value, value_len,
550 &content_length, &has_cl)) {
551 4 error = "invalid or conflicting Content-Length";
552 4 error_status = 400;
553 }
554 61 ▶ } else if (ascii_equal_ci(
Branch: 1 branch
BranchTrueFalseTotal
} else if (ascii_equal_ci( 20 41 61
555 name, name_len, "Transfer-Encoding", 17)) {
556 20 te_present = 1;
557 20 ▶ if (!parse_transfer_encoding(
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_transfer_encoding( 1 19 20
558 value, value_len,
559 &te_count, &chunked_count,
560 &final_chunked, &unsupported)) {
561 1 error = "invalid Transfer-Encoding";
562 1 error_status = 400;
563 }
564 41 ▶ } else if (ascii_equal_ci(
Branch: 1 branch
BranchTrueFalseTotal
} else if (ascii_equal_ci( 22 19 41
565 name, name_len, "Connection", 10)) {
566 22 ▶ if (!parse_connection(
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_connection( 1 21 22
567 value, value_len, &close_seen, &keep_seen)) {
568 1 error = "invalid Connection field";
569 1 error_status = 400;
570 }
571 19 ▶ } else if (ascii_equal_ci(name, name_len, "Expect", 6)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ascii_equal_ci(name, name_len, "Expect", 6)) { 3 16 19
572 3 int e = parse_expect(value, value_len);
573 3 ▶ if (minor == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (minor == 0) { 1 2 3
574 1 expect_mode = 0;
575 2 ▶ } else if (e < 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (e < 0) { 1 1 2
576 1 expect_mode = -1;
577 1 ▶ } else if (expect_mode >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (expect_mode >= 0) { 1 0 1
578 1 expect_mode = 1;
579 }
580 }
581 }
582  
583 149 ▶ if (!error && host_count > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && host_count > 1) { 131 18 149
if (!error && host_count > 1) { 1 130 131
584 1 error = "multiple Host fields";
585 1 error_status = 400;
586 }
587 149 ▶ if (!error && minor >= 1 && host_count != 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (!error && minor >= 1 && host_count != 1) { 130 19 149
if (!error && minor >= 1 && host_count != 1) { 122 8 130
if (!error && minor >= 1 && host_count != 1) { 2 120 122
588 2 error = "HTTP/1.1 semantics require exactly one Host field";
589 2 error_status = 400;
590 }
591 149 ▶ if (!error && te_present && has_cl) {
Branch: 3 branches
BranchTrueFalseTotal
if (!error && te_present && has_cl) { 128 21 149
if (!error && te_present && has_cl) { 19 109 128
if (!error && te_present && has_cl) { 3 16 19
592 3 error = "Transfer-Encoding and Content-Length cannot be combined";
593 3 error_status = 400;
594 }
595 149 ▶ if (!error && minor == 0 && te_present) {
Branch: 3 branches
BranchTrueFalseTotal
if (!error && minor == 0 && te_present) { 125 24 149
if (!error && minor == 0 && te_present) { 8 117 125
if (!error && minor == 0 && te_present) { 1 7 8
596 1 error = "HTTP/1.0 request must not contain Transfer-Encoding";
597 1 error_status = 400;
598 }
599 149 ▶ if (!error && is_connect
Branch: 2 branches
BranchTrueFalseTotal
if (!error && is_connect 124 25 149
if (!error && is_connect 9 115 124
600 9 ▶ && (te_present || (has_cl && content_length != 0))) {
Branch: 3 branches
BranchTrueFalseTotal
&& (te_present || (has_cl && content_length != 0))) { 8 1 9
&& (te_present || (has_cl && content_length != 0))) { 2 6 8
&& (te_present || (has_cl && content_length != 0))) { 1 1 2
601 2 error = "CONNECT request must not contain content framing";
602 2 error_status = 400;
603 }
604 149 ▶ if (!error && is_connect && host_count == 1
Branch: 3 branches
BranchTrueFalseTotal
if (!error && is_connect && host_count == 1 122 27 149
if (!error && is_connect && host_count == 1 7 115 122
if (!error && is_connect && host_count == 1 7 0 7
605 7 ▶ && !connect_host_matches_target(
Branch: 1 branch
BranchTrueFalseTotal
&& !connect_host_matches_target( 1 6 7
606 host_value, host_value_len, target, target_len)) {
607 1 error = "CONNECT Host must identify request target";
608 1 error_status = 400;
609 }
610  
611 149 ▶ if (!error && te_present) {
Branch: 2 branches
BranchTrueFalseTotal
if (!error && te_present) { 121 28 149
if (!error && te_present) { 14 107 121
612 14 ▶ if (chunked_count != 1 || !final_chunked) {
Branch: 2 branches
BranchTrueFalseTotal
if (chunked_count != 1 || !final_chunked) { 13 1 14
if (chunked_count != 1 || !final_chunked) { 1 12 13
613 2 error =
614 "chunked must be the final and only chunked transfer coding";
615 2 error_status = 400;
616 12 ▶ } else if (unsupported) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (unsupported) { 1 11 12
617 1 error = "unsupported transfer coding";
618 1 error_status = 501;
619 } else {
620 11 body_mode = UB_BODY_CHUNKED;
621 }
622 135 ▶ } else if (!error && has_cl) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (!error && has_cl) { 107 28 135
} else if (!error && has_cl) { 31 76 107
623 31 body_mode = (is_connect && content_length == 0)
624 31 ▶ ? UB_BODY_NONE : UB_BODY_CONTENT_LENGTH;
Branch: 2 branches
BranchTrueFalseTotal
? UB_BODY_NONE : UB_BODY_CONTENT_LENGTH; 1 30 31
? UB_BODY_NONE : UB_BODY_CONTENT_LENGTH; 0 1 1
625 }
626  
627 149 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 31 118 149
628 31 return new_error_result(aTHX_ error_status, error);
629 }
630  
631 118 ▶ if (uniform_api != NULL && request_out != NULL
Branch: 2 branches
BranchTrueFalseTotal
if (uniform_api != NULL && request_out != NULL 17 101 118
if (uniform_api != NULL && request_out != NULL 17 0 17
632 17 ▶ && !is_connect && target_len > 0
Branch: 2 branches
BranchTrueFalseTotal
&& !is_connect && target_len > 0 17 0 17
&& !is_connect && target_len > 0 17 0 17
633 17 ▶ && (target[0] == '/'
Branch: 1 branch
BranchTrueFalseTotal
&& (target[0] == '/' 0 17 17
634 0 ▶ || (target_len == 1 && target[0] == '*'))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (target_len == 1 && target[0] == '*'))) { 0 0 0
|| (target_len == 1 && target[0] == '*'))) { 0 0 0
635 uhttp_native_input input;
636  
637 17 uhttp_native_input_init(&input, UHTTP_KIND_REQUEST);
638 17 input.flags =
639 UHTTP_HEADERS_LOSSLESS
640 | UHTTP_TRAILERS_LOSSLESS
641 | UHTTP_TARGET_EXACT;
642  
643 17 ▶ if (body_mode == UB_BODY_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_mode == UB_BODY_NONE) { 8 9 17
644 8 input.flags |= UHTTP_COMPLETE | UHTTP_HAS_BUFFERED_BODY;
645 8 input.body.data = "";
646 8 input.body.len = 0;
647 } else {
648 9 input.flags |=
649 UHTTP_MUTABLE
650 | UHTTP_BODY_MUTABLE
651 | UHTTP_TRAILERS_MUTABLE;
652 }
653  
654 17 version_bytes[0] = '1';
655 17 version_bytes[1] = '.';
656 17 version_bytes[2] = (char)('0' + minor);
657 17 input.version.data = version_bytes;
658 17 input.version.len = 3;
659 17 input.method.data = method;
660 17 input.method.len = (STRLEN)method_len;
661 17 input.target.data = target;
662 17 input.target.len = (STRLEN)target_len;
663  
664 47 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 30 17 47
665 30 const char *value = headers[i].value;
666 30 size_t value_len = headers[i].value_len;
667  
668 30 ▶ while (value_len && is_ows((unsigned char)*value)) {
Branch: 2 branches
BranchTrueFalseTotal
while (value_len && is_ows((unsigned char)*value)) { 30 0 30
while (value_len && is_ows((unsigned char)*value)) { 0 30 30
669 0 ++value;
670 0 --value_len;
671 }
672 30 while (value_len
673 30 ▶ && is_ows((unsigned char)value[value_len - 1]))
Branch: 2 branches
BranchTrueFalseTotal
&& is_ows((unsigned char)value[value_len - 1])) 30 0 30
&& is_ows((unsigned char)value[value_len - 1])) 0 30 30
674 0 --value_len;
675  
676 30 native_headers[i].name.data = headers[i].name;
677 30 native_headers[i].name.len = (STRLEN)headers[i].name_len;
678 30 native_headers[i].value.data = value;
679 30 native_headers[i].value.len = (STRLEN)value_len;
680 }
681  
682 17 input.headers = native_headers;
683 17 input.header_count = (Size_t)count;
684 17 native_request = uhttp_native_from_validated(
685 aTHX_ uniform_api, &input, UHTTP_NATIVE_TRUSTED
686 );
687 17 *request_out = native_request;
688 }
689  
690 118 hv = newHV();
691 118 hv_store(hv, "ok", 2, newSViv(1), 0);
692 118 hv_store(hv, "consumed", 8, newSViv(consumed), 0);
693 118 ▶ if (native_request == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (native_request == NULL) { 101 17 118
694 101 hv_store(hv, "version", 7, newSVpvf("1.%d", minor), 0);
695 101 hv_store(hv, "method", 6, newSVpvn(method, (STRLEN)method_len), 0);
696 101 hv_store(hv, "target", 6, newSVpvn(target, (STRLEN)target_len), 0);
697 101 list = headers_to_av(aTHX_ headers, count);
698 101 hv_store(hv, "headers", 7, newRV_noinc((SV *)list), 0);
699 }
700  
701 118 ▶ if (body_mode == UB_BODY_CHUNKED)
Branch: 1 branch
BranchTrueFalseTotal
if (body_mode == UB_BODY_CHUNKED) 11 107 118
702 11 hv_store(hv, "body_mode", 9, newSVpvs("chunked"), 0);
703 107 ▶ else if (body_mode == UB_BODY_CONTENT_LENGTH)
Branch: 1 branch
BranchTrueFalseTotal
else if (body_mode == UB_BODY_CONTENT_LENGTH) 30 77 107
704 30 hv_store(hv, "body_mode", 9, newSVpvs("content-length"), 0);
705 else
706 77 hv_store(hv, "body_mode", 9, newSVpvs("none"), 0);
707  
708 118 ▶ if (has_cl)
Branch: 1 branch
BranchTrueFalseTotal
if (has_cl) 31 87 118
709 31 hv_store(hv, "content_length", 14, newSVuv(content_length), 0);
710  
711 118 ▶ hv_store(
Branch: 3 branches
BranchTrueFalseTotal
hv_store( 109 9 118
hv_store( 7 102 109
hv_store( 3 4 7
712 hv, "keep_alive", 10,
713 newSViv(close_seen ? 0 : (minor >= 1 ? 1 : (keep_seen ? 1 : 0))),
714 0
715 );
716 118 hv_store(hv, "expect_continue", 15, newSViv(expect_mode), 0);
717  
718 118 return newRV_noinc((SV *)hv);
719 }
720  
721 static SV *
722 179 ub_http1_parse_response_head_result(
723 pTHX_
724 const char *buf,
725 size_t buffer_len,
726 size_t last_len,
727 size_t max_headers,
728 const uhttp_native_api *uniform_api,
729 SV **response_out
730 )
731 {
732 int minor;
733 int status;
734 const char *reason;
735 size_t reason_len;
736 struct phr_header headers[UB_HTTP1_MAX_HEADERS];
737 uhttp_native_field native_headers[UB_HTTP1_MAX_HEADERS];
738 size_t count;
739 int consumed;
740 size_t i;
741 char version_bytes[3];
742 179 SV *native_response = NULL;
743 HV *hv;
744 AV *list;
745  
746 179 ▶ if (response_out != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (response_out != NULL) 6 173 179
747 6 *response_out = NULL;
748 179 ▶ if (last_len > buffer_len)
Branch: 1 branch
BranchTrueFalseTotal
if (last_len > buffer_len) 0 179 179
749 0 croak("last_len exceeds input window length");
750 179 ▶ if (max_headers == 0 || max_headers > UB_HTTP1_MAX_HEADERS)
Branch: 2 branches
BranchTrueFalseTotal
if (max_headers == 0 || max_headers > UB_HTTP1_MAX_HEADERS) 179 0 179
if (max_headers == 0 || max_headers > UB_HTTP1_MAX_HEADERS) 0 179 179
751 0 croak("max_headers must be between 1 and %d", UB_HTTP1_MAX_HEADERS);
752  
753 179 count = max_headers;
754 179 consumed = phr_parse_response(
755 buf, buffer_len, &minor, &status,
756 &reason, &reason_len, headers, &count, last_len
757 );
758  
759 179 ▶ if (consumed == -2)
Branch: 1 branch
BranchTrueFalseTotal
if (consumed == -2) 114 65 179
760 114 return NULL;
761  
762 65 ▶ if (consumed == -1 || minor < 0 || minor > 9
Branch: 3 branches
BranchTrueFalseTotal
if (consumed == -1 || minor < 0 || minor > 9 65 0 65
if (consumed == -1 || minor < 0 || minor > 9 65 0 65
if (consumed == -1 || minor < 0 || minor > 9 65 0 65
763 65 ▶ || status < 100 || status > 599
Branch: 2 branches
BranchTrueFalseTotal
|| status < 100 || status > 599 65 0 65
|| status < 100 || status > 599 65 0 65
764 65 ▶ || buffer_len < 13 || !memEQ(buf, "HTTP/1.", 7)
Branch: 2 branches
BranchTrueFalseTotal
|| buffer_len < 13 || !memEQ(buf, "HTTP/1.", 7) 65 0 65
|| buffer_len < 13 || !memEQ(buf, "HTTP/1.", 7) 65 0 65
765 65 ▶ || buf[7] < '0' || buf[7] > '9' || buf[8] != ' '
Branch: 3 branches
BranchTrueFalseTotal
|| buf[7] < '0' || buf[7] > '9' || buf[8] != ' ' 65 0 65
|| buf[7] < '0' || buf[7] > '9' || buf[8] != ' ' 65 0 65
|| buf[7] < '0' || buf[7] > '9' || buf[8] != ' ' 65 0 65
766 65 ▶ || buf[9] < '0' || buf[9] > '9'
Branch: 2 branches
BranchTrueFalseTotal
|| buf[9] < '0' || buf[9] > '9' 65 0 65
|| buf[9] < '0' || buf[9] > '9' 65 0 65
767 65 ▶ || buf[10] < '0' || buf[10] > '9'
Branch: 2 branches
BranchTrueFalseTotal
|| buf[10] < '0' || buf[10] > '9' 65 0 65
|| buf[10] < '0' || buf[10] > '9' 65 0 65
768 65 ▶ || buf[11] < '0' || buf[11] > '9' || buf[12] != ' '
Branch: 3 branches
BranchTrueFalseTotal
|| buf[11] < '0' || buf[11] > '9' || buf[12] != ' ' 65 0 65
|| buf[11] < '0' || buf[11] > '9' || buf[12] != ' ' 65 0 65
|| buf[11] < '0' || buf[11] > '9' || buf[12] != ' ' 65 0 65
769 65 ▶ || reason != buf + 13
Branch: 1 branch
BranchTrueFalseTotal
|| reason != buf + 13 65 0 65
770 65 ▶ || !valid_field_value(reason, reason_len)
Branch: 1 branch
BranchTrueFalseTotal
|| !valid_field_value(reason, reason_len) 65 0 65
771 65 ▶ || !strict_headers(headers, count)) {
Branch: 1 branch
BranchTrueFalseTotal
|| !strict_headers(headers, count)) { 0 65 65
772 0 return new_error_result(aTHX_ 0, "malformed HTTP/1 response");
773 }
774  
775 65 ▶ if (uniform_api != NULL && response_out != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (uniform_api != NULL && response_out != NULL) { 5 60 65
if (uniform_api != NULL && response_out != NULL) { 5 0 5
776 uhttp_native_input input;
777 5 int informational =
778 5 ▶ status >= 100 && status < 200 && status != 101 ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
status >= 100 && status < 200 && status != 101 ? 1 : 0; 5 0 5
status >= 100 && status < 200 && status != 101 ? 1 : 0; 2 3 5
status >= 100 && status < 200 && status != 101 ? 1 : 0; 1 1 2
779  
780 5 uhttp_native_input_init(&input, UHTTP_KIND_RESPONSE);
781 5 input.flags =
782 UHTTP_HEADERS_LOSSLESS
783 | UHTTP_TRAILERS_LOSSLESS;
784  
785 5 ▶ if (informational) {
Branch: 1 branch
BranchTrueFalseTotal
if (informational) { 1 4 5
786 1 input.flags |= UHTTP_COMPLETE;
787 } else {
788 4 input.flags |=
789 UHTTP_MUTABLE
790 | UHTTP_BODY_MUTABLE
791 | UHTTP_TRAILERS_MUTABLE;
792 }
793  
794 5 version_bytes[0] = '1';
795 5 version_bytes[1] = '.';
796 5 version_bytes[2] = (char)('0' + minor);
797 5 input.version.data = version_bytes;
798 5 input.version.len = 3;
799 5 input.status = (IV)status;
800 5 input.reason.data = reason;
801 5 input.reason.len = (STRLEN)reason_len;
802  
803 11 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 6 5 11
804 6 const char *value = headers[i].value;
805 6 size_t value_len = headers[i].value_len;
806  
807 6 ▶ while (value_len && is_ows((unsigned char)*value)) {
Branch: 2 branches
BranchTrueFalseTotal
while (value_len && is_ows((unsigned char)*value)) { 6 0 6
while (value_len && is_ows((unsigned char)*value)) { 0 6 6
808 0 ++value;
809 0 --value_len;
810 }
811 6 while (value_len
812 6 ▶ && is_ows((unsigned char)value[value_len - 1]))
Branch: 2 branches
BranchTrueFalseTotal
&& is_ows((unsigned char)value[value_len - 1])) 6 0 6
&& is_ows((unsigned char)value[value_len - 1])) 0 6 6
813 0 --value_len;
814  
815 6 native_headers[i].name.data = headers[i].name;
816 6 native_headers[i].name.len = (STRLEN)headers[i].name_len;
817 6 native_headers[i].value.data = value;
818 6 native_headers[i].value.len = (STRLEN)value_len;
819 }
820  
821 5 input.headers = native_headers;
822 5 input.header_count = (Size_t)count;
823 5 native_response = uhttp_native_from_validated(
824 aTHX_ uniform_api, &input, UHTTP_NATIVE_TRUSTED
825 );
826 5 *response_out = native_response;
827 }
828  
829 65 hv = newHV();
830 65 hv_store(hv, "ok", 2, newSViv(1), 0);
831 65 hv_store(hv, "consumed", 8, newSViv(consumed), 0);
832 65 hv_store(hv, "status", 6, newSViv(status), 0);
833 65 ▶ if (native_response == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (native_response == NULL) { 60 5 65
834 60 hv_store(hv, "version", 7, newSVpvf("1.%d", minor), 0);
835 60 hv_store(
836 hv, "reason", 6,
837 newSVpvn(reason, (STRLEN)reason_len), 0
838 );
839 60 list = headers_to_av(aTHX_ headers, count);
840 60 hv_store(hv, "headers", 7, newRV_noinc((SV *)list), 0);
841 }
842  
843 65 return newRV_noinc((SV *)hv);
844 }
845  
846 typedef struct {
847 const char *data;
848 size_t length;
849 size_t offset;
850 int valid;
851 } ub_http1_borrowed_window;
852  
853 typedef struct {
854 SV *engine;
855 CV *input_cv;
856 CV *head_cv;
857 CV *eof_cv;
858 uhttp_native_api uniform_api;
859 int uniform_native;
860 int role;
861 int direct_head;
862 size_t max_headers;
863 size_t max_head_size;
864 } ub_http1_input_context;
865  
866 static ub_http1_borrowed_window *
867 16 ub_http1_window_from_sv(pTHX_ SV *sv)
868 {
869 SV *inner;
870 ub_http1_borrowed_window *window;
871  
872 16 ▶ if (!SvROK(sv)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv) 16 0 16
873 16 ▶ || !sv_derived_from(sv, "Unblock::HTTP1::_Native::BorrowedWindow"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(sv, "Unblock::HTTP1::_Native::BorrowedWindow")) 0 16 16
874 0 croak("not an Unblock::HTTP1 borrowed input window");
875  
876 16 inner = SvRV(sv);
877 16 window = INT2PTR(ub_http1_borrowed_window *, SvIV(inner));
878 16 ▶ if (window == NULL || !window->valid)
Branch: 2 branches
BranchTrueFalseTotal
if (window == NULL || !window->valid) 16 0 16
if (window == NULL || !window->valid) 0 16 16
879 0 croak("borrowed input window is no longer valid");
880 16 ▶ if (window->offset > window->length)
Branch: 1 branch
BranchTrueFalseTotal
if (window->offset > window->length) 0 16 16
881 0 croak("corrupt borrowed input window");
882 16 return window;
883 }
884  
885 static const char *
886 586 ub_http1_buffer_view(pTHX_ SV *buffer, STRLEN *length)
887 {
888 586 ▶ if (SvROK(buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(buffer) 7 579 586
889 7 ▶ && sv_derived_from(buffer, "Unblock::HTTP1::_Native::BorrowedWindow")) {
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(buffer, "Unblock::HTTP1::_Native::BorrowedWindow")) { 7 0 7
890 ub_http1_borrowed_window *window =
891 7 ub_http1_window_from_sv(aTHX_ buffer);
892 7 size_t remaining = window->length - window->offset;
893 if (remaining > (size_t)~(STRLEN)0)
894 croak("borrowed input window exceeds Perl string length range");
895 7 *length = (STRLEN)remaining;
896 7 return window->data + window->offset;
897 }
898  
899 579 return SvPVbyte(buffer, *length);
900 }
901  
902 static CV *
903 26 ub_http1_engine_method_cv(
904 pTHX_
905 ub_http1_input_context *context,
906 CV **slot,
907 const char *name
908 )
909 {
910 GV *gv;
911 CV *cv;
912  
913 26 ▶ if (*slot != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (*slot != NULL) 4 22 26
914 4 return *slot;
915  
916 22 ▶ if (!SvROK(context->engine))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(context->engine)) 0 22 22
917 0 croak("Unblock::HTTP1 native input engine is not an object");
918  
919 22 gv = gv_fetchmethod_autoload(SvSTASH(SvRV(context->engine)), name, 0);
920 22 ▶ if (gv == NULL || (cv = GvCV(gv)) == NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (gv == NULL || (cv = GvCV(gv)) == NULL) 22 0 22
if (gv == NULL || (cv = GvCV(gv)) == NULL) 0 22 22
921 0 croak("Unblock::HTTP1 native input method %s is unavailable", name);
922  
923 22 *slot = (CV *)SvREFCNT_inc((SV *)cv);
924 22 return *slot;
925 }
926  
927 static int
928 0 ub_http1_call_engine_scalar(
929 pTHX_
930 ub_http1_input_context *context,
931 CV **slot,
932 const char *name,
933 SV *arg
934 )
935 {
936 0 CV *cv = ub_http1_engine_method_cv(aTHX_ context, slot, name);
937 0 int result = 0;
938 int count;
939 0 SV *error = NULL;
940 0 dSP;
941  
942 0 ENTER;
943 0 SAVETMPS;
944 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
945 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
946 0 ▶ XPUSHs(context->engine);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(context->engine); 0 0 0
947 0 ▶ if (arg != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (arg != NULL) 0 0 0
948 0 ▶ XPUSHs(arg);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(arg); 0 0 0
949 0 PUTBACK;
950 0 count = call_sv((SV *)cv, G_SCALAR | G_EVAL);
951 0 SPAGAIN;
952 0 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 0 0 0
if (SvTRUE(ERRSV)) 0 0 0
953 0 ▶ error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
error = newSVsv(ERRSV); 0 0 0
954 0 ▶ else if (count > 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) 0 0 0
955 0 result = POPi;
956 0 PUTBACK;
957 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
958 0 LEAVE;
959  
960 0 ▶ if (error != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (error != NULL) { 0 0 0
961 0 const char *message = SvPV_nolen(error);
962 0 croak("%s", message);
963 }
964  
965 0 return result;
966 }
967  
968 static int
969 16 ub_http1_call_engine_input(
970 pTHX_
971 ub_http1_input_context *context,
972 SV *window,
973 size_t length,
974 SV *head,
975 SV *message,
976 size_t *consumed,
977 int *head_ready
978 )
979 {
980 16 CV *cv = ub_http1_engine_method_cv(
981 aTHX_ context, &context->input_cv, "_input_borrowed"
982 );
983 16 int result = 0;
984 int count;
985 16 SV *error = NULL;
986 SV *ready_sv;
987 SV *consumed_sv;
988 SV *status_sv;
989 UV consumed_uv;
990 16 dSP;
991  
992 16 ENTER;
993 16 SAVETMPS;
994 16 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 16 0 16
995 16 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 16 16
996 16 ▶ XPUSHs(context->engine);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(context->engine); 0 16 16
997 16 ▶ XPUSHs(window);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(window); 0 16 16
998 16 mPUSHu((UV)length);
999 16 ▶ XPUSHs(head != NULL ? head : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(head != NULL ? head : &PL_sv_undef); 0 16 16
XPUSHs(head != NULL ? head : &PL_sv_undef); 12 4 16
1000 16 ▶ XPUSHs(message != NULL ? message : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(message != NULL ? message : &PL_sv_undef); 0 16 16
XPUSHs(message != NULL ? message : &PL_sv_undef); 12 4 16
1001 16 PUTBACK;
1002 16 count = call_sv((SV *)cv, G_ARRAY | G_EVAL);
1003 16 SPAGAIN;
1004 16 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 16 0 16
if (SvTRUE(ERRSV)) { 0 16 16
1005 0 ▶ error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
error = newSVsv(ERRSV); 0 0 0
1006 } else {
1007 16 ▶ if (count != 3)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 3) 0 16 16
1008 0 croak("Unblock::HTTP1 native input returned the wrong number of values");
1009 16 ready_sv = POPs;
1010 16 consumed_sv = POPs;
1011 16 status_sv = POPs;
1012 16 consumed_uv = SvUV(consumed_sv);
1013 16 ▶ if (consumed_uv > (UV)length)
Branch: 1 branch
BranchTrueFalseTotal
if (consumed_uv > (UV)length) 0 16 16
1014 0 croak("Unblock::HTTP1 native input consumed beyond its window");
1015 16 *consumed = (size_t)consumed_uv;
1016 16 *head_ready = SvTRUE(ready_sv) ? 1 : 0;
1017 16 result = SvIV(status_sv);
1018 }
1019 16 PUTBACK;
1020 16 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 16 0 16
1021 16 LEAVE;
1022  
1023 16 ▶ if (error != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (error != NULL) { 0 16 16
1024 0 const char *message = SvPV_nolen(error);
1025 0 croak("%s", message);
1026 }
1027  
1028 16 return result;
1029 }
1030  
1031 static int
1032 10 ub_http1_call_engine_head(
1033 pTHX_
1034 ub_http1_input_context *context,
1035 SV *head,
1036 SV *message,
1037 int *head_ready
1038 )
1039 {
1040 10 CV *cv = ub_http1_engine_method_cv(
1041 aTHX_ context, &context->head_cv, "_input_native_head"
1042 );
1043 10 int result = 0;
1044 int count;
1045 10 SV *error = NULL;
1046 SV *ready_sv;
1047 SV *status_sv;
1048 10 dSP;
1049  
1050 10 ENTER;
1051 10 SAVETMPS;
1052 10 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 10 0 10
1053 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
1054 10 ▶ XPUSHs(context->engine);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(context->engine); 0 10 10
1055 10 ▶ XPUSHs(head);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(head); 0 10 10
1056 10 ▶ XPUSHs(message != NULL ? message : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(message != NULL ? message : &PL_sv_undef); 0 10 10
XPUSHs(message != NULL ? message : &PL_sv_undef); 10 0 10
1057 10 PUTBACK;
1058 10 count = call_sv((SV *)cv, G_ARRAY | G_EVAL);
1059 10 SPAGAIN;
1060 10 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 10 0 10
if (SvTRUE(ERRSV)) { 0 10 10
1061 0 ▶ error = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
error = newSVsv(ERRSV); 0 0 0
1062 } else {
1063 10 ▶ if (count != 2)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 2) 0 10 10
1064 0 croak("Unblock::HTTP1 native head dispatch returned the wrong number of values");
1065 10 ready_sv = POPs;
1066 10 status_sv = POPs;
1067 10 *head_ready = SvTRUE(ready_sv) ? 1 : 0;
1068 10 result = SvIV(status_sv);
1069 }
1070 10 PUTBACK;
1071 10 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 10 0 10
1072 10 LEAVE;
1073  
1074 10 ▶ if (error != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (error != NULL) { 0 10 10
1075 0 const char *message = SvPV_nolen(error);
1076 0 croak("%s", message);
1077 }
1078  
1079 10 return result;
1080 }
1081  
1082 static int
1083 50 ub_http1_input_can_direct_head(pTHX_ ub_http1_input_context *context)
1084 {
1085 HV *engine_hv;
1086 SV **value;
1087 50 int active = 0;
1088  
1089 50 ▶ if (context == NULL || context->role == 0
Branch: 2 branches
BranchTrueFalseTotal
if (context == NULL || context->role == 0 50 0 50
if (context == NULL || context->role == 0 50 0 50
1090 50 ▶ || context->engine == NULL || !SvROK(context->engine)
Branch: 2 branches
BranchTrueFalseTotal
|| context->engine == NULL || !SvROK(context->engine) 50 0 50
|| context->engine == NULL || !SvROK(context->engine) 50 0 50
1091 50 ▶ || SvTYPE(SvRV(context->engine)) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(context->engine)) != SVt_PVHV) 0 50 50
1092 0 return 0;
1093  
1094 50 engine_hv = (HV *)SvRV(context->engine);
1095  
1096 50 value = hv_fetch(engine_hv, "closed", 6, 0);
1097 50 ▶ if (value != NULL && SvOK(*value) && SvTRUE(*value))
Branch: 3 branches
BranchTrueFalseTotal
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 50 0 50
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 50 0 50
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 0 50 50
1098 0 return 0;
1099  
1100 50 value = hv_fetch(engine_hv, "switched", 8, 0);
1101 50 ▶ if (value != NULL && SvOK(*value) && SvTRUE(*value))
Branch: 3 branches
BranchTrueFalseTotal
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 50 0 50
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 50 0 50
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 0 50 50
1102 0 return 0;
1103  
1104 50 value = hv_fetch(engine_hv, "input", 5, 0);
1105 50 ▶ if (value != NULL && SvOK(*value) && SvCUR(*value) != 0)
Branch: 3 branches
BranchTrueFalseTotal
if (value != NULL && SvOK(*value) && SvCUR(*value) != 0) 50 0 50
if (value != NULL && SvOK(*value) && SvCUR(*value) != 0) 50 0 50
if (value != NULL && SvOK(*value) && SvCUR(*value) != 0) 0 50 50
1106 0 return 0;
1107  
1108 50 value = hv_fetch(engine_hv, "rx", 2, 0);
1109 50 ▶ if (value != NULL && SvOK(*value) && SvTRUE(*value))
Branch: 3 branches
BranchTrueFalseTotal
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 48 2 50
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 6 42 48
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 6 0 6
1110 6 return 0;
1111  
1112 44 value = hv_fetch(engine_hv, "active", 6, 0);
1113 44 ▶ if (value != NULL && SvOK(*value) && SvTRUE(*value))
Branch: 3 branches
BranchTrueFalseTotal
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 44 0 44
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 8 36 44
if (value != NULL && SvOK(*value) && SvTRUE(*value)) 8 0 8
1114 8 active = 1;
1115  
1116 44 ▶ return context->role == 1 ? !active : active;
Branch: 1 branch
BranchTrueFalseTotal
return context->role == 1 ? !active : active; 35 9 44
1117 }
1118  
1119 static void *
1120 22 ub_http1_input_create(pTHX_ SV *engine)
1121 {
1122 ub_http1_input_context *context;
1123  
1124 22 ▶ if (!SvROK(engine)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(engine) 22 0 22
1125 22 ▶ || !sv_derived_from(engine, "Unblock::HTTP1::_Engine"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(engine, "Unblock::HTTP1::_Engine")) 0 22 22
1126 0 return NULL;
1127  
1128 22 Newxz(context, 1, ub_http1_input_context);
1129 22 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 22 22
1130 0 return NULL;
1131  
1132 22 context->engine = SvREFCNT_inc(engine);
1133 22 context->uniform_native = uhttp_native_init(
1134 aTHX_ &context->uniform_api, UHTTP_NATIVE_ABI_VERSION
1135 );
1136 22 context->role = sv_derived_from(engine, "Unblock::HTTP1::Server") ? 1
1137 22 ▶ : sv_derived_from(engine, "Unblock::HTTP1::Client") ? 2 : 0;
Branch: 2 branches
BranchTrueFalseTotal
: sv_derived_from(engine, "Unblock::HTTP1::Client") ? 2 : 0; 3 19 22
: sv_derived_from(engine, "Unblock::HTTP1::Client") ? 2 : 0; 3 0 3
1138 22 context->direct_head = 0;
1139 22 context->max_headers = 100;
1140 22 context->max_head_size = 65536;
1141  
1142 22 ▶ if (SvTYPE(SvRV(engine)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(engine)) == SVt_PVHV) { 22 0 22
1143 22 HV *engine_hv = (HV *)SvRV(engine);
1144 SV **value;
1145  
1146 22 value = hv_fetch(engine_hv, "max_headers", 11, 0);
1147 22 ▶ if (value != NULL && SvOK(*value))
Branch: 2 branches
BranchTrueFalseTotal
if (value != NULL && SvOK(*value)) 22 0 22
if (value != NULL && SvOK(*value)) 22 0 22
1148 22 context->max_headers = (size_t)SvUV(*value);
1149  
1150 22 value = hv_fetch(engine_hv, "max_head_size", 13, 0);
1151 22 ▶ if (value != NULL && SvOK(*value))
Branch: 2 branches
BranchTrueFalseTotal
if (value != NULL && SvOK(*value)) 22 0 22
if (value != NULL && SvOK(*value)) 22 0 22
1152 22 context->max_head_size = (size_t)SvUV(*value);
1153  
1154 }
1155  
1156 22 context->direct_head = ub_http1_input_can_direct_head(aTHX_ context);
1157 22 return context;
1158 }
1159  
1160 static int
1161 28 ub_http1_input_borrowed(
1162 pTHX_
1163 void *opaque,
1164 const char *data,
1165 size_t length,
1166 size_t *consumed
1167 )
1168 {
1169 28 ub_http1_input_context *context = (ub_http1_input_context *)opaque;
1170 28 ub_http1_borrowed_window *window = NULL;
1171 28 SV *object = NULL;
1172 28 SV *head = NULL;
1173 28 SV *message = NULL;
1174 28 SV *window_arg = &PL_sv_undef;
1175 28 int head_ready = 0;
1176 28 int head_only = 0;
1177 28 int need_window = 1;
1178 int result;
1179  
1180 28 ▶ if (context == NULL || consumed == NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (context == NULL || consumed == NULL) 28 0 28
if (context == NULL || consumed == NULL) 0 28 28
1181 0 croak("invalid Unblock::HTTP1 native input context");
1182  
1183 28 *consumed = 0;
1184 28 ▶ if (data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (data == NULL) 0 28 28
1185 0 data = "";
1186  
1187 28 context->direct_head = ub_http1_input_can_direct_head(aTHX_ context);
1188  
1189 28 ▶ if (context->direct_head && context->role == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (context->direct_head && context->role == 1) { 24 4 28
if (context->direct_head && context->role == 1) { 18 6 24
1190 18 head = ub_http1_parse_request_head_result(
1191 aTHX_
1192 data,
1193 length,
1194 0,
1195 context->max_headers,
1196 18 ▶ context->uniform_native ? &context->uniform_api : NULL,
Branch: 1 branch
BranchTrueFalseTotal
context->uniform_native ? &context->uniform_api : NULL, 18 0 18
1197 &message
1198 );
1199  
1200 18 ▶ if (head == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (head == NULL) { 1 17 18
1201 1 ▶ if (length <= context->max_head_size)
Branch: 1 branch
BranchTrueFalseTotal
if (length <= context->max_head_size) 1 0 1
1202 1 return UB_HTTP1_INPUT_MORE;
1203 0 head = new_error_result(
1204 aTHX_ 431, "request head exceeds configured limit"
1205 );
1206 0 need_window = 0;
1207 17 ▶ } else if (SvROK(head) && SvTYPE(SvRV(head)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvROK(head) && SvTYPE(SvRV(head)) == SVt_PVHV) { 17 0 17
} else if (SvROK(head) && SvTYPE(SvRV(head)) == SVt_PVHV) { 17 0 17
1208 17 HV *head_hv = (HV *)SvRV(head);
1209 17 SV **ok_sv = hv_fetch(head_hv, "ok", 2, 0);
1210  
1211 34 ▶ if (ok_sv != NULL && SvTRUE(*ok_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok_sv != NULL && SvTRUE(*ok_sv)) { 17 0 17
if (ok_sv != NULL && SvTRUE(*ok_sv)) { 17 0 17
1212 17 SV **consumed_sv = hv_fetch(head_hv, "consumed", 8, 0);
1213 17 ▶ if (consumed_sv != NULL && SvOK(*consumed_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (consumed_sv != NULL && SvOK(*consumed_sv)) { 17 0 17
if (consumed_sv != NULL && SvOK(*consumed_sv)) { 17 0 17
1214 17 UV head_consumed = SvUV(*consumed_sv);
1215 17 ▶ if (head_consumed <= (UV)length
Branch: 1 branch
BranchTrueFalseTotal
if (head_consumed <= (UV)length 17 0 17
1216 17 ▶ && head_consumed == (UV)length) {
Branch: 1 branch
BranchTrueFalseTotal
&& head_consumed == (UV)length) { 8 9 17
1217 8 need_window = 0;
1218 8 head_only = 1;
1219 }
1220 }
1221 } else {
1222 0 need_window = 0;
1223 }
1224 }
1225 10 ▶ } else if (context->direct_head && context->role == 2) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (context->direct_head && context->role == 2) { 6 4 10
} else if (context->direct_head && context->role == 2) { 6 0 6
1226 6 head = ub_http1_parse_response_head_result(
1227 aTHX_
1228 data,
1229 length,
1230 0,
1231 context->max_headers,
1232 6 ▶ context->uniform_native ? &context->uniform_api : NULL,
Branch: 1 branch
BranchTrueFalseTotal
context->uniform_native ? &context->uniform_api : NULL, 6 0 6
1233 &message
1234 );
1235  
1236 6 ▶ if (head == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (head == NULL) { 1 5 6
1237 1 ▶ if (length <= context->max_head_size)
Branch: 1 branch
BranchTrueFalseTotal
if (length <= context->max_head_size) 1 0 1
1238 1 return UB_HTTP1_INPUT_MORE;
1239 0 head = new_error_result(
1240 aTHX_ 0, "HTTP/1 response head exceeds configured limit"
1241 );
1242 0 need_window = 0;
1243 5 ▶ } else if (SvROK(head) && SvTYPE(SvRV(head)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvROK(head) && SvTYPE(SvRV(head)) == SVt_PVHV) { 5 0 5
} else if (SvROK(head) && SvTYPE(SvRV(head)) == SVt_PVHV) { 5 0 5
1244 5 HV *head_hv = (HV *)SvRV(head);
1245 5 SV **ok_sv = hv_fetch(head_hv, "ok", 2, 0);
1246  
1247 10 ▶ if (ok_sv != NULL && SvTRUE(*ok_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok_sv != NULL && SvTRUE(*ok_sv)) { 5 0 5
if (ok_sv != NULL && SvTRUE(*ok_sv)) { 5 0 5
1248 5 SV **consumed_sv = hv_fetch(head_hv, "consumed", 8, 0);
1249 5 ▶ if (consumed_sv != NULL && SvOK(*consumed_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (consumed_sv != NULL && SvOK(*consumed_sv)) { 5 0 5
if (consumed_sv != NULL && SvOK(*consumed_sv)) { 5 0 5
1250 5 UV head_consumed = SvUV(*consumed_sv);
1251 5 ▶ if (head_consumed <= (UV)length
Branch: 1 branch
BranchTrueFalseTotal
if (head_consumed <= (UV)length 5 0 5
1252 5 ▶ && head_consumed == (UV)length) {
Branch: 1 branch
BranchTrueFalseTotal
&& head_consumed == (UV)length) { 2 3 5
1253 2 need_window = 0;
1254 2 head_only = 1;
1255 }
1256 }
1257 } else {
1258 0 need_window = 0;
1259 }
1260 }
1261 }
1262  
1263 26 ▶ if (head_only) {
Branch: 1 branch
BranchTrueFalseTotal
if (head_only) { 10 16 26
1264 int jump_status;
1265 dJMPENV;
1266  
1267 10 *consumed = length;
1268 10 JMPENV_PUSH(jump_status);
1269 10 ▶ if (jump_status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (jump_status == 0) { 10 0 10
1270 10 result = ub_http1_call_engine_head(
1271 aTHX_ context, head, message, &head_ready
1272 );
1273 10 JMPENV_POP;
1274 } else {
1275 0 JMPENV_POP;
1276 0 ▶ if (message != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (message != NULL) 0 0 0
1277 0 SvREFCNT_dec(message);
1278 0 SvREFCNT_dec(head);
1279 0 ▶ JMPENV_JUMP(jump_status);
Branch: 2 branches
BranchTrueFalseTotal
JMPENV_JUMP(jump_status); 0 0 0
JMPENV_JUMP(jump_status); 0 0 0
1280 }
1281  
1282 10 ▶ if (message != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (message != NULL) 10 0 10
1283 10 SvREFCNT_dec(message);
1284 10 SvREFCNT_dec(head);
1285  
1286 10 ▶ if (result < UB_HTTP1_INPUT_OK || result > UB_HTTP1_INPUT_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
if (result < UB_HTTP1_INPUT_OK || result > UB_HTTP1_INPUT_SWITCH 10 0 10
if (result < UB_HTTP1_INPUT_OK || result > UB_HTTP1_INPUT_SWITCH 10 0 10
1287 10 ▶ || result == 2)
Branch: 1 branch
BranchTrueFalseTotal
|| result == 2) 0 10 10
1288 0 croak("Unblock::HTTP1 engine returned invalid native input status");
1289  
1290 10 context->direct_head = head_ready;
1291 10 return result;
1292 }
1293  
1294 16 ▶ if (need_window) {
Branch: 1 branch
BranchTrueFalseTotal
if (need_window) { 16 0 16
1295 SV *inner;
1296  
1297 16 Newxz(window, 1, ub_http1_borrowed_window);
1298 16 ▶ if (window == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (window == NULL) { 0 16 16
1299 0 ▶ if (head != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (head != NULL) 0 0 0
1300 0 SvREFCNT_dec(head);
1301 0 croak("unable to allocate borrowed input window");
1302 }
1303  
1304 16 window->data = data;
1305 16 window->length = length;
1306 16 window->offset = 0;
1307 16 window->valid = 1;
1308  
1309 16 inner = newSViv(PTR2IV(window));
1310 16 object = newRV_noinc(inner);
1311 16 sv_bless(object,
1312 gv_stashpv("Unblock::HTTP1::_Native::BorrowedWindow", GV_ADD));
1313 16 window_arg = object;
1314 }
1315  
1316 {
1317 int jump_status;
1318 dJMPENV;
1319  
1320 16 JMPENV_PUSH(jump_status);
1321 16 ▶ if (jump_status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (jump_status == 0) { 16 0 16
1322 16 result = ub_http1_call_engine_input(
1323 aTHX_
1324 context,
1325 window_arg,
1326 length,
1327 head,
1328 message,
1329 consumed,
1330 &head_ready
1331 );
1332 16 JMPENV_POP;
1333 } else {
1334 0 JMPENV_POP;
1335 0 ▶ if (window != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (window != NULL) 0 0 0
1336 0 window->valid = 0;
1337 0 ▶ if (object != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (object != NULL) 0 0 0
1338 0 SvREFCNT_dec(object);
1339 0 ▶ if (message != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (message != NULL) 0 0 0
1340 0 SvREFCNT_dec(message);
1341 0 ▶ if (head != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (head != NULL) 0 0 0
1342 0 SvREFCNT_dec(head);
1343 0 ▶ JMPENV_JUMP(jump_status);
Branch: 2 branches
BranchTrueFalseTotal
JMPENV_JUMP(jump_status); 0 0 0
JMPENV_JUMP(jump_status); 0 0 0
1344 }
1345 }
1346  
1347 16 ▶ if (window != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (window != NULL) 16 0 16
1348 16 window->valid = 0;
1349 16 ▶ if (object != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (object != NULL) 16 0 16
1350 16 SvREFCNT_dec(object);
1351 16 ▶ if (message != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (message != NULL) 12 4 16
1352 12 SvREFCNT_dec(message);
1353 16 ▶ if (head != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (head != NULL) 12 4 16
1354 12 SvREFCNT_dec(head);
1355  
1356 16 ▶ if (result < UB_HTTP1_INPUT_OK || result > UB_HTTP1_INPUT_SWITCH
Branch: 2 branches
BranchTrueFalseTotal
if (result < UB_HTTP1_INPUT_OK || result > UB_HTTP1_INPUT_SWITCH 16 0 16
if (result < UB_HTTP1_INPUT_OK || result > UB_HTTP1_INPUT_SWITCH 16 0 16
1357 16 ▶ || result == 2)
Branch: 1 branch
BranchTrueFalseTotal
|| result == 2) 0 16 16
1358 0 croak("Unblock::HTTP1 engine returned invalid native input status");
1359  
1360 16 ▶ if (context->role)
Branch: 1 branch
BranchTrueFalseTotal
if (context->role) 16 0 16
1361 16 context->direct_head = head_ready;
1362  
1363 16 return result;
1364 }
1365  
1366 static int
1367 0 ub_http1_input_eof(pTHX_ void *opaque)
1368 {
1369 0 ub_http1_input_context *context = (ub_http1_input_context *)opaque;
1370 int result;
1371  
1372 0 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 0 0
1373 0 croak("invalid Unblock::HTTP1 native input context");
1374  
1375 0 result = ub_http1_call_engine_scalar(
1376 aTHX_
1377 context,
1378 &context->eof_cv,
1379 "_borrowed_input_eof",
1380 NULL
1381 );
1382  
1383 0 ▶ if (result != UB_HTTP1_INPUT_OK
Branch: 1 branch
BranchTrueFalseTotal
if (result != UB_HTTP1_INPUT_OK 0 0 0
1384 0 ▶ && result != UB_HTTP1_INPUT_CLOSED
Branch: 1 branch
BranchTrueFalseTotal
&& result != UB_HTTP1_INPUT_CLOSED 0 0 0
1385 0 ▶ && result != UB_HTTP1_INPUT_SWITCH)
Branch: 1 branch
BranchTrueFalseTotal
&& result != UB_HTTP1_INPUT_SWITCH) 0 0 0
1386 0 croak("Unblock::HTTP1 engine returned invalid native EOF status");
1387  
1388 0 return result;
1389 }
1390  
1391 static void
1392 22 ub_http1_input_destroy(pTHX_ void *opaque)
1393 {
1394 22 ub_http1_input_context *context = (ub_http1_input_context *)opaque;
1395  
1396 PERL_UNUSED_CONTEXT;
1397 22 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 22 22
1398 0 return;
1399  
1400 22 ▶ if (context->input_cv != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->input_cv != NULL) 14 8 22
1401 14 SvREFCNT_dec((SV *)context->input_cv);
1402 22 ▶ if (context->head_cv != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->head_cv != NULL) 8 14 22
1403 8 SvREFCNT_dec((SV *)context->head_cv);
1404 22 ▶ if (context->eof_cv != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->eof_cv != NULL) 0 22 22
1405 0 SvREFCNT_dec((SV *)context->eof_cv);
1406 22 ▶ if (context->engine != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context->engine != NULL) 22 0 22
1407 22 SvREFCNT_dec(context->engine);
1408 22 Safefree(context);
1409 }
1410  
1411 static const ub_http1_input_ops_v1 ub_http1_input_ops = {
1412 UB_HTTP1_INPUT_ABI_VERSION,
1413 sizeof(ub_http1_input_ops_v1),
1414 "Unblock::HTTP1 borrowed input",
1415 ub_http1_input_create,
1416 ub_http1_input_borrowed,
1417 ub_http1_input_eof,
1418 ub_http1_input_destroy
1419 };
1420  
1421 MODULE = Unblock::HTTP1 PACKAGE = Unblock::HTTP1::_Native
1422 PROTOTYPES: DISABLE
1423  
1424 BOOT:
1425 MY_CXT_INIT;
1426  
1427 void
1428 CLONE(...)
1429 CODE:
1430 #ifdef USE_ITHREADS
1431 MY_CXT_CLONE;
1432 Zero(&MY_CXT, 1, my_cxt_t);
1433 #endif
1434  
1435 SV *
1436 _simple_response_wire(request, response, reasons)
1437 SV *request
1438 SV *response
1439 HV *reasons
1440 PREINIT:
1441 dMY_CXT;
1442 uhttp_native_view req, res;
1443 Size_t i, count;
1444 SV *name, *value, **reason;
1445 129 STRLEN len, body_len = 0;
1446 129 const char *bytes, *body = "";
1447 IV status;
1448 char code[4];
1449 SV *wire;
1450 CODE:
1451 129 RETVAL = &PL_sv_undef;
1452 129 ▶ if (!MY_CXT.initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (!MY_CXT.initialized) { 26 103 129
1453 26 MY_CXT.compatible = uhttp_native_init(aTHX_ &MY_CXT.uniform_api,
1454 UHTTP_NATIVE_ABI_VERSION);
1455 26 MY_CXT.initialized = 1;
1456 }
1457 129 ▶ if (!MY_CXT.compatible
Branch: 1 branch
BranchTrueFalseTotal
if (!MY_CXT.compatible 129 0 129
1458 129 ▶ || !uhttp_native_inspect(aTHX_ &MY_CXT.uniform_api, request, &req)
Branch: 1 branch
BranchTrueFalseTotal
|| !uhttp_native_inspect(aTHX_ &MY_CXT.uniform_api, request, &req) 115 14 129
1459 115 ▶ || !uhttp_native_inspect(aTHX_ &MY_CXT.uniform_api, response, &res)
Branch: 1 branch
BranchTrueFalseTotal
|| !uhttp_native_inspect(aTHX_ &MY_CXT.uniform_api, response, &res) 111 4 115
1460 111 ▶ || req.kind != UHTTP_KIND_REQUEST || res.kind != UHTTP_KIND_RESPONSE)
Branch: 2 branches
BranchTrueFalseTotal
|| req.kind != UHTTP_KIND_REQUEST || res.kind != UHTTP_KIND_RESPONSE) 111 0 111
|| req.kind != UHTTP_KIND_REQUEST || res.kind != UHTTP_KIND_RESPONSE) 0 111 111
1461 70 XSRETURN_UNDEF;
1462 111 ▶ if (SvOK(req.version)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(req.version)) { 102 9 111
1463 102 bytes = SvPV(req.version, len);
1464 102 ▶ if (!ascii_equal_cs(bytes, len, "1.1", 3)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!ascii_equal_cs(bytes, len, "1.1", 3)) XSRETURN_UNDEF; 7 95 102
1465 }
1466 104 ▶ if (SvOK(res.version)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(res.version)) { 0 104 104
1467 0 bytes = SvPV(res.version, len);
1468 0 ▶ if (!ascii_equal_cs(bytes, len, "1.1", 3)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!ascii_equal_cs(bytes, len, "1.1", 3)) XSRETURN_UNDEF; 0 0 0
1469 }
1470 104 bytes = SvPV(req.method, len);
1471 104 ▶ if (ascii_equal_cs(bytes, len, "HEAD", 4)
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_cs(bytes, len, "HEAD", 4) 101 3 104
1472 104 ▶ || ascii_equal_cs(bytes, len, "CONNECT", 7)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
|| ascii_equal_cs(bytes, len, "CONNECT", 7)) XSRETURN_UNDEF; 3 98 101
1473 98 count = uhttp_native_field_count(aTHX_ &req.headers);
1474 207 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 129 78 207
1475 129 uhttp_native_field_at(aTHX_ &req.headers, i, &name, &value);
1476 129 bytes = SvPV(name, len);
1477 129 ▶ if (ascii_equal_ci(bytes, len, "Connection", 10)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_ci(bytes, len, "Connection", 10)) XSRETURN_UNDEF; 20 109 129
1478 }
1479 78 status = SvIV(res.status);
1480 78 ▶ if (status < 200 || status > 599 || status == 204 || status == 205
Branch: 4 branches
BranchTrueFalseTotal
if (status < 200 || status > 599 || status == 204 || status == 205 77 1 78
if (status < 200 || status > 599 || status == 204 || status == 205 77 0 77
if (status < 200 || status > 599 || status == 204 || status == 205 70 7 77
if (status < 200 || status > 599 || status == 204 || status == 205 69 1 70
1481 69 ▶ || status == 304 || uhttp_native_field_count(aTHX_ &res.trailers))
Branch: 2 branches
BranchTrueFalseTotal
|| status == 304 || uhttp_native_field_count(aTHX_ &res.trailers)) 68 1 69
|| status == 304 || uhttp_native_field_count(aTHX_ &res.trailers)) 2 66 68
1482 12 XSRETURN_UNDEF;
1483 /* Uniform validates canonical fields. Framing fields still require the
1484 * general HTTP/1 checks; never bypass them based on their spelling. */
1485 66 count = uhttp_native_field_count(aTHX_ &res.headers);
1486 70 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 11 59 70
1487 11 uhttp_native_field_at(aTHX_ &res.headers, i, &name, &value);
1488 11 ▶ if (SvUTF8(name) || SvUTF8(value)) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (SvUTF8(name) || SvUTF8(value)) XSRETURN_UNDEF; 11 0 11
if (SvUTF8(name) || SvUTF8(value)) XSRETURN_UNDEF; 0 11 11
1489 11 bytes = SvPV(name, len);
1490 11 ▶ if (ascii_equal_ci(bytes, len, "Content-Length", 14)
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_equal_ci(bytes, len, "Content-Length", 14) 7 4 11
1491 7 ▶ || ascii_equal_ci(bytes, len, "Transfer-Encoding", 17)
Branch: 1 branch
BranchTrueFalseTotal
|| ascii_equal_ci(bytes, len, "Transfer-Encoding", 17) 6 1 7
1492 11 ▶ || ascii_equal_ci(bytes, len, "Connection", 10)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
|| ascii_equal_ci(bytes, len, "Connection", 10)) XSRETURN_UNDEF; 2 4 6
1493 }
1494 59 ▶ if (SvUTF8(res.body) || SvUTF8(res.reason)) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (SvUTF8(res.body) || SvUTF8(res.reason)) XSRETURN_UNDEF; 59 0 59
if (SvUTF8(res.body) || SvUTF8(res.reason)) XSRETURN_UNDEF; 0 59 59
1495 117 ▶ if (res.flags & UHTTP_HAS_BUFFERED_BODY) body = SvPV(res.body, body_len);
Branch: 1 branch
BranchTrueFalseTotal
if (res.flags & UHTTP_HAS_BUFFERED_BODY) body = SvPV(res.body, body_len); 58 1 59
1496 59 wire = sv_2mortal(newSVpvs("HTTP/1.1 "));
1497 59 code[0] = '0' + status / 100;
1498 59 code[1] = '0' + (status / 10) % 10;
1499 59 code[2] = '0' + status % 10;
1500 59 code[3] = '\0';
1501 59 sv_catpvn(wire, code, 3);
1502 59 sv_catpvs(wire, " ");
1503 59 ▶ if (SvOK(res.reason)) sv_catsv(wire, res.reason);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(res.reason)) sv_catsv(wire, res.reason); 1 58 59
1504 58 ▶ else if ((reason = hv_fetch(reasons, code, 3, 0)) && SvOK(*reason))
Branch: 2 branches
BranchTrueFalseTotal
else if ((reason = hv_fetch(reasons, code, 3, 0)) && SvOK(*reason)) 57 1 58
else if ((reason = hv_fetch(reasons, code, 3, 0)) && SvOK(*reason)) 57 0 57
1505 57 sv_catsv(wire, *reason);
1506 59 sv_catpvs(wire, "\r\n");
1507 63 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 4 59 63
1508 4 uhttp_native_field_at(aTHX_ &res.headers, i, &name, &value);
1509 4 sv_catsv(wire, name);
1510 4 sv_catpvs(wire, ": ");
1511 4 sv_catsv(wire, value);
1512 4 sv_catpvs(wire, "\r\n");
1513 }
1514 59 sv_catpvf(wire, "Content-Length: %" UVuf "\r\n\r\n", (UV)body_len);
1515 59 sv_catpvn(wire, body, body_len);
1516 59 RETVAL = SvREFCNT_inc(wire);
1517 OUTPUT:
1518 RETVAL
1519  
1520 UV
1521 _borrowed_input_operations_address()
1522 CODE:
1523 1 ▶ RETVAL = PTR2UV(&ub_http1_input_ops);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(&ub_http1_input_ops); 0 1 1
1524 OUTPUT:
1525 RETVAL
1526  
1527 UV
1528 _borrowed_input_operations_size()
1529 CODE:
1530 1 ▶ RETVAL = (UV)sizeof(ub_http1_input_ops_v1);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)sizeof(ub_http1_input_ops_v1); 0 1 1
1531 OUTPUT:
1532 RETVAL
1533  
1534 void
1535 _borrowed_input_once(engine, buffer)
1536 SV *engine
1537 SV *buffer
1538 PREINIT:
1539 STRLEN buffer_len;
1540 const char *data;
1541 void *context;
1542 16 size_t consumed = 0;
1543 int status;
1544 PPCODE:
1545 16 data = SvPVbyte(buffer, buffer_len);
1546 16 context = ub_http1_input_create(aTHX_ engine);
1547 16 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 16 16
1548 0 croak("engine does not support Unblock::HTTP1 native input");
1549 16 status = ub_http1_input_borrowed(
1550 aTHX_ context, data, (size_t)buffer_len, &consumed
1551 );
1552 16 ub_http1_input_destroy(aTHX_ context);
1553 16 ▶ XPUSHs(sv_2mortal(newSViv(status)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(status))); 0 16 16
1554 16 ▶ XPUSHs(sv_2mortal(newSVuv((UV)consumed)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv((UV)consumed))); 0 16 16
1555  
1556 const char *
1557 pico_version(CLASS)
1558 const char *CLASS
1559 CODE:
1560 (void)CLASS;
1561 0 RETVAL = PICOHTTPPARSER_VERSION;
1562 OUTPUT:
1563 RETVAL
1564  
1565 SV *
1566 parse_request_head(CLASS, buffer, last_len = 0, max_headers = 100, offset = 0)
1567 const char *CLASS
1568 SV *buffer
1569 UV last_len
1570 UV max_headers
1571 UV offset
1572 PREINIT:
1573 STRLEN buffer_len;
1574 const char *buf;
1575 CODE:
1576 (void)CLASS;
1577 304 buf = ub_http1_buffer_view(aTHX_ buffer, &buffer_len);
1578 304 ▶ if (offset > (UV)buffer_len)
Branch: 1 branch
BranchTrueFalseTotal
if (offset > (UV)buffer_len) 0 304 304
1579 0 croak("offset exceeds buffer length");
1580 304 buf += (size_t)offset;
1581 304 buffer_len -= (STRLEN)offset;
1582 304 RETVAL = ub_http1_parse_request_head_result(
1583 aTHX_
1584 buf,
1585 (size_t)buffer_len,
1586 (size_t)last_len,
1587 (size_t)max_headers,
1588 NULL,
1589 NULL
1590 );
1591 304 ▶ if (RETVAL == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == NULL) 170 134 304
1592 170 XSRETURN_UNDEF;
1593 OUTPUT:
1594 RETVAL
1595  
1596 SV *
1597 parse_response_head(CLASS, buffer, last_len = 0, max_headers = 100, offset = 0)
1598 const char *CLASS
1599 SV *buffer
1600 UV last_len
1601 UV max_headers
1602 UV offset
1603 PREINIT:
1604 STRLEN buffer_len;
1605 const char *buf;
1606 CODE:
1607 (void)CLASS;
1608 173 buf = ub_http1_buffer_view(aTHX_ buffer, &buffer_len);
1609 173 ▶ if (offset > (UV)buffer_len)
Branch: 1 branch
BranchTrueFalseTotal
if (offset > (UV)buffer_len) 0 173 173
1610 0 croak("offset exceeds buffer length");
1611 173 buf += (size_t)offset;
1612 173 buffer_len -= (STRLEN)offset;
1613 173 RETVAL = ub_http1_parse_response_head_result(
1614 aTHX_
1615 buf,
1616 (size_t)buffer_len,
1617 (size_t)last_len,
1618 (size_t)max_headers,
1619 NULL,
1620 NULL
1621 );
1622 173 ▶ if (RETVAL == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == NULL) 113 60 173
1623 113 XSRETURN_UNDEF;
1624 OUTPUT:
1625 RETVAL
1626  
1627 SV *
1628 parse_trailers(CLASS, buffer, last_len = 0, max_headers = 100, offset = 0)
1629 const char *CLASS
1630 SV *buffer
1631 UV last_len
1632 UV max_headers
1633 UV offset
1634 PREINIT:
1635 STRLEN buffer_len;
1636 const char *buf;
1637 struct phr_header headers[UB_HTTP1_MAX_HEADERS];
1638 size_t count;
1639 int consumed;
1640 HV *hv;
1641 AV *list;
1642 CODE:
1643 (void)CLASS;
1644 45 buf = ub_http1_buffer_view(aTHX_ buffer, &buffer_len);
1645 45 ▶ if (offset > (UV)buffer_len)
Branch: 1 branch
BranchTrueFalseTotal
if (offset > (UV)buffer_len) 0 45 45
1646 0 croak("offset exceeds buffer length");
1647 45 buf += (size_t)offset;
1648 45 buffer_len -= (STRLEN)offset;
1649 45 ▶ if (last_len > (UV)buffer_len)
Branch: 1 branch
BranchTrueFalseTotal
if (last_len > (UV)buffer_len) 0 45 45
1650 0 croak("last_len exceeds input window length");
1651 45 ▶ if (max_headers == 0 || max_headers > UB_HTTP1_MAX_HEADERS)
Branch: 2 branches
BranchTrueFalseTotal
if (max_headers == 0 || max_headers > UB_HTTP1_MAX_HEADERS) 45 0 45
if (max_headers == 0 || max_headers > UB_HTTP1_MAX_HEADERS) 0 45 45
1652 0 croak("max_headers must be between 1 and %d", UB_HTTP1_MAX_HEADERS);
1653 45 count = (size_t)max_headers;
1654 45 consumed = phr_parse_headers(buf, (size_t)buffer_len, headers, &count, (size_t)last_len);
1655 45 ▶ if (consumed == -2) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (consumed == -2) XSRETURN_UNDEF; 29 16 45
1656 16 ▶ if (consumed == -1 || !strict_headers(headers, count)) {
Branch: 2 branches
BranchTrueFalseTotal
if (consumed == -1 || !strict_headers(headers, count)) { 16 0 16
if (consumed == -1 || !strict_headers(headers, count)) { 0 16 16
1657 0 RETVAL = new_error_result(aTHX_ 0, "malformed HTTP/1 trailer section");
1658 } else {
1659 16 hv = newHV();
1660 16 hv_store(hv, "ok", 2, newSViv(1), 0);
1661 16 hv_store(hv, "consumed", 8, newSViv(consumed), 0);
1662 16 list = headers_to_av(aTHX_ headers, count);
1663 16 hv_store(hv, "headers", 7, newRV_noinc((SV *)list), 0);
1664 16 RETVAL = newRV_noinc((SV *)hv);
1665 }
1666 OUTPUT:
1667 RETVAL
1668  
1669 MODULE = Unblock::HTTP1 PACKAGE = Unblock::HTTP1::_Native::BorrowedDriver
1670  
1671 SV *
1672 new(CLASS, engine)
1673 const char *CLASS
1674 SV *engine
1675 PREINIT:
1676 ub_http1_input_context *context;
1677 SV *inner;
1678 SV *object;
1679 CODE:
1680 6 context = (ub_http1_input_context *)ub_http1_input_create(aTHX_ engine);
1681 6 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 6 6
1682 0 croak("engine does not support Unblock::HTTP1 native input");
1683 6 inner = newSViv(PTR2IV(context));
1684 6 object = newRV_noinc(inner);
1685 6 sv_bless(object, gv_stashpv(CLASS, GV_ADD));
1686 6 RETVAL = object;
1687 OUTPUT:
1688 RETVAL
1689  
1690 void
1691 feed(self, buffer)
1692 SV *self
1693 SV *buffer
1694 PREINIT:
1695 SV *inner;
1696 ub_http1_input_context *context;
1697 STRLEN buffer_len;
1698 const char *data;
1699 12 size_t consumed = 0;
1700 int status;
1701 PPCODE:
1702 12 ▶ if (!SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self) 12 0 12
1703 12 ▶ || !sv_derived_from(self, "Unblock::HTTP1::_Native::BorrowedDriver"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(self, "Unblock::HTTP1::_Native::BorrowedDriver")) 0 12 12
1704 0 croak("not an Unblock::HTTP1 borrowed input driver");
1705 12 inner = SvRV(self);
1706 12 context = INT2PTR(ub_http1_input_context *, SvIV(inner));
1707 12 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 12 12
1708 0 croak("borrowed input driver has already been released");
1709 12 data = SvPVbyte(buffer, buffer_len);
1710 12 status = ub_http1_input_borrowed(
1711 aTHX_ context, data, (size_t)buffer_len, &consumed
1712 );
1713 12 ▶ XPUSHs(sv_2mortal(newSViv(status)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(status))); 0 12 12
1714 12 ▶ XPUSHs(sv_2mortal(newSVuv((UV)consumed)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVuv((UV)consumed))); 0 12 12
1715  
1716 UV
1717 feed_repeat(self, buffer, count)
1718 SV *self
1719 SV *buffer
1720 UV count
1721 PREINIT:
1722 SV *inner;
1723 ub_http1_input_context *context;
1724 STRLEN buffer_len;
1725 const char *data;
1726 UV i;
1727 CODE:
1728 0 ▶ if (!SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self) 0 0 0
1729 0 ▶ || !sv_derived_from(self, "Unblock::HTTP1::_Native::BorrowedDriver"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(self, "Unblock::HTTP1::_Native::BorrowedDriver")) 0 0 0
1730 0 croak("not an Unblock::HTTP1 borrowed input driver");
1731 0 inner = SvRV(self);
1732 0 context = INT2PTR(ub_http1_input_context *, SvIV(inner));
1733 0 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 0 0
1734 0 croak("borrowed input driver has already been released");
1735 0 data = SvPVbyte(buffer, buffer_len);
1736 0 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 0 0 0
1737 0 size_t consumed = 0;
1738 0 int status = ub_http1_input_borrowed(
1739 aTHX_ context, data, (size_t)buffer_len, &consumed
1740 );
1741 0 ▶ if (status != UB_HTTP1_INPUT_OK
Branch: 1 branch
BranchTrueFalseTotal
if (status != UB_HTTP1_INPUT_OK 0 0 0
1742 0 ▶ || consumed != (size_t)buffer_len)
Branch: 1 branch
BranchTrueFalseTotal
|| consumed != (size_t)buffer_len) 0 0 0
1743 0 croak("borrowed repeat input did not consume complete window");
1744 }
1745 0 ▶ RETVAL = count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = count; 0 0 0
1746 OUTPUT:
1747 RETVAL
1748  
1749 int
1750 eof(self)
1751 SV *self
1752 PREINIT:
1753 SV *inner;
1754 ub_http1_input_context *context;
1755 CODE:
1756 0 ▶ if (!SvROK(self)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self) 0 0 0
1757 0 ▶ || !sv_derived_from(self, "Unblock::HTTP1::_Native::BorrowedDriver"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(self, "Unblock::HTTP1::_Native::BorrowedDriver")) 0 0 0
1758 0 croak("not an Unblock::HTTP1 borrowed input driver");
1759 0 inner = SvRV(self);
1760 0 context = INT2PTR(ub_http1_input_context *, SvIV(inner));
1761 0 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 0 0
1762 0 croak("borrowed input driver has already been released");
1763 0 RETVAL = ub_http1_input_eof(aTHX_ context);
1764 OUTPUT:
1765 RETVAL
1766  
1767 void
1768 DESTROY(self)
1769 SV *self
1770 PREINIT:
1771 SV *inner;
1772 ub_http1_input_context *context;
1773 CODE:
1774 6 ▶ if (!SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) 0 6 6
1775 0 XSRETURN_EMPTY;
1776 6 inner = SvRV(self);
1777 6 context = INT2PTR(ub_http1_input_context *, SvIV(inner));
1778 6 ▶ if (context == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (context == NULL) 0 6 6
1779 0 XSRETURN_EMPTY;
1780 6 ub_http1_input_destroy(aTHX_ context);
1781 6 sv_setiv(inner, 0);
1782  
1783 MODULE = Unblock::HTTP1 PACKAGE = Unblock::HTTP1::_Native::BorrowedWindow
1784  
1785 SV *
1786 slice(self, offset, length)
1787 SV *self
1788 UV offset
1789 UV length
1790 PREINIT:
1791 ub_http1_borrowed_window *window;
1792 CODE:
1793 9 window = ub_http1_window_from_sv(aTHX_ self);
1794 9 ▶ if (offset > (UV)window->length
Branch: 1 branch
BranchTrueFalseTotal
if (offset > (UV)window->length 9 0 9
1795 9 ▶ || length > (UV)(window->length - (size_t)offset))
Branch: 1 branch
BranchTrueFalseTotal
|| length > (UV)(window->length - (size_t)offset)) 0 9 9
1796 0 croak("borrowed input slice exceeds window");
1797 9 RETVAL = newSVpvn(
1798 window->data + (size_t)offset,
1799 (STRLEN)length
1800 );
1801 OUTPUT:
1802 RETVAL
1803  
1804 UV
1805 remaining(self)
1806 SV *self
1807 PREINIT:
1808 ub_http1_borrowed_window *window;
1809 CODE:
1810 0 window = ub_http1_window_from_sv(aTHX_ self);
1811 0 ▶ RETVAL = (UV)(window->length - window->offset);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)(window->length - window->offset); 0 0 0
1812 OUTPUT:
1813 RETVAL
1814  
1815 UV
1816 consumed(self)
1817 SV *self
1818 PREINIT:
1819 ub_http1_borrowed_window *window;
1820 CODE:
1821 0 window = ub_http1_window_from_sv(aTHX_ self);
1822 0 ▶ RETVAL = (UV)window->offset;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)window->offset; 0 0 0
1823 OUTPUT:
1824 RETVAL
1825  
1826 SV *
1827 take(self, length)
1828 SV *self
1829 UV length
1830 PREINIT:
1831 ub_http1_borrowed_window *window;
1832 size_t remaining;
1833 CODE:
1834 0 window = ub_http1_window_from_sv(aTHX_ self);
1835 0 remaining = window->length - window->offset;
1836 0 ▶ if (length > (UV)remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (length > (UV)remaining) 0 0 0
1837 0 croak("borrowed input take exceeds remaining window");
1838 0 RETVAL = newSVpvn(window->data + window->offset, (STRLEN)length);
1839 0 window->offset += (size_t)length;
1840 OUTPUT:
1841 RETVAL
1842  
1843 void
1844 discard(self, length)
1845 SV *self
1846 UV length
1847 PREINIT:
1848 ub_http1_borrowed_window *window;
1849 size_t remaining;
1850 CODE:
1851 0 window = ub_http1_window_from_sv(aTHX_ self);
1852 0 remaining = window->length - window->offset;
1853 0 ▶ if (length > (UV)remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (length > (UV)remaining) 0 0 0
1854 0 croak("borrowed input discard exceeds remaining window");
1855 0 window->offset += (size_t)length;
1856  
1857 void
1858 clear(self)
1859 SV *self
1860 PREINIT:
1861 ub_http1_borrowed_window *window;
1862 CODE:
1863 0 window = ub_http1_window_from_sv(aTHX_ self);
1864 0 window->offset = window->length;
1865  
1866 SV *
1867 remaining_bytes(self)
1868 SV *self
1869 PREINIT:
1870 ub_http1_borrowed_window *window;
1871 size_t remaining;
1872 CODE:
1873 0 window = ub_http1_window_from_sv(aTHX_ self);
1874 0 remaining = window->length - window->offset;
1875 0 RETVAL = newSVpvn(window->data + window->offset, (STRLEN)remaining);
1876 OUTPUT:
1877 RETVAL
1878  
1879 void
1880 DESTROY(self)
1881 SV *self
1882 PREINIT:
1883 SV *inner;
1884 ub_http1_borrowed_window *window;
1885 CODE:
1886 16 ▶ if (!SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) 0 16 16
1887 0 XSRETURN_EMPTY;
1888 16 inner = SvRV(self);
1889 16 window = INT2PTR(ub_http1_borrowed_window *, SvIV(inner));
1890 16 ▶ if (window == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (window == NULL) 0 16 16
1891 0 XSRETURN_EMPTY;
1892 16 Safefree(window);
1893 16 sv_setiv(inner, 0);
1894  
1895 MODULE = Unblock::HTTP1 PACKAGE = Unblock::HTTP1::_Native::Chunked
1896  
1897 SV *
1898 new(CLASS, max_chunk_extension_size = 16384)
1899 const char *CLASS
1900 UV max_chunk_extension_size
1901 PREINIT:
1902 struct phr_chunked_decoder *decoder;
1903 SV *inner;
1904 SV *obj;
1905 CODE:
1906 if (max_chunk_extension_size > (UV)SIZE_MAX)
1907 croak("max_chunk_extension_size exceeds native size range");
1908 27 Newxz(decoder, 1, struct phr_chunked_decoder);
1909 27 decoder->consume_trailer = 0;
1910 27 decoder->_max_chunk_ext_size = (size_t)max_chunk_extension_size;
1911 27 inner = newSViv(PTR2IV(decoder));
1912 27 obj = newRV_noinc(inner);
1913 27 sv_bless(obj, gv_stashpv(CLASS, GV_ADD));
1914 27 RETVAL = obj;
1915 OUTPUT:
1916 RETVAL
1917  
1918 void
1919 feed(self, input, emit = 1, offset = 0)
1920 SV *self
1921 SV *input
1922 int emit
1923 UV offset
1924 PREINIT:
1925 struct phr_chunked_decoder *decoder;
1926 SV *inner;
1927 STRLEN input_len;
1928 const char *input_bytes;
1929 char *scratch;
1930 size_t decoded_len;
1931 ssize_t result;
1932 size_t leftover;
1933 PPCODE:
1934 64 ▶ if (!SvROK(self) || !sv_derived_from(self, "Unblock::HTTP1::_Native::Chunked"))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || !sv_derived_from(self, "Unblock::HTTP1::_Native::Chunked")) 64 0 64
if (!SvROK(self) || !sv_derived_from(self, "Unblock::HTTP1::_Native::Chunked")) 0 64 64
1935 0 croak("not an Unblock::HTTP1 chunked decoder object");
1936 64 inner = SvRV(self);
1937 64 decoder = INT2PTR(struct phr_chunked_decoder *, SvIV(inner));
1938 64 ▶ if (!decoder) croak("chunked decoder has already been released");
Branch: 1 branch
BranchTrueFalseTotal
if (!decoder) croak("chunked decoder has already been released"); 0 64 64
1939 64 input_bytes = ub_http1_buffer_view(aTHX_ input, &input_len);
1940 64 ▶ if (offset > (UV)input_len)
Branch: 1 branch
BranchTrueFalseTotal
if (offset > (UV)input_len) 0 64 64
1941 0 croak("offset exceeds input length");
1942 64 input_bytes += (size_t)offset;
1943 64 input_len -= (STRLEN)offset;
1944 64 ▶ Newx(scratch, input_len ? input_len : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(scratch, input_len ? input_len : 1, char); 64 0 64
1945 64 ▶ if (input_len) Copy(input_bytes, scratch, input_len, char);
Branch: 1 branch
BranchTrueFalseTotal
if (input_len) Copy(input_bytes, scratch, input_len, char); 64 0 64
1946 64 decoded_len = (size_t)input_len;
1947 64 result = phr_decode_chunked(decoder, scratch, &decoded_len);
1948 64 ▶ if (result == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (result == -1) { 7 57 64
1949 7 Safefree(scratch);
1950 7 croak("malformed HTTP/1 chunked body");
1951 }
1952 57 leftover = result >= 0 ? (size_t)result : 0;
1953 57 ▶ XPUSHs(sv_2mortal(newSViv(result >= 0 ? 1 : 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(result >= 0 ? 1 : 0))); 0 57 57
1954 57 ▶ if (emit)
Branch: 1 branch
BranchTrueFalseTotal
if (emit) 57 0 57
1955 57 ▶ XPUSHs(sv_2mortal(newSVpvn(scratch, (STRLEN)decoded_len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(scratch, (STRLEN)decoded_len))); 0 57 57
1956 else
1957 0 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 0 0
1958 57 ▶ if (leftover)
Branch: 1 branch
BranchTrueFalseTotal
if (leftover) 17 40 57
1959 17 ▶ XPUSHs(sv_2mortal(newSVpvn(scratch + decoded_len, (STRLEN)leftover)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(scratch + decoded_len, (STRLEN)leftover))); 0 17 17
1960 else
1961 40 ▶ XPUSHs(sv_2mortal(newSVpvn("", 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn("", 0))); 0 40 40
1962 57 Safefree(scratch);
1963  
1964 void
1965 DESTROY(self)
1966 SV *self
1967 PREINIT:
1968 SV *inner;
1969 struct phr_chunked_decoder *decoder;
1970 CODE:
1971 27 ▶ if (!SvROK(self)) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) XSRETURN_EMPTY; 0 27 27
1972 27 inner = SvRV(self);
1973 27 decoder = INT2PTR(struct phr_chunked_decoder *, SvIV(inner));
1974 27 ▶ if (!decoder) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!decoder) XSRETURN_EMPTY; 0 27 27
1975 27 Safefree(decoder);
1976 27 sv_setiv(inner, 0);
« vendor/picohttpparser/picohttpparser.c ↑ Summary blib/lib/Unblock/HTTP1.pm »