include/punk/punk_wshandshake.h

filter: Statementstmt 95.7% 290 / 304 (1 uncoverable) Branchbran 73.9% 245 / 364 (34 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.8% 535 / 668 (35 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_cookie.h ↑ Summary include/punk/punk_mrows.h »
1 /* punk_wshandshake.h - the WebSocket upgrade handshake, in C.
2 *
3 * Punk::WebSocket::_dispatch, reached from the dispatcher once a route's
4 * guards have passed: validate the upgrade request, negotiate a subprotocol,
5 * answer with the RFC 6455 Sec-WebSocket-Accept, then take the socket over
6 * (Hyperman's detach ABI, or the blocking psgix.io fallback) and hand the
7 * connection to the route's handler. The frame codec and the connection object
8 * are already C (punk_ws.h / punk_wsconn.h, xs/websocket.xs); this is the last
9 * Perl piece, so lib/Punk/WebSocket.pm becomes documentation only.
10 *
11 * SHA-1 and base64 are bundled here (a public-domain SHA-1, ~80 lines) rather
12 * than pulled from Digest::SHA / OpenSSL: it keeps the dist zero-dependency and
13 * takes the two core-module calls off the once-per-connection path.
14 *
15 * Must be included after punk_context.h (pcx_call_meth / pcx_call2).
16 */
17  
18 #ifndef PUNK_WSHANDSHAKE_H
19 #define PUNK_WSHANDSHAKE_H
20  
21 /* the RFC 6455 magic GUID appended to the client key before the digest */
22 #define PW_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
23  
24 /* ---- SHA-1 (public domain) ------------------------------------------------ */
25  
26 #define PW_ROL(v, b) (((v) << (b)) | ((v) >> (32 - (b))))
27  
28 70 static void pw_sha1(const unsigned char *msg, size_t len, unsigned char out[20]) {
29 70 U32 h0 = 0x67452301, h1 = 0xEFCDAB89, h2 = 0x98BADCFE,
30 70 h3 = 0x10325476, h4 = 0xC3D2E1F0;
31 70 size_t total = ((len + 8) / 64 + 1) * 64, off;
32 unsigned char *m;
33 70 U32 lo = (U32)(len << 3), hi = (U32)(len >> 29);
34 70 Newxz(m, total, unsigned char);
35 70 memcpy(m, msg, len);
36 70 m[len] = 0x80;
37 70 m[total - 1] = (unsigned char)(lo & 0xff);
38 70 m[total - 2] = (unsigned char)((lo >> 8) & 0xff);
39 70 m[total - 3] = (unsigned char)((lo >> 16) & 0xff);
40 70 m[total - 4] = (unsigned char)((lo >> 24) & 0xff);
41 70 m[total - 5] = (unsigned char)(hi & 0xff);
42 70 m[total - 6] = (unsigned char)((hi >> 8) & 0xff);
43 70 m[total - 7] = (unsigned char)((hi >> 16) & 0xff);
44 70 m[total - 8] = (unsigned char)((hi >> 24) & 0xff);
45 210 ▶ for (off = 0; off < total; off += 64) {
Branch: 1 branch
BranchTrueFalseTotal
for (off = 0; off < total; off += 64) { 140 70 210
46 U32 w[80], a, b, c, d, e, f, k, tmp;
47 int i;
48 2380 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 2240 140 2380
49 2240 w[i] = ((U32)m[off + 4*i] << 24) | ((U32)m[off + 4*i + 1] << 16)
50 2240 | ((U32)m[off + 4*i + 2] << 8) | (U32)m[off + 4*i + 3];
51 9100 ▶ for (i = 16; i < 80; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 16; i < 80; i++) 8960 140 9100
52 8960 w[i] = PW_ROL(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1);
53 140 a = h0; b = h1; c = h2; d = h3; e = h4;
54 11340 ▶ for (i = 0; i < 80; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 80; i++) { 11200 140 11340
55 11200 ▶ if (i < 20) { f = (b & c) | ((~b) & d); k = 0x5A827999; }
Branch: 1 branch
BranchTrueFalseTotal
if (i < 20) { f = (b & c) | ((~b) & d); k = 0x5A827999; } 2800 8400 11200
56 8400 ▶ else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; }
Branch: 1 branch
BranchTrueFalseTotal
else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; } 2800 5600 8400
57 5600 ▶ else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; }
Branch: 1 branch
BranchTrueFalseTotal
else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } 2800 2800 5600
58 2800 else { f = b ^ c ^ d; k = 0xCA62C1D6; }
59 11200 tmp = PW_ROL(a, 5) + f + e + k + w[i];
60 11200 e = d; d = c; c = PW_ROL(b, 30); b = a; a = tmp;
61 }
62 140 h0 += a; h1 += b; h2 += c; h3 += d; h4 += e;
63 }
64 70 Safefree(m);
65 {
66 U32 hh[5]; int i;
67 70 hh[0] = h0; hh[1] = h1; hh[2] = h2; hh[3] = h3; hh[4] = h4;
68 420 ▶ for (i = 0; i < 5; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) { 350 70 420
69 350 out[4*i] = (unsigned char)((hh[i] >> 24) & 0xff);
70 350 out[4*i + 1] = (unsigned char)((hh[i] >> 16) & 0xff);
71 350 out[4*i + 2] = (unsigned char)((hh[i] >> 8) & 0xff);
72 350 out[4*i + 3] = (unsigned char)(hh[i] & 0xff);
73 }
74 }
75 70 }
76  
77 /* ---- base64 --------------------------------------------------------------- */
78  
79 70 static void pw_base64(const unsigned char *in, size_t len, char *out) {
80 static const char B64[] =
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
82 70 size_t i = 0, o = 0;
83 490 ▶ for (; i + 3 <= len; i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i + 3 <= len; i += 3) { 420 70 490
84 420 U32 n = ((U32)in[i] << 16) | ((U32)in[i+1] << 8) | in[i+2];
85 420 out[o++] = B64[(n >> 18) & 63]; out[o++] = B64[(n >> 12) & 63];
86 420 out[o++] = B64[(n >> 6) & 63]; out[o++] = B64[n & 63];
87 }
88 /*
89 # uncoverable branch false
90 # uncoverable condition right count:1 class:ignore_covered_err */
91 70 ▶ if (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < len) { 70 0 70
92 70 int rem = (int)(len - i);
93 70 U32 n = (U32)in[i] << 16;
94 /*
95 # uncoverable branch false
96 # uncoverable condition right count:1 class:ignore_covered_err */
97 70 ▶ if (rem == 2) n |= (U32)in[i+1] << 8;
Branch: 1 branch
BranchTrueFalseTotal
if (rem == 2) n |= (U32)in[i+1] << 8; 70 0 70
98 70 out[o++] = B64[(n >> 18) & 63];
99 70 out[o++] = B64[(n >> 12) & 63];
100 70 ▶ out[o++] = rem == 2 ? B64[(n >> 6) & 63] : '='; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
out[o++] = rem == 2 ? B64[(n >> 6) & 63] : '='; /* # uncoverable condition right count:1 class:ignore_covered_err */ 70 0 70
101 70 out[o++] = '=';
102 }
103 70 out[o] = '\0';
104 70 }
105  
106 /* ---- request helpers ------------------------------------------------------ */
107  
108 617 static const char *pw_env(pTHX_ HV *env, const char *k, STRLEN *len) {
109 617 SV **e = hv_fetch(env, k, (I32)strlen(k), 0);
110 /*
111 # uncoverable condition right count:2 class:ignore_covered_err
112 # uncoverable condition right count:3 class:ignore_covered_err */
113 1112 ▶ if (e && *e && SvOK(*e)) return SvPV_const(*e, *len);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 495 122 617
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 495 0 495
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 495 0 495
114 122 *len = 0; return "";
115 }
116  
117 /* Is `needle` (already lowercase) one of the comma-separated tokens of a
118 * header value, compared without case? A token, not a substring:
119 * `Upgrade: notwebsocket` is not an upgrade to websocket. A protocol may
120 * carry a version after a slash, which is still that protocol. */
121 186 static int pw_ci_has(const char *hay, STRLEN hlen, const char *needle) {
122 186 STRLEN nl = strlen(needle), i = 0;
123 /*
124 # uncoverable branch true
125 # uncoverable condition left count:1 class:ignore_covered_err */
126 186 ▶ if (nl == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (nl == 0) return 1; 0 186 186
127 193 ▶ while (i < hlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < hlen) { 185 8 193
128 STRLEN s, e, j;
129 /*
130 # uncoverable condition right count:1 class:ignore_covered_err
131 # uncoverable condition left count:3 class:ignore_covered_err */
132 189 ▶ while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ','))
Branch: 4 branches
BranchTrueFalseTotal
while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ',')) 189 0 189
while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ',')) 2 187 189
while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ',')) 0 187 187
while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ',')) 2 185 187
133 4 i++;
134 185 s = i;
135 1679 ▶ while (i < hlen && hay[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < hlen && hay[i] != ',') i++; 1497 182 1679
while (i < hlen && hay[i] != ',') i++; 1494 3 1497
136 185 e = i;
137 /*
138 # uncoverable condition right count:1 class:ignore_covered_err
139 # uncoverable condition left count:3 class:ignore_covered_err */
140 186 ▶ while (e > s && (hay[e - 1] == ' ' || hay[e - 1] == '\t')) e--;
Branch: 3 branches
BranchTrueFalseTotal
while (e > s && (hay[e - 1] == ' ' || hay[e - 1] == '\t')) e--; 186 0 186
while (e > s && (hay[e - 1] == ' ' || hay[e - 1] == '\t')) e--; 1 185 186
while (e > s && (hay[e - 1] == ' ' || hay[e - 1] == '\t')) e--; 0 185 185
141 185 ▶ if (e - s < nl) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (e - s < nl) continue; 1 184 185
142 1622 ▶ for (j = 0; j < nl; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nl; j++) { 1443 179 1622
143 1443 char ch = hay[s + j];
144 1443 ▶ if (ch >= 'A' && ch <= 'Z') ch = (char)(ch + 32);
Branch: 2 branches
BranchTrueFalseTotal
if (ch >= 'A' && ch <= 'Z') ch = (char)(ch + 32); 1442 1 1443
if (ch >= 'A' && ch <= 'Z') ch = (char)(ch + 32); 96 1346 1442
145 1443 ▶ if (ch != needle[j]) break;
Branch: 1 branch
BranchTrueFalseTotal
if (ch != needle[j]) break; 5 1438 1443
146 }
147 184 ▶ if (j == nl && (e - s == nl || hay[s + nl] == '/')) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (j == nl && (e - s == nl || hay[s + nl] == '/')) return 1; 179 5 184
if (j == nl && (e - s == nl || hay[s + nl] == '/')) return 1; 2 177 179
if (j == nl && (e - s == nl || hay[s + nl] == '/')) return 1; 1 1 2
148 }
149 8 return 0;
150 }
151  
152 88 static int pw_is_b64_key(const char *k, STRLEN kl) {
153 STRLEN i;
154 /*
155 # uncoverable condition left count:2 class:ignore_covered_err
156 # uncoverable condition left count:3 class:ignore_covered_err */
157 88 ▶ if (kl != 24 || k[22] != '=' || k[23] != '=') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (kl != 24 || k[22] != '=' || k[23] != '=') return 0; 86 2 88
if (kl != 24 || k[22] != '=' || k[23] != '=') return 0; 86 0 86
if (kl != 24 || k[22] != '=' || k[23] != '=') return 0; 0 86 86
158 1978 ▶ for (i = 0; i < 22; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 22; i++) { 1892 86 1978
159 1892 char ch = k[i];
160 /*
161 # uncoverable branch true
162 # uncoverable condition left count:1 class:ignore_covered_err
163 # uncoverable condition left count:2 class:ignore_covered_err
164 # uncoverable condition left count:3 class:ignore_covered_err
165 # uncoverable condition left count:4 class:ignore_covered_err
166 # uncoverable condition left count:5 class:ignore_covered_err
167 # uncoverable condition left count:6 class:ignore_covered_err
168 # uncoverable condition left count:7 class:ignore_covered_err
169 # uncoverable condition left count:8 class:ignore_covered_err */
170 1912 ▶ if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
Branch: 5 branches
BranchTrueFalseTotal
if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') 1617 275 1892
if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') 796 821 1617
if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') 796 275 1071
if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') 0 796 796
if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') 0 20 20
171 275 ▶ || (ch >= '0' && ch <= '9') || ch == '+' || ch == '/'))
Branch: 3 branches
BranchTrueFalseTotal
|| (ch >= '0' && ch <= '9') || ch == '+' || ch == '/')) 236 39 275
|| (ch >= '0' && ch <= '9') || ch == '+' || ch == '/')) 0 236 236
|| (ch >= '0' && ch <= '9') || ch == '+' || ch == '/')) 20 19 39
172 0 return 0; /* # uncoverable statement */
173 }
174 86 return 1;
175 }
176  
177 /* A short text error response (the Perl $ERR), with an optional header. */
178 54 static SV *pw_err(pTHX_ IV status, const char *msg, const char *hk, const char *hv) {
179 54 AV *resp = newAV(), *headers = newAV(), *body = newAV();
180 54 STRLEN len = strlen(msg);
181 54 av_push(headers, newSVpvs("Content-Type"));
182 54 av_push(headers, newSVpvs("text/plain; charset=utf-8"));
183 54 av_push(headers, newSVpvs("Content-Length"));
184 54 av_push(headers, newSViv((IV)len));
185 54 ▶ if (hk) { av_push(headers, newSVpv(hk, 0)); av_push(headers, newSVpv(hv, 0)); }
Branch: 1 branch
BranchTrueFalseTotal
if (hk) { av_push(headers, newSVpv(hk, 0)); av_push(headers, newSVpv(hv, 0)); } 2 52 54
186 54 av_push(body, newSVpvn(msg, len));
187 54 av_push(resp, newSViv(status));
188 54 av_push(resp, newRV_noinc((SV *)headers));
189 54 av_push(resp, newRV_noinc((SV *)body));
190 54 return newRV_noinc((SV *)resp);
191 }
192  
193 /* an empty [ status, [], [] ] the server discards after the upgrade */
194 77 static SV *pw_empty(pTHX_ IV status) {
195 77 AV *resp = newAV();
196 77 av_push(resp, newSViv(status));
197 77 av_push(resp, newRV_noinc((SV *)newAV()));
198 77 av_push(resp, newRV_noinc((SV *)newAV()));
199 77 return newRV_noinc((SV *)resp);
200 }
201  
202 /* Call a Punk::WebSocket class method (_attach / _attach_blocking); +1. */
203 50 static SV *pw_class_call(pTHX_ const char *meth, SV **argv, int n) {
204 50 return pcx_call_meth(aTHX_ sv_2mortal(newSVpvs(PK_WEBSOCKET)), meth, argv, n, 1);
205 }
206  
207 /* Run the route handler ($code->($c, $ws)); on death close 1011 and rethrow. */
208 50 static void pw_run_handler(pTHX_ SV *code, SV *c, SV *ws) {
209 50 int died = 0;
210 50 SV *r = pcx_call2(aTHX_ code, c, ws, &died);
211 /*
212 # uncoverable branch false
213 # uncoverable condition right count:1 class:ignore_covered_err */
214 50 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 50 0 50
215 50 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 1 49 50
216 1 ▶ SV *e = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *e = sv_2mortal(newSVsv(ERRSV)); 1 0 1
217 SV *ca[2], *cr;
218 1 ca[0] = sv_2mortal(newSViv(1011));
219 1 ca[1] = sv_2mortal(newSVpvs("handler error"));
220 1 cr = pcx_call_meth(aTHX_ ws, "close", ca, 2, 0);
221 /*
222 # uncoverable branch true
223 # uncoverable condition left count:1 class:ignore_covered_err */
224 1 ▶ if (cr) SvREFCNT_dec(cr);
Branch: 1 branch
BranchTrueFalseTotal
if (cr) SvREFCNT_dec(cr); 0 1 1
225 1 croak_sv(e);
226 }
227 49 { SV *o = pcx_call_meth(aTHX_ ws, "_open", NULL, 0, 0);
228 /*
229 # uncoverable branch true
230 # uncoverable condition left count:1 class:ignore_covered_err */
231 49 ▶ if (o) SvREFCNT_dec(o); }
Branch: 1 branch
BranchTrueFalseTotal
if (o) SvREFCNT_dec(o); } 0 49 49
232 49 }
233  
234 /* ---- the upgrade ---------------------------------------------------------- */
235  
236 101 static SV *punk_ws_dispatch(pTHX_ SV *c, SV *rec, SV *env) {
237 /*
238 # uncoverable condition right count:1 class:ignore_covered_err
239 # uncoverable condition right count:2 class:ignore_covered_err */
240 101 ▶ HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL; 101 0 101
HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL; 101 0 101
241 /*
242 # uncoverable condition right count:1 class:ignore_covered_err
243 # uncoverable condition right count:2 class:ignore_covered_err */
244 101 ▶ HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL; 101 0 101
HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL; 101 0 101
245 101 HV *opts = NULL;
246 101 SV **x, *code = NULL, *protocol = NULL;
247 101 int is_connect = 0;
248 const char *method, *upg, *conn, *key, *ver;
249 STRLEN mlen, ul, cl, kl, vl;
250 unsigned char digest[20];
251 char accept[30];
252 SV *hs, *attach_rv;
253 HV *attach;
254  
255 /*
256 # uncoverable branch true
257 # uncoverable condition left count:1 class:ignore_covered_err
258 # uncoverable condition left count:2 class:ignore_covered_err */
259 101 ▶ if (!envh || !rech) return pw_err(aTHX_ 500, "bad websocket dispatch\n", NULL, NULL);
Branch: 2 branches
BranchTrueFalseTotal
if (!envh || !rech) return pw_err(aTHX_ 500, "bad websocket dispatch\n", NULL, NULL); 101 0 101
if (!envh || !rech) return pw_err(aTHX_ 500, "bad websocket dispatch\n", NULL, NULL); 0 101 101
260 101 x = hv_fetchs(rech, K_WS, 0);
261 /*
262 # uncoverable branch false
263 # uncoverable condition right count:1 class:ignore_covered_err
264 # uncoverable condition right count:2 class:ignore_covered_err
265 # uncoverable condition right count:3 class:ignore_covered_err
266 # uncoverable condition right count:4 class:ignore_covered_err */
267 101 ▶ if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x);
Branch: 4 branches
BranchTrueFalseTotal
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 101 0 101
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 101 0 101
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 101 0 101
if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); 101 0 101
268 101 x = hv_fetchs(rech, K_CODE, 0);
269 /*
270 # uncoverable condition right count:1 class:ignore_covered_err
271 # uncoverable condition right count:2 class:ignore_covered_err */
272 101 ▶ code = (x && *x) ? *x : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
code = (x && *x) ? *x : &PL_sv_undef; 101 0 101
code = (x && *x) ? *x : &PL_sv_undef; 101 0 101
273  
274 101 method = pw_env(aTHX_ envh, "REQUEST_METHOD", &mlen);
275  
276 /* Extended CONNECT (RFC 8441 on HTTP/2, RFC 9220 on HTTP/3): a
277 * multiplexed transport has no 101, no Upgrade header, and forbids
278 * Connection outright, so the handshake below simply does not exist
279 * there. The server having advertised ENABLE_CONNECT_PROTOCOL is the
280 * whole of the negotiation.
281 *
282 * The origin check and the subprotocol negotiation that follow are NOT
283 * skipped. Neither is HTTP/1.1-specific: an Extended CONNECT carries the
284 * user's cookies exactly as an upgrade does, so skipping the origin
285 * check here would open the hole it exists to close on precisely the
286 * transport a browser prefers. */
287 {
288 101 SV **cp = hv_fetchs(envh, "psgix.connect_protocol", 0);
289 /*
290 # uncoverable condition right count:2 class:ignore_covered_err
291 # uncoverable condition right count:3 class:ignore_covered_err */
292 101 ▶ if (cp && *cp && SvOK(*cp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cp && *cp && SvOK(*cp)) { 5 96 101
if (cp && *cp && SvOK(*cp)) { 5 0 5
if (cp && *cp && SvOK(*cp)) { 5 0 5
293 STRLEN pl;
294 5 const char *pv = SvPV_const(*cp, pl);
295 /*
296 # uncoverable branch false
297 # uncoverable condition right count:1 class:ignore_covered_err
298 # uncoverable condition right count:2 class:ignore_covered_err */
299 5 ▶ if (pl == 9 && memEQ(pv, "websocket", 9)) is_connect = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (pl == 9 && memEQ(pv, "websocket", 9)) is_connect = 1; 5 0 5
if (pl == 9 && memEQ(pv, "websocket", 9)) is_connect = 1; 5 0 5
300 }
301 }
302  
303 101 ▶ if (is_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_connect) { 5 96 101
304 5 ▶ if (!(mlen == 7 && memEQ(method, "CONNECT", 7))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(mlen == 7 && memEQ(method, "CONNECT", 7))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 4 1 5
if (!(mlen == 7 && memEQ(method, "CONNECT", 7))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 4 4
305 1 return pw_err(aTHX_ 400,
306 "websocket over HTTP/2 or HTTP/3 uses CONNECT\n", NULL, NULL);
307 } else {
308 /*
309 # uncoverable branch true
310 # uncoverable condition left count:1 class:ignore_covered_err
311 # uncoverable condition left count:2 class:ignore_covered_err */
312 96 ▶ if (!(mlen == 3 && memEQ(method, "GET", 3)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(mlen == 3 && memEQ(method, "GET", 3))) 96 0 96
if (!(mlen == 3 && memEQ(method, "GET", 3))) 0 96 96
313 0 return pw_err(aTHX_ 405, "websocket routes are GET\n", "Allow", "GET"); /* # uncoverable statement */
314  
315 96 upg = pw_env(aTHX_ envh, "HTTP_UPGRADE", &ul);
316 96 conn = pw_env(aTHX_ envh, "HTTP_CONNECTION", &cl);
317 96 ▶ if (!(pw_ci_has(upg, ul, "websocket") && pw_ci_has(conn, cl, "upgrade")))
Branch: 2 branches
BranchTrueFalseTotal
if (!(pw_ci_has(upg, ul, "websocket") && pw_ci_has(conn, cl, "upgrade"))) 90 6 96
if (!(pw_ci_has(upg, ul, "websocket") && pw_ci_has(conn, cl, "upgrade"))) 2 88 90
318 8 return pw_err(aTHX_ 400, "not a websocket upgrade\n", NULL, NULL);
319  
320 88 key = pw_env(aTHX_ envh, "HTTP_SEC_WEBSOCKET_KEY", &kl);
321 88 ▶ if (!pw_is_b64_key(key, kl))
Branch: 1 branch
BranchTrueFalseTotal
if (!pw_is_b64_key(key, kl)) 2 86 88
322 2 return pw_err(aTHX_ 400, "bad Sec-WebSocket-Key\n", NULL, NULL);
323  
324 86 ver = pw_env(aTHX_ envh, "HTTP_SEC_WEBSOCKET_VERSION", &vl);
325 86 ▶ if (!(vl == 2 && memEQ(ver, "13", 2))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(vl == 2 && memEQ(ver, "13", 2))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 84 2 86
if (!(vl == 2 && memEQ(ver, "13", 2))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 84 84
326 2 return pw_err(aTHX_ 426, "unsupported websocket version\n",
327 "Sec-WebSocket-Version", "13");
328 }
329  
330 /* Origin. The upgrade carries the user's cookies and the same-origin
331 * policy does not cover it, so a page anywhere could otherwise open an
332 * authenticated socket - see pk_origin_ok for what counts as this
333 * application's own origin.
334 *
335 * A browser always sends Origin on an upgrade. A client that is not a
336 * browser - Punk::Test, a mobile app, a daemon - sends none, has no
337 * ambient cookies to be used against it, and is not the attack this
338 * stops: an absent header passes. */
339 {
340 88 ▶ SV **ov = opts ? hv_fetchs(opts, "origin", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **ov = opts ? hv_fetchs(opts, "origin", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 88 0 88
341 /*
342 # uncoverable condition right count:2 class:ignore_covered_err
343 # uncoverable condition right count:4 class:ignore_covered_err */
344 88 ▶ int off = ov && *ov && !SvROK(*ov) && !SvTRUE(*ov);
Branch: 4 branches
BranchTrueFalseTotal
int off = ov && *ov && !SvROK(*ov) && !SvTRUE(*ov); 22 66 88
int off = ov && *ov && !SvROK(*ov) && !SvTRUE(*ov); 22 0 22
int off = ov && *ov && !SvROK(*ov) && !SvTRUE(*ov); 11 11 22
int off = ov && *ov && !SvROK(*ov) && !SvTRUE(*ov); 11 0 11
345 88 ▶ if (!off) {
Branch: 1 branch
BranchTrueFalseTotal
if (!off) { 77 11 88
346 STRLEN ol;
347 77 const char *o = pw_env(aTHX_ envh, "HTTP_ORIGIN", &ol);
348 77 ▶ if (ol) {
Branch: 1 branch
BranchTrueFalseTotal
if (ol) { 27 50 77
349 int ok;
350 /*
351 # uncoverable condition right count:2 class:ignore_covered_err
352 # uncoverable condition right count:3 class:ignore_covered_err */
353 29 ▶ if (ov && *ov && SvROK(*ov) && SvTYPE(SvRV(*ov)) == SVt_PVCV) {
Branch: 4 branches
BranchTrueFalseTotal
if (ov && *ov && SvROK(*ov) && SvTYPE(SvRV(*ov)) == SVt_PVCV) { 11 16 27
if (ov && *ov && SvROK(*ov) && SvTYPE(SvRV(*ov)) == SVt_PVCV) { 11 0 11
if (ov && *ov && SvROK(*ov) && SvTYPE(SvRV(*ov)) == SVt_PVCV) { 11 0 11
if (ov && *ov && SvROK(*ov) && SvTYPE(SvRV(*ov)) == SVt_PVCV) { 2 9 11
354 2 SV *osv = sv_2mortal(newSVpvn(o, ol));
355 2 int died = 0;
356 2 SV *r = pcx_call2(aTHX_ *ov, c, osv, &died);
357 /*
358 # uncoverable condition right count:1 class:ignore_covered_err
359 # uncoverable condition right count:2 class:ignore_covered_err */
360 2 ▶ ok = (!died && r && SvTRUE(r));
Branch: 3 branches
BranchTrueFalseTotal
ok = (!died && r && SvTRUE(r)); 2 0 2
ok = (!died && r && SvTRUE(r)); 2 0 2
ok = (!died && r && SvTRUE(r)); 1 1 2
361 /*
362 # uncoverable branch false
363 # uncoverable condition right count:1 class:ignore_covered_err */
364 2 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 2 0 2
365 /*
366 # uncoverable branch true
367 # uncoverable condition left count:1 class:ignore_covered_err
368 # uncoverable condition left count:2 class:ignore_covered_err
369 # uncoverable condition right count:2 class:ignore_covered_err */
370 2 ▶ if (died) croak_sv(sv_2mortal(newSVsv(ERRSV)));
Branch: 2 branches
BranchTrueFalseTotal
if (died) croak_sv(sv_2mortal(newSVsv(ERRSV))); 0 2 2
if (died) croak_sv(sv_2mortal(newSVsv(ERRSV))); 0 0 0
371 }
372 else {
373 9 ▶ AV *extra = (ov && *ov && SvROK(*ov)
Branch: 2 branches
BranchTrueFalseTotal
AV *extra = (ov && *ov && SvROK(*ov) 9 0 9
AV *extra = (ov && *ov && SvROK(*ov) 9 0 9
374 9 ▶ && SvTYPE(SvRV(*ov)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*ov)) == SVt_PVAV) 9 0 9
375 34 ▶ ? (AV *)SvRV(*ov) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*ov) : NULL; 9 16 25
376 25 ok = pk_origin_ok(aTHX_ c, o, ol, extra);
377 }
378 27 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 14 13 27
379 14 return pw_err(aTHX_ 403, "origin not allowed\n",
380 NULL, NULL);
381 }
382 }
383 }
384  
385 /* subprotocol: only when the route declares a list. First offered value
386 * that the route accepts wins; a declared list with no match is a 400. */
387 /*
388 # uncoverable condition right count:1 class:ignore_covered_err
389 # uncoverable condition right count:3 class:ignore_covered_err
390 # uncoverable condition right count:4 class:ignore_covered_err
391 # uncoverable condition right count:5 class:ignore_covered_err */
392 74 ▶ if (opts && (x = hv_fetchs(opts, "protocols", 0))
Branch: 2 branches
BranchTrueFalseTotal
if (opts && (x = hv_fetchs(opts, "protocols", 0)) 74 0 74
if (opts && (x = hv_fetchs(opts, "protocols", 0)) 3 71 74
393 3 ▶ && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
&& *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 3 0 3
&& *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 3 0 3
&& *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { 3 0 3
394 3 AV *want = (AV *)SvRV(*x);
395 3 STRLEN pl; const char *ps = pw_env(aTHX_ envh, "HTTP_SEC_WEBSOCKET_PROTOCOL", &pl);
396 3 int saw_offer = 0;
397 3 STRLEN i = 0;
398 6 ▶ while (i < pl && !protocol) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < pl && !protocol) { 4 2 6
while (i < pl && !protocol) { 3 1 4
399 STRLEN s, e2, j; SSize_t wi, wn;
400 /*
401 # uncoverable branch true count:2
402 # uncoverable branch true class:ignore_covered_err
403 # uncoverable condition left count:1 class:ignore_covered_err
404 # uncoverable condition right count:1 class:ignore_covered_err
405 # uncoverable condition left count:2 class:ignore_covered_err
406 # uncoverable condition left count:3 class:ignore_covered_err
407 # uncoverable condition left count:4 class:ignore_covered_err */
408 3 ▶ while (i < pl && (ps[i] == ' ' || ps[i] == '\t' || ps[i] == ',')) i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i < pl && (ps[i] == ' ' || ps[i] == '\t' || ps[i] == ',')) i++; 3 0 3
while (i < pl && (ps[i] == ' ' || ps[i] == '\t' || ps[i] == ',')) i++; 0 3 3
while (i < pl && (ps[i] == ' ' || ps[i] == '\t' || ps[i] == ',')) i++; 0 3 3
while (i < pl && (ps[i] == ' ' || ps[i] == '\t' || ps[i] == ',')) i++; 0 3 3
409 3 s = i;
410 24 ▶ while (i < pl && ps[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < pl && ps[i] != ',') i++; 22 2 24
while (i < pl && ps[i] != ',') i++; 21 1 22
411 3 e2 = i;
412 /*
413 # uncoverable branch true count:2
414 # uncoverable branch true class:ignore_covered_err
415 # uncoverable condition left count:1 class:ignore_covered_err
416 # uncoverable condition right count:1 class:ignore_covered_err
417 # uncoverable condition left count:2 class:ignore_covered_err
418 # uncoverable condition left count:3 class:ignore_covered_err */
419 3 ▶ while (e2 > s && (ps[e2-1] == ' ' || ps[e2-1] == '\t')) e2--;
Branch: 3 branches
BranchTrueFalseTotal
while (e2 > s && (ps[e2-1] == ' ' || ps[e2-1] == '\t')) e2--; 3 0 3
while (e2 > s && (ps[e2-1] == ' ' || ps[e2-1] == '\t')) e2--; 0 3 3
while (e2 > s && (ps[e2-1] == ' ' || ps[e2-1] == '\t')) e2--; 0 3 3
420 /*
421 # uncoverable branch true
422 # uncoverable condition left count:1 class:ignore_covered_err */
423 3 ▶ if (e2 == s) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (e2 == s) continue; 0 3 3
424 3 saw_offer = 1;
425 3 wn = av_len(want) + 1;
426 7 ▶ for (wi = 0; wi < wn; wi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (wi = 0; wi < wn; wi++) { 6 1 7
427 6 SV **wp = av_fetch(want, wi, 0);
428 STRLEN wl; const char *ws2;
429 /*
430 # uncoverable branch true
431 # uncoverable condition left count:1 class:ignore_covered_err
432 # uncoverable condition left count:2 class:ignore_covered_err */
433 6 ▶ if (!wp || !*wp) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!wp || !*wp) continue; 6 0 6
if (!wp || !*wp) continue; 0 6 6
434 6 ws2 = SvPV_const(*wp, wl);
435 /*
436 # uncoverable branch false
437 # uncoverable condition right count:1 class:ignore_covered_err */
438 6 ▶ if (wl == e2 - s) {
Branch: 1 branch
BranchTrueFalseTotal
if (wl == e2 - s) { 6 0 6
439 32 ▶ for (j = 0; j < wl && ws2[j] == ps[s + j]; j++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < wl && ws2[j] == ps[s + j]; j++) ; 30 2 32
for (j = 0; j < wl && ws2[j] == ps[s + j]; j++) ; 26 4 30
440 6 ▶ if (j == wl) { protocol = sv_2mortal(newSVpvn(ps + s, wl)); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (j == wl) { protocol = sv_2mortal(newSVpvn(ps + s, wl)); break; } 2 4 6
441 }
442 }
443 }
444 3 ▶ if (saw_offer && !protocol) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (saw_offer && !protocol) /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
if (saw_offer && !protocol) /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 2 3
445 1 return pw_err(aTHX_ 400, "no acceptable subprotocol\n", NULL, NULL);
446 }
447  
448 /* The HTTP/1.1 response, and ONLY for HTTP/1.1. Extended CONNECT has no
449 * 101 to send and, more to the point, no Sec-WebSocket-Key to hash: the
450 * key parse above is skipped on that path, so running this would take
451 * the digest of an uninitialised pointer. */
452 73 ▶ if (is_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_connect) { 3 70 73
453 3 hs = sv_2mortal(newSVpvs(""));
454 } else {
455 /* Sec-WebSocket-Accept = base64(sha1(key . GUID)) */
456 {
457 unsigned char kg[24 + sizeof(PW_GUID) - 1];
458 70 memcpy(kg, key, 24);
459 70 memcpy(kg + 24, PW_GUID, sizeof(PW_GUID) - 1);
460 70 pw_sha1(kg, sizeof(kg), digest);
461 70 pw_base64(digest, 20, accept);
462 }
463  
464 70 hs = sv_2mortal(newSVpvs(
465 "HTTP/1.1 101 Switching Protocols\r\n"
466 "Upgrade: websocket\r\n"
467 "Connection: Upgrade\r\n"
468 "Sec-WebSocket-Accept: "));
469 70 sv_catpv(hs, accept);
470 70 sv_catpvs(hs, "\r\n");
471 70 ▶ if (protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol) { 2 68 70
472 2 sv_catpvs(hs, "Sec-WebSocket-Protocol: ");
473 2 sv_catsv(hs, protocol);
474 2 sv_catpvs(hs, "\r\n");
475 }
476 70 sv_catpvs(hs, "\r\n");
477 }
478  
479 /* the attach options: the route opts minus the two the handshake has
480 * already spent (`protocols`, `origin`), plus the negotiated `protocol`
481 * (the connection object reads these). */
482 73 attach = newHV();
483 /*
484 # uncoverable branch false
485 # uncoverable condition right count:1 class:ignore_covered_err */
486 73 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 73 0 73
487 HE *he;
488 73 hv_iterinit(opts);
489 106 ▶ while ((he = hv_iternext(opts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts))) { 33 73 106
490 33 ▶ STRLEN hkl; const char *hk = HePV(he, hkl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN hkl; const char *hk = HePV(he, hkl); 0 33 33
491 50 ▶ if (hkl == 9 && memEQ(hk, "protocols", 9)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (hkl == 9 && memEQ(hk, "protocols", 9)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 31 33
if (hkl == 9 && memEQ(hk, "protocols", 9)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
492 31 ▶ if (hkl == 6 && memEQ(hk, "origin", 6)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (hkl == 6 && memEQ(hk, "origin", 6)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */ 17 14 31
if (hkl == 6 && memEQ(hk, "origin", 6)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */ 17 0 17
493 14 (void)hv_store(attach, hk, (I32)hkl, newSVsv(hv_iterval(opts, he)), 0);
494 }
495 }
496 73 ▶ if (protocol) (void)hv_stores(attach, "protocol", newSVsv(protocol));
Branch: 1 branch
BranchTrueFalseTotal
if (protocol) (void)hv_stores(attach, "protocol", newSVsv(protocol)); 2 71 73
497 73 attach_rv = sv_2mortal(newRV_noinc((SV *)attach));
498  
499 /* Extended CONNECT, tried first because when it applies nothing below
500 * can work: there is no descriptor to detach and no psgix.io to own.
501 *
502 * The acceptance IS the response here - a 200 with the negotiated
503 * subprotocol, sent by opening the stream handle - so no handshake
504 * bytes are written and `hs` goes unused on this path. Everything after
505 * that is the same codec the detached path drives. */
506 73 ▶ if (is_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_connect) { 3 70 73
507 3 const hm_abi *A = NULL;
508 3 void *loop = NULL, *h = NULL;
509 3 AV *hdrs = newAV();
510 /*
511 # uncoverable branch true
512 # uncoverable condition left count:1 class:ignore_covered_err */
513 3 ▶ if (protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol) { 0 3 3
514 0 av_push(hdrs, newSVpvs("sec-websocket-protocol")); /* # uncoverable statement */
515 0 av_push(hdrs, newSVsv(protocol)); /* # uncoverable statement */
516 }
517 3 h = punk_hm_stream_open(aTHX_ envh, &A, &loop, 200, hdrs);
518 3 SvREFCNT_dec((SV *)hdrs);
519 3 ▶ if (!h)
Branch: 1 branch
BranchTrueFalseTotal
if (!h) 3 0 3
520 3 return pw_err(aTHX_ 501,
521 "this server cannot carry a websocket over HTTP/2 or HTTP/3 "
522 "(needs Hyperman's ABI v8)\n", NULL, NULL);
523 {
524 SV *ws, *argv[2];
525 0 argv[0] = sv_2mortal(newSViv(PTR2IV(h)));
526 0 argv[1] = attach_rv;
527 0 ws = pw_class_call(aTHX_ "_attach_stream", argv, 2);
528 0 ▶ pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 0 0
529 /*
530 # uncoverable branch false
531 # uncoverable condition right count:1 class:ignore_covered_err */
532 0 ▶ if (ws) SvREFCNT_dec(ws);
Branch: 1 branch
BranchTrueFalseTotal
if (ws) SvREFCNT_dec(ws); 0 0 0
533 }
534 /* The response already went out with the stream handle; this is the
535 * sentinel the server discards, as the 101 is on the detach path. */
536 0 return pw_empty(aTHX_ 200);
537 }
538  
539 /* HTTP/1.1 over TLS: the socket cannot be handed over, because the TLS
540 * session's state belongs to the server and conn_detach refuses it (-3).
541 * The upgrade rides a 101 stream handle instead - the same seam the
542 * Extended CONNECT path uses, opened with the 101 and its handshake
543 * headers rather than a 200, so the RFC 6455 codec above it is again
544 * the same one. Plaintext HTTP/1.1 detaches below: the fd handoff is
545 * cheaper and there is no session to strand. Needs Hyperman 0.48+, whose
546 * stream handle turns a 101 into a tunnel; an older one returns no handle
547 * here and the request falls through to the detach that has always
548 * refused it. */
549 {
550 STRLEN hl;
551 70 const char *https = pw_env(aTHX_ envh, "HTTPS", &hl);
552 70 ▶ if (https && hl) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (https && hl) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 70 0 70
if (https && hl) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 68 70
553 2 const hm_abi *A = NULL;
554 2 void *loop = NULL, *h = NULL;
555 2 AV *hdrs = newAV();
556 2 av_push(hdrs, newSVpvs("Upgrade"));
557 2 av_push(hdrs, newSVpvs("websocket"));
558 2 av_push(hdrs, newSVpvs("Connection"));
559 2 av_push(hdrs, newSVpvs("Upgrade"));
560 2 av_push(hdrs, newSVpvs("Sec-WebSocket-Accept"));
561 2 av_push(hdrs, newSVpv(accept, 0));
562 2 ▶ if (protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (protocol) { 1 1 2
563 1 av_push(hdrs, newSVpvs("Sec-WebSocket-Protocol"));
564 1 av_push(hdrs, newSVsv(protocol));
565 }
566 2 h = punk_hm_stream_open(aTHX_ envh, &A, &loop, 101, hdrs);
567 2 SvREFCNT_dec((SV *)hdrs);
568 /*
569 # uncoverable branch false
570 # uncoverable condition right count:1 class:ignore_covered_err */
571 2 ▶ if (h) {
Branch: 1 branch
BranchTrueFalseTotal
if (h) { 2 0 2
572 SV *ws, *argv[2];
573 2 argv[0] = sv_2mortal(newSViv(PTR2IV(h)));
574 2 argv[1] = attach_rv;
575 2 ws = pw_class_call(aTHX_ "_attach_stream", argv, 2);
576 2 ▶ pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 0 2
577 /*
578 # uncoverable branch false
579 # uncoverable condition right count:1 class:ignore_covered_err */
580 2 ▶ if (ws) SvREFCNT_dec(ws);
Branch: 1 branch
BranchTrueFalseTotal
if (ws) SvREFCNT_dec(ws); 2 0 2
581 2 return pw_empty(aTHX_ 101);
582 }
583 }
584 }
585  
586 /* Hyperman: detach the socket and drive it on the worker loop. */
587 68 x = hv_fetchs(envh, "psgix.hyperman.conn", 0);
588 /*
589 # uncoverable condition right count:2 class:ignore_covered_err
590 # uncoverable condition right count:3 class:ignore_covered_err */
591 68 ▶ if (x && *x && SvTRUE(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvTRUE(*x)) { 38 30 68
if (x && *x && SvTRUE(*x)) { 38 0 38
if (x && *x && SvTRUE(*x)) { 38 0 38
592 int ok;
593 { /* _hm_available() - a no-arg function, not a method */
594 38 dSP; int count;
595 38 ENTER; SAVETMPS;
596 38 ▶ PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); PUTBACK; 0 38 38
597 38 count = call_pv(PK_WEBSOCKET "::_hm_available", G_SCALAR);
598 38 SPAGAIN;
599 38 ▶ ok = count > 0 ? SvTRUE(TOPs) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ok = count > 0 ? SvTRUE(TOPs) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 38 0 38
600 /*
601 # uncoverable branch false
602 # uncoverable condition right count:1 class:ignore_covered_err */
603 38 ▶ if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) (void)POPs; 38 0 38
604 38 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 38 0 38
605 }
606 /*
607 # uncoverable branch false
608 # uncoverable condition right count:1 class:ignore_covered_err */
609 38 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 38 0 38
610 38 IV fd; int died = 0;
611 SV *ws, *argv[3];
612 {
613 38 dSP; int count;
614 38 ENTER; SAVETMPS;
615 38 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(env); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(env); PUTBACK; 0 38 38
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(env); PUTBACK; 0 38 38
616 38 count = call_pv(PK_WEBSOCKET "::_hm_detach", G_SCALAR | G_EVAL);
617 38 SPAGAIN;
618 38 ▶ died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 38 0 38
619 /*
620 # uncoverable condition right count:1 class:ignore_covered_err
621 # uncoverable condition left count:2 class:ignore_covered_err */
622 38 ▶ fd = (count > 0) ? (died ? (POPs, 0) : POPi) : 0;
Branch: 2 branches
BranchTrueFalseTotal
fd = (count > 0) ? (died ? (POPs, 0) : POPi) : 0; 38 0 38
fd = (count > 0) ? (died ? (POPs, 0) : POPi) : 0; 0 38 38
623 38 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 38 0 38
624 }
625 /*
626 # uncoverable branch true
627 # uncoverable condition left count:1 class:ignore_covered_err */
628 38 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 0 38 38
629 0 ▶ const char *em = SvPV_nolen(ERRSV); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
const char *em = SvPV_nolen(ERRSV); /* # uncoverable statement */ 0 0 0
630 /*
631 # uncoverable statement
632 # uncoverable branch true
633 # uncoverable branch false
634 # uncoverable condition left count:1
635 # uncoverable condition right count:1
636 # uncoverable condition left count:2
637 # uncoverable condition right count:2 */
638 0 ▶ if (strstr(em, "draining") || strstr(em, "already detached"))
Branch: 2 branches
BranchTrueFalseTotal
if (strstr(em, "draining") || strstr(em, "already detached")) 0 0 0
if (strstr(em, "draining") || strstr(em, "already detached")) 0 0 0
639 0 return pw_err(aTHX_ 400, "cannot upgrade this connection\n", /* # uncoverable statement */
640 NULL, NULL);
641 0 ▶ croak_sv(ERRSV); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak_sv(ERRSV); /* # uncoverable statement */ 0 0 0
642 }
643 38 argv[0] = sv_2mortal(newSViv(fd));
644 38 argv[1] = hs;
645 38 argv[2] = attach_rv;
646 38 ws = pw_class_call(aTHX_ "_attach", argv, 3);
647 38 ▶ pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */ 38 0 38
648 /*
649 # uncoverable branch false class:ignore_covered_err
650 # uncoverable condition right count:1 class:ignore_covered_err */
651 37 ▶ if (ws) SvREFCNT_dec(ws);
Branch: 1 branch
BranchTrueFalseTotal
if (ws) SvREFCNT_dec(ws); 37 0 37
652 37 return pw_empty(aTHX_ 101); /* discarded by Hyperman */
653 }
654 }
655  
656 /* Blocking fallback: own the socket inside the handler (pins a worker). */
657 30 ▶ x = opts ? hv_fetchs(opts, K_BLOCKING, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
x = opts ? hv_fetchs(opts, K_BLOCKING, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 30 0 30
658 /*
659 # uncoverable condition right count:2 class:ignore_covered_err
660 # uncoverable condition right count:3 class:ignore_covered_err */
661 30 ▶ if (x && *x && SvTRUE(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvTRUE(*x)) { 10 20 30
if (x && *x && SvTRUE(*x)) { 10 0 10
if (x && *x && SvTRUE(*x)) { 10 0 10
662 10 SV **iop = hv_fetchs(envh, "psgix.io", 0);
663 /*
664 # uncoverable branch false
665 # uncoverable condition right count:1 class:ignore_covered_err
666 # uncoverable condition right count:2 class:ignore_covered_err
667 # uncoverable condition right count:3 class:ignore_covered_err */
668 10 ▶ if (iop && *iop && SvOK(*iop)) {
Branch: 3 branches
BranchTrueFalseTotal
if (iop && *iop && SvOK(*iop)) { 10 0 10
if (iop && *iop && SvOK(*iop)) { 10 0 10
if (iop && *iop && SvOK(*iop)) { 10 0 10
669 10 IO *io = sv_2io(*iop);
670 /*
671 # uncoverable condition right count:1 class:ignore_covered_err
672 # uncoverable condition right count:2 class:ignore_covered_err */
673 10 ▶ IV fd = (io && IoIFP(io)) ? (IV)PerlIO_fileno(IoIFP(io)) : -1;
Branch: 2 branches
BranchTrueFalseTotal
IV fd = (io && IoIFP(io)) ? (IV)PerlIO_fileno(IoIFP(io)) : -1; 10 0 10
IV fd = (io && IoIFP(io)) ? (IV)PerlIO_fileno(IoIFP(io)) : -1; 10 0 10
674 SV *ws, *argv[3], *rb;
675 10 argv[0] = sv_2mortal(newSViv(fd));
676 10 argv[1] = hs; /* _attach_blocking flushes it */
677 10 argv[2] = attach_rv;
678 10 ws = pw_class_call(aTHX_ "_attach_blocking", argv, 3);
679 10 ▶ pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */ 10 0 10
680 10 ▶ rb = pcx_call_meth(aTHX_ ws ? ws : &PL_sv_undef, "_run_blocking", /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
rb = pcx_call_meth(aTHX_ ws ? ws : &PL_sv_undef, "_run_blocking", /* # uncoverable condition left count:1 class:ignore_covered_err */ 10 0 10
681 NULL, 0, 0);
682 /*
683 # uncoverable branch true
684 # uncoverable condition left count:1 class:ignore_covered_err */
685 10 ▶ if (rb) SvREFCNT_dec(rb);
Branch: 1 branch
BranchTrueFalseTotal
if (rb) SvREFCNT_dec(rb); 0 10 10
686 /*
687 # uncoverable branch false
688 # uncoverable condition right count:1 class:ignore_covered_err */
689 10 ▶ if (ws) SvREFCNT_dec(ws);
Branch: 1 branch
BranchTrueFalseTotal
if (ws) SvREFCNT_dec(ws); 10 0 10
690 10 return pw_empty(aTHX_ 200); /* the socket is gone */
691 }
692 }
693  
694 20 return pw_err(aTHX_ 501,
695 "this server cannot upgrade websockets (needs Hyperman 0.11+, "
696 "or blocking => 1 with psgix.io)\n", NULL, NULL);
697 }
698  
699 #endif /* PUNK_WSHANDSHAKE_H */
« include/punk/punk_cookie.h ↑ Summary include/punk/punk_mrows.h »