Proxy.xs

filter: Statementstmt 93.5% 652 / 697 Branchbran 59.2% 468 / 790 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.3% 1120 / 1487 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Reverse/Proxy.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "rp_compat.h" /* pre-5.16 perl shims (XS_INTERNAL, mg_findext) */
6 #include "fetch_abi.h" /* vendored copy; runtime version-checked in BOOT */
7  
8 /* Raw sockets for the WebSocket/Upgrade tunnel (Unix). On Windows the tunnel
9 * is unsupported and returns 501 (Hyperman, which provides psgix.io, is
10 * Unix-first anyway). */
11 #ifndef WIN32
12 # include <sys/types.h>
13 # include <sys/socket.h>
14 # include <netdb.h>
15 # include <unistd.h>
16 # include <errno.h>
17 # include <fcntl.h>
18 #endif
19  
20 /* Resolved once at boot from Fetch::_abi_ptr and version-checked. NULL means
21 * the running Fetch is too old to provide the ABI; Reverse::Proxy.pm treats
22 * that as a hard load-time error (there is no pure-Perl fallback). */
23 static const fetch_abi *FETCH = NULL;
24 static int FETCH_SEEN = 0; /* the provider's abi_version, 0 if unresolved */
25  
26 /* ---- a tiny closure: a coderef carrying captured SVs -------------------- *
27 * Lets to_app and the psgi.streaming app be built in C rather than Perl. The
28 * captured args live in an AV attached to an anonymous CV by magic. */
29 typedef struct rp_clos { AV *cap; } rp_clos;
30  
31 15 static int rp_clos_free(pTHX_ SV *sv, MAGIC *mg) {
32 15 rp_clos *c = (rp_clos *)mg->mg_ptr;
33 PERL_UNUSED_ARG(sv);
34 15 ▶ if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); }
Branch: 2 branches
BranchTrueFalseTotal
if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } 15 0 15
if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } 15 0 15
35 15 return 0;
36 }
37 static MGVTBL rp_clos_vtbl = { NULL, NULL, NULL, NULL, rp_clos_free,
38 NULL, NULL, NULL };
39  
40 15 static SV *rp_closure(pTHX_ XSUBADDR_t body, AV *cap) {
41 15 CV *cv = (CV *)newXS(NULL, body, __FILE__);
42 rp_clos *c;
43 15 Newxz(c, 1, rp_clos);
44 15 c->cap = cap; /* takes ownership */
45 15 sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &rp_clos_vtbl, (char *)c, 0);
46 15 return newRV_noinc((SV *)cv);
47 }
48 38 static AV *rp_clos_cap(pTHX_ CV *cv) {
49 38 MAGIC *mg = mg_findext((SV *)cv, PERL_MAGIC_ext, &rp_clos_vtbl);
50 38 ▶ return mg ? ((rp_clos *)mg->mg_ptr)->cap : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return mg ? ((rp_clos *)mg->mg_ptr)->cap : NULL; 38 0 38
51 }
52  
53 /* ---- hop-by-hop and X-Forwarded-* name sets (lowercased) ---------------- */
54  
55 107 static int rp_is_hop(const char *s, STRLEN n) {
56 107 switch (n) {
57 1 case 2: return memEQ(s, "te", 2);
58 1 ▶ case 7: return memEQ(s, "upgrade", 7) || memEQ(s, "trailer", 7);
Branch: 2 branches
BranchTrueFalseTotal
case 7: return memEQ(s, "upgrade", 7) || memEQ(s, "trailer", 7); 0 1 1
case 7: return memEQ(s, "upgrade", 7) || memEQ(s, "trailer", 7); 0 0 0
59 40 ▶ case 10: return memEQ(s, "connection", 10) || memEQ(s, "keep-alive", 10);
Branch: 2 branches
BranchTrueFalseTotal
case 10: return memEQ(s, "connection", 10) || memEQ(s, "keep-alive", 10); 9 31 40
case 10: return memEQ(s, "connection", 10) || memEQ(s, "keep-alive", 10); 3 6 9
60 2 case 17: return memEQ(s, "transfer-encoding", 17);
61 0 case 18: return memEQ(s, "proxy-authenticate", 18);
62 0 case 19: return memEQ(s, "proxy-authorization", 19);
63 63 default: return 0;
64 }
65 }
66  
67 9 static int rp_is_xfwd(const char *s, STRLEN n) {
68 9 switch (n) {
69 1 case 15: return memEQ(s, "x-forwarded-for", 15);
70 0 case 16: return memEQ(s, "x-forwarded-host", 16);
71 2 case 17: return memEQ(s, "x-forwarded-proto", 17);
72 6 default: return 0;
73 }
74 }
75  
76 /* Lowercase into caller buffer (len n, no NUL needed by callers here). */
77 101 static void rp_lc(const char *src, STRLEN n, char *dst) {
78 STRLEN i;
79 1291 ▶ for (i = 0; i < n; i++) dst[i] = (char)toLOWER((U8)src[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) dst[i] = (char)toLOWER((U8)src[i]); 171 1019 1190
for (i = 0; i < n; i++) dst[i] = (char)toLOWER((U8)src[i]); 1190 101 1291
80 101 }
81  
82 /* Push a header name+value pair (as mortal SVs) onto the flat AV. name/val are
83 * copied. */
84 38 static void rp_push(pTHX_ AV *av, const char *name, STRLEN nl,
85 const char *val, STRLEN vl) {
86 38 av_push(av, newSVpvn(name, nl));
87 38 av_push(av, newSVpvn(val, vl));
88 38 }
89  
90 /* Env string field, or NULL if missing/empty; sets *len. */
91 403 static const char *rp_env(pTHX_ HV *env, const char *key, STRLEN klen, STRLEN *len) {
92 403 SV **e = hv_fetch(env, key, (I32)klen, 0);
93 403 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 154 249 403
if (e && *e && SvOK(*e)) { 154 0 154
if (e && *e && SvOK(*e)) { 154 0 154
94 STRLEN l;
95 154 const char *p = SvPV_const(*e, l);
96 154 ▶ if (l) { *len = l; return p; }
Branch: 1 branch
BranchTrueFalseTotal
if (l) { *len = l; return p; } 125 29 154
97 }
98 278 *len = 0;
99 278 return NULL;
100 }
101  
102 /* ---- response shaper: Fetch response -> PSGI [status,\@hdrs,\@body] ------ */
103  
104 31 static SV *rp_map(pTHX_ int ok, int status, AV *headers, SV *body, SV *err,
105 void *ud) {
106 31 AV *resp = newAV();
107 PERL_UNUSED_ARG(ud);
108  
109 31 ▶ if (!ok) { /* 502, mirroring PP _bad_gateway */
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { /* 502, mirroring PP _bad_gateway */ 2 29 31
110 2 AV *h = newAV();
111 2 AV *b = newAV();
112 SV *msg;
113 2 av_push(h, newSVpvs("Content-Type"));
114 2 av_push(h, newSVpvs("text/plain"));
115 4 ▶ if (err && SvOK(err)) {
Branch: 2 branches
BranchTrueFalseTotal
if (err && SvOK(err)) { 2 0 2
if (err && SvOK(err)) { 2 0 2
116 2 STRLEN el; const char *ep = SvPV_const(err, el);
117 2 ▶ while (el && isSPACE((U8)ep[el - 1])) el--; /* rstrip */
Branch: 2 branches
BranchTrueFalseTotal
while (el && isSPACE((U8)ep[el - 1])) el--; /* rstrip */ 2 0 2
while (el && isSPACE((U8)ep[el - 1])) el--; /* rstrip */ 0 2 2
118 2 msg = newSVpvs("502 Bad Gateway: ");
119 2 sv_catpvn(msg, ep, el);
120 } else {
121 0 msg = newSVpvs("502 Bad Gateway: upstream request failed");
122 }
123 2 av_push(b, msg);
124 2 av_push(resp, newSViv(502));
125 2 av_push(resp, newRV_noinc((SV *)h));
126 2 av_push(resp, newRV_noinc((SV *)b));
127 2 return newRV_noinc((SV *)resp);
128 }
129  
130 {
131 29 AV *out = newAV();
132 29 AV *b = newAV();
133 29 ▶ SSize_t n = headers ? av_len(headers) + 1 : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = headers ? av_len(headers) + 1 : 0, i; 29 0 29
134 125 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 96 29 125
135 96 SV **k = av_fetch(headers, i, 0);
136 96 SV **v = av_fetch(headers, i + 1, 0);
137 96 ▶ if (k && *k) {
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) { 96 0 96
if (k && *k) { 96 0 96
138 96 STRLEN kl; const char *ks = SvPV_const(*k, kl);
139 96 char stackbuf[64], *lc = stackbuf;
140 int drop;
141 96 ▶ if (kl > sizeof(stackbuf)) Newx(lc, kl, char);
Branch: 1 branch
BranchTrueFalseTotal
if (kl > sizeof(stackbuf)) Newx(lc, kl, char); 0 96 96
142 96 rp_lc(ks, kl, lc);
143 96 drop = rp_is_hop(lc, kl);
144 96 ▶ if (lc != stackbuf) Safefree(lc);
Branch: 1 branch
BranchTrueFalseTotal
if (lc != stackbuf) Safefree(lc); 0 96 96
145 96 ▶ if (drop) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (drop) continue; 32 64 96
146 64 av_push(out, newSVsv(*k));
147 64 ▶ av_push(out, (v && *v) ? newSVsv(*v) : newSVpvs(""));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, (v && *v) ? newSVsv(*v) : newSVpvs("")); 64 0 64
av_push(out, (v && *v) ? newSVsv(*v) : newSVpvs("")); 64 0 64
148 }
149 }
150 29 ▶ av_push(b, body ? newSVsv(body) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
av_push(b, body ? newSVsv(body) : newSVpvs("")); 29 0 29
151 29 av_push(resp, newSViv(status));
152 29 av_push(resp, newRV_noinc((SV *)out));
153 29 av_push(resp, newRV_noinc((SV *)b));
154 29 return newRV_noinc((SV *)resp);
155 }
156 }
157  
158 /* ---- build the outbound header list from the PSGI env (== _req_headers) -- */
159  
160 32 static AV *rp_req_headers(pTHX_ HV *env, int preserve_host, SV *via) {
161 32 AV *hav = (AV *)sv_2mortal((SV *)newAV());
162 32 HV *conn_hop = (HV *)sv_2mortal((SV *)newHV());
163 HE *he;
164 STRLEN l;
165 const char *p;
166  
167 /* extra hop-by-hop names named in the client's own Connection header */
168 32 p = rp_env(aTHX_ env, "HTTP_CONNECTION", 15, &l);
169 32 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 1 31 32
170 1 const char *s = p, *end = p + l;
171 3 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 2 1 3
172 2 const char *t = s;
173 20 ▶ while (t < end && *t != ',') t++;
Branch: 2 branches
BranchTrueFalseTotal
while (t < end && *t != ',') t++; 19 1 20
while (t < end && *t != ',') t++; 18 1 19
174 { /* trim token [s,t) */
175 2 const char *a = s, *b = t;
176 2 char buf[64], *lc = buf; STRLEN tn;
177 3 ▶ while (a < b && isSPACE((U8)*a)) a++;
Branch: 2 branches
BranchTrueFalseTotal
while (a < b && isSPACE((U8)*a)) a++; 3 0 3
while (a < b && isSPACE((U8)*a)) a++; 1 2 3
178 2 ▶ while (b > a && isSPACE((U8)b[-1])) b--;
Branch: 2 branches
BranchTrueFalseTotal
while (b > a && isSPACE((U8)b[-1])) b--; 2 0 2
while (b > a && isSPACE((U8)b[-1])) b--; 0 2 2
179 2 tn = (STRLEN)(b - a);
180 2 ▶ if (tn) {
Branch: 1 branch
BranchTrueFalseTotal
if (tn) { 2 0 2
181 2 ▶ if (tn > sizeof(buf)) Newx(lc, tn, char);
Branch: 1 branch
BranchTrueFalseTotal
if (tn > sizeof(buf)) Newx(lc, tn, char); 0 2 2
182 2 rp_lc(a, tn, lc);
183 2 (void)hv_store(conn_hop, lc, (I32)tn, &PL_sv_yes, 0);
184 2 ▶ if (lc != buf) Safefree(lc);
Branch: 1 branch
BranchTrueFalseTotal
if (lc != buf) Safefree(lc); 0 2 2
185 }
186 }
187 2 s = t + 1;
188 }
189 }
190  
191 /* forward each HTTP_* header (skip Host, hop-by-hop, X-Forwarded-*) */
192 32 hv_iterinit(env);
193 333 ▶ while ((he = hv_iternext(env))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(env))) { 301 32 333
194 I32 kl;
195 301 const char *k = hv_iterkey(he, &kl);
196 char namebuf[128], lcbuf[128];
197 301 char *name = namebuf, *lc = lcbuf;
198 STRLEN nl;
199 SV *val;
200 STRLEN i;
201 301 ▶ if (kl < 6 || memNE(k, "HTTP_", 5)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl < 6 || memNE(k, "HTTP_", 5)) continue; 301 0 301
if (kl < 6 || memNE(k, "HTTP_", 5)) continue; 289 12 301
202 12 nl = (STRLEN)(kl - 5);
203 12 ▶ if (nl > sizeof(namebuf)) { Newx(name, nl, char); Newx(lc, nl, char); }
Branch: 1 branch
BranchTrueFalseTotal
if (nl > sizeof(namebuf)) { Newx(name, nl, char); Newx(lc, nl, char); } 0 12 12
204 105 ▶ for (i = 0; i < nl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nl; i++) { 93 12 105
205 93 char c = k[5 + i];
206 93 ▶ if (c == '_') c = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (c == '_') c = '-'; 8 85 93
207 93 name[i] = c;
208 93 ▶ lc[i] = (char)toLOWER((U8)c);
Branch: 1 branch
BranchTrueFalseTotal
lc[i] = (char)toLOWER((U8)c); 85 8 93
209 }
210 do {
211 12 ▶ if (nl == 4 && memEQ(lc, "host", 4)) break; /* handled below */
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 4 && memEQ(lc, "host", 4)) break; /* handled below */ 4 8 12
if (nl == 4 && memEQ(lc, "host", 4)) break; /* handled below */ 4 0 4
212 8 ▶ if (rp_is_hop(lc, nl) || rp_is_xfwd(lc, nl)) break;
Branch: 2 branches
BranchTrueFalseTotal
if (rp_is_hop(lc, nl) || rp_is_xfwd(lc, nl)) break; 5 3 8
if (rp_is_hop(lc, nl) || rp_is_xfwd(lc, nl)) break; 3 2 5
213 3 ▶ if (hv_exists(conn_hop, lc, (I32)nl)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(conn_hop, lc, (I32)nl)) break; 1 2 3
214 /* titlecase name in place: Upper after start/'-', lower else */
215 {
216 2 int at_start = 1; STRLEN j;
217 16 ▶ for (j = 0; j < nl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nl; j++) { 14 2 16
218 14 ▶ if (name[j] == '-') { at_start = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (name[j] == '-') { at_start = 1; } 2 12 14
219 20 ▶ else { name[j] = at_start ? (char)toUPPER((U8)name[j])
Branch: 2 branches
BranchTrueFalseTotal
else { name[j] = at_start ? (char)toUPPER((U8)name[j]) 4 8 12
else { name[j] = at_start ? (char)toUPPER((U8)name[j]) 0 4 4
220 8 ▶ : (char)toLOWER((U8)name[j]);
Branch: 1 branch
BranchTrueFalseTotal
: (char)toLOWER((U8)name[j]); 8 0 8
221 12 at_start = 0; }
222 }
223 }
224 2 val = hv_iterval(env, he);
225 {
226 2 STRLEN vl; const char *vp = SvPV_const(val, vl);
227 2 rp_push(aTHX_ hav, name, nl, vp, vl);
228 }
229 } while (0);
230 12 ▶ if (name != namebuf) { Safefree(name); Safefree(lc); }
Branch: 1 branch
BranchTrueFalseTotal
if (name != namebuf) { Safefree(name); Safefree(lc); } 0 12 12
231 }
232  
233 /* Content-Type (Content-Length is added by Fetch from the body) */
234 32 p = rp_env(aTHX_ env, "CONTENT_TYPE", 12, &l);
235 32 ▶ if (p) rp_push(aTHX_ hav, "Content-Type", 12, p, l);
Branch: 1 branch
BranchTrueFalseTotal
if (p) rp_push(aTHX_ hav, "Content-Type", 12, p, l); 0 32 32
236  
237 /* Host, only when preserving it */
238 32 ▶ if (preserve_host) {
Branch: 1 branch
BranchTrueFalseTotal
if (preserve_host) { 1 31 32
239 1 p = rp_env(aTHX_ env, "HTTP_HOST", 9, &l);
240 1 ▶ if (p) rp_push(aTHX_ hav, "Host", 4, p, l);
Branch: 1 branch
BranchTrueFalseTotal
if (p) rp_push(aTHX_ hav, "Host", 4, p, l); 1 0 1
241 }
242  
243 /* X-Forwarded-For: chain REMOTE_ADDR onto any existing value */
244 {
245 32 STRLEN cl; const char *client = rp_env(aTHX_ env, "REMOTE_ADDR", 11, &cl);
246 32 ▶ if (client) {
Branch: 1 branch
BranchTrueFalseTotal
if (client) { 32 0 32
247 32 STRLEN xl; const char *xff = rp_env(aTHX_ env, "HTTP_X_FORWARDED_FOR", 20, &xl);
248 32 SV *v = newSVpvs("");
249 32 ▶ if (xff) { sv_catpvn(v, xff, xl); sv_catpvs(v, ", "); }
Branch: 1 branch
BranchTrueFalseTotal
if (xff) { sv_catpvn(v, xff, xl); sv_catpvs(v, ", "); } 1 31 32
250 32 sv_catpvn(v, client, cl);
251 32 av_push(hav, newSVpvs("X-Forwarded-For"));
252 32 av_push(hav, v);
253 }
254 }
255  
256 /* X-Forwarded-Proto: existing, else psgi.url_scheme, else http */
257 {
258 32 STRLEN pl; const char *proto = rp_env(aTHX_ env, "HTTP_X_FORWARDED_PROTO", 22, &pl);
259 32 ▶ if (!proto) proto = rp_env(aTHX_ env, "psgi.url_scheme", 15, &pl);
Branch: 1 branch
BranchTrueFalseTotal
if (!proto) proto = rp_env(aTHX_ env, "psgi.url_scheme", 15, &pl); 31 1 32
260 32 av_push(hav, newSVpvs("X-Forwarded-Proto"));
261 32 ▶ av_push(hav, proto ? newSVpvn(proto, pl) : newSVpvs("http"));
Branch: 1 branch
BranchTrueFalseTotal
av_push(hav, proto ? newSVpvn(proto, pl) : newSVpvs("http")); 32 0 32
262 }
263  
264 /* X-Forwarded-Host: existing, else client Host */
265 {
266 32 STRLEN hl; const char *xfh = rp_env(aTHX_ env, "HTTP_X_FORWARDED_HOST", 21, &hl);
267 32 ▶ if (!xfh) xfh = rp_env(aTHX_ env, "HTTP_HOST", 9, &hl);
Branch: 1 branch
BranchTrueFalseTotal
if (!xfh) xfh = rp_env(aTHX_ env, "HTTP_HOST", 9, &hl); 32 0 32
268 32 ▶ if (xfh) rp_push(aTHX_ hav, "X-Forwarded-Host", 16, xfh, hl);
Branch: 1 branch
BranchTrueFalseTotal
if (xfh) rp_push(aTHX_ hav, "X-Forwarded-Host", 16, xfh, hl); 4 28 32
269 }
270  
271 /* Via */
272 32 ▶ if (via && SvOK(via)) {
Branch: 2 branches
BranchTrueFalseTotal
if (via && SvOK(via)) { 32 0 32
if (via && SvOK(via)) { 31 1 32
273 31 STRLEN vl; const char *vp = SvPV_const(via, vl);
274 31 ▶ if (vl) rp_push(aTHX_ hav, "Via", 3, vp, vl);
Branch: 1 branch
BranchTrueFalseTotal
if (vl) rp_push(aTHX_ hav, "Via", 3, vp, vl); 31 0 31
275 }
276  
277 32 return hav;
278 }
279  
280 /* ---- $self field helpers ------------------------------------------------ */
281  
282 205 static SV *rp_self(pTHX_ HV *self, const char *k, STRLEN kl) {
283 205 SV **e = hv_fetch(self, k, (I32)kl, 0);
284 205 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 194 11 205
return (e && *e) ? *e : NULL; 194 0 194
285 }
286 70 static int rp_self_bool(pTHX_ HV *self, const char *k, STRLEN kl) {
287 70 SV *v = rp_self(aTHX_ self, k, kl);
288 70 ▶ return (v && SvTRUE(v)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvTRUE(v)) ? 1 : 0; 70 0 70
return (v && SvTRUE(v)) ? 1 : 0; 5 65 70
289 }
290  
291 /* case-insensitive: does the comma/space token list contain `tok`? */
292 4 static int rp_ci_token(const char *hay, STRLEN hl, const char *tok, STRLEN tl) {
293 4 STRLEN i = 0;
294 6 ▶ while (i < hl) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < hl) { 5 1 6
295 STRLEN s, e, j;
296 7 ▶ while (i < hl && (hay[i] == ',' || isSPACE((U8)hay[i]))) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < hl && (hay[i] == ',' || isSPACE((U8)hay[i]))) i++; 7 0 7
while (i < hl && (hay[i] == ',' || isSPACE((U8)hay[i]))) i++; 1 6 7
while (i < hl && (hay[i] == ',' || isSPACE((U8)hay[i]))) i++; 1 5 6
297 5 s = i;
298 43 ▶ while (i < hl && hay[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < hl && hay[i] != ',') i++; 39 4 43
while (i < hl && hay[i] != ',') i++; 38 1 39
299 5 e = i;
300 5 ▶ while (e > s && isSPACE((U8)hay[e - 1])) e--;
Branch: 2 branches
BranchTrueFalseTotal
while (e > s && isSPACE((U8)hay[e - 1])) e--; 5 0 5
while (e > s && isSPACE((U8)hay[e - 1])) e--; 0 5 5
301 5 ▶ if (e - s == tl) {
Branch: 1 branch
BranchTrueFalseTotal
if (e - s == tl) { 3 2 5
302 24 ▶ for (j = 0; j < tl; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < tl; j++) 21 3 24
303 21 ▶ if (toLOWER((U8)hay[s + j]) != (U8)tok[j]) break;
Branch: 2 branches
BranchTrueFalseTotal
if (toLOWER((U8)hay[s + j]) != (U8)tok[j]) break; 3 18 21
if (toLOWER((U8)hay[s + j]) != (U8)tok[j]) break; 0 21 21
304 3 ▶ if (j == tl) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == tl) return 1; 3 0 3
305 }
306 }
307 1 return 0;
308 }
309  
310 /* this request asks for an HTTP Upgrade (WebSocket et al) */
311 35 static int rp_is_upgrade(pTHX_ HV *env) {
312 STRLEN ul, cl;
313 35 const char *u = rp_env(aTHX_ env, "HTTP_UPGRADE", 12, &ul);
314 const char *c;
315 35 ▶ if (!u) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!u) return 0; 31 4 35
316 4 c = rp_env(aTHX_ env, "HTTP_CONNECTION", 15, &cl);
317 4 ▶ return (c && rp_ci_token(c, cl, "upgrade", 7)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (c && rp_ci_token(c, cl, "upgrade", 7)) ? 1 : 0; 4 0 4
return (c && rp_ci_token(c, cl, "upgrade", 7)) ? 1 : 0; 3 1 4
318 }
319  
320 /* $path is under $prefix (equal, prefix is '/', or a '/' follows the prefix) */
321 7 static int rp_prefix_match(const char *path, STRLEN pl, const char *pre, STRLEN prl) {
322 7 ▶ if (prl == 1 && pre[0] == '/') return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (prl == 1 && pre[0] == '/') return 1; 2 5 7
if (prl == 1 && pre[0] == '/') return 1; 2 0 2
323 5 ▶ if (pl == prl && memEQ(path, pre, pl)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (pl == prl && memEQ(path, pre, pl)) return 1; 1 4 5
if (pl == prl && memEQ(path, pre, pl)) return 1; 1 0 1
324 4 ▶ if (pl > prl && memEQ(path, pre, prl) && path[prl] == '/') return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (pl > prl && memEQ(path, pre, prl) && path[prl] == '/') return 1; 4 0 4
if (pl > prl && memEQ(path, pre, prl) && path[prl] == '/') return 1; 2 2 4
if (pl > prl && memEQ(path, pre, prl) && path[prl] == '/') return 1; 1 1 2
325 3 return 0;
326 }
327  
328 /* Resolve the upstream ($base) and forward path ($path) for this request.
329 * Returns a mortal base SV (or NULL for "no target"); *path_out gets a mortal
330 * forward-path SV. self_sv is the blessed proxy for the resolver callback. */
331 37 static SV *rp_resolve(pTHX_ SV *self_sv, HV *self, HV *env, SV **path_out) {
332 STRLEN pil;
333 37 const char *pi = rp_env(aTHX_ env, "PATH_INFO", 9, &pil);
334 37 ▶ SV *path = pi ? newSVpvn(pi, pil) : newSVpvs("/");
Branch: 1 branch
BranchTrueFalseTotal
SV *path = pi ? newSVpvn(pi, pil) : newSVpvs("/"); 37 0 37
335 37 SV *up = rp_self(aTHX_ self, "upstream", 8);
336 37 sv_2mortal(path);
337 37 *path_out = path;
338  
339 37 ▶ if (up && SvOK(up)) return sv_2mortal(newSVsv(up));
Branch: 2 branches
BranchTrueFalseTotal
if (up && SvOK(up)) return sv_2mortal(newSVsv(up)); 29 8 37
if (up && SvOK(up)) return sv_2mortal(newSVsv(up)); 29 0 29
340  
341 {
342 8 SV *routes = rp_self(aTHX_ self, "routes", 6);
343 8 ▶ if (routes && SvROK(routes) && SvTYPE(SvRV(routes)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (routes && SvROK(routes) && SvTYPE(SvRV(routes)) == SVt_PVAV) { 5 3 8
if (routes && SvROK(routes) && SvTYPE(SvRV(routes)) == SVt_PVAV) { 5 0 5
if (routes && SvROK(routes) && SvTYPE(SvRV(routes)) == SVt_PVAV) { 5 0 5
344 5 AV *ra = (AV *)SvRV(routes);
345 5 SSize_t n = av_len(ra) + 1, i;
346 5 STRLEN pl2; const char *ps = SvPV_const(path, pl2);
347 8 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7 1 8
348 7 SV **rp = av_fetch(ra, i, 0);
349 AV *pair; SV **pre, **tgt;
350 7 ▶ if (!rp || !*rp || !SvROK(*rp)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!rp || !*rp || !SvROK(*rp)) continue; 7 0 7
if (!rp || !*rp || !SvROK(*rp)) continue; 7 0 7
if (!rp || !*rp || !SvROK(*rp)) continue; 0 7 7
351 7 pair = (AV *)SvRV(*rp);
352 7 pre = av_fetch(pair, 0, 0);
353 7 tgt = av_fetch(pair, 1, 0);
354 7 ▶ if (pre && *pre && tgt && *tgt) {
Branch: 4 branches
BranchTrueFalseTotal
if (pre && *pre && tgt && *tgt) { 7 0 7
if (pre && *pre && tgt && *tgt) { 7 0 7
if (pre && *pre && tgt && *tgt) { 7 0 7
if (pre && *pre && tgt && *tgt) { 7 0 7
355 7 STRLEN prl; const char *pres = SvPV_const(*pre, prl);
356 7 ▶ if (rp_prefix_match(ps, pl2, pres, prl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (rp_prefix_match(ps, pl2, pres, prl)) { 4 3 7
357 4 SV *fwd = newSVpvn(ps + prl, pl2 - prl);
358 4 ▶ if (!SvCUR(fwd) || *SvPVX(fwd) != '/')
Branch: 2 branches
BranchTrueFalseTotal
if (!SvCUR(fwd) || *SvPVX(fwd) != '/') 3 1 4
if (!SvCUR(fwd) || *SvPVX(fwd) != '/') 2 1 3
359 3 sv_insert(fwd, 0, 0, "/", 1);
360 4 *path_out = sv_2mortal(fwd);
361 4 return sv_2mortal(newSVsv(*tgt));
362 }
363 }
364 }
365 1 return NULL;
366 }
367 }
368  
369 { /* resolver coderef */
370 3 SV *rv = rp_self(aTHX_ self, "resolver", 8);
371 3 ▶ if (rv && SvROK(rv) && SvTYPE(SvRV(rv)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
if (rv && SvROK(rv) && SvTYPE(SvRV(rv)) == SVt_PVCV) { 3 0 3
if (rv && SvROK(rv) && SvTYPE(SvRV(rv)) == SVt_PVCV) { 3 0 3
if (rv && SvROK(rv) && SvTYPE(SvRV(rv)) == SVt_PVCV) { 3 0 3
372 3 dSP; int n; SV *ret = NULL;
373 3 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 3 3
374 3 ▶ EXTEND(SP, 1); PUSHs(sv_2mortal(newRV_inc((SV *)env)));
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); PUSHs(sv_2mortal(newRV_inc((SV *)env))); 0 3 3
375 3 PUTBACK;
376 3 n = call_sv(rv, G_SCALAR);
377 3 SPAGAIN;
378 3 ▶ if (n > 0) { SV *t = POPs; if (SvOK(t) && SvCUR(t)) ret = newSVsv(t); }
Branch: 3 branches
BranchTrueFalseTotal
if (n > 0) { SV *t = POPs; if (SvOK(t) && SvCUR(t)) ret = newSVsv(t); } 3 0 3
if (n > 0) { SV *t = POPs; if (SvOK(t) && SvCUR(t)) ret = newSVsv(t); } 2 1 3
if (n > 0) { SV *t = POPs; if (SvOK(t) && SvCUR(t)) ret = newSVsv(t); } 2 0 2
379 3 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 3 0 3
380 3 ▶ return ret ? sv_2mortal(ret) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return ret ? sv_2mortal(ret) : NULL; 2 1 3
381 }
382 }
383 0 return NULL;
384 }
385  
386 /* Append the request target - path, then ?query - to an outbound request.
387 *
388 * PATH_INFO reaches a PSGI app percent-DECODED, so what the client wrote as
389 * %XX is a real byte by the time it gets here. Splicing that into a request
390 * line verbatim means "%0d%0a" in the client's URL ends the line and starts a
391 * second request on the upstream connection: request smuggling, straight past
392 * anything the proxy itself enforces. A decoded '?' or '#' truncates the
393 * target the same way, and a decoded space breaks the line into three.
394 *
395 * So re-encode rather than reject: a path that decoded to a space still
396 * reaches the upstream as the path the client asked for, and one that decoded
397 * to a CRLF reaches it as %0D%0A, which is one path segment and not two
398 * requests. '%' is re-encoded because a literal one here can only have come
399 * from %25. QUERY_STRING is handed over undecoded, so it needs the control
400 * guard but must otherwise stay byte for byte. */
401 33 static void rp_cat_target(pTHX_ SV *out, SV *path, HV *env) {
402 static const char hex[] = "0123456789ABCDEF";
403 STRLEN pl, ql, i;
404 33 const char *pp = SvPV_const(path, pl), *qp;
405 char e[3];
406 33 e[0] = '%';
407 33 ▶ if (!pl || pp[0] != '/') sv_catpvs(out, "/");
Branch: 2 branches
BranchTrueFalseTotal
if (!pl || pp[0] != '/') sv_catpvs(out, "/"); 33 0 33
if (!pl || pp[0] != '/') sv_catpvs(out, "/"); 0 33 33
408 210 ▶ for (i = 0; i < pl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pl; i++) { 177 33 210
409 177 const unsigned char c = (unsigned char)pp[i];
410 177 ▶ if (c <= 0x20 || c >= 0x7f || c == '%' || c == '#' || c == '?') {
Branch: 5 branches
BranchTrueFalseTotal
if (c <= 0x20 || c >= 0x7f || c == '%' || c == '#' || c == '?') { 160 17 177
if (c <= 0x20 || c >= 0x7f || c == '%' || c == '#' || c == '?') { 159 1 160
if (c <= 0x20 || c >= 0x7f || c == '%' || c == '#' || c == '?') { 158 1 159
if (c <= 0x20 || c >= 0x7f || c == '%' || c == '#' || c == '?') { 157 1 158
if (c <= 0x20 || c >= 0x7f || c == '%' || c == '#' || c == '?') { 1 156 157
411 21 e[1] = hex[c >> 4]; e[2] = hex[c & 0x0f];
412 21 sv_catpvn(out, e, 3);
413 }
414 156 else sv_catpvn(out, (const char *)&c, 1);
415 }
416 33 qp = rp_env(aTHX_ env, "QUERY_STRING", 12, &ql);
417 33 ▶ if (!qp) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!qp) return; 29 4 33
418 4 sv_catpvs(out, "?");
419 42 ▶ for (i = 0; i < ql; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ql; i++) { 38 4 42
420 38 const unsigned char c = (unsigned char)qp[i];
421 38 ▶ if (c <= 0x20 || c >= 0x7f || c == '#') {
Branch: 3 branches
BranchTrueFalseTotal
if (c <= 0x20 || c >= 0x7f || c == '#') { 35 3 38
if (c <= 0x20 || c >= 0x7f || c == '#') { 35 0 35
if (c <= 0x20 || c >= 0x7f || c == '#') { 0 35 35
422 3 e[1] = hex[c >> 4]; e[2] = hex[c & 0x0f];
423 3 sv_catpvn(out, e, 3);
424 }
425 35 else sv_catpvn(out, (const char *)&c, 1);
426 }
427 }
428  
429 /* base + path + ?query -> mortal URL SV */
430 32 static SV *rp_target_url(pTHX_ SV *base, SV *path, HV *env) {
431 32 STRLEN bl; const char *bp = SvPV_const(base, bl);
432 SV *url;
433 32 ▶ while (bl > 0 && bp[bl - 1] == '/') bl--; /* trim trailing '/' */
Branch: 2 branches
BranchTrueFalseTotal
while (bl > 0 && bp[bl - 1] == '/') bl--; /* trim trailing '/' */ 32 0 32
while (bl > 0 && bp[bl - 1] == '/') bl--; /* trim trailing '/' */ 0 32 32
434 32 url = newSVpvn(bp, bl);
435 32 rp_cat_target(aTHX_ url, path, env);
436 32 return sv_2mortal(url);
437 }
438  
439 /* Read CONTENT_LENGTH bytes from psgi.input; mortal body SV or NULL. */
440 32 static SV *rp_read_body(pTHX_ HV *env) {
441 32 STRLEN ll; const char *lp = rp_env(aTHX_ env, "CONTENT_LENGTH", 14, &ll);
442 SV **inp;
443 32 IV len, off = 0;
444 SV *body, *in;
445 32 ▶ if (!lp) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!lp) return NULL; 31 1 32
446 1 len = (IV)atol(lp);
447 1 ▶ if (len <= 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 0) return NULL; 0 1 1
448 1 inp = hv_fetchs(env, "psgi.input", 0);
449 1 ▶ if (!inp || !*inp || !SvOK(*inp)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!inp || !*inp || !SvOK(*inp)) return NULL; 1 0 1
if (!inp || !*inp || !SvOK(*inp)) return NULL; 1 0 1
if (!inp || !*inp || !SvOK(*inp)) return NULL; 0 1 1
450 1 in = *inp;
451 1 body = sv_2mortal(newSVpvs(""));
452 2 ▶ while (off < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < len) { 1 1 2
453 1 dSP; int n; IV got;
454 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
455 1 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 1 1
456 1 PUSHs(in);
457 1 PUSHs(body); /* aliased: read() fills it */
458 1 PUSHs(sv_2mortal(newSViv(len - off)));
459 1 PUSHs(sv_2mortal(newSViv(off)));
460 1 PUTBACK;
461 1 n = call_method("read", G_SCALAR);
462 1 SPAGAIN;
463 1 ▶ got = n > 0 ? POPi : 0;
Branch: 1 branch
BranchTrueFalseTotal
got = n > 0 ? POPi : 0; 1 0 1
464 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
465 1 ▶ if (got <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (got <= 0) break; 0 1 1
466 1 off += got;
467 }
468 1 return body;
469 }
470  
471 /* One Fetch UA per loop (async) or per process (blocking), cached in
472 * $self->{_ua}; built in C via the ABI ua_new on a miss. Borrowed. */
473 32 static SV *rp_get_ua(pTHX_ SV *self_sv, HV *self, HV *env, int *async) {
474 32 SV **loopp = hv_fetchs(env, "psgix.loop", 0);
475 32 SV **nbp = hv_fetchs(env, "psgi.nonblocking", 0);
476 32 ▶ SV *loop = (loopp && *loopp && SvOK(*loopp)) ? *loopp : NULL;
Branch: 3 branches
BranchTrueFalseTotal
SV *loop = (loopp && *loopp && SvOK(*loopp)) ? *loopp : NULL; 0 32 32
SV *loop = (loopp && *loopp && SvOK(*loopp)) ? *loopp : NULL; 0 0 0
SV *loop = (loopp && *loopp && SvOK(*loopp)) ? *loopp : NULL; 0 0 0
477 32 ▶ int nb = (nbp && *nbp && SvTRUE(*nbp)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
int nb = (nbp && *nbp && SvTRUE(*nbp)) ? 1 : 0; 0 32 32
int nb = (nbp && *nbp && SvTRUE(*nbp)) ? 1 : 0; 0 0 0
int nb = (nbp && *nbp && SvTRUE(*nbp)) ? 1 : 0; 0 0 0
478 32 SV **cp = hv_fetchs(self, "_ua", 0);
479 32 ▶ HV *cache = (cp && *cp && SvROK(*cp)) ? (HV *)SvRV(*cp) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
HV *cache = (cp && *cp && SvROK(*cp)) ? (HV *)SvRV(*cp) : NULL; 32 0 32
HV *cache = (cp && *cp && SvROK(*cp)) ? (HV *)SvRV(*cp) : NULL; 32 0 32
HV *cache = (cp && *cp && SvROK(*cp)) ? (HV *)SvRV(*cp) : NULL; 32 0 32
480 32 char key[32]; STRLEN kl = 0;
481 SV **uap, *ua;
482 PERL_UNUSED_ARG(self_sv);
483  
484 32 ▶ if (nb && loop) { *async = 1; kl = (STRLEN)my_snprintf(key, sizeof(key),
Branch: 2 branches
BranchTrueFalseTotal
if (nb && loop) { *async = 1; kl = (STRLEN)my_snprintf(key, sizeof(key), 0 32 32
if (nb && loop) { *async = 1; kl = (STRLEN)my_snprintf(key, sizeof(key), 0 0 0
485 0 "%" UVuf, PTR2UV(SvRV(loop))); }
486 32 else { *async = 0; loop = NULL; key[0] = '\0'; }
487  
488 32 ▶ if (cache && (uap = hv_fetch(cache, key, (I32)kl, 0)) && *uap && SvOK(*uap))
Branch: 4 branches
BranchTrueFalseTotal
if (cache && (uap = hv_fetch(cache, key, (I32)kl, 0)) && *uap && SvOK(*uap)) 32 0 32
if (cache && (uap = hv_fetch(cache, key, (I32)kl, 0)) && *uap && SvOK(*uap)) 22 10 32
if (cache && (uap = hv_fetch(cache, key, (I32)kl, 0)) && *uap && SvOK(*uap)) 22 0 22
if (cache && (uap = hv_fetch(cache, key, (I32)kl, 0)) && *uap && SvOK(*uap)) 22 0 22
489 22 return *uap;
490  
491 { /* miss: build the UA in C (ABI ua_new) with the proxy's options, cache it */
492 10 SV *kv[6]; int nkv = 0;
493 10 SV *ps = rp_self(aTHX_ self, "pool_size", 9);
494 10 SV *tv = rp_self(aTHX_ self, "tls_verify", 10);
495 10 ▶ if (loop) { kv[nkv++] = sv_2mortal(newSVpvs("loop")); kv[nkv++] = loop; }
Branch: 1 branch
BranchTrueFalseTotal
if (loop) { kv[nkv++] = sv_2mortal(newSVpvs("loop")); kv[nkv++] = loop; } 0 10 10
496 10 kv[nkv++] = sv_2mortal(newSVpvs("pool_size"));
497 10 ▶ kv[nkv++] = ps ? ps : sv_2mortal(newSViv(64));
Branch: 1 branch
BranchTrueFalseTotal
kv[nkv++] = ps ? ps : sv_2mortal(newSViv(64)); 0 10 10
498 10 kv[nkv++] = sv_2mortal(newSVpvs("tls_verify"));
499 10 ▶ kv[nkv++] = tv ? tv : sv_2mortal(newSViv(1));
Branch: 1 branch
BranchTrueFalseTotal
kv[nkv++] = tv ? tv : sv_2mortal(newSViv(1)); 0 10 10
500 10 ua = FETCH->ua_new(aTHX_ kv, nkv); /* +1 owned */
501 10 ▶ if (cache) { (void)hv_store(cache, key, (I32)kl, ua, 0); return ua; }
Branch: 1 branch
BranchTrueFalseTotal
if (cache) { (void)hv_store(cache, key, (I32)kl, ua, 0); return ua; } 10 0 10
502 0 return sv_2mortal(ua);
503 }
504 }
505  
506 /* Build headers, issue the request and shape the PSGI response, all in C. */
507 31 static SV *rp_dispatch(pTHX_ SV *ua_sv, HV *self, HV *env,
508 const char *method, SV *url, SV *body) {
509 31 int preserve = rp_self_bool(aTHX_ self, "preserve_host", 13);
510 31 SV *via = rp_self(aTHX_ self, "via", 3);
511 31 double timeout = 0;
512 31 SV *tv = rp_self(aTHX_ self, "timeout", 7);
513 31 AV *hav = rp_req_headers(aTHX_ env, preserve, via);
514 31 fetch_hdr *hdrs = NULL;
515 31 int nhdrs = 0;
516 31 const char *bp = NULL; STRLEN blen = 0;
517 31 STRLEN ul; const char *us = SvPV_const(url, ul);
518 SV *f;
519 31 ▶ if (tv && SvOK(tv)) timeout = SvNV(tv);
Branch: 2 branches
BranchTrueFalseTotal
if (tv && SvOK(tv)) timeout = SvNV(tv); 31 0 31
if (tv && SvOK(tv)) timeout = SvNV(tv); 31 0 31
520 {
521 31 SSize_t n = av_len(hav) + 1, i;
522 31 nhdrs = (int)(n / 2);
523 31 ▶ if (nhdrs > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nhdrs > 0) { 31 0 31
524 31 Newx(hdrs, nhdrs, fetch_hdr);
525 130 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 99 31 130
526 99 SV **k = av_fetch(hav, i, 0);
527 99 SV **v = av_fetch(hav, i + 1, 0);
528 99 hdrs[i / 2].name = SvPV_const(*k, hdrs[i / 2].nlen);
529 198 ▶ hdrs[i / 2].val = SvPV_const(*v, hdrs[i / 2].vlen);
Branch: 1 branch
BranchTrueFalseTotal
hdrs[i / 2].val = SvPV_const(*v, hdrs[i / 2].vlen); 99 0 99
530 }
531 }
532 }
533 32 ▶ if (body && SvOK(body)) bp = SvPV_const(body, blen);
Branch: 2 branches
BranchTrueFalseTotal
if (body && SvOK(body)) bp = SvPV_const(body, blen); 1 30 31
if (body && SvOK(body)) bp = SvPV_const(body, blen); 1 0 1
534 31 f = FETCH->request(aTHX_ ua_sv, method, us, hdrs, nhdrs,
535 bp, blen, timeout, -1, rp_map, NULL);
536 31 ▶ if (hdrs) Safefree(hdrs);
Branch: 1 branch
BranchTrueFalseTotal
if (hdrs) Safefree(hdrs); 31 0 31
537 31 return f; /* +1 owned */
538 }
539  
540 /* ---- streaming: drive a psgi.streaming writer from the Fetch callbacks --- */
541  
542 typedef struct rp_sctx { SV *responder; SV *writer; } rp_sctx;
543  
544 /* headers arrived: open the writer via $responder->([status, \@stripped]) */
545 1 static void rp_stream_on_headers(pTHX_ int status, AV *headers, void *ud) {
546 1 rp_sctx *s = (rp_sctx *)ud;
547 1 AV *out = newAV();
548 AV *pr;
549 1 ▶ SSize_t n = headers ? av_len(headers) + 1 : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = headers ? av_len(headers) + 1 : 0, i; 1 0 1
550 1 dSP; int cnt;
551 4 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 3 1 4
552 3 SV **k = av_fetch(headers, i, 0);
553 3 SV **v = av_fetch(headers, i + 1, 0);
554 3 ▶ if (k && *k) {
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) { 3 0 3
if (k && *k) { 3 0 3
555 3 STRLEN kl; const char *ks = SvPV_const(*k, kl);
556 3 char sb[64], *lc = sb; int drop;
557 3 ▶ if (kl > sizeof(sb)) Newx(lc, kl, char);
Branch: 1 branch
BranchTrueFalseTotal
if (kl > sizeof(sb)) Newx(lc, kl, char); 0 3 3
558 3 rp_lc(ks, kl, lc); drop = rp_is_hop(lc, kl);
559 3 ▶ if (lc != sb) Safefree(lc);
Branch: 1 branch
BranchTrueFalseTotal
if (lc != sb) Safefree(lc); 0 3 3
560 3 ▶ if (drop) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (drop) continue; 2 1 3
561 1 av_push(out, newSVsv(*k));
562 1 ▶ av_push(out, (v && *v) ? newSVsv(*v) : newSVpvs(""));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, (v && *v) ? newSVsv(*v) : newSVpvs("")); 1 0 1
av_push(out, (v && *v) ? newSVsv(*v) : newSVpvs("")); 1 0 1
563 }
564 }
565 1 pr = newAV();
566 1 av_push(pr, newSViv(status));
567 1 av_push(pr, newRV_noinc((SV *)out));
568 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
569 1 ▶ EXTEND(SP, 1); PUSHs(sv_2mortal(newRV_noinc((SV *)pr)));
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); PUSHs(sv_2mortal(newRV_noinc((SV *)pr))); 0 1 1
570 1 PUTBACK;
571 1 cnt = call_sv(s->responder, G_SCALAR);
572 1 SPAGAIN;
573 1 ▶ if (cnt > 0) { SV *w = POPs; if (SvOK(w)) s->writer = SvREFCNT_inc(w); }
Branch: 2 branches
BranchTrueFalseTotal
if (cnt > 0) { SV *w = POPs; if (SvOK(w)) s->writer = SvREFCNT_inc(w); } 1 0 1
if (cnt > 0) { SV *w = POPs; if (SvOK(w)) s->writer = SvREFCNT_inc(w); } 1 0 1
574 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
575 1 }
576  
577 /* a body chunk arrived: $writer->write($chunk) */
578 3 static void rp_stream_on_body(pTHX_ const char *chunk, STRLEN len, void *ud) {
579 3 rp_sctx *s = (rp_sctx *)ud;
580 3 dSP;
581 3 ▶ if (!s->writer) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->writer) return; 0 3 3
582 3 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 3 3
583 3 ▶ EXTEND(SP, 2); PUSHs(s->writer); PUSHs(sv_2mortal(newSVpvn(chunk, len)));
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); PUSHs(s->writer); PUSHs(sv_2mortal(newSVpvn(chunk, len))); 0 3 3
584 3 PUTBACK;
585 3 call_method("write", G_VOID | G_DISCARD);
586 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
587 }
588  
589 /* completion: close the writer, or (if nothing was sent yet) emit a 502 */
590 1 static void rp_stream_on_done(pTHX_ int ok, SV *err, void *ud) {
591 1 rp_sctx *s = (rp_sctx *)ud;
592 1 dSP;
593 PERL_UNUSED_ARG(ok);
594 1 ▶ if (s->writer) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->writer) { 1 0 1
595 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 1); PUSHs(s->writer);
Branch: 2 branches
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 1); PUSHs(s->writer); 0 1 1
ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 1); PUSHs(s->writer); 0 1 1
596 1 ▶ PUTBACK; call_method("close", G_VOID | G_DISCARD); FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; call_method("close", G_VOID | G_DISCARD); FREETMPS; LEAVE; 1 0 1
597 } else {
598 0 AV *h = newAV(), *pr = newAV();
599 0 SV *w = NULL; int cnt;
600 0 av_push(h, newSVpvs("Content-Type")); av_push(h, newSVpvs("text/plain"));
601 0 av_push(pr, newSViv(502)); av_push(pr, newRV_noinc((SV *)h));
602 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
603 0 ▶ EXTEND(SP, 1); PUSHs(sv_2mortal(newRV_noinc((SV *)pr)));
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); PUSHs(sv_2mortal(newRV_noinc((SV *)pr))); 0 0 0
604 0 PUTBACK; cnt = call_sv(s->responder, G_SCALAR); SPAGAIN;
605 0 ▶ if (cnt > 0) { SV *r = POPs; if (SvOK(r)) w = SvREFCNT_inc(r); }
Branch: 2 branches
BranchTrueFalseTotal
if (cnt > 0) { SV *r = POPs; if (SvOK(r)) w = SvREFCNT_inc(r); } 0 0 0
if (cnt > 0) { SV *r = POPs; if (SvOK(r)) w = SvREFCNT_inc(r); } 0 0 0
606 0 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 0 0 0
607 0 ▶ if (w) {
Branch: 1 branch
BranchTrueFalseTotal
if (w) { 0 0 0
608 0 SV *msg = newSVpvs("502 Bad Gateway: ");
609 0 ▶ if (err && SvOK(err)) { STRLEN el; const char *ep = SvPV_const(err, el);
Branch: 2 branches
BranchTrueFalseTotal
if (err && SvOK(err)) { STRLEN el; const char *ep = SvPV_const(err, el); 0 0 0
if (err && SvOK(err)) { STRLEN el; const char *ep = SvPV_const(err, el); 0 0 0
610 0 sv_catpvn(msg, ep, el); }
611 0 else sv_catpvs(msg, "upstream request failed");
612 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
613 0 ▶ EXTEND(SP, 2); PUSHs(w); PUSHs(sv_2mortal(msg));
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); PUSHs(w); PUSHs(sv_2mortal(msg)); 0 0 0
614 0 ▶ PUTBACK; call_method("write", G_VOID | G_DISCARD); FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; call_method("write", G_VOID | G_DISCARD); FREETMPS; LEAVE; 0 0 0
615 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 1); PUSHs(w);
Branch: 2 branches
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 1); PUSHs(w); 0 0 0
ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 1); PUSHs(w); 0 0 0
616 0 ▶ PUTBACK; call_method("close", G_VOID | G_DISCARD); FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; call_method("close", G_VOID | G_DISCARD); FREETMPS; LEAVE; 0 0 0
617 0 SvREFCNT_dec(w);
618 }
619 }
620 1 ▶ if (s->responder) SvREFCNT_dec(s->responder);
Branch: 1 branch
BranchTrueFalseTotal
if (s->responder) SvREFCNT_dec(s->responder); 1 0 1
621 1 ▶ if (s->writer) SvREFCNT_dec(s->writer);
Branch: 1 branch
BranchTrueFalseTotal
if (s->writer) SvREFCNT_dec(s->writer); 1 0 1
622 1 Safefree(s);
623 1 }
624  
625 /* ---- plain PSGI responses (built in C) ---------------------------------- */
626  
627 5 static SV *rp_plain(pTHX_ int code, const char *msg, STRLEN mlen) {
628 5 AV *h = newAV(), *b = newAV(), *r = newAV();
629 5 av_push(h, newSVpvs("Content-Type"));
630 5 av_push(h, newSVpvs("text/plain"));
631 5 av_push(b, newSVpvn(msg, mlen));
632 5 av_push(r, newSViv(code));
633 5 av_push(r, newRV_noinc((SV *)h));
634 5 av_push(r, newRV_noinc((SV *)b));
635 5 return newRV_noinc((SV *)r);
636 }
637  
638 2 static SV *rp_no_upstream(pTHX_ HV *env) {
639 2 STRLEN pil; const char *pi = rp_env(aTHX_ env, "PATH_INFO", 9, &pil);
640 2 SV *m = newSVpvs("No upstream for ");
641 2 ▶ sv_catpvn(m, pi ? pi : "/", pi ? pil : 1);
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvn(m, pi ? pi : "/", pi ? pil : 1); 2 0 2
sv_catpvn(m, pi ? pi : "/", pi ? pil : 1); 2 0 2
642 2 { STRLEN l; const char *p = SvPV_const(m, l); SV *r = rp_plain(aTHX_ 404, p, l);
643 2 SvREFCNT_dec(m); return r; }
644 }
645  
646 /* ---- WebSocket / Upgrade transparent byte tunnel (C) -------------------- */
647  
648 #ifndef WIN32
649 /* scheme://host[:port] -> host/port/tls; returns 1 on success */
650 2 static int rp_parse_base(const char *b, STRLEN bl, char *host, size_t hostsz,
651 int *port, int *tls) {
652 2 const char *end = b + bl, *p = b, *sep = NULL, *q, *hs, *he, *colon = NULL;
653 2 int is_tls = 0;
654 11 ▶ for (q = b; q + 2 < end; q++)
Branch: 1 branch
BranchTrueFalseTotal
for (q = b; q + 2 < end; q++) 11 0 11
655 11 ▶ if (q[0] == ':' && q[1] == '/' && q[2] == '/') { sep = q; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (q[0] == ':' && q[1] == '/' && q[2] == '/') { sep = q; break; } 2 9 11
if (q[0] == ':' && q[1] == '/' && q[2] == '/') { sep = q; break; } 2 0 2
if (q[0] == ':' && q[1] == '/' && q[2] == '/') { sep = q; break; } 2 0 2
656 2 ▶ if (sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (sep) { 2 0 2
657 2 size_t sl = (size_t)(sep - b);
658 2 ▶ if (sl == 5 && memEQ(b, "https", 5)) is_tls = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (sl == 5 && memEQ(b, "https", 5)) is_tls = 1; 1 1 2
if (sl == 5 && memEQ(b, "https", 5)) is_tls = 1; 1 0 1
659 1 ▶ else if (sl == 3 && memEQ(b, "wss", 3)) is_tls = 1;
Branch: 2 branches
BranchTrueFalseTotal
else if (sl == 3 && memEQ(b, "wss", 3)) is_tls = 1; 0 1 1
else if (sl == 3 && memEQ(b, "wss", 3)) is_tls = 1; 0 0 0
660 2 p = sep + 3;
661 }
662 2 hs = he = p;
663 20 ▶ while (he < end && *he != ':' && *he != '/') he++;
Branch: 3 branches
BranchTrueFalseTotal
while (he < end && *he != ':' && *he != '/') he++; 20 0 20
while (he < end && *he != ':' && *he != '/') he++; 18 2 20
while (he < end && *he != ':' && *he != '/') he++; 18 0 18
664 2 ▶ if (he < end && *he == ':') colon = he;
Branch: 2 branches
BranchTrueFalseTotal
if (he < end && *he == ':') colon = he; 2 0 2
if (he < end && *he == ':') colon = he; 2 0 2
665 2 ▶ if ((size_t)(he - hs) == 0 || (size_t)(he - hs) >= hostsz) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if ((size_t)(he - hs) == 0 || (size_t)(he - hs) >= hostsz) return 0; 2 0 2
if ((size_t)(he - hs) == 0 || (size_t)(he - hs) >= hostsz) return 0; 0 2 2
666 2 memcpy(host, hs, (size_t)(he - hs));
667 2 host[he - hs] = '\0';
668 2 ▶ if (colon) { *port = atoi(colon + 1); if (*port <= 0) *port = is_tls ? 443 : 80; }
Branch: 3 branches
BranchTrueFalseTotal
if (colon) { *port = atoi(colon + 1); if (*port <= 0) *port = is_tls ? 443 : 80; } 2 0 2
if (colon) { *port = atoi(colon + 1); if (*port <= 0) *port = is_tls ? 443 : 80; } 0 2 2
if (colon) { *port = atoi(colon + 1); if (*port <= 0) *port = is_tls ? 443 : 80; } 0 0 0
669 0 ▶ else { *port = is_tls ? 443 : 80; }
Branch: 1 branch
BranchTrueFalseTotal
else { *port = is_tls ? 443 : 80; } 0 0 0
670 2 *tls = is_tls;
671 2 return 1;
672 }
673  
674 2 static ssize_t rp_read(int fd, char *b, size_t n) {
675 ssize_t r;
676 2 ▶ do { r = read(fd, b, n); } while (r < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
do { r = read(fd, b, n); } while (r < 0 && errno == EINTR); 0 2 2
do { r = read(fd, b, n); } while (r < 0 && errno == EINTR); 0 0 0
677 2 return r;
678 }
679 2 static int rp_write_all(int fd, const char *b, size_t n) {
680 2 size_t off = 0;
681 4 ▶ while (off < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < n) { 2 2 4
682 2 ssize_t w = write(fd, b + off, n - off);
683 2 ▶ if (w < 0) { if (errno == EINTR) continue; return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (w < 0) { if (errno == EINTR) continue; return -1; } 0 2 2
if (w < 0) { if (errno == EINTR) continue; return -1; } 0 0 0
684 2 ▶ if (w == 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (w == 0) return -1; 0 2 2
685 2 off += (size_t)w;
686 }
687 2 return 0;
688 }
689  
690 /* Serialize the raw Upgrade request: client headers verbatim (Upgrade/
691 * Connection/Sec-WebSocket-* survive), Host adjusted, X-Forwarded-* added. */
692 1 static SV *rp_raw_request(pTHX_ HV *env, SV *path, const char *host, int port,
693 int preserve, SV *via) {
694 1 SV *req = newSVpvs("");
695 1 SV **mp = hv_fetchs(env, "REQUEST_METHOD", 0);
696 STRLEN ml; HE *he;
697 char pb[16];
698 2 ▶ if (mp && *mp && SvOK(*mp)) { const char *m = SvPV_const(*mp, ml); sv_catpvn(req, m, ml); }
Branch: 3 branches
BranchTrueFalseTotal
if (mp && *mp && SvOK(*mp)) { const char *m = SvPV_const(*mp, ml); sv_catpvn(req, m, ml); } 1 0 1
if (mp && *mp && SvOK(*mp)) { const char *m = SvPV_const(*mp, ml); sv_catpvn(req, m, ml); } 1 0 1
if (mp && *mp && SvOK(*mp)) { const char *m = SvPV_const(*mp, ml); sv_catpvn(req, m, ml); } 1 0 1
699 0 else sv_catpvs(req, "GET");
700 1 sv_catpvs(req, " ");
701 1 rp_cat_target(aTHX_ req, path, env); /* encoded: see the note */
702 1 sv_catpvs(req, " HTTP/1.1\r\nHost: ");
703 {
704 1 ▶ STRLEN hl; const char *hh = preserve ? rp_env(aTHX_ env, "HTTP_HOST", 9, &hl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN hl; const char *hh = preserve ? rp_env(aTHX_ env, "HTTP_HOST", 9, &hl) : NULL; 0 1 1
705 /* the client's own Host, reflected into a request line we serialize:
706 * stop at the first control byte so a bare LF cannot smuggle either */
707 1 ▶ if (hh) { STRLEN i = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hh) { STRLEN i = 0; 0 1 1
708 0 ▶ while (i < hl && (unsigned char)hh[i] > ' '
Branch: 1 branch
BranchTrueFalseTotal
while (i < hl && (unsigned char)hh[i] > ' ' 0 0 0
709 0 ▶ && (unsigned char)hh[i] != 0x7f) i++;
Branch: 2 branches
BranchTrueFalseTotal
&& (unsigned char)hh[i] != 0x7f) i++; 0 0 0
&& (unsigned char)hh[i] != 0x7f) i++; 0 0 0
710 0 sv_catpvn(req, hh, i); }
711 else {
712 1 sv_catpv(req, host);
713 1 ▶ if (port != 80 && port != 443)
Branch: 2 branches
BranchTrueFalseTotal
if (port != 80 && port != 443) 1 0 1
if (port != 80 && port != 443) 1 0 1
714 1 { sv_catpvs(req, ":"); sv_catpvn(req, pb, (STRLEN)my_snprintf(pb, sizeof pb, "%d", port)); }
715 }
716 }
717 1 sv_catpvs(req, "\r\n");
718 1 hv_iterinit(env);
719 15 ▶ while ((he = hv_iternext(env))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(env))) { 14 1 15
720 14 I32 kl; const char *k = hv_iterkey(he, &kl);
721 14 char nb[128], lb[128]; char *name = nb, *lc = lb; STRLEN nl, i;
722 14 ▶ if (kl < 6 || memNE(k, "HTTP_", 5)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl < 6 || memNE(k, "HTTP_", 5)) continue; 14 0 14
if (kl < 6 || memNE(k, "HTTP_", 5)) continue; 9 5 14
723 5 nl = (STRLEN)(kl - 5);
724 5 ▶ if (nl > sizeof(nb)) { Newx(name, nl, char); Newx(lc, nl, char); }
Branch: 1 branch
BranchTrueFalseTotal
if (nl > sizeof(nb)) { Newx(name, nl, char); Newx(lc, nl, char); } 0 5 5
725 64 ▶ for (i = 0; i < nl; i++) { char c = k[5 + i]; if (c == '_') c = '-';
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nl; i++) { char c = k[5 + i]; if (c == '_') c = '-'; 4 55 59
for (i = 0; i < nl; i++) { char c = k[5 + i]; if (c == '_') c = '-'; 59 5 64
726 59 ▶ name[i] = c; lc[i] = (char)toLOWER((U8)c); }
Branch: 1 branch
BranchTrueFalseTotal
name[i] = c; lc[i] = (char)toLOWER((U8)c); } 55 4 59
727 5 ▶ if (!((nl == 4 && memEQ(lc, "host", 4)) || rp_is_xfwd(lc, nl))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!((nl == 4 && memEQ(lc, "host", 4)) || rp_is_xfwd(lc, nl))) { 1 4 5
if (!((nl == 4 && memEQ(lc, "host", 4)) || rp_is_xfwd(lc, nl))) { 0 1 1
if (!((nl == 4 && memEQ(lc, "host", 4)) || rp_is_xfwd(lc, nl))) { 4 0 4
728 4 int at = 1; STRLEN j; SV *val;
729 59 ▶ for (j = 0; j < nl; j++) { if (name[j] == '-') at = 1;
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < nl; j++) { if (name[j] == '-') at = 1; 4 51 55
for (j = 0; j < nl; j++) { if (name[j] == '-') at = 1; 55 4 59
730 51 ▶ else { name[j] = at ? (char)toUPPER((U8)name[j]) : (char)toLOWER((U8)name[j]); at = 0; } }
Branch: 3 branches
BranchTrueFalseTotal
else { name[j] = at ? (char)toUPPER((U8)name[j]) : (char)toLOWER((U8)name[j]); at = 0; } } 8 43 51
else { name[j] = at ? (char)toUPPER((U8)name[j]) : (char)toLOWER((U8)name[j]); at = 0; } } 0 8 8
else { name[j] = at ? (char)toUPPER((U8)name[j]) : (char)toLOWER((U8)name[j]); at = 0; } } 43 0 43
731 4 val = hv_iterval(env, he);
732 4 { STRLEN vl; const char *vp = SvPV_const(val, vl);
733 4 sv_catpvn(req, name, nl); sv_catpvs(req, ": "); sv_catpvn(req, vp, vl);
734 4 sv_catpvs(req, "\r\n"); }
735 }
736 5 ▶ if (name != nb) { Safefree(name); Safefree(lc); }
Branch: 1 branch
BranchTrueFalseTotal
if (name != nb) { Safefree(name); Safefree(lc); } 0 5 5
737 }
738 {
739 1 STRLEN cl; const char *client = rp_env(aTHX_ env, "REMOTE_ADDR", 11, &cl);
740 1 ▶ if (client) {
Branch: 1 branch
BranchTrueFalseTotal
if (client) { 1 0 1
741 1 STRLEN xl; const char *xff = rp_env(aTHX_ env, "HTTP_X_FORWARDED_FOR", 20, &xl);
742 1 sv_catpvs(req, "X-Forwarded-For: ");
743 1 ▶ if (xff) { sv_catpvn(req, xff, xl); sv_catpvs(req, ", "); }
Branch: 1 branch
BranchTrueFalseTotal
if (xff) { sv_catpvn(req, xff, xl); sv_catpvs(req, ", "); } 0 1 1
744 1 sv_catpvn(req, client, cl); sv_catpvs(req, "\r\n");
745 }
746 }
747 {
748 1 STRLEN pl2; const char *proto = rp_env(aTHX_ env, "HTTP_X_FORWARDED_PROTO", 22, &pl2);
749 1 ▶ if (!proto) proto = rp_env(aTHX_ env, "psgi.url_scheme", 15, &pl2);
Branch: 1 branch
BranchTrueFalseTotal
if (!proto) proto = rp_env(aTHX_ env, "psgi.url_scheme", 15, &pl2); 1 0 1
750 1 sv_catpvs(req, "X-Forwarded-Proto: ");
751 1 ▶ if (proto) sv_catpvn(req, proto, pl2); else sv_catpvs(req, "http");
Branch: 1 branch
BranchTrueFalseTotal
if (proto) sv_catpvn(req, proto, pl2); else sv_catpvs(req, "http"); 1 0 1
752 1 sv_catpvs(req, "\r\n");
753 }
754 1 ▶ if (via && SvOK(via)) { STRLEN vl; const char *vp = SvPV_const(via, vl);
Branch: 2 branches
BranchTrueFalseTotal
if (via && SvOK(via)) { STRLEN vl; const char *vp = SvPV_const(via, vl); 1 0 1
if (via && SvOK(via)) { STRLEN vl; const char *vp = SvPV_const(via, vl); 1 0 1
755 1 ▶ if (vl) { sv_catpvs(req, "Via: "); sv_catpvn(req, vp, vl); sv_catpvs(req, "\r\n"); } }
Branch: 1 branch
BranchTrueFalseTotal
if (vl) { sv_catpvs(req, "Via: "); sv_catpvn(req, vp, vl); sv_catpvs(req, "\r\n"); } } 1 0 1
756 1 sv_catpvs(req, "\r\n");
757 1 return req;
758 }
759 #endif /* !WIN32 */
760  
761 /* Hijack psgix.io, connect to the upstream, relay the Upgrade handshake, then
762 * splice bytes both ways until either side closes. Returns a PSGI triplet
763 * (200 after a handled hijack; 501/502 on error). */
764 3 static SV *rp_tunnel(pTHX_ HV *self, HV *env, SV *base, SV *path) {
765 #ifdef WIN32
766 PERL_UNUSED_ARG(self); PERL_UNUSED_ARG(env);
767 PERL_UNUSED_ARG(base); PERL_UNUSED_ARG(path);
768 return rp_plain(aTHX_ 501, "Upgrade tunnel unsupported on this platform", 43);
769 #else
770 3 SV **iop = hv_fetchs(env, "psgix.io", 0);
771 3 ▶ SV *io = (iop && *iop && SvOK(*iop)) ? *iop : NULL;
Branch: 3 branches
BranchTrueFalseTotal
SV *io = (iop && *iop && SvOK(*iop)) ? *iop : NULL; 2 1 3
SV *io = (iop && *iop && SvOK(*iop)) ? *iop : NULL; 2 0 2
SV *io = (iop && *iop && SvOK(*iop)) ? *iop : NULL; 2 0 2
772 3 SV *via = rp_self(aTHX_ self, "via", 3);
773 3 int preserve = rp_self_bool(aTHX_ self, "preserve_host", 13);
774 3 int verify = rp_self_bool(aTHX_ self, "tls_verify", 10);
775 3 int cfd = -1, ufd, port, tls, seen = 0, fl;
776 char host[256], buf[65536];
777 STRLEN bl; const char *bp;
778 void *uc; /* Fetch-owned upstream connection (plain or TLS) */
779 SV *req, *resp; ssize_t n;
780  
781 3 ▶ if (!io) return rp_plain(aTHX_ 501, "Upgrade requires a server that provides psgix.io", 48);
Branch: 1 branch
BranchTrueFalseTotal
if (!io) return rp_plain(aTHX_ 501, "Upgrade requires a server that provides psgix.io", 48); 1 2 3
782 2 ▶ { IO *iio = sv_2io(io); if (iio && IoIFP(iio)) cfd = PerlIO_fileno(IoIFP(iio)); }
Branch: 2 branches
BranchTrueFalseTotal
{ IO *iio = sv_2io(io); if (iio && IoIFP(iio)) cfd = PerlIO_fileno(IoIFP(iio)); } 2 0 2
{ IO *iio = sv_2io(io); if (iio && IoIFP(iio)) cfd = PerlIO_fileno(IoIFP(iio)); } 2 0 2
783 2 ▶ if (cfd < 0) return rp_plain(aTHX_ 501, "psgix.io has no file descriptor", 31);
Branch: 1 branch
BranchTrueFalseTotal
if (cfd < 0) return rp_plain(aTHX_ 501, "psgix.io has no file descriptor", 31); 0 2 2
784  
785 2 bp = SvPV_const(base, bl);
786 2 ▶ if (!rp_parse_base(bp, bl, host, sizeof host, &port, &tls))
Branch: 1 branch
BranchTrueFalseTotal
if (!rp_parse_base(bp, bl, host, sizeof host, &port, &tls)) 0 2 2
787 0 return rp_plain(aTHX_ 502, "cannot parse upstream base", 26);
788  
789 /* connect through Fetch's ABI: it does the TCP connect and, for a
790 * wss/https upstream, the TLS handshake with Fetch's own OpenSSL. */
791 2 uc = FETCH->tunnel_connect(host, port, tls, verify);
792 2 ▶ if (!uc) return rp_plain(aTHX_ 502, "cannot connect to upstream", 26);
Branch: 1 branch
BranchTrueFalseTotal
if (!uc) return rp_plain(aTHX_ 502, "cannot connect to upstream", 26); 1 1 2
793 1 ufd = FETCH->tunnel_fd(uc);
794  
795 /* client socket may be non-blocking (Hyperman); make it blocking to splice */
796 1 fl = fcntl(cfd, F_GETFL, 0);
797 1 ▶ if (fl != -1) fcntl(cfd, F_SETFL, fl & ~O_NONBLOCK);
Branch: 1 branch
BranchTrueFalseTotal
if (fl != -1) fcntl(cfd, F_SETFL, fl & ~O_NONBLOCK); 1 0 1
798  
799 1 req = sv_2mortal(rp_raw_request(aTHX_ env, path, host, port, preserve, via));
800 1 { STRLEN rl; const char *rb = SvPV_const(req, rl);
801 1 ▶ if (FETCH->tunnel_write_all(uc, rb, (IV)rl) < 0) { FETCH->tunnel_close(uc);
Branch: 1 branch
BranchTrueFalseTotal
if (FETCH->tunnel_write_all(uc, rb, (IV)rl) < 0) { FETCH->tunnel_close(uc); 0 1 1
802 0 return rp_plain(aTHX_ 502, "write to upstream failed", 24); } }
803  
804 /* read the response header block (may carry first frame bytes), relay it */
805 1 resp = sv_2mortal(newSVpvs(""));
806 2 ▶ while (!seen) {
Branch: 1 branch
BranchTrueFalseTotal
while (!seen) { 1 1 2
807 static const char crlf2[] = "\r\n\r\n";
808 1 n = (ssize_t)FETCH->tunnel_read(uc, buf, (IV)sizeof buf);
809 1 ▶ if (n <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) break; 0 1 1
810 1 sv_catpvn(resp, buf, n);
811 1 ▶ if (ninstr(SvPVX(resp), SvPVX(resp) + SvCUR(resp), crlf2, crlf2 + 4))
Branch: 1 branch
BranchTrueFalseTotal
if (ninstr(SvPVX(resp), SvPVX(resp) + SvCUR(resp), crlf2, crlf2 + 4)) 1 0 1
812 1 seen = 1;
813 }
814 1 ▶ if (SvCUR(resp) == 0) { FETCH->tunnel_close(uc);
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(resp) == 0) { FETCH->tunnel_close(uc); 0 1 1
815 0 return rp_plain(aTHX_ 502, "no response from upstream", 25); }
816 1 { STRLEN rl; const char *rb = SvPV_const(resp, rl);
817 1 ▶ if (rp_write_all(cfd, rb, rl) < 0) { FETCH->tunnel_close(uc); return rp_plain(aTHX_ 200, "", 0); } }
Branch: 1 branch
BranchTrueFalseTotal
if (rp_write_all(cfd, rb, rl) < 0) { FETCH->tunnel_close(uc); return rp_plain(aTHX_ 200, "", 0); } } 0 1 1
818  
819 /* splice both directions until one end closes. Drain any TLS-buffered
820 * upstream bytes first (select would not report them). */
821 2 for (;;) {
822 3 fd_set rfds; struct timeval tv; int r, mx = (cfd > ufd ? cfd : ufd) + 1;
823 3 ▶ if (FETCH->tunnel_pending(uc) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (FETCH->tunnel_pending(uc) > 0) { 0 3 3
824 0 n = (ssize_t)FETCH->tunnel_read(uc, buf, (IV)sizeof buf);
825 0 ▶ if (n <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) break; 0 0 0
826 0 ▶ if (rp_write_all(cfd, buf, n) < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rp_write_all(cfd, buf, n) < 0) break; 0 0 0
827 0 continue;
828 }
829 51 ▶ FD_ZERO(&rfds); FD_SET(cfd, &rfds); FD_SET(ufd, &rfds);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&rfds); FD_SET(cfd, &rfds); FD_SET(ufd, &rfds); 48 3 51
830 3 tv.tv_sec = 60; tv.tv_usec = 0;
831 3 r = select(mx, &rfds, NULL, NULL, &tv);
832 3 ▶ if (r <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (r <= 0) break; 0 3 3
833 3 ▶ if (FD_ISSET(cfd, &rfds)) { n = rp_read(cfd, buf, sizeof buf);
Branch: 1 branch
BranchTrueFalseTotal
if (FD_ISSET(cfd, &rfds)) { n = rp_read(cfd, buf, sizeof buf); 2 1 3
834 2 ▶ if (n <= 0) break; if (FETCH->tunnel_write_all(uc, buf, (IV)n) < 0) break; }
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0) break; if (FETCH->tunnel_write_all(uc, buf, (IV)n) < 0) break; } 1 1 2
if (n <= 0) break; if (FETCH->tunnel_write_all(uc, buf, (IV)n) < 0) break; } 0 1 1
835 2 ▶ if (FD_ISSET(ufd, &rfds)) { n = (ssize_t)FETCH->tunnel_read(uc, buf, (IV)sizeof buf);
Branch: 1 branch
BranchTrueFalseTotal
if (FD_ISSET(ufd, &rfds)) { n = (ssize_t)FETCH->tunnel_read(uc, buf, (IV)sizeof buf); 1 1 2
836 1 ▶ if (n <= 0) break; if (rp_write_all(cfd, buf, n) < 0) break; }
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0) break; if (rp_write_all(cfd, buf, n) < 0) break; } 0 1 1
if (n <= 0) break; if (rp_write_all(cfd, buf, n) < 0) break; } 0 1 1
837 }
838 1 FETCH->tunnel_close(uc);
839 1 return rp_plain(aTHX_ 200, "", 0);
840 #endif
841 }
842  
843 /* ---- streaming: build headers, stream the request, pump the writer ------ */
844 1 static void rp_stream_run(pTHX_ SV *ua_sv, const char *method, const char *url,
845 HV *env, SV *body, int preserve_host, SV *via,
846 double timeout, int async, SV *responder) {
847 1 AV *hav = rp_req_headers(aTHX_ env, preserve_host, via);
848 1 fetch_hdr *hdrs = NULL;
849 1 int nhdrs = 0;
850 1 const char *bp = NULL; STRLEN blen = 0;
851 rp_sctx *s;
852 SV *f;
853 {
854 1 SSize_t n = av_len(hav) + 1, i;
855 1 nhdrs = (int)(n / 2);
856 1 ▶ if (nhdrs > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nhdrs > 0) { 1 0 1
857 1 Newx(hdrs, nhdrs, fetch_hdr);
858 4 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 3 1 4
859 3 SV **k = av_fetch(hav, i, 0);
860 3 SV **v = av_fetch(hav, i + 1, 0);
861 3 hdrs[i / 2].name = SvPV_const(*k, hdrs[i / 2].nlen);
862 6 ▶ hdrs[i / 2].val = SvPV_const(*v, hdrs[i / 2].vlen);
Branch: 1 branch
BranchTrueFalseTotal
hdrs[i / 2].val = SvPV_const(*v, hdrs[i / 2].vlen); 3 0 3
863 }
864 }
865 }
866 1 ▶ if (body && SvOK(body)) bp = SvPV_const(body, blen);
Branch: 2 branches
BranchTrueFalseTotal
if (body && SvOK(body)) bp = SvPV_const(body, blen); 0 1 1
if (body && SvOK(body)) bp = SvPV_const(body, blen); 0 0 0
867 1 Newxz(s, 1, rp_sctx);
868 1 s->responder = SvREFCNT_inc(responder);
869 1 s->writer = NULL;
870 1 f = FETCH->request_stream(aTHX_ ua_sv, method, url, hdrs, nhdrs,
871 bp, blen, timeout, -1,
872 rp_stream_on_headers, rp_stream_on_body,
873 rp_stream_on_done, s);
874 1 ▶ if (hdrs) Safefree(hdrs);
Branch: 1 branch
BranchTrueFalseTotal
if (hdrs) Safefree(hdrs); 1 0 1
875 1 ▶ if (async) {
Branch: 1 branch
BranchTrueFalseTotal
if (async) { 0 1 1
876 0 SvREFCNT_dec(f); /* the loop pumps it; callbacks fire */
877 } else { /* blocking: pump to completion now */
878 1 dSP;
879 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 1); PUSHs(sv_2mortal(f));
Branch: 2 branches
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 1); PUSHs(sv_2mortal(f)); 0 1 1
ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 1); PUSHs(sv_2mortal(f)); 0 1 1
880 1 PUTBACK;
881 1 call_method("get", G_VOID | G_DISCARD);
882 1 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 1 0 1
883 }
884 1 }
885  
886 /* the psgi.streaming coderef body: unpack the captures, run the stream */
887 XS_INTERNAL(rp_stream_cb);
888 1 XS_INTERNAL(rp_stream_cb) {
889 1 dXSARGS;
890 1 AV *cap = rp_clos_cap(aTHX_ cv);
891 1 ▶ SV *responder = items >= 1 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *responder = items >= 1 ? ST(0) : &PL_sv_undef; 1 0 1
892 SV *self_sv, *ua, *msv, *usv, *env_rv, *body, *tv;
893 HV *self;
894 1 ▶ if (!cap) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) XSRETURN_EMPTY; 0 1 1
895 1 self_sv = *av_fetch(cap, 0, 0);
896 1 ua = *av_fetch(cap, 1, 0);
897 1 msv = *av_fetch(cap, 2, 0);
898 1 usv = *av_fetch(cap, 3, 0);
899 1 env_rv = *av_fetch(cap, 4, 0);
900 1 body = *av_fetch(cap, 5, 0);
901 1 self = (HV *)SvRV(self_sv);
902 1 tv = rp_self(aTHX_ self, "timeout", 7);
903 5 ▶ rp_stream_run(aTHX_ ua, SvPV_nolen(msv), SvPV_nolen(usv),
Branch: 2 branches
BranchTrueFalseTotal
rp_stream_run(aTHX_ ua, SvPV_nolen(msv), SvPV_nolen(usv), 1 0 1
rp_stream_run(aTHX_ ua, SvPV_nolen(msv), SvPV_nolen(usv), 1 0 1
904 2 ▶ (HV *)SvRV(env_rv), SvOK(body) ? body : NULL,
Branch: 1 branch
BranchTrueFalseTotal
(HV *)SvRV(env_rv), SvOK(body) ? body : NULL, 0 1 1
905 rp_self_bool(aTHX_ self, "preserve_host", 13),
906 rp_self(aTHX_ self, "via", 3),
907 1 ▶ (tv && SvOK(tv)) ? SvNV(tv) : 0,
Branch: 1 branch
BranchTrueFalseTotal
(tv && SvOK(tv)) ? SvNV(tv) : 0, 1 0 1
908 1 (int)SvIV(*av_fetch(cap, 6, 0)), responder);
909 1 XSRETURN_EMPTY;
910 }
911  
912 /* the whole request path in C: resolve, then tunnel / stream / buffer */
913 37 static SV *rp_serve(pTHX_ SV *self_sv, SV *env_rv) {
914 HV *self, *env;
915 37 SV *path = NULL, *base;
916 37 ▶ if (!FETCH) croak("Reverse::Proxy: Fetch C ABI unavailable");
Branch: 1 branch
BranchTrueFalseTotal
if (!FETCH) croak("Reverse::Proxy: Fetch C ABI unavailable"); 0 37 37
917 37 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVHV) 37 0 37
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVHV) 0 37 37
918 0 croak("Reverse::Proxy: self must be a hashref");
919 37 ▶ if (!SvROK(env_rv) || SvTYPE(SvRV(env_rv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(env_rv) || SvTYPE(SvRV(env_rv)) != SVt_PVHV) 37 0 37
if (!SvROK(env_rv) || SvTYPE(SvRV(env_rv)) != SVt_PVHV) 0 37 37
920 0 croak("Reverse::Proxy: env must be a hashref");
921 37 self = (HV *)SvRV(self_sv);
922 37 env = (HV *)SvRV(env_rv);
923  
924 37 base = rp_resolve(aTHX_ self_sv, self, env, &path);
925 37 ▶ if (!base) return rp_no_upstream(aTHX_ env);
Branch: 1 branch
BranchTrueFalseTotal
if (!base) return rp_no_upstream(aTHX_ env); 2 35 37
926 35 ▶ if (rp_is_upgrade(aTHX_ env)) return rp_tunnel(aTHX_ self, env, base, path);
Branch: 1 branch
BranchTrueFalseTotal
if (rp_is_upgrade(aTHX_ env)) return rp_tunnel(aTHX_ self, env, base, path); 3 32 35
927 {
928 32 SV **mp = hv_fetchs(env, "REQUEST_METHOD", 0);
929 64 ▶ const char *method = (mp && *mp && SvOK(*mp)) ? SvPV_nolen(*mp) : "GET";
Branch: 3 branches
BranchTrueFalseTotal
const char *method = (mp && *mp && SvOK(*mp)) ? SvPV_nolen(*mp) : "GET"; 32 0 32
const char *method = (mp && *mp && SvOK(*mp)) ? SvPV_nolen(*mp) : "GET"; 32 0 32
const char *method = (mp && *mp && SvOK(*mp)) ? SvPV_nolen(*mp) : "GET"; 32 0 32
930 32 SV *url = rp_target_url(aTHX_ base, path, env);
931 32 SV *body = rp_read_body(aTHX_ env);
932 int async;
933 32 SV *ua = rp_get_ua(aTHX_ self_sv, self, env, &async);
934 32 ▶ if (rp_self_bool(aTHX_ self, "stream", 6)) {
Branch: 1 branch
BranchTrueFalseTotal
if (rp_self_bool(aTHX_ self, "stream", 6)) { 1 31 32
935 1 AV *cap = newAV(); /* built and run in C */
936 1 av_push(cap, SvREFCNT_inc(self_sv));
937 1 av_push(cap, SvREFCNT_inc(ua));
938 1 av_push(cap, newSVpv(method, 0));
939 1 av_push(cap, SvREFCNT_inc(url));
940 1 av_push(cap, SvREFCNT_inc(env_rv));
941 1 ▶ av_push(cap, body ? SvREFCNT_inc(body) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(cap, body ? SvREFCNT_inc(body) : newSV(0)); 0 1 1
942 1 av_push(cap, newSViv(async));
943 1 return rp_closure(aTHX_ rp_stream_cb, cap);
944 }
945 {
946 31 SV *f = rp_dispatch(aTHX_ ua, self, env, method, url, body);
947 31 ▶ if (async) return f; /* hand the Future over */
Branch: 1 branch
BranchTrueFalseTotal
if (async) return f; /* hand the Future over */ 0 31 31
948 { /* blocking: await triplet */
949 31 dSP; int n; SV *r;
950 31 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 31 31
951 31 ▶ EXTEND(SP, 1); PUSHs(sv_2mortal(f));
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); PUSHs(sv_2mortal(f)); 0 31 31
952 31 PUTBACK;
953 31 n = call_method("get", G_SCALAR);
954 31 SPAGAIN;
955 31 ▶ r = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
r = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; 31 0 31
956 31 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 31 0 31
957 31 return r;
958 }
959 }
960 }
961 }
962  
963 /* the PSGI app coderef body (to_app): call rp_serve($self, $env) */
964 XS_INTERNAL(rp_app_cb);
965 37 XS_INTERNAL(rp_app_cb) {
966 37 dXSARGS;
967 37 AV *cap = rp_clos_cap(aTHX_ cv);
968 37 ▶ SV *self_sv = cap ? *av_fetch(cap, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *self_sv = cap ? *av_fetch(cap, 0, 0) : NULL; 37 0 37
969 37 ▶ SV *env = items >= 1 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *env = items >= 1 ? ST(0) : &PL_sv_undef; 37 0 37
970 SV *resp;
971 37 ▶ if (!self_sv) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!self_sv) XSRETURN_EMPTY; 0 37 37
972 37 resp = rp_serve(aTHX_ self_sv, env);
973 37 ST(0) = sv_2mortal(resp);
974 37 XSRETURN(1);
975 }
976  
977 MODULE = Reverse::Proxy PACKAGE = Reverse::Proxy
978  
979 PROTOTYPES: DISABLE
980  
981 BOOT:
982 {
983 12 IV p = 0;
984 12 dSP;
985 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
986 12 PUTBACK;
987 12 ▶ if (call_pv("Fetch::_abi_ptr", G_SCALAR) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (call_pv("Fetch::_abi_ptr", G_SCALAR) > 0) { 12 0 12
988 12 SPAGAIN;
989 12 p = POPi;
990 12 PUTBACK;
991 }
992 12 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 12 0 12
993 12 const fetch_abi *a = INT2PTR(const fetch_abi *, p);
994 12 ▶ if (a) {
Branch: 1 branch
BranchTrueFalseTotal
if (a) { 12 0 12
995 12 FETCH_SEEN = a->abi_version;
996 /* >=, NOT ==.
997 *
998 * The table is APPEND-ONLY by rule, so every field this build
999 * knows about keeps its offset in a later version - which is the
1000 * entire reason the version field exists. Requiring an exact
1001 * match turns each provider release into a breaking change for
1002 * every consumer of it, and that is not theoretical: Fetch 0.14
1003 * shipped ABI 2 (one new member, appended) and every 0.04
1004 * install refused to load against it, on every platform.
1005 *
1006 * A provider OLDER than this build is still refused - those
1007 * fields genuinely are not there. */
1008 12 ▶ if (a->abi_version >= FETCH_ABI_VERSION) FETCH = a;
Branch: 1 branch
BranchTrueFalseTotal
if (a->abi_version >= FETCH_ABI_VERSION) FETCH = a; 12 0 12
1009 }
1010 }
1011 12 sv_setiv(get_sv("Reverse::Proxy::_HAVE_XS", GV_ADD), FETCH ? 1 : 0);
1012 }
1013  
1014 # True when the Fetch C ABI resolved and is new enough for this build.
1015 int
1016 _abi_ok()
1017 CODE:
1018 13 ▶ RETVAL = FETCH ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FETCH ? 1 : 0; 0 13 13
1019 OUTPUT:
1020 RETVAL
1021  
1022 # What the installed Fetch actually offered: its abi_version, or 0 when the
1023 # ABI did not resolve at all. Only so the croak can name the numbers instead
1024 # of guessing at a cause.
1025 int
1026 _abi_seen()
1027 CODE:
1028 1 ▶ RETVAL = FETCH_SEEN;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FETCH_SEEN; 0 1 1
1029 OUTPUT:
1030 RETVAL
1031  
1032 # What this build was compiled against.
1033 int
1034 _abi_want()
1035 CODE:
1036 1 ▶ RETVAL = FETCH_ABI_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = FETCH_ABI_VERSION; 0 1 1
1037 OUTPUT:
1038 RETVAL
1039  
1040 # The whole request path, in C (rp_serve): resolve the target, then tunnel
1041 # (Upgrade), stream (stream => 1), or buffer through the Fetch ABI. Returns a
1042 # Fetch::Future (async servers) or the PSGI triplet (blocking). to_app's app
1043 # coderef calls the same rp_serve.
1044 SV *
1045 _serve(self_sv, env_rv)
1046 SV *self_sv
1047 SV *env_rv
1048 CODE:
1049 0 RETVAL = rp_serve(aTHX_ self_sv, env_rv);
1050 OUTPUT:
1051 RETVAL
1052  
1053 # Reverse::Proxy->new(%opt): validate exactly one target selector, normalise
1054 # routes (longest prefix first) and bless the config hash - all in C.
1055 SV *
1056 new(class, ...)
1057 SV *class
1058 CODE:
1059 {
1060 0 ▶ const char *cls = (SvROK(class) && SvOBJECT(SvRV(class)))
Branch: 1 branch
BranchTrueFalseTotal
const char *cls = (SvROK(class) && SvOBJECT(SvRV(class))) 0 0 0
1061 32 ▶ ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class);
Branch: 7 branches
BranchTrueFalseTotal
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 16 16
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 0 0 0
1062 16 HV *self = newHV();
1063 16 SV *upstream = NULL, *routes_arg = NULL, *resolver = NULL, *via = NULL;
1064 16 SV *preserve = NULL, *stream = NULL, *pool = NULL, *timeout = NULL, *tlsv = NULL;
1065 16 int i, nsel, have_via = 0;
1066 37 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 21 16 37
1067 21 const char *k = SvPV_nolen(ST(i));
1068 21 SV *v = ST(i + 1);
1069 21 ▶ if (strEQ(k, "upstream")) { if (SvOK(v)) upstream = v; }
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(k, "upstream")) { if (SvOK(v)) upstream = v; } 12 9 21
if (strEQ(k, "upstream")) { if (SvOK(v)) upstream = v; } 12 0 12
1070 9 ▶ else if (strEQ(k, "routes")) { if (SvOK(v)) routes_arg = v; }
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "routes")) { if (SvOK(v)) routes_arg = v; } 2 7 9
else if (strEQ(k, "routes")) { if (SvOK(v)) routes_arg = v; } 2 0 2
1071 7 ▶ else if (strEQ(k, "resolver")) { if (SvOK(v)) resolver = v; }
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "resolver")) { if (SvOK(v)) resolver = v; } 2 5 7
else if (strEQ(k, "resolver")) { if (SvOK(v)) resolver = v; } 2 0 2
1072 5 ▶ else if (strEQ(k, "preserve_host")) preserve = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "preserve_host")) preserve = v; 1 4 5
1073 4 ▶ else if (strEQ(k, "stream")) stream = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "stream")) stream = v; 1 3 4
1074 3 ▶ else if (strEQ(k, "pool_size")) pool = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "pool_size")) pool = v; 0 3 3
1075 3 ▶ else if (strEQ(k, "timeout")) timeout = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "timeout")) timeout = v; 2 1 3
1076 1 ▶ else if (strEQ(k, "tls_verify")) tlsv = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "tls_verify")) tlsv = v; 0 1 1
1077 1 ▶ else if (strEQ(k, "via")) { via = v; have_via = 1; }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "via")) { via = v; have_via = 1; } 1 0 1
1078 }
1079 16 nsel = (upstream ? 1 : 0) + (routes_arg ? 1 : 0) + (resolver ? 1 : 0);
1080 16 ▶ if (nsel != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (nsel != 1) 2 14 16
1081 2 croak("Reverse::Proxy->new: give exactly one of 'upstream', 'routes', 'resolver'");
1082 14 ▶ if (upstream) (void)hv_stores(self, "upstream", newSVsv(upstream));
Branch: 1 branch
BranchTrueFalseTotal
if (upstream) (void)hv_stores(self, "upstream", newSVsv(upstream)); 11 3 14
1083 14 ▶ if (resolver) (void)hv_stores(self, "resolver", newSVsv(resolver));
Branch: 1 branch
BranchTrueFalseTotal
if (resolver) (void)hv_stores(self, "resolver", newSVsv(resolver)); 1 13 14
1084 14 ▶ if (routes_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (routes_arg) { 2 12 14
1085 AV *src, *pairs; SSize_t n, j;
1086 2 ▶ if (!SvROK(routes_arg) || SvTYPE(SvRV(routes_arg)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(routes_arg) || SvTYPE(SvRV(routes_arg)) != SVt_PVAV) 2 0 2
if (!SvROK(routes_arg) || SvTYPE(SvRV(routes_arg)) != SVt_PVAV) 0 2 2
1087 0 croak("Reverse::Proxy->new: routes must be [ prefix => url, ... ]");
1088 2 src = (AV *)SvRV(routes_arg); n = av_len(src) + 1;
1089 2 ▶ if (n % 2) croak("Reverse::Proxy->new: routes must be [ prefix => url, ... ]");
Branch: 1 branch
BranchTrueFalseTotal
if (n % 2) croak("Reverse::Proxy->new: routes must be [ prefix => url, ... ]"); 0 2 2
1090 2 pairs = newAV();
1091 5 ▶ for (j = 0; j + 1 < n; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < n; j += 2) { 3 2 5
1092 3 SV **p = av_fetch(src, j, 0), **u = av_fetch(src, j + 1, 0);
1093 3 AV *pair = newAV();
1094 3 ▶ av_push(pair, (p && *p) ? newSVsv(*p) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(pair, (p && *p) ? newSVsv(*p) : newSV(0)); 3 0 3
av_push(pair, (p && *p) ? newSVsv(*p) : newSV(0)); 3 0 3
1095 3 ▶ av_push(pair, (u && *u) ? newSVsv(*u) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(pair, (u && *u) ? newSVsv(*u) : newSV(0)); 3 0 3
av_push(pair, (u && *u) ? newSVsv(*u) : newSV(0)); 3 0 3
1096 3 av_push(pairs, newRV_noinc((SV *)pair));
1097 }
1098 { /* longest prefix first (selection sort; routes are few) */
1099 2 SSize_t m = av_len(pairs) + 1, a, b;
1100 2 SV **arr = AvARRAY(pairs);
1101 3 ▶ for (a = 0; a + 1 < m; a++) {
Branch: 1 branch
BranchTrueFalseTotal
for (a = 0; a + 1 < m; a++) { 1 2 3
1102 1 SSize_t best = a;
1103 1 STRLEN bl = SvCUR(*av_fetch((AV *)SvRV(arr[a]), 0, 0));
1104 2 ▶ for (b = a + 1; b < m; b++) {
Branch: 1 branch
BranchTrueFalseTotal
for (b = a + 1; b < m; b++) { 1 1 2
1105 1 STRLEN l = SvCUR(*av_fetch((AV *)SvRV(arr[b]), 0, 0));
1106 1 ▶ if (l > bl) { best = b; bl = l; }
Branch: 1 branch
BranchTrueFalseTotal
if (l > bl) { best = b; bl = l; } 0 1 1
1107 }
1108 1 ▶ if (best != a) { SV *t = arr[a]; arr[a] = arr[best]; arr[best] = t; }
Branch: 1 branch
BranchTrueFalseTotal
if (best != a) { SV *t = arr[a]; arr[a] = arr[best]; arr[best] = t; } 0 1 1
1109 }
1110 }
1111 2 (void)hv_stores(self, "routes", newRV_noinc((SV *)pairs));
1112 }
1113 14 ▶ (void)hv_stores(self, "preserve_host", newSViv(preserve && SvTRUE(preserve) ? 1 : 0));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(self, "preserve_host", newSViv(preserve && SvTRUE(preserve) ? 1 : 0)); 1 13 14
(void)hv_stores(self, "preserve_host", newSViv(preserve && SvTRUE(preserve) ? 1 : 0)); 1 0 1
1114 14 ▶ (void)hv_stores(self, "stream", newSViv(stream && SvTRUE(stream) ? 1 : 0));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(self, "stream", newSViv(stream && SvTRUE(stream) ? 1 : 0)); 1 13 14
(void)hv_stores(self, "stream", newSViv(stream && SvTRUE(stream) ? 1 : 0)); 1 0 1
1115 14 ▶ (void)hv_stores(self, "pool_size", newSViv((pool && SvOK(pool)) ? SvIV(pool) : 64));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(self, "pool_size", newSViv((pool && SvOK(pool)) ? SvIV(pool) : 64)); 0 14 14
(void)hv_stores(self, "pool_size", newSViv((pool && SvOK(pool)) ? SvIV(pool) : 64)); 0 0 0
1116 14 ▶ (void)hv_stores(self, "timeout", newSVnv((timeout && SvOK(timeout)) ? SvNV(timeout) : 30));
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(self, "timeout", newSVnv((timeout && SvOK(timeout)) ? SvNV(timeout) : 30)); 2 12 14
(void)hv_stores(self, "timeout", newSVnv((timeout && SvOK(timeout)) ? SvNV(timeout) : 30)); 2 0 2
1117 14 ▶ (void)hv_stores(self, "tls_verify", newSViv((tlsv && SvOK(tlsv)) ? (SvTRUE(tlsv) ? 1 : 0) : 1));
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(self, "tls_verify", newSViv((tlsv && SvOK(tlsv)) ? (SvTRUE(tlsv) ? 1 : 0) : 1)); 0 14 14
(void)hv_stores(self, "tls_verify", newSViv((tlsv && SvOK(tlsv)) ? (SvTRUE(tlsv) ? 1 : 0) : 1)); 0 0 0
(void)hv_stores(self, "tls_verify", newSViv((tlsv && SvOK(tlsv)) ? (SvTRUE(tlsv) ? 1 : 0) : 1)); 0 0 0
1118 14 ▶ (void)hv_stores(self, "via", have_via ? newSVsv(via) : newSVpvs("Reverse::Proxy"));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(self, "via", have_via ? newSVsv(via) : newSVpvs("Reverse::Proxy")); 1 13 14
1119 14 (void)hv_stores(self, "_ua", newRV_noinc((SV *)newHV()));
1120 14 RETVAL = sv_bless(newRV_noinc((SV *)self), gv_stashpv(cls, GV_ADD));
1121 }
1122 OUTPUT:
1123 RETVAL
1124  
1125 # to_app: the PSGI app coderef, built in C (captures $self, calls rp_serve).
1126 SV *
1127 to_app(self_sv)
1128 SV *self_sv
1129 CODE:
1130 {
1131 14 AV *cap = newAV();
1132 14 av_push(cap, SvREFCNT_inc(self_sv));
1133 14 RETVAL = rp_closure(aTHX_ rp_app_cb, cap);
1134 }
1135 OUTPUT:
1136 RETVAL
↑ Summary blib/lib/Reverse/Proxy.pm »