| 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 |
38 |
| static void pw_sha1(const unsigned char *msg, size_t len, unsigned char out[20]) { |
| 29 |
38 |
| U32 h0 = 0x67452301, h1 = 0xEFCDAB89, h2 = 0x98BADCFE, |
| 30 |
38 |
| h3 = 0x10325476, h4 = 0xC3D2E1F0; |
| 31 |
38 |
| size_t total = ((len + 8) / 64 + 1) * 64, off; |
| 32 |
|
| unsigned char *m; |
| 33 |
38 |
| U32 lo = (U32)(len << 3), hi = (U32)(len >> 29); |
| 34 |
38 |
| Newxz(m, total, unsigned char); |
| 35 |
38 |
| memcpy(m, msg, len); |
| 36 |
38 |
| m[len] = 0x80; |
| 37 |
38 |
| m[total - 1] = (unsigned char)(lo & 0xff); |
| 38 |
38 |
| m[total - 2] = (unsigned char)((lo >> 8) & 0xff); |
| 39 |
38 |
| m[total - 3] = (unsigned char)((lo >> 16) & 0xff); |
| 40 |
38 |
| m[total - 4] = (unsigned char)((lo >> 24) & 0xff); |
| 41 |
38 |
| m[total - 5] = (unsigned char)(hi & 0xff); |
| 42 |
38 |
| m[total - 6] = (unsigned char)((hi >> 8) & 0xff); |
| 43 |
38 |
| m[total - 7] = (unsigned char)((hi >> 16) & 0xff); |
| 44 |
38 |
| m[total - 8] = (unsigned char)((hi >> 24) & 0xff); |
| 45 |
114 |
▶ | for (off = 0; off < total; off += 64) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (off = 0; off < total; off += 64) { |
76 |
38 |
114 |
|
| 46 |
|
| U32 w[80], a, b, c, d, e, f, k, tmp; |
| 47 |
|
| int i; |
| 48 |
1292 |
▶ | for (i = 0; i < 16; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; i++) |
1216 |
76 |
1292 |
|
| 49 |
1216 |
| w[i] = ((U32)m[off + 4*i] << 24) | ((U32)m[off + 4*i + 1] << 16) |
| 50 |
1216 |
| | ((U32)m[off + 4*i + 2] << 8) | (U32)m[off + 4*i + 3]; |
| 51 |
4940 |
▶ | for (i = 16; i < 80; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 16; i < 80; i++) |
4864 |
76 |
4940 |
|
| 52 |
4864 |
| w[i] = PW_ROL(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1); |
| 53 |
76 |
| a = h0; b = h1; c = h2; d = h3; e = h4; |
| 54 |
6156 |
▶ | for (i = 0; i < 80; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 80; i++) { |
6080 |
76 |
6156 |
|
| 55 |
6080 |
▶ | if (i < 20) { f = (b & c) | ((~b) & d); k = 0x5A827999; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < 20) { f = (b & c) | ((~b) & d); k = 0x5A827999; } |
1520 |
4560 |
6080 |
|
| 56 |
4560 |
▶ | else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; } |
1520 |
3040 |
4560 |
|
| 57 |
3040 |
▶ | else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } |
1520 |
1520 |
3040 |
|
| 58 |
1520 |
| else { f = b ^ c ^ d; k = 0xCA62C1D6; } |
| 59 |
6080 |
| tmp = PW_ROL(a, 5) + f + e + k + w[i]; |
| 60 |
6080 |
| e = d; d = c; c = PW_ROL(b, 30); b = a; a = tmp; |
| 61 |
|
| } |
| 62 |
76 |
| h0 += a; h1 += b; h2 += c; h3 += d; h4 += e; |
| 63 |
|
| } |
| 64 |
38 |
| Safefree(m); |
| 65 |
|
| { |
| 66 |
|
| U32 hh[5]; int i; |
| 67 |
38 |
| hh[0] = h0; hh[1] = h1; hh[2] = h2; hh[3] = h3; hh[4] = h4; |
| 68 |
228 |
▶ | for (i = 0; i < 5; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 5; i++) { |
190 |
38 |
228 |
|
| 69 |
190 |
| out[4*i] = (unsigned char)((hh[i] >> 24) & 0xff); |
| 70 |
190 |
| out[4*i + 1] = (unsigned char)((hh[i] >> 16) & 0xff); |
| 71 |
190 |
| out[4*i + 2] = (unsigned char)((hh[i] >> 8) & 0xff); |
| 72 |
190 |
| out[4*i + 3] = (unsigned char)(hh[i] & 0xff); |
| 73 |
|
| } |
| 74 |
|
| } |
| 75 |
38 |
| } |
| 76 |
|
| |
| 77 |
|
| /* ---- base64 --------------------------------------------------------------- */ |
| 78 |
|
| |
| 79 |
38 |
| static void pw_base64(const unsigned char *in, size_t len, char *out) { |
| 80 |
|
| static const char B64[] = |
| 81 |
|
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 82 |
38 |
| size_t i = 0, o = 0; |
| 83 |
266 |
▶ | for (; i + 3 <= len; i += 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i + 3 <= len; i += 3) { |
228 |
38 |
266 |
|
| 84 |
228 |
| U32 n = ((U32)in[i] << 16) | ((U32)in[i+1] << 8) | in[i+2]; |
| 85 |
228 |
| out[o++] = B64[(n >> 18) & 63]; out[o++] = B64[(n >> 12) & 63]; |
| 86 |
228 |
| out[o++] = B64[(n >> 6) & 63]; out[o++] = B64[n & 63]; |
| 87 |
|
| } |
| 88 |
38 |
▶ | if (i < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < len) { |
38 |
0 |
38 |
|
| 89 |
38 |
| int rem = (int)(len - i); |
| 90 |
38 |
| U32 n = (U32)in[i] << 16; |
| 91 |
38 |
▶ | if (rem == 2) n |= (U32)in[i+1] << 8; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rem == 2) n |= (U32)in[i+1] << 8; |
38 |
0 |
38 |
|
| 92 |
38 |
| out[o++] = B64[(n >> 18) & 63]; |
| 93 |
38 |
| out[o++] = B64[(n >> 12) & 63]; |
| 94 |
38 |
▶ | out[o++] = rem == 2 ? B64[(n >> 6) & 63] : '='; |
Branch: 1 branch
| Branch | True | False | Total |
| out[o++] = rem == 2 ? B64[(n >> 6) & 63] : '='; |
38 |
0 |
38 |
|
| 95 |
38 |
| out[o++] = '='; |
| 96 |
|
| } |
| 97 |
38 |
| out[o] = '\0'; |
| 98 |
38 |
| } |
| 99 |
|
| |
| 100 |
|
| /* ---- request helpers ------------------------------------------------------ */ |
| 101 |
|
| |
| 102 |
380 |
| static const char *pw_env(pTHX_ HV *env, const char *k, STRLEN *len) { |
| 103 |
380 |
| SV **e = hv_fetch(env, k, (I32)strlen(k), 0); |
| 104 |
704 |
▶ | if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); |
324 |
56 |
380 |
| if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); |
324 |
0 |
324 |
| if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); |
324 |
0 |
324 |
|
| 105 |
56 |
| *len = 0; return ""; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
|
| /* Is `needle` (already lowercase) one of the comma-separated tokens of a |
| 109 |
|
| * header value, compared without case? A token, not a substring: |
| 110 |
|
| * `Upgrade: notwebsocket` is not an upgrade to websocket. A protocol may |
| 111 |
|
| * carry a version after a slash, which is still that protocol. */ |
| 112 |
117 |
| static int pw_ci_has(const char *hay, STRLEN hlen, const char *needle) { |
| 113 |
117 |
| STRLEN nl = strlen(needle), i = 0; |
| 114 |
117 |
▶ | if (nl == 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nl == 0) return 1; |
0 |
117 |
117 |
|
| 115 |
124 |
▶ | while (i < hlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < hlen) { |
117 |
7 |
124 |
|
| 116 |
|
| STRLEN s, e, j; |
| 117 |
121 |
▶ | while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ',')) |
Branch: 4 branches
| Branch | True | False | Total |
| while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ',')) |
121 |
0 |
121 |
| while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ',')) |
2 |
119 |
121 |
| while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ',')) |
0 |
119 |
119 |
| while (i < hlen && (hay[i] == ' ' || hay[i] == '\t' || hay[i] == ',')) |
2 |
117 |
119 |
|
| 118 |
4 |
| i++; |
| 119 |
117 |
| s = i; |
| 120 |
1067 |
▶ | while (i < hlen && hay[i] != ',') i++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < hlen && hay[i] != ',') i++; |
953 |
114 |
1067 |
| while (i < hlen && hay[i] != ',') i++; |
950 |
3 |
953 |
|
| 121 |
117 |
| e = i; |
| 122 |
118 |
▶ | while (e > s && (hay[e - 1] == ' ' || hay[e - 1] == '\t')) e--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (e > s && (hay[e - 1] == ' ' || hay[e - 1] == '\t')) e--; |
118 |
0 |
118 |
| while (e > s && (hay[e - 1] == ' ' || hay[e - 1] == '\t')) e--; |
1 |
117 |
118 |
| while (e > s && (hay[e - 1] == ' ' || hay[e - 1] == '\t')) e--; |
0 |
117 |
117 |
|
| 123 |
117 |
▶ | if (e - s < nl) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e - s < nl) continue; |
1 |
116 |
117 |
|
| 124 |
1010 |
▶ | for (j = 0; j < nl; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < nl; j++) { |
899 |
111 |
1010 |
|
| 125 |
899 |
| char ch = hay[s + j]; |
| 126 |
899 |
▶ | if (ch >= 'A' && ch <= 'Z') ch = (char)(ch + 32); |
Branch: 2 branches
| Branch | True | False | Total |
| if (ch >= 'A' && ch <= 'Z') ch = (char)(ch + 32); |
898 |
1 |
899 |
| if (ch >= 'A' && ch <= 'Z') ch = (char)(ch + 32); |
62 |
836 |
898 |
|
| 127 |
899 |
▶ | if (ch != needle[j]) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch != needle[j]) break; |
5 |
894 |
899 |
|
| 128 |
|
| } |
| 129 |
116 |
▶ | if (j == nl && (e - s == nl || hay[s + nl] == '/')) return 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (j == nl && (e - s == nl || hay[s + nl] == '/')) return 1; |
111 |
5 |
116 |
| if (j == nl && (e - s == nl || hay[s + nl] == '/')) return 1; |
2 |
109 |
111 |
| if (j == nl && (e - s == nl || hay[s + nl] == '/')) return 1; |
1 |
1 |
2 |
|
| 130 |
|
| } |
| 131 |
7 |
| return 0; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
54 |
| static int pw_is_b64_key(const char *k, STRLEN kl) { |
| 135 |
|
| STRLEN i; |
| 136 |
54 |
▶ | if (kl != 24 || k[22] != '=' || k[23] != '=') return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (kl != 24 || k[22] != '=' || k[23] != '=') return 0; |
53 |
1 |
54 |
| if (kl != 24 || k[22] != '=' || k[23] != '=') return 0; |
53 |
0 |
53 |
| if (kl != 24 || k[22] != '=' || k[23] != '=') return 0; |
0 |
53 |
53 |
|
| 137 |
1219 |
▶ | for (i = 0; i < 22; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 22; i++) { |
1166 |
53 |
1219 |
|
| 138 |
1166 |
| char ch = k[i]; |
| 139 |
1175 |
▶ | if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
Branch: 5 branches
| Branch | True | False | Total |
| if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
1018 |
148 |
1166 |
| if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
488 |
530 |
1018 |
| if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
488 |
148 |
636 |
| if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
0 |
488 |
488 |
| if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') |
0 |
9 |
9 |
|
| 140 |
148 |
▶ | || (ch >= '0' && ch <= '9') || ch == '+' || ch == '/')) |
Branch: 3 branches
| Branch | True | False | Total |
| || (ch >= '0' && ch <= '9') || ch == '+' || ch == '/')) |
132 |
16 |
148 |
| || (ch >= '0' && ch <= '9') || ch == '+' || ch == '/')) |
0 |
132 |
132 |
| || (ch >= '0' && ch <= '9') || ch == '+' || ch == '/')) |
9 |
7 |
16 |
|
| 141 |
0 |
| return 0; |
| 142 |
|
| } |
| 143 |
53 |
| return 1; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| /* A short text error response (the Perl $ERR), with an optional header. */ |
| 147 |
46 |
| static SV *pw_err(pTHX_ IV status, const char *msg, const char *hk, const char *hv) { |
| 148 |
46 |
| AV *resp = newAV(), *headers = newAV(), *body = newAV(); |
| 149 |
46 |
| STRLEN len = strlen(msg); |
| 150 |
46 |
| av_push(headers, newSVpvs("Content-Type")); |
| 151 |
46 |
| av_push(headers, newSVpvs("text/plain; charset=utf-8")); |
| 152 |
46 |
| av_push(headers, newSVpvs("Content-Length")); |
| 153 |
46 |
| av_push(headers, newSViv((IV)len)); |
| 154 |
46 |
▶ | if (hk) { av_push(headers, newSVpv(hk, 0)); av_push(headers, newSVpv(hv, 0)); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (hk) { av_push(headers, newSVpv(hk, 0)); av_push(headers, newSVpv(hv, 0)); } |
1 |
45 |
46 |
|
| 155 |
46 |
| av_push(body, newSVpvn(msg, len)); |
| 156 |
46 |
| av_push(resp, newSViv(status)); |
| 157 |
46 |
| av_push(resp, newRV_noinc((SV *)headers)); |
| 158 |
46 |
| av_push(resp, newRV_noinc((SV *)body)); |
| 159 |
46 |
| return newRV_noinc((SV *)resp); |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| /* an empty [ status, [], [] ] the server discards after the upgrade */ |
| 163 |
29 |
| static SV *pw_empty(pTHX_ IV status) { |
| 164 |
29 |
| AV *resp = newAV(); |
| 165 |
29 |
| av_push(resp, newSViv(status)); |
| 166 |
29 |
| av_push(resp, newRV_noinc((SV *)newAV())); |
| 167 |
29 |
| av_push(resp, newRV_noinc((SV *)newAV())); |
| 168 |
29 |
| return newRV_noinc((SV *)resp); |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
|
| /* Call a Punk::WebSocket class method (_attach / _attach_blocking); +1. */ |
| 172 |
20 |
| static SV *pw_class_call(pTHX_ const char *meth, SV **argv, int n) { |
| 173 |
20 |
| return pcx_call_meth(aTHX_ sv_2mortal(newSVpvs(PK_WEBSOCKET)), meth, argv, n, 1); |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
|
| /* Run the route handler ($code->($c, $ws)); on death close 1011 and rethrow. */ |
| 177 |
20 |
| static void pw_run_handler(pTHX_ SV *code, SV *c, SV *ws) { |
| 178 |
20 |
| int died = 0; |
| 179 |
20 |
| SV *r = pcx_call2(aTHX_ code, c, ws, &died); |
| 180 |
20 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
20 |
0 |
20 |
|
| 181 |
20 |
▶ | if (died) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) { |
0 |
20 |
20 |
|
| 182 |
0 |
▶ | SV *e = sv_2mortal(newSVsv(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = sv_2mortal(newSVsv(ERRSV)); |
0 |
0 |
0 |
|
| 183 |
|
| SV *ca[2], *cr; |
| 184 |
0 |
| ca[0] = sv_2mortal(newSViv(1011)); |
| 185 |
0 |
| ca[1] = sv_2mortal(newSVpvs("handler error")); |
| 186 |
0 |
| cr = pcx_call_meth(aTHX_ ws, "close", ca, 2, 0); |
| 187 |
0 |
▶ | if (cr) SvREFCNT_dec(cr); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cr) SvREFCNT_dec(cr); |
0 |
0 |
0 |
|
| 188 |
0 |
| croak_sv(e); |
| 189 |
|
| } |
| 190 |
20 |
| { SV *o = pcx_call_meth(aTHX_ ws, "_open", NULL, 0, 0); |
| 191 |
20 |
▶ | if (o) SvREFCNT_dec(o); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (o) SvREFCNT_dec(o); } |
0 |
20 |
20 |
|
| 192 |
20 |
| } |
| 193 |
|
| |
| 194 |
|
| /* ---- the upgrade ---------------------------------------------------------- */ |
| 195 |
|
| |
| 196 |
65 |
| static SV *punk_ws_dispatch(pTHX_ SV *c, SV *rec, SV *env) { |
| 197 |
65 |
▶ | HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL; |
65 |
0 |
65 |
| HV *envh = (SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) ? (HV *)SvRV(env) : NULL; |
65 |
0 |
65 |
|
| 198 |
65 |
▶ | HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL; |
65 |
0 |
65 |
| HV *rech = (SvROK(rec) && SvTYPE(SvRV(rec)) == SVt_PVHV) ? (HV *)SvRV(rec) : NULL; |
65 |
0 |
65 |
|
| 199 |
65 |
| HV *opts = NULL; |
| 200 |
65 |
| SV **x, *code = NULL, *protocol = NULL; |
| 201 |
65 |
| int is_connect = 0; |
| 202 |
|
| const char *method, *upg, *conn, *key, *ver; |
| 203 |
|
| STRLEN mlen, ul, cl, kl, vl; |
| 204 |
|
| unsigned char digest[20]; |
| 205 |
|
| char accept[30]; |
| 206 |
|
| SV *hs, *attach_rv; |
| 207 |
|
| HV *attach; |
| 208 |
|
| |
| 209 |
65 |
▶ | if (!envh || !rech) return pw_err(aTHX_ 500, "bad websocket dispatch\n", NULL, NULL); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!envh || !rech) return pw_err(aTHX_ 500, "bad websocket dispatch\n", NULL, NULL); |
65 |
0 |
65 |
| if (!envh || !rech) return pw_err(aTHX_ 500, "bad websocket dispatch\n", NULL, NULL); |
0 |
65 |
65 |
|
| 210 |
65 |
| x = hv_fetchs(rech, K_WS, 0); |
| 211 |
65 |
▶ | if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); |
Branch: 4 branches
| Branch | True | False | Total |
| if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); |
65 |
0 |
65 |
| if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); |
65 |
0 |
65 |
| if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); |
65 |
0 |
65 |
| if (x && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVHV) opts = (HV *)SvRV(*x); |
65 |
0 |
65 |
|
| 212 |
65 |
| x = hv_fetchs(rech, K_CODE, 0); |
| 213 |
65 |
▶ | code = (x && *x) ? *x : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| code = (x && *x) ? *x : &PL_sv_undef; |
65 |
0 |
65 |
| code = (x && *x) ? *x : &PL_sv_undef; |
65 |
0 |
65 |
|
| 214 |
|
| |
| 215 |
65 |
| method = pw_env(aTHX_ envh, "REQUEST_METHOD", &mlen); |
| 216 |
|
| |
| 217 |
|
| /* Extended CONNECT (RFC 8441 on HTTP/2, RFC 9220 on HTTP/3): a |
| 218 |
|
| * multiplexed transport has no 101, no Upgrade header, and forbids |
| 219 |
|
| * Connection outright, so the handshake below simply does not exist |
| 220 |
|
| * there. The server having advertised ENABLE_CONNECT_PROTOCOL is the |
| 221 |
|
| * whole of the negotiation. |
| 222 |
|
| * |
| 223 |
|
| * The origin check and the subprotocol negotiation that follow are NOT |
| 224 |
|
| * skipped. Neither is HTTP/1.1-specific: an Extended CONNECT carries the |
| 225 |
|
| * user's cookies exactly as an upgrade does, so skipping the origin |
| 226 |
|
| * check here would open the hole it exists to close on precisely the |
| 227 |
|
| * transport a browser prefers. */ |
| 228 |
|
| { |
| 229 |
65 |
| SV **cp = hv_fetchs(envh, "psgix.connect_protocol", 0); |
| 230 |
65 |
▶ | if (cp && *cp && SvOK(*cp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (cp && *cp && SvOK(*cp)) { |
4 |
61 |
65 |
| if (cp && *cp && SvOK(*cp)) { |
4 |
0 |
4 |
| if (cp && *cp && SvOK(*cp)) { |
4 |
0 |
4 |
|
| 231 |
|
| STRLEN pl; |
| 232 |
4 |
| const char *pv = SvPV_const(*cp, pl); |
| 233 |
4 |
▶ | if (pl == 9 && memEQ(pv, "websocket", 9)) is_connect = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (pl == 9 && memEQ(pv, "websocket", 9)) is_connect = 1; |
4 |
0 |
4 |
| if (pl == 9 && memEQ(pv, "websocket", 9)) is_connect = 1; |
4 |
0 |
4 |
|
| 234 |
|
| } |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
65 |
▶ | if (is_connect) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_connect) { |
4 |
61 |
65 |
|
| 238 |
4 |
▶ | if (!(mlen == 7 && memEQ(method, "CONNECT", 7))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(mlen == 7 && memEQ(method, "CONNECT", 7))) |
4 |
0 |
4 |
| if (!(mlen == 7 && memEQ(method, "CONNECT", 7))) |
0 |
4 |
4 |
|
| 239 |
0 |
| return pw_err(aTHX_ 400, |
| 240 |
|
| "websocket over HTTP/2 or HTTP/3 uses CONNECT\n", NULL, NULL); |
| 241 |
|
| } else { |
| 242 |
61 |
▶ | if (!(mlen == 3 && memEQ(method, "GET", 3))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(mlen == 3 && memEQ(method, "GET", 3))) |
61 |
0 |
61 |
| if (!(mlen == 3 && memEQ(method, "GET", 3))) |
0 |
61 |
61 |
|
| 243 |
0 |
| return pw_err(aTHX_ 405, "websocket routes are GET\n", "Allow", "GET"); |
| 244 |
|
| |
| 245 |
61 |
| upg = pw_env(aTHX_ envh, "HTTP_UPGRADE", &ul); |
| 246 |
61 |
| conn = pw_env(aTHX_ envh, "HTTP_CONNECTION", &cl); |
| 247 |
61 |
▶ | if (!(pw_ci_has(upg, ul, "websocket") && pw_ci_has(conn, cl, "upgrade"))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(pw_ci_has(upg, ul, "websocket") && pw_ci_has(conn, cl, "upgrade"))) |
56 |
5 |
61 |
| if (!(pw_ci_has(upg, ul, "websocket") && pw_ci_has(conn, cl, "upgrade"))) |
2 |
54 |
56 |
|
| 248 |
7 |
| return pw_err(aTHX_ 400, "not a websocket upgrade\n", NULL, NULL); |
| 249 |
|
| |
| 250 |
54 |
| key = pw_env(aTHX_ envh, "HTTP_SEC_WEBSOCKET_KEY", &kl); |
| 251 |
54 |
▶ | if (!pw_is_b64_key(key, kl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pw_is_b64_key(key, kl)) |
1 |
53 |
54 |
|
| 252 |
1 |
| return pw_err(aTHX_ 400, "bad Sec-WebSocket-Key\n", NULL, NULL); |
| 253 |
|
| |
| 254 |
53 |
| ver = pw_env(aTHX_ envh, "HTTP_SEC_WEBSOCKET_VERSION", &vl); |
| 255 |
53 |
▶ | if (!(vl == 2 && memEQ(ver, "13", 2))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(vl == 2 && memEQ(ver, "13", 2))) |
52 |
1 |
53 |
| if (!(vl == 2 && memEQ(ver, "13", 2))) |
0 |
52 |
52 |
|
| 256 |
1 |
| return pw_err(aTHX_ 426, "unsupported websocket version\n", |
| 257 |
|
| "Sec-WebSocket-Version", "13"); |
| 258 |
|
| } |
| 259 |
|
| |
| 260 |
|
| /* Origin. The upgrade carries the user's cookies and the same-origin |
| 261 |
|
| * policy does not cover it, so a page anywhere could otherwise open an |
| 262 |
|
| * authenticated socket - see pk_origin_ok for what counts as this |
| 263 |
|
| * application's own origin. |
| 264 |
|
| * |
| 265 |
|
| * A browser always sends Origin on an upgrade. A client that is not a |
| 266 |
|
| * browser - Punk::Test, a mobile app, a daemon - sends none, has no |
| 267 |
|
| * ambient cookies to be used against it, and is not the attack this |
| 268 |
|
| * stops: an absent header passes. */ |
| 269 |
|
| { |
| 270 |
56 |
▶ | SV **ov = opts ? hv_fetchs(opts, "origin", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **ov = opts ? hv_fetchs(opts, "origin", 0) : NULL; |
56 |
0 |
56 |
|
| 271 |
56 |
▶ | int off = ov && *ov && !SvROK(*ov) && !SvTRUE(*ov); |
Branch: 4 branches
| Branch | True | False | Total |
| int off = ov && *ov && !SvROK(*ov) && !SvTRUE(*ov); |
22 |
34 |
56 |
| 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 |
|
| 272 |
56 |
▶ | if (!off) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!off) { |
45 |
11 |
56 |
|
| 273 |
|
| STRLEN ol; |
| 274 |
45 |
| const char *o = pw_env(aTHX_ envh, "HTTP_ORIGIN", &ol); |
| 275 |
45 |
▶ | if (ol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ol) { |
27 |
18 |
45 |
|
| 276 |
|
| int ok; |
| 277 |
29 |
▶ | if (ov && *ov && SvROK(*ov) && SvTYPE(SvRV(*ov)) == SVt_PVCV) { |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 278 |
2 |
| SV *osv = sv_2mortal(newSVpvn(o, ol)); |
| 279 |
2 |
| int died = 0; |
| 280 |
2 |
| SV *r = pcx_call2(aTHX_ *ov, c, osv, &died); |
| 281 |
2 |
▶ | ok = (!died && r && SvTRUE(r)); |
Branch: 3 branches
| Branch | True | False | Total |
| ok = (!died && r && SvTRUE(r)); |
2 |
0 |
2 |
| ok = (!died && r && SvTRUE(r)); |
2 |
0 |
2 |
| ok = (!died && r && SvTRUE(r)); |
1 |
1 |
2 |
|
| 282 |
2 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
2 |
0 |
2 |
|
| 283 |
2 |
▶ | if (died) croak_sv(sv_2mortal(newSVsv(ERRSV))); |
Branch: 2 branches
| Branch | True | False | Total |
| if (died) croak_sv(sv_2mortal(newSVsv(ERRSV))); |
0 |
2 |
2 |
| if (died) croak_sv(sv_2mortal(newSVsv(ERRSV))); |
0 |
0 |
0 |
|
| 284 |
|
| } |
| 285 |
|
| else { |
| 286 |
9 |
▶ | AV *extra = (ov && *ov && SvROK(*ov) |
Branch: 2 branches
| Branch | True | False | Total |
| AV *extra = (ov && *ov && SvROK(*ov) |
9 |
0 |
9 |
| AV *extra = (ov && *ov && SvROK(*ov) |
9 |
0 |
9 |
|
| 287 |
9 |
▶ | && SvTYPE(SvRV(*ov)) == SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(SvRV(*ov)) == SVt_PVAV) |
9 |
0 |
9 |
|
| 288 |
34 |
▶ | ? (AV *)SvRV(*ov) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (AV *)SvRV(*ov) : NULL; |
9 |
16 |
25 |
|
| 289 |
25 |
| ok = pk_origin_ok(aTHX_ c, o, ol, extra); |
| 290 |
|
| } |
| 291 |
27 |
▶ | if (!ok) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) |
14 |
13 |
27 |
|
| 292 |
14 |
| return pw_err(aTHX_ 403, "origin not allowed\n", |
| 293 |
|
| NULL, NULL); |
| 294 |
|
| } |
| 295 |
|
| } |
| 296 |
|
| } |
| 297 |
|
| |
| 298 |
|
| /* subprotocol: only when the route declares a list. First offered value |
| 299 |
|
| * that the route accepts wins; a declared list with no match is a 400. */ |
| 300 |
42 |
▶ | if (opts && (x = hv_fetchs(opts, "protocols", 0)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (opts && (x = hv_fetchs(opts, "protocols", 0)) |
42 |
0 |
42 |
| if (opts && (x = hv_fetchs(opts, "protocols", 0)) |
3 |
39 |
42 |
|
| 301 |
3 |
▶ | && *x && SvROK(*x) && SvTYPE(SvRV(*x)) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| && *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 |
|
| 302 |
3 |
| AV *want = (AV *)SvRV(*x); |
| 303 |
3 |
| STRLEN pl; const char *ps = pw_env(aTHX_ envh, "HTTP_SEC_WEBSOCKET_PROTOCOL", &pl); |
| 304 |
3 |
| int saw_offer = 0; |
| 305 |
3 |
| STRLEN i = 0; |
| 306 |
6 |
▶ | while (i < pl && !protocol) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < pl && !protocol) { |
4 |
2 |
6 |
| while (i < pl && !protocol) { |
3 |
1 |
4 |
|
| 307 |
|
| STRLEN s, e2, j; SSize_t wi, wn; |
| 308 |
3 |
▶ | while (i < pl && (ps[i] == ' ' || ps[i] == '\t' || ps[i] == ',')) i++; |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 309 |
3 |
| s = i; |
| 310 |
24 |
▶ | while (i < pl && ps[i] != ',') i++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < pl && ps[i] != ',') i++; |
22 |
2 |
24 |
| while (i < pl && ps[i] != ',') i++; |
21 |
1 |
22 |
|
| 311 |
3 |
| e2 = i; |
| 312 |
3 |
▶ | while (e2 > s && (ps[e2-1] == ' ' || ps[e2-1] == '\t')) e2--; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 313 |
3 |
▶ | if (e2 == s) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e2 == s) continue; |
0 |
3 |
3 |
|
| 314 |
3 |
| saw_offer = 1; |
| 315 |
3 |
| wn = av_len(want) + 1; |
| 316 |
7 |
▶ | for (wi = 0; wi < wn; wi++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (wi = 0; wi < wn; wi++) { |
6 |
1 |
7 |
|
| 317 |
6 |
| SV **wp = av_fetch(want, wi, 0); |
| 318 |
|
| STRLEN wl; const char *ws2; |
| 319 |
6 |
▶ | if (!wp || !*wp) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!wp || !*wp) continue; |
6 |
0 |
6 |
| if (!wp || !*wp) continue; |
0 |
6 |
6 |
|
| 320 |
6 |
| ws2 = SvPV_const(*wp, wl); |
| 321 |
6 |
▶ | if (wl == e2 - s) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wl == e2 - s) { |
6 |
0 |
6 |
|
| 322 |
32 |
▶ | for (j = 0; j < wl && ws2[j] == ps[s + j]; j++) ; |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 323 |
6 |
▶ | if (j == wl) { protocol = sv_2mortal(newSVpvn(ps + s, wl)); break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == wl) { protocol = sv_2mortal(newSVpvn(ps + s, wl)); break; } |
2 |
4 |
6 |
|
| 324 |
|
| } |
| 325 |
|
| } |
| 326 |
|
| } |
| 327 |
3 |
▶ | if (saw_offer && !protocol) |
Branch: 2 branches
| Branch | True | False | Total |
| if (saw_offer && !protocol) |
3 |
0 |
3 |
| if (saw_offer && !protocol) |
1 |
2 |
3 |
|
| 328 |
1 |
| return pw_err(aTHX_ 400, "no acceptable subprotocol\n", NULL, NULL); |
| 329 |
|
| } |
| 330 |
|
| |
| 331 |
|
| /* The HTTP/1.1 response, and ONLY for HTTP/1.1. Extended CONNECT has no |
| 332 |
|
| * 101 to send and, more to the point, no Sec-WebSocket-Key to hash: the |
| 333 |
|
| * key parse above is skipped on that path, so running this would take |
| 334 |
|
| * the digest of an uninitialised pointer. */ |
| 335 |
41 |
▶ | if (is_connect) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_connect) { |
3 |
38 |
41 |
|
| 336 |
3 |
| hs = sv_2mortal(newSVpvs("")); |
| 337 |
|
| } else { |
| 338 |
|
| /* Sec-WebSocket-Accept = base64(sha1(key . GUID)) */ |
| 339 |
|
| { |
| 340 |
|
| unsigned char kg[24 + sizeof(PW_GUID) - 1]; |
| 341 |
38 |
| memcpy(kg, key, 24); |
| 342 |
38 |
| memcpy(kg + 24, PW_GUID, sizeof(PW_GUID) - 1); |
| 343 |
38 |
| pw_sha1(kg, sizeof(kg), digest); |
| 344 |
38 |
| pw_base64(digest, 20, accept); |
| 345 |
|
| } |
| 346 |
|
| |
| 347 |
38 |
| hs = sv_2mortal(newSVpvs( |
| 348 |
|
| "HTTP/1.1 101 Switching Protocols\r\n" |
| 349 |
|
| "Upgrade: websocket\r\n" |
| 350 |
|
| "Connection: Upgrade\r\n" |
| 351 |
|
| "Sec-WebSocket-Accept: ")); |
| 352 |
38 |
| sv_catpv(hs, accept); |
| 353 |
38 |
| sv_catpvs(hs, "\r\n"); |
| 354 |
38 |
▶ | if (protocol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protocol) { |
2 |
36 |
38 |
|
| 355 |
2 |
| sv_catpvs(hs, "Sec-WebSocket-Protocol: "); |
| 356 |
2 |
| sv_catsv(hs, protocol); |
| 357 |
2 |
| sv_catpvs(hs, "\r\n"); |
| 358 |
|
| } |
| 359 |
38 |
| sv_catpvs(hs, "\r\n"); |
| 360 |
|
| } |
| 361 |
|
| |
| 362 |
|
| /* the attach options: the route opts minus the two the handshake has |
| 363 |
|
| * already spent (`protocols`, `origin`), plus the negotiated `protocol` |
| 364 |
|
| * (the connection object reads these). */ |
| 365 |
41 |
| attach = newHV(); |
| 366 |
41 |
▶ | if (opts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts) { |
41 |
0 |
41 |
|
| 367 |
|
| HE *he; |
| 368 |
41 |
| hv_iterinit(opts); |
| 369 |
60 |
▶ | while ((he = hv_iternext(opts))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(opts))) { |
19 |
41 |
60 |
|
| 370 |
19 |
▶ | STRLEN hkl; const char *hk = HePV(he, hkl); |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN hkl; const char *hk = HePV(he, hkl); |
0 |
19 |
19 |
|
| 371 |
36 |
▶ | if (hkl == 9 && memEQ(hk, "protocols", 9)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (hkl == 9 && memEQ(hk, "protocols", 9)) continue; |
2 |
17 |
19 |
| if (hkl == 9 && memEQ(hk, "protocols", 9)) continue; |
2 |
0 |
2 |
|
| 372 |
17 |
▶ | if (hkl == 6 && memEQ(hk, "origin", 6)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (hkl == 6 && memEQ(hk, "origin", 6)) continue; |
17 |
0 |
17 |
| if (hkl == 6 && memEQ(hk, "origin", 6)) continue; |
17 |
0 |
17 |
|
| 373 |
0 |
| (void)hv_store(attach, hk, (I32)hkl, newSVsv(hv_iterval(opts, he)), 0); |
| 374 |
|
| } |
| 375 |
|
| } |
| 376 |
41 |
▶ | if (protocol) (void)hv_stores(attach, "protocol", newSVsv(protocol)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (protocol) (void)hv_stores(attach, "protocol", newSVsv(protocol)); |
2 |
39 |
41 |
|
| 377 |
41 |
| attach_rv = sv_2mortal(newRV_noinc((SV *)attach)); |
| 378 |
|
| |
| 379 |
|
| /* Extended CONNECT, tried first because when it applies nothing below |
| 380 |
|
| * can work: there is no descriptor to detach and no psgix.io to own. |
| 381 |
|
| * |
| 382 |
|
| * The acceptance IS the response here - a 200 with the negotiated |
| 383 |
|
| * subprotocol, sent by opening the stream handle - so no handshake |
| 384 |
|
| * bytes are written and `hs` goes unused on this path. Everything after |
| 385 |
|
| * that is the same codec the detached path drives. */ |
| 386 |
41 |
▶ | if (is_connect) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_connect) { |
3 |
38 |
41 |
|
| 387 |
3 |
| const hm_abi *A = NULL; |
| 388 |
3 |
| void *loop = NULL, *h = NULL; |
| 389 |
3 |
| AV *hdrs = newAV(); |
| 390 |
3 |
▶ | if (protocol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protocol) { |
0 |
3 |
3 |
|
| 391 |
0 |
| av_push(hdrs, newSVpvs("sec-websocket-protocol")); |
| 392 |
0 |
| av_push(hdrs, newSVsv(protocol)); |
| 393 |
|
| } |
| 394 |
3 |
| h = punk_hm_stream_open(aTHX_ envh, &A, &loop, 200, hdrs); |
| 395 |
3 |
| SvREFCNT_dec((SV *)hdrs); |
| 396 |
3 |
▶ | if (!h) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) |
3 |
0 |
3 |
|
| 397 |
3 |
| return pw_err(aTHX_ 501, |
| 398 |
|
| "this server cannot carry a websocket over HTTP/2 or HTTP/3 " |
| 399 |
|
| "(needs Hyperman's ABI v8)\n", NULL, NULL); |
| 400 |
|
| { |
| 401 |
|
| SV *ws, *argv[2]; |
| 402 |
0 |
| argv[0] = sv_2mortal(newSViv(PTR2IV(h))); |
| 403 |
0 |
| argv[1] = attach_rv; |
| 404 |
0 |
| ws = pw_class_call(aTHX_ "_attach_stream", argv, 2); |
| 405 |
0 |
▶ | pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); |
0 |
0 |
0 |
|
| 406 |
0 |
▶ | if (ws) SvREFCNT_dec(ws); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws) SvREFCNT_dec(ws); |
0 |
0 |
0 |
|
| 407 |
|
| } |
| 408 |
|
| /* The response already went out with the stream handle; this is the |
| 409 |
|
| * sentinel the server discards, as the 101 is on the detach path. */ |
| 410 |
0 |
| return pw_empty(aTHX_ 200); |
| 411 |
|
| } |
| 412 |
|
| |
| 413 |
|
| /* HTTP/1.1 over TLS: the socket cannot be handed over, because the TLS |
| 414 |
|
| * session's state belongs to the server and conn_detach refuses it (-3). |
| 415 |
|
| * The upgrade rides a 101 stream handle instead - the same seam the |
| 416 |
|
| * Extended CONNECT path uses, opened with the 101 and its handshake |
| 417 |
|
| * headers rather than a 200, so the RFC 6455 codec above it is again |
| 418 |
|
| * the same one. Plaintext HTTP/1.1 detaches below: the fd handoff is |
| 419 |
|
| * cheaper and there is no session to strand. Needs Hyperman 0.48+, whose |
| 420 |
|
| * stream handle turns a 101 into a tunnel; an older one returns no handle |
| 421 |
|
| * here and the request falls through to the detach that has always |
| 422 |
|
| * refused it. */ |
| 423 |
|
| { |
| 424 |
|
| STRLEN hl; |
| 425 |
38 |
| const char *https = pw_env(aTHX_ envh, "HTTPS", &hl); |
| 426 |
38 |
▶ | if (https && hl) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (https && hl) { |
38 |
0 |
38 |
| if (https && hl) { |
2 |
36 |
38 |
|
| 427 |
2 |
| const hm_abi *A = NULL; |
| 428 |
2 |
| void *loop = NULL, *h = NULL; |
| 429 |
2 |
| AV *hdrs = newAV(); |
| 430 |
2 |
| av_push(hdrs, newSVpvs("Upgrade")); |
| 431 |
2 |
| av_push(hdrs, newSVpvs("websocket")); |
| 432 |
2 |
| av_push(hdrs, newSVpvs("Connection")); |
| 433 |
2 |
| av_push(hdrs, newSVpvs("Upgrade")); |
| 434 |
2 |
| av_push(hdrs, newSVpvs("Sec-WebSocket-Accept")); |
| 435 |
2 |
| av_push(hdrs, newSVpv(accept, 0)); |
| 436 |
2 |
▶ | if (protocol) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protocol) { |
1 |
1 |
2 |
|
| 437 |
1 |
| av_push(hdrs, newSVpvs("Sec-WebSocket-Protocol")); |
| 438 |
1 |
| av_push(hdrs, newSVsv(protocol)); |
| 439 |
|
| } |
| 440 |
2 |
| h = punk_hm_stream_open(aTHX_ envh, &A, &loop, 101, hdrs); |
| 441 |
2 |
| SvREFCNT_dec((SV *)hdrs); |
| 442 |
2 |
▶ | if (h) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (h) { |
2 |
0 |
2 |
|
| 443 |
|
| SV *ws, *argv[2]; |
| 444 |
2 |
| argv[0] = sv_2mortal(newSViv(PTR2IV(h))); |
| 445 |
2 |
| argv[1] = attach_rv; |
| 446 |
2 |
| ws = pw_class_call(aTHX_ "_attach_stream", argv, 2); |
| 447 |
2 |
▶ | pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); |
2 |
0 |
2 |
|
| 448 |
2 |
▶ | if (ws) SvREFCNT_dec(ws); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws) SvREFCNT_dec(ws); |
2 |
0 |
2 |
|
| 449 |
2 |
| return pw_empty(aTHX_ 101); |
| 450 |
|
| } |
| 451 |
|
| } |
| 452 |
|
| } |
| 453 |
|
| |
| 454 |
|
| /* Hyperman: detach the socket and drive it on the worker loop. */ |
| 455 |
36 |
| x = hv_fetchs(envh, "psgix.hyperman.conn", 0); |
| 456 |
36 |
▶ | if (x && *x && SvTRUE(*x)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (x && *x && SvTRUE(*x)) { |
18 |
18 |
36 |
| if (x && *x && SvTRUE(*x)) { |
18 |
0 |
18 |
| if (x && *x && SvTRUE(*x)) { |
18 |
0 |
18 |
|
| 457 |
|
| int ok; |
| 458 |
|
| { /* _hm_available() - a no-arg function, not a method */ |
| 459 |
18 |
| dSP; int count; |
| 460 |
18 |
| ENTER; SAVETMPS; |
| 461 |
18 |
▶ | PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); PUTBACK; |
0 |
18 |
18 |
|
| 462 |
18 |
| count = call_pv(PK_WEBSOCKET "::_hm_available", G_SCALAR); |
| 463 |
18 |
| SPAGAIN; |
| 464 |
18 |
▶ | ok = count > 0 ? SvTRUE(TOPs) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| ok = count > 0 ? SvTRUE(TOPs) : 0; |
18 |
0 |
18 |
|
| 465 |
18 |
▶ | if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) (void)POPs; |
18 |
0 |
18 |
|
| 466 |
18 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
18 |
0 |
18 |
|
| 467 |
|
| } |
| 468 |
18 |
▶ | if (ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) { |
18 |
0 |
18 |
|
| 469 |
18 |
| IV fd; int died = 0; |
| 470 |
|
| SV *ws, *argv[3]; |
| 471 |
|
| { |
| 472 |
18 |
| dSP; int count; |
| 473 |
18 |
| ENTER; SAVETMPS; |
| 474 |
18 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(env); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(env); PUTBACK; |
0 |
18 |
18 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(env); PUTBACK; |
0 |
18 |
18 |
|
| 475 |
18 |
| count = call_pv(PK_WEBSOCKET "::_hm_detach", G_SCALAR | G_EVAL); |
| 476 |
18 |
| SPAGAIN; |
| 477 |
18 |
▶ | died = SvTRUE(ERRSV) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| died = SvTRUE(ERRSV) ? 1 : 0; |
18 |
0 |
18 |
|
| 478 |
18 |
▶ | fd = (count > 0) ? (died ? (POPs, 0) : POPi) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| fd = (count > 0) ? (died ? (POPs, 0) : POPi) : 0; |
18 |
0 |
18 |
| fd = (count > 0) ? (died ? (POPs, 0) : POPi) : 0; |
0 |
18 |
18 |
|
| 479 |
18 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
18 |
0 |
18 |
|
| 480 |
|
| } |
| 481 |
18 |
▶ | if (died) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) { |
0 |
18 |
18 |
|
| 482 |
0 |
▶ | const char *em = SvPV_nolen(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *em = SvPV_nolen(ERRSV); |
0 |
0 |
0 |
|
| 483 |
0 |
▶ | if (strstr(em, "draining") || strstr(em, "already detached")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (strstr(em, "draining") || strstr(em, "already detached")) |
0 |
0 |
0 |
| if (strstr(em, "draining") || strstr(em, "already detached")) |
0 |
0 |
0 |
|
| 484 |
0 |
| return pw_err(aTHX_ 400, "cannot upgrade this connection\n", |
| 485 |
|
| NULL, NULL); |
| 486 |
0 |
▶ | croak_sv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| croak_sv(ERRSV); |
0 |
0 |
0 |
|
| 487 |
|
| } |
| 488 |
18 |
| argv[0] = sv_2mortal(newSViv(fd)); |
| 489 |
18 |
| argv[1] = hs; |
| 490 |
18 |
| argv[2] = attach_rv; |
| 491 |
18 |
| ws = pw_class_call(aTHX_ "_attach", argv, 3); |
| 492 |
18 |
▶ | pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); |
18 |
0 |
18 |
|
| 493 |
18 |
▶ | if (ws) SvREFCNT_dec(ws); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws) SvREFCNT_dec(ws); |
18 |
0 |
18 |
|
| 494 |
18 |
| return pw_empty(aTHX_ 101); /* discarded by Hyperman */ |
| 495 |
|
| } |
| 496 |
|
| } |
| 497 |
|
| |
| 498 |
|
| /* Blocking fallback: own the socket inside the handler (pins a worker). */ |
| 499 |
18 |
▶ | x = opts ? hv_fetchs(opts, K_BLOCKING, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| x = opts ? hv_fetchs(opts, K_BLOCKING, 0) : NULL; |
18 |
0 |
18 |
|
| 500 |
18 |
▶ | if (x && *x && SvTRUE(*x)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (x && *x && SvTRUE(*x)) { |
0 |
18 |
18 |
| if (x && *x && SvTRUE(*x)) { |
0 |
0 |
0 |
| if (x && *x && SvTRUE(*x)) { |
0 |
0 |
0 |
|
| 501 |
0 |
| SV **iop = hv_fetchs(envh, "psgix.io", 0); |
| 502 |
0 |
▶ | if (iop && *iop && SvOK(*iop)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (iop && *iop && SvOK(*iop)) { |
0 |
0 |
0 |
| if (iop && *iop && SvOK(*iop)) { |
0 |
0 |
0 |
| if (iop && *iop && SvOK(*iop)) { |
0 |
0 |
0 |
|
| 503 |
0 |
| IO *io = sv_2io(*iop); |
| 504 |
0 |
▶ | IV fd = (io && IoIFP(io)) ? (IV)PerlIO_fileno(IoIFP(io)) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| IV fd = (io && IoIFP(io)) ? (IV)PerlIO_fileno(IoIFP(io)) : -1; |
0 |
0 |
0 |
| IV fd = (io && IoIFP(io)) ? (IV)PerlIO_fileno(IoIFP(io)) : -1; |
0 |
0 |
0 |
|
| 505 |
|
| SV *ws, *argv[3], *rb; |
| 506 |
0 |
| argv[0] = sv_2mortal(newSViv(fd)); |
| 507 |
0 |
| argv[1] = hs; /* _attach_blocking flushes it */ |
| 508 |
0 |
| argv[2] = attach_rv; |
| 509 |
0 |
| ws = pw_class_call(aTHX_ "_attach_blocking", argv, 3); |
| 510 |
0 |
▶ | pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| pw_run_handler(aTHX_ code, c, ws ? ws : &PL_sv_undef); |
0 |
0 |
0 |
|
| 511 |
0 |
▶ | rb = pcx_call_meth(aTHX_ ws ? ws : &PL_sv_undef, "_run_blocking", |
Branch: 1 branch
| Branch | True | False | Total |
| rb = pcx_call_meth(aTHX_ ws ? ws : &PL_sv_undef, "_run_blocking", |
0 |
0 |
0 |
|
| 512 |
|
| NULL, 0, 0); |
| 513 |
0 |
▶ | if (rb) SvREFCNT_dec(rb); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rb) SvREFCNT_dec(rb); |
0 |
0 |
0 |
|
| 514 |
0 |
▶ | if (ws) SvREFCNT_dec(ws); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ws) SvREFCNT_dec(ws); |
0 |
0 |
0 |
|
| 515 |
0 |
| return pw_empty(aTHX_ 200); /* the socket is gone */ |
| 516 |
|
| } |
| 517 |
|
| } |
| 518 |
|
| |
| 519 |
18 |
| return pw_err(aTHX_ 501, |
| 520 |
|
| "this server cannot upgrade websockets (needs Hyperman 0.11+, " |
| 521 |
|
| "or blocking => 1 with psgix.io)\n", NULL, NULL); |
| 522 |
|
| } |
| 523 |
|
| |
| 524 |
|
| #endif /* PUNK_WSHANDSHAKE_H */ |