feersum_utils.c.inc

filter: Statementstmt 83.1% 188 / 226 Branchbran 65.7% 121 / 184 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.3% 309 / 410 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« feersum_psgi.c.inc ↑ Summary feersum_h1.c.inc »
1 char DATE_BUF[DATE_HEADER_LENGTH+1] = "Date: Thu, 01 Jan 1970 00:00:00 GMT\015\012";
2  
3 char header_key_buf[HEADER_KEY_BUFSZ] = "HTTP_";
4  
5 int FEERSUM_FREELIST_MAX = 32;
6  
7 struct iomatrix *iomatrix_freelist = NULL;
8 int iomatrix_freelist_count = 0;
9 struct feer_req *feer_req_freelist = NULL;
10 int feer_req_freelist_count = 0;
11  
12 HV *feer_stash, *feer_conn_stash;
13 HV *feer_conn_reader_stash = NULL, *feer_conn_writer_stash = NULL;
14 MGVTBL psgix_io_vtbl;
15 struct feer_server *default_server = NULL;
16 struct ev_loop *feersum_ev_loop = NULL;
17 ev_timer date_timer;
18 int date_timer_refs = 0;
19 AV *psgi_ver;
20 SV *psgi_serv10, *psgi_serv11;
21 SV *method_GET, *method_POST, *method_HEAD, *method_PUT, *method_PATCH, *method_DELETE, *method_OPTIONS;
22 SV *status_200, *status_201, *status_204, *status_301, *status_302, *status_304;
23 SV *status_400, *status_404, *status_500;
24 SV *empty_query_sv;
25 SV *psgi_env_version = NULL;
26 SV *psgi_env_errors = NULL;
27  
28 const unsigned char ascii_lower[256] = {
29 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
30 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
31 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
32 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
33 64,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
34 'p','q','r','s','t','u','v','w','x','y','z',91,92,93,94,95,
35 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
36 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
37 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
38 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
39 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
40 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
41 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
42 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
43 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
44 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
45 };
46  
47 const unsigned char ascii_upper[256] = {
48 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
49 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
50 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
51 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
52 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
53 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
54 96,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
55 'P','Q','R','S','T','U','V','W','X','Y','Z',123,124,125,126,127,
56 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
57 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
58 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
59 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
60 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
61 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
62 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
63 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
64 };
65  
66 const unsigned char ascii_upper_dash[256] = {
67 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
68 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
69 32,33,34,35,36,37,38,39,40,41,42,43,44,'_',46,47,
70 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
71 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
72 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
73 96,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
74 'P','Q','R','S','T','U','V','W','X','Y','Z',123,124,125,126,127,
75 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
76 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
77 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
78 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
79 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
80 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
81 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
82 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
83 };
84  
85 const unsigned char ascii_lower_dash[256] = {
86 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
87 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
88 32,33,34,35,36,37,38,39,40,41,42,43,44,'_',46,47,
89 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
90 64,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
91 'p','q','r','s','t','u','v','w','x','y','z',91,92,93,94,95,
92 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
93 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
94 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
95 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
96 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
97 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
98 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
99 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
100 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
101 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
102 };
103  
104 const unsigned char hex_decode_table[256] = {
105 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
106 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
107 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
108 0,1,2,3,4,5,6,7,8,9,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
109 0xFF,10,11,12,13,14,15,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
110 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
111 0xFF,10,11,12,13,14,15,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
112 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
113 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
114 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
115 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
116 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
117 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
118 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
119 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
120 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
121 };
122  
123 // Returns 0-15 for valid hex digit, -1 for invalid
124 #define hex_decode(ch) ((int)(signed char)hex_decode_table[(unsigned char)(ch)])
125  
126 /* neither side pre-lowered; lengths already checked */
127 INLINE_UNLESS_DEBUG bool
128 16 str_case_eq_both(const char *a, const char *b, size_t len)
129 {
130 116 ▶ for (size_t i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < len; i++) 104 12 116
131 104 ▶ if (ascii_lower[(unsigned char)a[i]] != ascii_lower[(unsigned char)b[i]]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ascii_lower[(unsigned char)a[i]] != ascii_lower[(unsigned char)b[i]]) return 0; 4 100 104
132 12 return 1;
133 }
134  
135 /* a must be a pre-lowered literal; only b is lowered */
136 INLINE_UNLESS_DEBUG bool
137 4890 str_case_eq_fixed(const char *a, const char *b, size_t len)
138 {
139 41966 ▶ for (size_t i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < len; i++) 37410 4556 41966
140 37410 ▶ if (a[i] != ascii_lower[(unsigned char)b[i]]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a[i] != ascii_lower[(unsigned char)b[i]]) return 0; 334 37076 37410
141 4556 return 1;
142 }
143  
144 /* Strict 1*DIGIT with OWS; grok_number would accept "0 but true" */
145 static bool
146 252 feer_parse_content_length(const char *p, size_t len, UV *out)
147 {
148 252 const char *end = p + len;
149 252 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 250 2 252
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 250 250
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 250 250
150 252 ▶ while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--;
Branch: 3 branches
BranchTrueFalseTotal
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 250 2 252
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 0 250 250
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 0 250 250
151 252 ▶ if (p == end) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (p == end) return false; 2 250 252
152  
153 250 UV value = 0;
154 731 ▶ for (; p < end; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; p < end; p++) { 488 243 731
155 488 ▶ if (*p < '0' || *p > '9') return false;
Branch: 2 branches
BranchTrueFalseTotal
if (*p < '0' || *p > '9') return false; 484 4 488
if (*p < '0' || *p > '9') return false; 3 481 484
156 481 UV digit = (UV)(*p - '0');
157 481 ▶ if (value > (UV_MAX - digit) / 10) return false;
Branch: 1 branch
BranchTrueFalseTotal
if (value > (UV_MAX - digit) / 10) return false; 0 481 481
158 481 value = value * 10 + digit;
159 }
160 243 *out = value;
161 243 return true;
162 }
163  
164 static bool
165 1050 feer_header_has_token(const char *vp, STRLEN vlen, const char *tok, STRLEN tlen)
166 {
167 1050 STRLEN i = 0;
168 1071 ▶ while (i < vlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < vlen) { 1050 21 1071
169 1050 ▶ while (i < vlen && (vp[i] == ',' || vp[i] == ' ' || vp[i] == '\t'))
Branch: 4 branches
BranchTrueFalseTotal
while (i < vlen && (vp[i] == ',' || vp[i] == ' ' || vp[i] == '\t')) 1050 0 1050
while (i < vlen && (vp[i] == ',' || vp[i] == ' ' || vp[i] == '\t')) 0 1050 1050
while (i < vlen && (vp[i] == ',' || vp[i] == ' ' || vp[i] == '\t')) 0 1050 1050
while (i < vlen && (vp[i] == ',' || vp[i] == ' ' || vp[i] == '\t')) 0 1050 1050
170 0 i++;
171 1050 STRLEN start = i;
172 6380 ▶ while (i < vlen && vp[i] != ',')
Branch: 2 branches
BranchTrueFalseTotal
while (i < vlen && vp[i] != ',') 5330 1050 6380
while (i < vlen && vp[i] != ',') 5330 0 5330
173 5330 i++;
174 1050 STRLEN end = i;
175 1050 ▶ while (end > start && (vp[end-1] == ' ' || vp[end-1] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (end > start && (vp[end-1] == ' ' || vp[end-1] == '\t')) 1050 0 1050
while (end > start && (vp[end-1] == ' ' || vp[end-1] == '\t')) 0 1050 1050
while (end > start && (vp[end-1] == ' ' || vp[end-1] == '\t')) 0 1050 1050
176 0 end--;
177 1050 ▶ if (end - start == tlen && str_case_eq_fixed(tok, vp + start, tlen))
Branch: 2 branches
BranchTrueFalseTotal
if (end - start == tlen && str_case_eq_fixed(tok, vp + start, tlen)) 1029 21 1050
if (end - start == tlen && str_case_eq_fixed(tok, vp + start, tlen)) 1029 0 1029
178 1029 return true;
179 }
180 21 return false;
181 }
182  
183 /* memmem(3) is not portable and core ninstr is not public API on every perl */
184 static const char *
185 0 feer_memfind(const char *hay, size_t hay_len, const char *needle, size_t n_len)
186 {
187 0 const char *p = hay, *end;
188 0 ▶ if (n_len == 0 || hay_len < n_len) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (n_len == 0 || hay_len < n_len) return NULL; 0 0 0
if (n_len == 0 || hay_len < n_len) return NULL; 0 0 0
189 0 end = hay + (hay_len - n_len); /* last valid match start */
190 0 ▶ while (p <= end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p <= end) { 0 0 0
191 0 p = (const char *)memchr(p, needle[0], (size_t)(end - p) + 1);
192 0 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 0 0
193 0 ▶ if (memcmp(p, needle, n_len) == 0) return p;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(p, needle, n_len) == 0) return p; 0 0 0
194 0 p++;
195 }
196 0 return NULL;
197 }
198  
199 static void
200 1134 uri_decode_sv (SV *sv)
201 {
202 STRLEN len;
203 char *ptr, *end, *decoded;
204  
205 1134 ptr = SvPV(sv, len);
206 1134 end = SvEND(sv);
207  
208 24083 ▶ while (ptr < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (ptr < end) { 22952 1131 24083
209 22952 ▶ if (unlikely(*ptr == '%')) goto needs_decode;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(*ptr == '%')) goto needs_decode; 3 22949 22952
210 22949 ptr++;
211 }
212 1131 return;
213  
214 3 needs_decode:
215  
216 // in place: everything before the first % is already decoded
217 3 decoded = ptr;
218  
219 31 ▶ for (; ptr < end; ptr++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; ptr < end; ptr++) { 28 3 31
220 28 ▶ if (unlikely(*ptr == '%') && likely(end - ptr > 2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(*ptr == '%') && likely(end - ptr > 2)) { 9 19 28
if (unlikely(*ptr == '%') && likely(end - ptr > 2)) { 8 1 9
221 8 int c1 = hex_decode(ptr[1]);
222 8 int c2 = hex_decode(ptr[2]);
223 8 ▶ if (likely(c1 != -1 && c2 != -1)) {
Branch: 2 branches
BranchTrueFalseTotal
if (likely(c1 != -1 && c2 != -1)) { 7 1 8
if (likely(c1 != -1 && c2 != -1)) { 7 0 7
224 7 *decoded++ = (c1 << 4) + c2;
225 7 ptr += 2;
226 7 continue;
227 }
228 }
229 21 *decoded++ = *ptr;
230 }
231  
232 3 *decoded = '\0';
233  
234 3 SvCUR_set(sv, decoded - SvPVX(sv));
235 }
236  
237 static void
238 1139 feersum_set_conn_remote_info(pTHX_ struct feer_conn *c)
239 {
240 1139 ▶ if (c->remote_addr) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->remote_addr) return; 147 992 1139
241 992 struct sockaddr *sa = (struct sockaddr *)&c->sa;
242 992 switch (sa->sa_family) {
243 986 case AF_INET: {
244 986 struct sockaddr_in *in = (struct sockaddr_in *)sa;
245 char buf[INET_ADDRSTRLEN];
246 986 ▶ if (inet_ntop(AF_INET, &in->sin_addr, buf, sizeof(buf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_ntop(AF_INET, &in->sin_addr, buf, sizeof(buf))) { 986 0 986
247 986 c->remote_addr = newSVpv(buf, 0);
248 } else {
249 0 c->remote_addr = newSVpvs("0.0.0.0");
250 }
251 986 c->remote_port = newSViv(ntohs(in->sin_port));
252 986 break;
253 }
254 #ifdef AF_INET6
255 4 case AF_INET6: {
256 4 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
257 char buf[INET6_ADDRSTRLEN];
258 4 ▶ if (inet_ntop(AF_INET6, &in6->sin6_addr, buf, sizeof(buf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (inet_ntop(AF_INET6, &in6->sin6_addr, buf, sizeof(buf))) { 4 0 4
259 4 c->remote_addr = newSVpv(buf, 0);
260 } else {
261 0 c->remote_addr = newSVpvs("::");
262 }
263 4 c->remote_port = newSViv(ntohs(in6->sin6_port));
264 4 break;
265 }
266 #endif
267 #ifdef AF_UNIX
268 2 case AF_UNIX:
269 2 c->remote_addr = newSVpvs("unix");
270 2 c->remote_port = newSViv(0);
271 2 break;
272 #endif
273 0 default:
274 0 c->remote_addr = newSVpvs("unspec");
275 0 c->remote_port = newSViv(0);
276 0 break;
277 }
278 }
279  
280 static SV*
281 1123 feersum_env_method(pTHX_ struct feer_req *r)
282 {
283 1123 switch (r->method_len) {
284 927 case 3:
285 927 ▶ if (likely(memcmp(r->method, "GET", 3) == 0))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(memcmp(r->method, "GET", 3) == 0)) 927 0 927
286 927 return SvREFCNT_inc_simple_NN(method_GET);
287 0 ▶ if (memcmp(r->method, "PUT", 3) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(r->method, "PUT", 3) == 0) 0 0 0
288 0 return SvREFCNT_inc_simple_NN(method_PUT);
289 0 break;
290 192 case 4:
291 192 ▶ if (likely(memcmp(r->method, "POST", 4) == 0))
Branch: 1 branch
BranchTrueFalseTotal
if (likely(memcmp(r->method, "POST", 4) == 0)) 156 36 192
292 156 return SvREFCNT_inc_simple_NN(method_POST);
293 36 ▶ if (memcmp(r->method, "HEAD", 4) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(r->method, "HEAD", 4) == 0) 36 0 36
294 36 return SvREFCNT_inc_simple_NN(method_HEAD);
295 0 break;
296 1 case 5:
297 1 ▶ if (memcmp(r->method, "PATCH", 5) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(r->method, "PATCH", 5) == 0) 1 0 1
298 1 return SvREFCNT_inc_simple_NN(method_PATCH);
299 0 break;
300 1 case 6:
301 1 ▶ if (memcmp(r->method, "DELETE", 6) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(r->method, "DELETE", 6) == 0) 1 0 1
302 1 return SvREFCNT_inc_simple_NN(method_DELETE);
303 0 break;
304 2 case 7:
305 2 ▶ if (memcmp(r->method, "OPTIONS", 7) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(r->method, "OPTIONS", 7) == 0) 2 0 2
306 2 return SvREFCNT_inc_simple_NN(method_OPTIONS);
307 0 break;
308 }
309 0 return newSVpvn(r->method, r->method_len);
310 }
311  
312 INLINE_UNLESS_DEBUG static SV*
313 1097 feersum_env_uri(pTHX_ struct feer_req *r)
314 {
315 1097 return newSVpvn(r->uri, r->uri_len);
316 }
317  
318 INLINE_UNLESS_DEBUG static SV*
319 1099 feersum_env_protocol(pTHX_ struct feer_req *r)
320 {
321 1099 ▶ return (r->minor_version == 1) ? psgi_serv11 : psgi_serv10;
Branch: 1 branch
BranchTrueFalseTotal
return (r->minor_version == 1) ? psgi_serv11 : psgi_serv10; 898 201 1099
322 }
323  
324 static const char *
325 2231 feer_absolute_authority(const char *uri, STRLEN uri_len, STRLEN *authority_len)
326 {
327 2231 ▶ if (!uri_len || uri[0] == '/') return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!uri_len || uri[0] == '/') return NULL; 2231 0 2231
if (!uri_len || uri[0] == '/') return NULL; 2219 12 2231
328 12 const char *p = uri, *end = uri + uri_len;
329 /* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */
330 54 ▶ while (p < end && ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')
Branch: 5 branches
BranchTrueFalseTotal
while (p < end && ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') 54 0 54
while (p < end && ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') 42 12 54
while (p < end && ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') 42 0 42
while (p < end && ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') 0 12 12
while (p < end && ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') 0 0 0
331 12 ▶ || (*p >= '0' && *p <= '9')
Branch: 2 branches
BranchTrueFalseTotal
|| (*p >= '0' && *p <= '9') 10 2 12
|| (*p >= '0' && *p <= '9') 0 10 10
332 54 ▶ || *p == '+' || *p == '-' || *p == '.')) p++;
Branch: 3 branches
BranchTrueFalseTotal
|| *p == '+' || *p == '-' || *p == '.')) p++; 0 12 12
|| *p == '+' || *p == '-' || *p == '.')) p++; 0 12 12
|| *p == '+' || *p == '-' || *p == '.')) p++; 0 12 12
333 12 ▶ if (p == uri || end - p < 3 || p[0] != ':' || p[1] != '/' || p[2] != '/')
Branch: 5 branches
BranchTrueFalseTotal
if (p == uri || end - p < 3 || p[0] != ':' || p[1] != '/' || p[2] != '/') 10 2 12
if (p == uri || end - p < 3 || p[0] != ':' || p[1] != '/' || p[2] != '/') 10 0 10
if (p == uri || end - p < 3 || p[0] != ':' || p[1] != '/' || p[2] != '/') 10 0 10
if (p == uri || end - p < 3 || p[0] != ':' || p[1] != '/' || p[2] != '/') 10 0 10
if (p == uri || end - p < 3 || p[0] != ':' || p[1] != '/' || p[2] != '/') 0 10 10
334 2 return NULL;
335 10 const char *authority = p + 3;
336 10 p = authority;
337 130 ▶ while (p < end && *p != '/' && *p != '?') p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p != '/' && *p != '?') p++; 128 2 130
while (p < end && *p != '/' && *p != '?') p++; 122 6 128
while (p < end && *p != '/' && *p != '?') p++; 120 2 122
338 10 *authority_len = p - authority;
339 10 return authority;
340 }
341  
342 INLINE_UNLESS_DEBUG static void
343 1136 feersum_set_path_and_query(pTHX_ struct feer_req *r)
344 {
345 1136 const char *uri = r->uri;
346 1136 STRLEN uri_len = r->uri_len;
347  
348 /* absolute-form (RFC 9112 3.2.2): drop scheme://authority so PATH_INFO
349 * is the path; REQUEST_URI keeps the raw target */
350 STRLEN authority_len;
351 1136 const char *authority = feer_absolute_authority(uri, uri_len, &authority_len);
352 1136 ▶ if (unlikely(authority != NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(authority != NULL)) { 5 1131 1136
353 5 const char *q = authority + authority_len, *end = uri + uri_len;
354 5 ▶ if (q < end && *q == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == '/') { 4 1 5
if (q < end && *q == '/') { 3 1 4
355 3 uri = q;
356 3 uri_len = end - q;
357 }
358 else {
359 /* no path segment: "http://h" -> "/", "http://h?x" -> "/?x" */
360 2 r->path = newSVpvs("/");
361 1 r->query = (q < end) ? newSVpvn(q + 1, end - (q + 1))
362 2 ▶ : SvREFCNT_inc_simple_NN(empty_query_sv);
Branch: 1 branch
BranchTrueFalseTotal
: SvREFCNT_inc_simple_NN(empty_query_sv); 1 1 2
363 2 return;
364 }
365 }
366  
367 1134 const char *qpos = (const char *)memchr(uri, '?', uri_len);
368 1134 ▶ if (qpos != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (qpos != NULL) { 25 1109 1134
369 25 r->path = newSVpvn(uri, (qpos - uri));
370 25 qpos++;
371 25 r->query = newSVpvn(qpos, uri_len - (qpos - uri));
372 } else {
373 1109 r->path = newSVpvn(uri, uri_len);
374 1109 r->query = SvREFCNT_inc_simple_NN(empty_query_sv);
375 }
376 1134 uri_decode_sv(r->path);
377 }
378  
379 INLINE_UNLESS_DEBUG static SV*
380 84 feersum_env_path(pTHX_ struct feer_req *r)
381 {
382 84 ▶ if (unlikely(!r->path)) feersum_set_path_and_query(aTHX_ r);
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!r->path)) feersum_set_path_and_query(aTHX_ r); 42 42 84
383 84 return r->path;
384 }
385  
386 INLINE_UNLESS_DEBUG static SV*
387 28 feersum_env_query(pTHX_ struct feer_req *r)
388 {
389 28 ▶ if (unlikely(!r->query)) feersum_set_path_and_query(aTHX_ r);
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!r->query)) feersum_set_path_and_query(aTHX_ r); 0 28 28
390 28 return r->query;
391 }
392  
393 INLINE_UNLESS_DEBUG static SV*
394 38 feersum_env_addr(pTHX_ struct feer_conn *c)
395 {
396 38 feersum_set_conn_remote_info(aTHX_ c);
397 38 return c->remote_addr;
398 }
399  
400 INLINE_UNLESS_DEBUG static SV*
401 6 feersum_env_port(pTHX_ struct feer_conn *c)
402 {
403 6 feersum_set_conn_remote_info(aTHX_ c);
404 6 return c->remote_port;
405 }
406  
407 static const char*
408 35 find_header_value(struct feer_req *r, const char *name, size_t name_len, size_t *value_len)
409 {
410 152 ▶ for (size_t i = 0; i < r->num_headers; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < r->num_headers; i++) { 139 13 152
411 139 struct phr_header *hdr = &r->headers[i];
412 // a NULL name is an obs-fold continuation
413 161 ▶ if (hdr->name && hdr->name_len == name_len &&
Branch: 1 branch
BranchTrueFalseTotal
if (hdr->name && hdr->name_len == name_len && 139 0 139
414 22 str_case_eq_fixed(name, hdr->name, name_len))
415 {
416 22 *value_len = hdr->value_len;
417 22 return hdr->value;
418 }
419 }
420 13 *value_len = 0;
421 13 return NULL;
422 }
423  
424 static int
425 9 feer_socketpair_nb(int sv[2])
426 {
427 #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
428 9 ▶ if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, sv) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, sv) == 0) 9 0 9
429 9 return 0;
430 0 ▶ if (errno != EINVAL) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (errno != EINVAL) return -1; 0 0 0
431 /* fallthrough: kernel too old for SOCK_CLOEXEC/SOCK_NONBLOCK */
432 #endif
433 0 ▶ if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) 0 0 0
434 0 return -1;
435 0 if (fcntl(sv[0], F_SETFL, O_NONBLOCK) < 0 ||
436 0 ▶ fcntl(sv[1], F_SETFL, O_NONBLOCK) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
fcntl(sv[1], F_SETFL, O_NONBLOCK) < 0 || 0 0 0
437 0 ▶ fcntl(sv[0], F_SETFD, FD_CLOEXEC) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
fcntl(sv[0], F_SETFD, FD_CLOEXEC) < 0 || 0 0 0
438 0 fcntl(sv[1], F_SETFD, FD_CLOEXEC) < 0)
439 {
440 0 close(sv[0]);
441 0 close(sv[1]);
442 0 return -1;
443 }
444 0 return 0;
445 }
446  
447 static SV*
448 176 newSV_buf(STRLEN size)
449 {
450 176 SV *sv = newSV(size);
451 176 SvPOK_on(sv);
452 176 SvCUR_set(sv, 0);
453 176 ▶ if (size > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (size > 0) 176 0 176
454 176 *SvPVX(sv) = '\0';
455 176 return sv;
456 }
457  
458 static time_t last_generated_time = 0;
459  
460 1236 static void date_timer_cb(EV_P_ ev_timer *w, int revents) {
461 PERL_UNUSED_VAR(EV_A);
462 PERL_UNUSED_VAR(revents);
463 PERL_UNUSED_VAR(w);
464  
465 time_t now;
466 #if defined(__linux__) && defined(CLOCK_REALTIME_COARSE)
467 struct timespec ts;
468 1236 ▶ if (likely(clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(clock_gettime(CLOCK_REALTIME_COARSE, &ts) == 0)) { 1236 0 1236
469 1236 now = ts.tv_sec;
470 } else {
471 0 now = time(NULL);
472 }
473 #else
474 now = time(NULL);
475 #endif
476 1236 ▶ if (now == last_generated_time) return;
Branch: 1 branch
BranchTrueFalseTotal
if (now == last_generated_time) return; 182 1054 1236
477  
478 1054 last_generated_time = now;
479 struct tm tm_buf;
480 1054 ▶ if (unlikely(!gmtime_r(&now, &tm_buf))) return;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!gmtime_r(&now, &tm_buf))) return; 0 1054 1054
481  
482 /* RFC 7231 requires English day/month names; strftime is locale-dependent */
483 static const char *days[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
484 static const char *months[] = {"Jan","Feb","Mar","Apr","May","Jun",
485 "Jul","Aug","Sep","Oct","Nov","Dec"};
486 1054 snprintf(DATE_BUF + 6, DATE_VALUE_LENGTH + 1,
487 "%s, %02d %s %04d %02d:%02d:%02d GMT",
488 1054 days[tm_buf.tm_wday], tm_buf.tm_mday, months[tm_buf.tm_mon],
489 1054 tm_buf.tm_year + 1900, tm_buf.tm_hour, tm_buf.tm_min, tm_buf.tm_sec);
490 1054 DATE_BUF[DATE_HEADER_LENGTH-2] = '\r';
491 1054 DATE_BUF[DATE_HEADER_LENGTH-1] = '\n';
492 }
493  
494 static int
495 872 format_content_length(char *buf, size_t len)
496 {
497 static const char prefix[] = "Content-Length: ";
498 872 memcpy(buf, prefix, 16);
499  
500 872 char *p = buf + 16;
501  
502 872 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 1 871 872
503 1 *p++ = '0';
504 } else {
505 char tmp[20];
506 871 char *t = tmp;
507 2043 ▶ while (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (len > 0) { 1172 871 2043
508 1172 *t++ = '0' + (len % 10);
509 1172 len /= 10;
510 }
511 2043 ▶ while (t > tmp) {
Branch: 1 branch
BranchTrueFalseTotal
while (t > tmp) { 1172 871 2043
512 1172 *p++ = *--t;
513 }
514 }
515  
516 872 *p++ = '\r'; *p++ = '\n'; *p++ = '\r'; *p++ = '\n';
517 872 return p - buf;
518 }
519  
520 #ifdef FEERSUM_HAS_OUTQ
521 /* bytes the kernel still holds (unsent + unacked); -1 when it cannot say */
522 static int
523 6 feer_sock_outq(int fd)
524 {
525 int n;
526 # if defined(__linux__) && defined(TIOCOUTQ)
527 6 ▶ if (ioctl(fd, TIOCOUTQ, &n) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(fd, TIOCOUTQ, &n) < 0) return -1; 0 6 6
528 # elif defined(SO_NWRITE)
529 socklen_t len = sizeof(n);
530 if (getsockopt(fd, SOL_SOCKET, SO_NWRITE, &n, &len) < 0) return -1;
531 # elif defined(FIONWRITE)
532 if (ioctl(fd, FIONWRITE, &n) < 0) return -1;
533 # endif
534 6 return n < 0 ? -1 : n;
535 }
536 #endif
« feersum_psgi.c.inc ↑ Summary feersum_h1.c.inc »