feersum_utils.c.inc

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