| 1 | #ifndef HM_HTTP2_H | ||
| 2 | #define HM_HTTP2_H | ||
| 3 | |||
| 4 | /* HTTP/2 (cleartext h2c) via nghttp2. Enabled by Makefile.PL when nghttp2 is | ||
| 5 | * found (-DHM_HAVE_NGHTTP2, -lnghttp2); otherwise the entry points compile to | ||
| 6 | * stubs and `http2 => 1` errors at runtime. | ||
| 7 | * | ||
| 8 | * Included from hm_core.h after the connection/HTTP/PSGI helpers it reuses | ||
| 9 | * (hm_wb_put, hm_flush, hm_call_app, hm_new_input, hm_slurp_body, | ||
| 10 | * hm_pct_decode, hm_access_log, hm_500_resp) and the Future machinery | ||
| 11 | * (hm_closure, hm_any_on_ready, hm_any_state, hmf_*). A connection in h2 mode | ||
| 12 | * holds one nghttp2 session; each stream is an independent request whose | ||
| 13 | * response resolves through the same sync/Future/streaming paths as HTTP/1.1, | ||
| 14 | * with nghttp2 owning framing, HPACK, flow control, and multiplexing. */ | ||
| 15 | |||
| 16 | #ifdef HM_HAVE_NGHTTP2 | ||
| 17 | |||
| 18 | #include <nghttp2/nghttp2.h> | ||
| 19 | |||
| 20 | typedef struct hm_h2_stream { | ||
| 21 | int32_t id; | ||
| 22 | HV *env; /* being built (moved out at dispatch) */ | ||
| 23 | SV *env_rv; /* kept while awaiting, for access_log */ | ||
| 24 | SV *body; /* accumulating request body, or NULL */ | ||
| 25 | SV *resp_body; /* response bytes the data provider feeds */ | ||
| 26 | size_t resp_off; /* data-provider read cursor */ | ||
| 27 | SV *src_sv; /* streamed file body: owned, or NULL */ | ||
| 28 | int src_fd; /* its fd, read at src_off */ | ||
| 29 | off_t src_off; | ||
| 30 | UV src_rem; /* bytes still owed on the stream */ | ||
| 31 | size_t blen; /* response body length (logging) */ | ||
| 32 | int status; /* response status (logging) */ | ||
| 33 | int resp_status; /* streaming: stashed until close */ | ||
| 34 | SV *resp_headers; /* streaming: stashed headers arrayref */ | ||
| 35 | int awaiting; /* parked on a Future */ | ||
| 36 | SV *resp_f; /* that Future, held so a RST_STREAM can | ||
| 37 | * cancel it the way an HTTP/1 close does */ | ||
| 38 | size_t hdr_bytes; /* request header octets so far (bounded) */ | ||
| 39 | unsigned char refused; /* over the body ceiling: 413 sent, the | ||
| 40 | * rest of the stream is dropped unread */ | ||
| 41 | unsigned char producing; /* an open stream handle is feeding this: | ||
| 42 | * the end of resp_body is not the end of | ||
| 43 | * the body, so the provider defers */ | ||
| 44 | unsigned char prod_done; /* ... and now it is (stream_close ran) */ | ||
| 45 | unsigned char ready; /* the request is complete and waiting to | ||
| 46 | * be handed to the application, once | ||
| 47 | * nghttp2 is off the stack (hm_h2_input) */ | ||
| 48 | unsigned char tunnel; /* an Extended CONNECT stream: what body | ||
| 49 | * arrived before the read half was | ||
| 50 | * registered is the tunnel's, and goes to | ||
| 51 | * it (hm_stream_on_data) */ | ||
| 52 | unsigned char in_app; /* its handler is on the stack */ | ||
| 53 | struct hm_h2_stream *next; | ||
| 54 | } hm_h2_stream; | ||
| 55 | |||
| 56 | typedef struct hm_h2_sess { | ||
| 57 | nghttp2_session *session; | ||
| 58 | hm_conn *conn; | ||
| 59 | hm_h2_stream *streams; /* singly-linked, for cleanup */ | ||
| 60 | int in_recv; /* inside nghttp2_session_mem_recv: no | ||
| 61 | * send, no close, no second recv until | ||
| 62 | * it returns (hm_h2_input) */ | ||
| 63 | int in_send; /* inside nghttp2_session_send: a second | ||
| 64 | * one would re-enter the library */ | ||
| 65 | int dispatching; /* an application call for this | ||
| 66 | * session is on the C stack: requests | ||
| 67 | * that complete meanwhile wait for it */ | ||
| 68 | } hm_h2_sess; | ||
| 69 | |||
| 70 | /* The request header block a stream may carry, in octets (name + value + | ||
| 71 | * RFC 9113's 32 per field), the bound SETTINGS_MAX_HEADER_LIST_SIZE | ||
| 72 | * advertises and the header callback enforces. HTTP/1.1 refuses a block | ||
| 73 | * over 64KB too (hm_readable), so the two transports agree. */ | ||
| 74 | #define HM_H2_MAX_HEADER_LIST 65536 | ||
| 75 | |||
| 76 | static int hm_h2_flush_send(pTHX_ hm_h2_sess *s); | ||
| 77 | static void hm_h2_respond(pTHX_ hm_h2_sess *s, hm_h2_stream *st, SV *resp); | ||
| 78 | |||
| 79 | /* the 24-byte HTTP/2 connection preface */ | ||
| 80 | static const char HM_H2_PREFACE[24] = | ||
| 81 | { 'P','R','I',' ','*',' ','H','T','T','P','/','2','.','0','\r','\n', | ||
| 82 | '\r','\n','S','M','\r','\n','\r','\n' }; | ||
| 83 | |||
| 84 | /* ---- request: headers -> $env ------------------------------------------- */ | ||
| 85 | |||
| 86 | static void hm_h2_env_init(pTHX_ hm_h2_sess *s, HV *env) { | ||
| 87 | hm_loop *loop = s->conn->loop; | ||
| 88 | hm_listener *lst = s->conn->lst; | ||
| 89 | AV *ver = newAV(); | ||
| 90 | /* the shared header-key table lives in hm_core.h and is filled by | ||
| 91 | * hm_env_init; a process that only ever speaks HTTP/2 has to fill it | ||
| 92 | * from here or hm_hdrk_lookup never engages (it would fall back | ||
| 93 | * safely, just without the point of it) */ | ||
| 94 | hm_env_init(aTHX); | ||
| 95 | av_push(ver, newSViv(1)); av_push(ver, newSViv(1)); | ||
| 96 | hv_stores(env, "SCRIPT_NAME", newSVpvs("")); | ||
| 97 | hv_stores(env, "SERVER_PROTOCOL", newSVpvs("HTTP/2")); | ||
| 98 | /* | ||
| 99 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 100 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 101 | hv_stores(env, "SERVER_NAME", newSVpv(lst && lst->host ? lst->host : "0.0.0.0", 0)); | ||
| 102 | hv_stores(env, "SERVER_PORT", newSViv(lst ? lst->port : 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 103 | /* | ||
| 104 | # uncoverable branch false class:ignore_covered_err | ||
| 105 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 106 | if (s->conn->peer[0]) { | ||
| 107 | hv_stores(env, "REMOTE_ADDR", newSVpv(s->conn->peer, 0)); | ||
| 108 | hv_stores(env, "REMOTE_HOST", newSVpv(s->conn->peer, 0)); | ||
| 109 | } | ||
| 110 | /* | ||
| 111 | # uncoverable branch false class:ignore_covered_err | ||
| 112 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 113 | if (s->conn->peer_port) hv_stores(env, "REMOTE_PORT", newSViv(s->conn->peer_port)); | ||
| 114 | hv_stores(env, "QUERY_STRING", newSVpvs("")); | ||
| 115 | hv_stores(env, "psgi.version", newRV_noinc((SV *)ver)); | ||
| 116 | hv_stores(env, "psgi.url_scheme", newSVpv(s->conn->ssl ? "https" : "http", 0)); | ||
| 117 | hv_stores(env, "psgi.multithread", newSViv(0)); | ||
| 118 | hv_stores(env, "psgi.multiprocess", newSViv(1)); | ||
| 119 | hv_stores(env, "psgi.run_once", newSViv(0)); | ||
| 120 | hv_stores(env, "psgi.streaming", newSViv(1)); | ||
| 121 | hv_stores(env, "psgi.nonblocking", newSViv(1)); | ||
| 122 | hv_stores(env, "psgi.errors", newRV_inc((SV *)PL_stderrgv)); | ||
| 123 | /* The body is complete before the application ever sees it: a stream is | ||
| 124 | * dispatched on END_STREAM and psgi.input is built from the whole of | ||
| 125 | * st->body. Declared here with the rest of the environment rather than | ||
| 126 | * left to dispatch, because it is the only thing that tells an | ||
| 127 | * application a bodied request with no content-length - which is what h2 | ||
| 128 | * sends for a streamed upload, having no Transfer-Encoding to use - can | ||
| 129 | * be read to EOF safely. */ | ||
| 130 | hv_stores(env, "psgix.input.buffered", newSViv(1)); | ||
| 131 | hv_stores(env, "psgix.loop", hm_loop_to_sv(aTHX_ loop)); | ||
| 132 | if (loop->log_wants_t0) | ||
| 133 | hv_stores(env, "psgix.request_start", newSVnv((NV)hm_now_ns() / 1e9)); | ||
| 134 | /* HTTPS, SSL_PROTOCOL, SSL_CIPHER and the SSL_CLIENT_* set, exactly as | ||
| 135 | * the HTTP/1.1 builder reports them. ALPN h2 is the browser path, so an | ||
| 136 | * mTLS application that read SSL_CLIENT_S_DN over HTTP/1.1 lost the | ||
| 137 | * client's identity the moment a browser connected. */ | ||
| 138 | if (s->conn->ssl) hm_tls_env(aTHX_ s->conn, env); | ||
| 139 | } | ||
| 140 | |||
| 141 | /* Request headers into $env. Takes the HV rather than a stream, because it | ||
| 142 | * only ever touched st->env and HTTP/3 needs exactly this: its | ||
| 143 | * pseudo-headers are HTTP/2's and QPACK yields the same lowercase pairs, so | ||
| 144 | * sharing it is the difference between one implementation and two that drift. | ||
| 145 | * hm_http3.h calls it with its own stream's env. */ | ||
| 146 | static void hm_h2_add_header(pTHX_ HV *env, | ||
| 147 | const char *nm, size_t nl, | ||
| 148 | const char *vl, size_t vlen) { | ||
| 149 | if (nl && nm[0] == ':') { /* pseudo-headers */ /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 150 | if (nl == 7 && memcmp(nm, ":method", 7) == 0) | ||
| 151 | hv_stores(env, "REQUEST_METHOD", newSVpvn(vl, vlen)); | ||
| 152 | /* :scheme is the CLIENT'S claim. psgi.url_scheme already says what | ||
| 153 | * this connection is (hm_h2_env_init, from c->ssl), and an | ||
| 154 | * application deciding on https-ness must not be told "https" by a | ||
| 155 | * cleartext peer. Ignored, as :authority does not set SERVER_NAME: | ||
| 156 | * that is the listener's name, on HTTP/1.1 too. */ | ||
| 157 | else if (nl == 10 && memcmp(nm, ":authority", 10) == 0) /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 158 | hv_stores(env, "HTTP_HOST", newSVpvn(vl, vlen)); | ||
| 159 | else if (nl == 9 && memcmp(nm, ":protocol", 9) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 160 | /* RFC 8441 / RFC 9220 Extended CONNECT: the only pseudo-header | ||
| 161 | * that has no HTTP/1.1 counterpart. It is what distinguishes a | ||
| 162 | * WebSocket handshake over h2 or h3 from an ordinary CONNECT | ||
| 163 | * tunnel, so it is surfaced under its own key rather than being | ||
| 164 | * folded into HTTP_UPGRADE and made to look like something it | ||
| 165 | * is not. */ | ||
| 166 | hv_stores(env, "psgix.connect_protocol", newSVpvn(vl, vlen)); | ||
| 167 | } | ||
| 168 | else if (nl == 5 && memcmp(nm, ":path", 5) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 169 | const char *q = (const char *)memchr(vl, '?', vlen); | ||
| 170 | size_t pl = q ? (size_t)(q - vl) : vlen; | ||
| 171 | hv_stores(env, "REQUEST_URI", newSVpvn(vl, vlen)); | ||
| 172 | hv_stores(env, "PATH_INFO", hm_pct_decode(aTHX_ vl, pl)); | ||
| 173 | if (q) hv_stores(env, "QUERY_STRING", | ||
| 174 | newSVpvn(q + 1, vlen - pl - 1)); | ||
| 175 | } | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | if (nl == 14 && hm_strncasecmp(nm, "content-length", 14) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 179 | hv_stores(env, "CONTENT_LENGTH", newSVpvn(vl, vlen)); | ||
| 180 | } else if (nl == 12 && hm_strncasecmp(nm, "content-type", 12) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 181 | hv_stores(env, "CONTENT_TYPE", newSVpvn(vl, vlen)); | ||
| 182 | /* | ||
| 183 | # uncoverable condition right count:2 class:ignore_covered_err | ||
| 184 | # uncoverable condition right count:3 class:ignore_covered_err */ | ||
| 185 | } else if (nl == 4 && hm_strncasecmp(nm, "host", 4) == 0 | ||
| 186 | && hv_exists(env, "HTTP_HOST", 9)) { | ||
| 187 | /* :authority already named the host. A Host field beside it would | ||
| 188 | * be JOINED onto HTTP_HOST ("a, b"), which no application expects | ||
| 189 | * and which lets a client pick which of two names a vhost sees. | ||
| 190 | * RFC 9113 8.3.1: the authority is the one that counts. */ | ||
| 191 | /* | ||
| 192 | # uncoverable branch false class:ignore_covered_err | ||
| 193 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 194 | } else if (nl + 5 < 300) { | ||
| 195 | char keybuf[300]; | ||
| 196 | size_t i; | ||
| 197 | SV **old; | ||
| 198 | memcpy(keybuf, "HTTP_", 5); | ||
| 199 | for (i = 0; i < nl; i++) { | ||
| 200 | unsigned char ch = (unsigned char)nm[i]; | ||
| 201 | if (ch >= 'a' && ch <= 'z') ch -= 32; /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 202 | else if (ch == '-') ch = '_'; | ||
| 203 | /* | ||
| 204 | # uncoverable branch false | ||
| 205 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 206 | else if (ch == '_') return; /* aliases the hyphenated name; | ||
| 207 | * dropped as on HTTP/1.1 */ | ||
| 208 | keybuf[5 + i] = (char)ch; | ||
| 209 | } | ||
| 210 | { /* common headers store through the process-lifetime shared key | ||
| 211 | * SVs (hm_core.h), skipping the per-request hash + HEK churn */ | ||
| 212 | SV *ksv = hm_hdrk_lookup(keybuf, nl + 5); | ||
| 213 | if (ksv) { | ||
| 214 | HE *he = hv_fetch_ent(env, ksv, 0, 0); | ||
| 215 | if (he) { sv_catpvn(HeVAL(he), hm_hdr_join_sep(keybuf, nl + 5), 2); | ||
| 216 | sv_catpvn(HeVAL(he), vl, vlen); } | ||
| 217 | else (void)hv_store_ent(env, ksv, newSVpvn(vl, vlen), 0); | ||
| 218 | } else { | ||
| 219 | old = hv_fetch(env, keybuf, (I32)(nl + 5), 0); | ||
| 220 | if (old) { sv_catpvn(*old, hm_hdr_join_sep(keybuf, nl + 5), 2); | ||
| 221 | sv_catpvn(*old, vl, vlen); } | ||
| 222 | else hv_store(env, keybuf, (I32)(nl + 5), newSVpvn(vl, vlen), 0); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | /* ---- response ------------------------------------------------------------ */ | ||
| 229 | |||
| 230 | static SV *hm_h2_body_sv(pTHX_ SV **s2, size_t *blen) { | ||
| 231 | SV *out = newSVpvs(""); | ||
| 232 | /* | ||
| 233 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 234 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 235 | if (s2 && SvROK(*s2) && SvTYPE(SvRV(*s2)) == SVt_PVAV) { | ||
| 236 | AV *b = (AV *)SvRV(*s2); | ||
| 237 | SSize_t i, n = av_len(b) + 1; | ||
| 238 | for (i = 0; i < n; i++) { | ||
| 239 | SV **e = av_fetch(b, i, 0); | ||
| 240 | /* | ||
| 241 | # uncoverable branch false class:ignore_covered_err | ||
| 242 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 243 | if (e) { STRLEN l; const char *p = SvPV(*e, l); sv_catpvn(out, p, l); } | ||
| 244 | } | ||
| 245 | /* | ||
| 246 | # uncoverable branch false class:ignore_covered_err | ||
| 247 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 248 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 249 | } else if (s2 && SvROK(*s2)) { | ||
| 250 | SV *slurp = hm_slurp_body(aTHX_ *s2); | ||
| 251 | sv_catsv(out, slurp); | ||
| 252 | SvREFCNT_dec(slurp); | ||
| 253 | } | ||
| 254 | { STRLEN l; (void)SvPV(out, l); *blen = l; } | ||
| 255 | return out; | ||
| 256 | } | ||
| 257 | |||
| 258 | /* Move a liftable filehandle body onto the stream as an fd source the data | ||
| 259 | * provider reads per window, instead of slurping it. Getline-only sources | ||
| 260 | * stay on the slurp: the provider's buffer is fixed-size and a getline | ||
| 261 | * chunk is not. Returns 1 when st owns a source. */ | ||
| 262 | static int hm_h2_body_src(pTHX_ hm_h2_stream *st, SV **s2, AV *hav) { | ||
| 263 | hm_bsrc bs; | ||
| 264 | UV clv = 0; | ||
| 265 | int cl_seen; | ||
| 266 | /* | ||
| 267 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 268 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 269 | if (!(s2 && SvROK(*s2)) || SvTYPE(SvRV(*s2)) == SVt_PVAV) return 0; | ||
| 270 | cl_seen = hm_hav_clen(aTHX_ hav, &clv); | ||
| 271 | memset(&bs, 0, sizeof bs); | ||
| 272 | if (!hm_bsrc_lift(aTHX_ *s2, &bs, cl_seen, clv)) return 0; | ||
| 273 | /* | ||
| 274 | # uncoverable branch true | ||
| 275 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 276 | if (bs.kind != 1) { hm_bsrc_release(aTHX_ &bs); return 0; } | ||
| 277 | st->src_sv = bs.sv; /* ownership moves */ | ||
| 278 | st->src_fd = bs.fd; | ||
| 279 | st->src_off = bs.off; | ||
| 280 | st->src_rem = bs.remaining; | ||
| 281 | st->blen = (size_t)bs.remaining; | ||
| 282 | return 1; | ||
| 283 | } | ||
| 284 | |||
| 285 | static void hm_h2_src_free(pTHX_ hm_h2_stream *st) { | ||
| 286 | hm_bsrc bs; | ||
| 287 | if (!st->src_sv) return; | ||
| 288 | memset(&bs, 0, sizeof bs); | ||
| 289 | bs.kind = 1; | ||
| 290 | bs.fd = st->src_fd; | ||
| 291 | bs.sv = st->src_sv; | ||
| 292 | hm_bsrc_release(aTHX_ &bs); | ||
| 293 | st->src_sv = NULL; | ||
| 294 | st->src_rem = 0; | ||
| 295 | } | ||
| 296 | |||
| 297 | static ssize_t hm_h2_data_read(nghttp2_session *ses, int32_t sid, uint8_t *buf, | ||
| 298 | size_t length, uint32_t *data_flags, | ||
| 299 | nghttp2_data_source *source, void *ud) { | ||
| 300 | dTHX; | ||
| 301 | hm_h2_stream *st = (hm_h2_stream *)source->ptr; | ||
| 302 | STRLEN blen = 0; | ||
| 303 | const char *bp = ""; | ||
| 304 | size_t remain, n; | ||
| 305 | (void)ses; (void)sid; (void)ud; | ||
| 306 | if (st->src_sv) { /* file source: read per window */ | ||
| 307 | size_t want = st->src_rem < length ? (size_t)st->src_rem : length; | ||
| 308 | ssize_t got = want ? hm_os_pread(st->src_fd, buf, want, st->src_off) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 309 | /* | ||
| 310 | # uncoverable branch true class:ignore_covered_err | ||
| 311 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 312 | if (got < 0) got = 0; | ||
| 313 | st->src_off += got; | ||
| 314 | st->src_rem -= (UV)got; | ||
| 315 | if (st->src_rem == 0 || got == 0) { /* # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 316 | /* spent - or truncated under us, in which case the promised | ||
| 317 | * length cannot be met and ending the stream is all there is */ | ||
| 318 | st->src_rem = 0; | ||
| 319 | *data_flags |= NGHTTP2_DATA_FLAG_EOF; | ||
| 320 | } | ||
| 321 | return (ssize_t)got; | ||
| 322 | } | ||
| 323 | /* | ||
| 324 | # uncoverable branch false class:ignore_covered_err | ||
| 325 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 326 | if (st->resp_body) bp = SvPV(st->resp_body, blen); | ||
| 327 | remain = blen - st->resp_off; | ||
| 328 | n = remain < length ? remain : length; | ||
| 329 | if (n) memcpy(buf, bp + st->resp_off, n); | ||
| 330 | st->resp_off += n; | ||
| 331 | if (st->producing) { | ||
| 332 | /* A live producer (hm_stream.h): running out of buffered bytes is | ||
| 333 | * not end of body. Drop what has gone out rather than letting the | ||
| 334 | * cursor run away for the life of the stream, then defer - nothing | ||
| 335 | * more can be said until stream_write resumes us. */ | ||
| 336 | int drained = st->resp_off >= blen; | ||
| 337 | if (drained && st->resp_body) { /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 338 | sv_setpvs(st->resp_body, ""); | ||
| 339 | st->resp_off = 0; | ||
| 340 | } | ||
| 341 | /* The producer closing is not the end of the body while frames it | ||
| 342 | * wrote are still waiting here: only the last of them carries EOF. */ | ||
| 343 | if (st->prod_done && drained) *data_flags |= NGHTTP2_DATA_FLAG_EOF; | ||
| 344 | else if (n == 0) return NGHTTP2_ERR_DEFERRED; | ||
| 345 | return (ssize_t)n; | ||
| 346 | } | ||
| 347 | if (st->resp_off >= blen) *data_flags |= NGHTTP2_DATA_FLAG_EOF; | ||
| 348 | return (ssize_t)n; | ||
| 349 | } | ||
| 350 | |||
| 351 | static int hm_h2_hopbyhop(const char *n, size_t l) { | ||
| 352 | return (l == 10 && memcmp(n, "connection", 10) == 0) | ||
| 353 | || (l == 10 && memcmp(n, "keep-alive", 10) == 0) | ||
| 354 | || (l == 16 && memcmp(n, "proxy-connection", 16) == 0) | ||
| 355 | || (l == 17 && memcmp(n, "transfer-encoding", 17) == 0) | ||
| 356 | || (l == 7 && memcmp(n, "upgrade", 7) == 0) | ||
| 357 | || (l == 2 && memcmp(n, "te", 2) == 0); | ||
| 358 | } | ||
| 359 | |||
| 360 | /* Submit status + headers with a data provider over st->resp_body. */ | ||
| 361 | static void hm_h2_submit_response(pTHX_ hm_h2_sess *s, hm_h2_stream *st, | ||
| 362 | int status, AV *hav) { | ||
| 363 | SSize_t hn = hav ? av_len(hav) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 364 | /* :status, every header pair, Date, and one more slot for Alt-Svc. The | ||
| 365 | * count has to cover every nv this function can write - it is the | ||
| 366 | * bound on a heap array being filled by a loop, and adding a header | ||
| 367 | * without widening it was once a heap overflow. */ | ||
| 368 | size_t maxnv = 3 + (size_t)(hn / 2); | ||
| 369 | nghttp2_nv *nva = (nghttp2_nv *)hm_xmalloc(maxnv * sizeof(nghttp2_nv)); | ||
| 370 | char **freelist = (char **)hm_xmalloc(maxnv * sizeof(char *)); | ||
| 371 | size_t nfree = 0, n = 0; | ||
| 372 | char sbuf[8]; | ||
| 373 | int sl, has_date = 0; | ||
| 374 | nghttp2_data_provider prd; | ||
| 375 | SSize_t i; | ||
| 376 | |||
| 377 | /* A status outside three digits is the application's bug; it must not | ||
| 378 | * become an over-read of sbuf, which is exactly 8 bytes. */ | ||
| 379 | /* | ||
| 380 | # uncoverable branch true class:ignore_covered_err | ||
| 381 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 382 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 383 | if (status < 100 || status > 999) status = 500; | ||
| 384 | sl = snprintf(sbuf, sizeof(sbuf), "%d", status); | ||
| 385 | nva[n].name = (uint8_t *)":status"; nva[n].namelen = 7; | ||
| 386 | nva[n].value = (uint8_t *)sbuf; nva[n].valuelen = (size_t)sl; | ||
| 387 | nva[n].flags = NGHTTP2_NV_FLAG_NONE; n++; | ||
| 388 | |||
| 389 | for (i = 0; i + 1 < hn; i += 2) { | ||
| 390 | SV **k = av_fetch(hav, i, 0); | ||
| 391 | SV **v = av_fetch(hav, i + 1, 0); | ||
| 392 | STRLEN kl, vl; const char *ks, *vs; | ||
| 393 | char *lname; STRLEN j; | ||
| 394 | /* | ||
| 395 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 396 | # uncoverable condition left count:2 class:ignore_covered_err | ||
| 397 | # uncoverable condition left count:3 class:ignore_covered_err | ||
| 398 | # uncoverable condition left count:4 class:ignore_covered_err */ | ||
| 399 | if (!k || !*k || !v || !*v) continue; | ||
| 400 | ks = SvPV(*k, kl); vs = SvPV(*v, vl); | ||
| 401 | lname = (char *)hm_xmalloc(kl ? kl : 1); /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 402 | for (j = 0; j < kl; j++) { | ||
| 403 | unsigned char ch = (unsigned char)ks[j]; | ||
| 404 | lname[j] = (ch >= 'A' && ch <= 'Z') ? (char)(ch + 32) : (char)ch; | ||
| 405 | } | ||
| 406 | if (hm_h2_hopbyhop(lname, kl)) { free(lname); continue; } | ||
| 407 | /* | ||
| 408 | # uncoverable branch true class:ignore_covered_err | ||
| 409 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 410 | # uncoverable condition left count:2 class:ignore_covered_err | ||
| 411 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 412 | if (kl == 4 && memcmp(lname, "date", 4) == 0) has_date = 1; | ||
| 413 | freelist[nfree++] = lname; | ||
| 414 | nva[n].name = (uint8_t *)lname; nva[n].namelen = kl; | ||
| 415 | nva[n].value = (uint8_t *)vs; nva[n].valuelen = vl; | ||
| 416 | nva[n].flags = NGHTTP2_NV_FLAG_NONE; n++; | ||
| 417 | } | ||
| 418 | /* | ||
| 419 | # uncoverable branch false class:ignore_covered_err | ||
| 420 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 421 | if (!has_date) { | ||
| 422 | const char *dh = hm_date_header(s->conn->loop); | ||
| 423 | /* | ||
| 424 | # uncoverable branch false class:ignore_covered_err | ||
| 425 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 426 | if (dh) { | ||
| 427 | nva[n].name = (uint8_t *)"date"; nva[n].namelen = 4; | ||
| 428 | nva[n].value = (uint8_t *)(dh + 6); /* past "Date: " */ | ||
| 429 | nva[n].valuelen = HM_DATE_LEN - 8; /* ... and CRLF */ | ||
| 430 | nva[n].flags = NGHTTP2_NV_FLAG_NONE; n++; | ||
| 431 | } | ||
| 432 | } | ||
| 433 | |||
| 434 | /* Alt-Svc, the same advertisement the HTTP/1.1 path makes: an h2 client | ||
| 435 | * on this origin is told it can use HTTP/3 next time. Precomputed per | ||
| 436 | * listener, so this is a pointer and not an snprintf per response. */ | ||
| 437 | /* | ||
| 438 | # uncoverable branch true class:ignore_covered_err | ||
| 439 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 440 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 441 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 442 | if (s->conn->lst && s->conn->lst->altsvc) { | ||
| 443 | nva[n].name = (uint8_t *)"alt-svc"; nva[n].namelen = 7; /* # uncoverable statement class:ignore_covered_err */ | ||
| 444 | nva[n].value = (uint8_t *)s->conn->lst->altsvc; /* # uncoverable statement class:ignore_covered_err */ | ||
| 445 | nva[n].valuelen = strlen(s->conn->lst->altsvc); /* # uncoverable statement class:ignore_covered_err */ | ||
| 446 | nva[n].flags = NGHTTP2_NV_FLAG_NONE; n++; /* # uncoverable statement class:ignore_covered_err */ | ||
| 447 | } | ||
| 448 | st->status = status; | ||
| 449 | st->resp_off = 0; | ||
| 450 | prd.source.ptr = st; | ||
| 451 | prd.read_callback = hm_h2_data_read; | ||
| 452 | /* | ||
| 453 | # uncoverable branch true | ||
| 454 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 455 | if (nghttp2_submit_response(s->session, st->id, nva, n, &prd) != 0) { | ||
| 456 | /* refused (a closed stream, an invalid field): the client gets a | ||
| 457 | * reset rather than silence, and the bytes are not counted */ | ||
| 458 | nghttp2_submit_rst_stream(s->session, NGHTTP2_FLAG_NONE, st->id, /* # uncoverable statement */ | ||
| 459 | NGHTTP2_INTERNAL_ERROR); | ||
| 460 | } else { | ||
| 461 | s->conn->loop->bytes_out += st->blen; | ||
| 462 | } | ||
| 463 | |||
| 464 | for (n = 0; n < nfree; n++) free(freelist[n]); | ||
| 465 | free(freelist); | ||
| 466 | free(nva); | ||
| 467 | } | ||
| 468 | |||
| 469 | static void hm_h2_submit_status(pTHX_ hm_h2_sess *s, hm_h2_stream *st, | ||
| 470 | int status, const char *ctype) { | ||
| 471 | AV *hav = newAV(); | ||
| 472 | av_push(hav, newSVpvs("content-type")); | ||
| 473 | av_push(hav, newSVpv(ctype, 0)); | ||
| 474 | hm_h2_submit_response(aTHX_ s, st, status, hav); | ||
| 475 | SvREFCNT_dec((SV *)hav); | ||
| 476 | } | ||
| 477 | |||
| 478 | /* psgi.streaming responder for h2: 3-arg delivers, 2-arg buffers via a | ||
| 479 | * Hyperman::Writer (h2 form) that submits at close. */ | ||
| 480 | XS_INTERNAL(hm_xs_h2responder); | ||
| 481 | XS_INTERNAL(hm_xs_h2responder) { | ||
| 482 | dXSARGS; | ||
| 483 | hm_clos *cl = hm_clos_of(aTHX_ cv); | ||
| 484 | hm_loop *loop = hm_cur_loop; | ||
| 485 | hm_conn *c; | ||
| 486 | hm_h2_sess *s; | ||
| 487 | hm_h2_stream *st; | ||
| 488 | AV *rav, *hav = NULL; | ||
| 489 | SSize_t n; | ||
| 490 | SV **s0, **s1; | ||
| 491 | int fd, status; | ||
| 492 | UV gen; | ||
| 493 | int32_t sid; | ||
| 494 | /* | ||
| 495 | # uncoverable branch true class:ignore_covered_err | ||
| 496 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 497 | if (!cl) XSRETURN_EMPTY; | ||
| 498 | fd = (int)cl->i; gen = cl->u; sid = (int32_t)SvIV(cl->d); | ||
| 499 | /* | ||
| 500 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 501 | # uncoverable condition right count:2 class:ignore_covered_err | ||
| 502 | # uncoverable condition right count:3 class:ignore_covered_err */ | ||
| 503 | c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; | ||
| 504 | /* | ||
| 505 | # uncoverable branch true class:ignore_covered_err | ||
| 506 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 507 | # uncoverable condition left count:2 class:ignore_covered_err | ||
| 508 | # uncoverable condition left count:3 class:ignore_covered_err */ | ||
| 509 | if (!(c && c->id == gen && c->h2)) XSRETURN_EMPTY; | ||
| 510 | s = (hm_h2_sess *)c->h2; /* # uncoverable statement class:ignore_covered_err */ | ||
| 511 | st = (hm_h2_stream *)nghttp2_session_get_stream_user_data(s->session, sid); /* # uncoverable statement class:ignore_covered_err */ | ||
| 512 | /* | ||
| 513 | # uncoverable statement class:ignore_covered_err | ||
| 514 | # uncoverable branch true | ||
| 515 | # uncoverable condition left count:1 */ | ||
| 516 | if (!st) XSRETURN_EMPTY; | ||
| 517 | /* answered already, or a writer is open on it: one response per stream */ | ||
| 518 | /* | ||
| 519 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 520 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 521 | if (st->status || st->producing || st->resp_headers) | ||
| 522 | croak("Hyperman responder: a response has already been started " | ||
| 523 | "on this stream"); | ||
| 524 | /* | ||
| 525 | # uncoverable branch true class:ignore_covered_err | ||
| 526 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 527 | # uncoverable condition left count:2 class:ignore_covered_err | ||
| 528 | # uncoverable condition left count:3 class:ignore_covered_err */ | ||
| 529 | if (items < 1 || !(SvROK(ST(0)) && SvTYPE(SvRV(ST(0))) == SVt_PVAV)) | ||
| 530 | croak("Hyperman responder expects an array reference"); /* # uncoverable statement */ | ||
| 531 | rav = (AV *)SvRV(ST(0)); | ||
| 532 | n = av_len(rav) + 1; | ||
| 533 | s0 = av_fetch(rav, 0, 0); | ||
| 534 | s1 = av_fetch(rav, 1, 0); | ||
| 535 | status = s0 ? (int)SvIV(*s0) : 200; /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 536 | /* | ||
| 537 | # uncoverable branch false class:ignore_covered_err | ||
| 538 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 539 | # uncoverable condition right count:2 class:ignore_covered_err | ||
| 540 | # uncoverable condition right count:3 class:ignore_covered_err */ | ||
| 541 | if (s1 && SvROK(*s1) && SvTYPE(SvRV(*s1)) == SVt_PVAV) hav = (AV *)SvRV(*s1); | ||
| 542 | |||
| 543 | if (n >= 3) { | ||
| 544 | SV **s2 = av_fetch(rav, 2, 0); | ||
| 545 | /* | ||
| 546 | # uncoverable branch true class:ignore_covered_err | ||
| 547 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 548 | if (st->resp_body) { SvREFCNT_dec(st->resp_body); st->resp_body = NULL; } | ||
| 549 | hm_h2_src_free(aTHX_ st); | ||
| 550 | /* | ||
| 551 | # uncoverable branch false class:ignore_covered_err | ||
| 552 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 553 | if (!hm_h2_body_src(aTHX_ st, s2, hav)) | ||
| 554 | st->resp_body = hm_h2_body_sv(aTHX_ s2, &st->blen); | ||
| 555 | hm_h2_submit_response(aTHX_ s, st, status, hav); | ||
| 556 | hm_h2_flush_send(aTHX_ s); | ||
| 557 | XSRETURN_EMPTY; | ||
| 558 | } | ||
| 559 | /* 2-arg: begin a streamed body, return a writer */ | ||
| 560 | st->resp_status = status; | ||
| 561 | /* | ||
| 562 | # uncoverable branch false class:ignore_covered_err | ||
| 563 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 564 | if (hav) st->resp_headers = newRV_inc((SV *)hav); | ||
| 565 | /* | ||
| 566 | # uncoverable branch false class:ignore_covered_err | ||
| 567 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 568 | if (!st->resp_body) st->resp_body = newSVpvs(""); | ||
| 569 | { | ||
| 570 | AV *w = newAV(); | ||
| 571 | SV *wrv; | ||
| 572 | av_push(w, newSViv(fd)); | ||
| 573 | av_push(w, newSVuv(gen)); | ||
| 574 | av_push(w, newSViv(sid)); | ||
| 575 | av_push(w, newSVpvs("h2")); | ||
| 576 | wrv = newRV_noinc((SV *)w); | ||
| 577 | sv_bless(wrv, gv_stashpv("Hyperman::Writer", GV_ADD)); | ||
| 578 | ST(0) = sv_2mortal(wrv); | ||
| 579 | XSRETURN(1); | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | static void hm_h2_run_delayed(pTHX_ hm_h2_sess *s, hm_h2_stream *st, SV *code) { | ||
| 584 | SV *responder = hm_closure(aTHX_ hm_xs_h2responder, NULL, NULL, NULL, | ||
| 585 | sv_2mortal(newSViv(st->id)), | ||
| 586 | (IV)s->conn->fd, s->conn->id); | ||
| 587 | int32_t sid = st->id; | ||
| 588 | dSP; | ||
| 589 | ENTER; SAVETMPS; | ||
| 590 | PUSHMARK(SP); | ||
| 591 | XPUSHs(responder); | ||
| 592 | PUTBACK; | ||
| 593 | call_sv(code, G_DISCARD | G_EVAL); | ||
| 594 | /* The coderef may have waited, and the stream may have been reset | ||
| 595 | * meanwhile: look it up again. A 500 only for a stream that has not | ||
| 596 | * answered; one that died with a handle still producing is RESET, the | ||
| 597 | * h2 spelling of the HTTP/1 close without the terminating chunk. */ | ||
| 598 | st = s->session | ||
| 599 | ? (hm_h2_stream *)nghttp2_session_get_stream_user_data(s->session, sid) | ||
| 600 | : NULL; | ||
| 601 | if (SvTRUE(ERRSV) && st) { /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 602 | if (st->producing) { | ||
| 603 | hm_warn_died(aTHX_ "streaming handler"); | ||
| 604 | nghttp2_submit_rst_stream(s->session, NGHTTP2_FLAG_NONE, sid, | ||
| 605 | NGHTTP2_INTERNAL_ERROR); | ||
| 606 | /* | ||
| 607 | # uncoverable branch false class:ignore_covered_err | ||
| 608 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 609 | } else if (!st->status) { | ||
| 610 | /* | ||
| 611 | # uncoverable branch true class:ignore_covered_err | ||
| 612 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 613 | if (st->resp_body) SvREFCNT_dec(st->resp_body); | ||
| 614 | st->resp_body = newSVpvs("Internal Server Error"); | ||
| 615 | st->blen = 21; | ||
| 616 | hm_h2_submit_status(aTHX_ s, st, 500, "text/plain"); | ||
| 617 | } | ||
| 618 | } | ||
| 619 | FREETMPS; LEAVE; | ||
| 620 | SvREFCNT_dec(responder); | ||
| 621 | } | ||
| 622 | |||
| 623 | /* Writer (h2 form) called from xs/writer.xs. */ | ||
| 624 | /* 1 buffered, -1 stale (the stream is gone, or already closed: this shape | ||
| 625 | * buffers to close, so a write after it has nowhere to go). */ | ||
| 626 | static int hm_h2_writer_write(pTHX_ int fd, UV gen, int32_t sid, SV *data) { | ||
| 627 | hm_loop *loop = hm_cur_loop; | ||
| 628 | /* | ||
| 629 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 630 | # uncoverable condition right count:2 class:ignore_covered_err | ||
| 631 | # uncoverable condition right count:3 class:ignore_covered_err */ | ||
| 632 | hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; | ||
| 633 | hm_h2_sess *s; | ||
| 634 | hm_h2_stream *st; | ||
| 635 | /* | ||
| 636 | # uncoverable branch true class:ignore_covered_err | ||
| 637 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 638 | # uncoverable condition left count:2 class:ignore_covered_err | ||
| 639 | # uncoverable condition left count:3 class:ignore_covered_err */ | ||
| 640 | if (!(c && c->id == gen && c->h2)) return -1; | ||
| 641 | s = (hm_h2_sess *)c->h2; | ||
| 642 | /* | ||
| 643 | # uncoverable branch true | ||
| 644 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 645 | if (!s->session) return -1; | ||
| 646 | st = (hm_h2_stream *)nghttp2_session_get_stream_user_data(s->session, sid); | ||
| 647 | /* | ||
| 648 | # uncoverable branch true class:ignore_covered_err | ||
| 649 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 650 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 651 | if (!st || st->status) return -1; | ||
| 652 | /* | ||
| 653 | # uncoverable branch true class:ignore_covered_err | ||
| 654 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 655 | if (!st->resp_body) st->resp_body = newSVpvs(""); | ||
| 656 | sv_catsv(st->resp_body, data); | ||
| 657 | return 1; | ||
| 658 | } | ||
| 659 | |||
| 660 | static void hm_h2_writer_close(pTHX_ int fd, UV gen, int32_t sid) { | ||
| 661 | hm_loop *loop = hm_cur_loop; | ||
| 662 | /* | ||
| 663 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 664 | # uncoverable condition right count:2 class:ignore_covered_err | ||
| 665 | # uncoverable condition right count:3 class:ignore_covered_err */ | ||
| 666 | hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; | ||
| 667 | hm_h2_sess *s; | ||
| 668 | hm_h2_stream *st; | ||
| 669 | AV *hav; | ||
| 670 | /* | ||
| 671 | # uncoverable branch true class:ignore_covered_err | ||
| 672 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 673 | # uncoverable condition left count:2 class:ignore_covered_err | ||
| 674 | # uncoverable condition left count:3 class:ignore_covered_err */ | ||
| 675 | if (!(c && c->id == gen && c->h2)) return; | ||
| 676 | s = (hm_h2_sess *)c->h2; | ||
| 677 | /* | ||
| 678 | # uncoverable branch true | ||
| 679 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 680 | if (!s->session) return; | ||
| 681 | st = (hm_h2_stream *)nghttp2_session_get_stream_user_data(s->session, sid); | ||
| 682 | /* a second close resubmitted the response from offset 0 and overran | ||
| 683 | * its content-length; status is set by the first submit */ | ||
| 684 | /* | ||
| 685 | # uncoverable branch true class:ignore_covered_err | ||
| 686 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 687 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 688 | if (!st || st->status) return; | ||
| 689 | /* | ||
| 690 | # uncoverable branch false class:ignore_covered_err | ||
| 691 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 692 | { STRLEN l; if (st->resp_body) { (void)SvPV(st->resp_body, l); st->blen = l; } } | ||
| 693 | hav = (st->resp_headers && SvROK(st->resp_headers)) | ||
| 694 | ? (AV *)SvRV(st->resp_headers) : NULL; | ||
| 695 | hm_h2_submit_response(aTHX_ s, st, st->resp_status ? st->resp_status : 200, hav); /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 696 | hm_h2_flush_send(aTHX_ s); | ||
| 697 | } | ||
| 698 | |||
| 699 | /* sync array / streaming CV / invalid -> a response */ | ||
| 700 | static void hm_h2_respond(pTHX_ hm_h2_sess *s, hm_h2_stream *st, SV *resp) { | ||
| 701 | /* | ||
| 702 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 703 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 704 | if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV) { | ||
| 705 | AV *rav = (AV *)SvRV(resp); | ||
| 706 | SV **s0 = av_fetch(rav, 0, 0); | ||
| 707 | SV **s1 = av_fetch(rav, 1, 0); | ||
| 708 | SV **s2 = av_fetch(rav, 2, 0); | ||
| 709 | int status = s0 ? (int)SvIV(*s0) : 200; /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 710 | AV *hav = (s1 && SvROK(*s1) && SvTYPE(SvRV(*s1)) == SVt_PVAV) | ||
| 711 | ? (AV *)SvRV(*s1) : NULL; | ||
| 712 | /* | ||
| 713 | # uncoverable branch true class:ignore_covered_err | ||
| 714 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 715 | if (st->resp_body) { SvREFCNT_dec(st->resp_body); st->resp_body = NULL; } | ||
| 716 | hm_h2_src_free(aTHX_ st); | ||
| 717 | if (!hm_h2_body_src(aTHX_ st, s2, hav)) | ||
| 718 | st->resp_body = hm_h2_body_sv(aTHX_ s2, &st->blen); | ||
| 719 | hm_h2_submit_response(aTHX_ s, st, status, hav); | ||
| 720 | /* | ||
| 721 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 722 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 723 | } else if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVCV) { | ||
| 724 | hm_h2_run_delayed(aTHX_ s, st, resp); | ||
| 725 | } else { | ||
| 726 | /* | ||
| 727 | # uncoverable statement class:ignore_covered_err | ||
| 728 | # uncoverable branch true | ||
| 729 | # uncoverable condition left count:1 */ | ||
| 730 | if (st->resp_body) SvREFCNT_dec(st->resp_body); | ||
| 731 | st->resp_body = newSVpvs("Internal Server Error"); | ||
| 732 | st->blen = 21; | ||
| 733 | hm_h2_submit_status(aTHX_ s, st, 500, "text/plain"); | ||
| 734 | } | ||
| 735 | } | ||
| 736 | |||
| 737 | /* Deliver a parked async response to a stream (from the Future continuation). */ | ||
| 738 | static void hm_h2_deliver(pTHX_ int fd, UV gen, int32_t sid, SV *resp) { | ||
| 739 | hm_loop *loop = hm_cur_loop; | ||
| 740 | /* | ||
| 741 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 742 | # uncoverable condition right count:2 class:ignore_covered_err | ||
| 743 | # uncoverable condition right count:3 class:ignore_covered_err */ | ||
| 744 | hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; | ||
| 745 | hm_h2_sess *s; | ||
| 746 | hm_h2_stream *st; | ||
| 747 | /* | ||
| 748 | # uncoverable branch true class:ignore_covered_err | ||
| 749 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 750 | # uncoverable condition left count:2 class:ignore_covered_err | ||
| 751 | # uncoverable condition left count:3 class:ignore_covered_err */ | ||
| 752 | if (!(c && c->id == gen && c->h2)) return; | ||
| 753 | s = (hm_h2_sess *)c->h2; | ||
| 754 | /* | ||
| 755 | # uncoverable branch true | ||
| 756 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 757 | if (!s->session) return; | ||
| 758 | st = (hm_h2_stream *)nghttp2_session_get_stream_user_data(s->session, sid); | ||
| 759 | if (!st) return; | ||
| 760 | st->awaiting = 0; | ||
| 761 | /* | ||
| 762 | # uncoverable branch false class:ignore_covered_err | ||
| 763 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 764 | if (st->resp_f) { SvREFCNT_dec(st->resp_f); st->resp_f = NULL; } | ||
| 765 | /* A stream handle answered this stream, or is still producing its body | ||
| 766 | * (the documented "open a handle after parking" pattern), and the | ||
| 767 | * Future then resolved: responding here freed the producer's buffer | ||
| 768 | * under it and submitted a second HEADERS. The handle owns the | ||
| 769 | * response; the Future's value is dropped, as hm_deliver drops it. */ | ||
| 770 | /* | ||
| 771 | # uncoverable branch true class:ignore_covered_err | ||
| 772 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 773 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 774 | if (st->producing || st->status) return; | ||
| 775 | hm_h2_respond(aTHX_ s, st, resp); | ||
| 776 | if (hm_logging(loop) && st->env_rv) | ||
| 777 | hm_access_log(aTHX_ loop, st->env_rv, st->status, (ssize_t)st->blen); | ||
| 778 | hm_h2_flush_send(aTHX_ s); | ||
| 779 | } | ||
| 780 | |||
| 781 | XS_INTERNAL(hm_xs_h2park_cb); | ||
| 782 | XS_INTERNAL(hm_xs_h2park_cb) { | ||
| 783 | dXSARGS; | ||
| 784 | hm_clos *cl = hm_clos_of(aTHX_ cv); | ||
| 785 | SV *f, *resp = NULL; | ||
| 786 | IV st; | ||
| 787 | int fd; UV gen; int32_t sid; | ||
| 788 | /* | ||
| 789 | # uncoverable branch true class:ignore_covered_err | ||
| 790 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 791 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 792 | if (!cl || items < 1) XSRETURN_EMPTY; | ||
| 793 | f = ST(0); | ||
| 794 | fd = (int)cl->i; gen = cl->u; sid = (int32_t)SvIV(cl->d); | ||
| 795 | st = hm_any_state(aTHX_ f); | ||
| 796 | if (st == HMF_DONE) { | ||
| 797 | if (hmf_is_future(aTHX_ f)) { | ||
| 798 | AV *vals = hmf_values_av(aTHX_ f); | ||
| 799 | /* | ||
| 800 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 801 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 802 | SV **e = (vals && av_len(vals) >= 0) ? av_fetch(vals, 0, 0) : NULL; | ||
| 803 | resp = e ? sv_mortalcopy(*e) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 804 | } else { | ||
| 805 | dSP; | ||
| 806 | SV *got = NULL; | ||
| 807 | int n; | ||
| 808 | ENTER; SAVETMPS; PUSHMARK(SP); | ||
| 809 | XPUSHs(f); | ||
| 810 | PUTBACK; | ||
| 811 | n = call_method("get", G_SCALAR | G_EVAL); | ||
| 812 | SPAGAIN; | ||
| 813 | if (n && !SvTRUE(ERRSV)) got = newSVsv(POPs); /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 814 | /* | ||
| 815 | # uncoverable branch false class:ignore_covered_err | ||
| 816 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 817 | else if (n) (void)POPs; | ||
| 818 | PUTBACK; FREETMPS; LEAVE; | ||
| 819 | if (got) resp = sv_2mortal(got); | ||
| 820 | } | ||
| 821 | } else if (st == HMF_FAILED) { | ||
| 822 | AV *errs = newAV(); | ||
| 823 | SV **e; | ||
| 824 | STRLEN l; | ||
| 825 | const char *msg = "unknown error"; | ||
| 826 | hm_any_values(aTHX_ f, st, errs); | ||
| 827 | e = av_len(errs) >= 0 ? av_fetch(errs, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 828 | /* | ||
| 829 | # uncoverable branch false class:ignore_covered_err | ||
| 830 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 831 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 832 | if (e && SvOK(*e)) msg = SvPV(*e, l); | ||
| 833 | hm_errlog("Hyperman: async response failed: %s%s", | ||
| 834 | /* | ||
| 835 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 836 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 837 | msg, (*msg && msg[strlen(msg)-1] == '\n') ? "" : "\n"); | ||
| 838 | SvREFCNT_dec((SV *)errs); | ||
| 839 | } | ||
| 840 | if (!(resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) /* # uncoverable condition left count:3 class:ignore_covered_err */ | ||
| 841 | resp = hm_500_resp(aTHX_ st == HMF_DONE | ||
| 842 | ? "Bad async response" : "Internal Server Error"); | ||
| 843 | hm_h2_deliver(aTHX_ fd, gen, sid, resp); | ||
| 844 | XSRETURN_EMPTY; | ||
| 845 | } | ||
| 846 | |||
| 847 | /* ---- dispatch (a request stream is complete) ---------------------------- */ | ||
| 848 | |||
| 849 | static void hm_h2_dispatch(pTHX_ hm_h2_sess *s, hm_h2_stream *st) { | ||
| 850 | hm_conn *c = s->conn; | ||
| 851 | hm_loop *loop = c->loop; | ||
| 852 | HV *env = st->env; | ||
| 853 | SV *env_rv, *resp; | ||
| 854 | st->env = NULL; | ||
| 855 | /* the h2c-upgrade stream arrives with psgi.input already set (built from | ||
| 856 | * the original HTTP/1.1 request); normal streams get it from the body */ | ||
| 857 | if (!hv_exists(env, "psgi.input", 10)) { | ||
| 858 | if (st->body) | ||
| 859 | hv_stores(env, "psgi.input", | ||
| 860 | hm_new_input(aTHX_ SvPVX(st->body), SvCUR(st->body))); | ||
| 861 | else | ||
| 862 | hv_stores(env, "psgi.input", hm_empty_input_sv(aTHX)); | ||
| 863 | } | ||
| 864 | /* psgix.hyperman.stream: [fd, connection generation, stream id] - the | ||
| 865 | * ticket ABI v6's stream_open takes. Deliberately not published as | ||
| 866 | * psgix.hyperman.conn, which names an fd an application may take over | ||
| 867 | * and which h2 has nothing to put behind it. */ | ||
| 868 | { | ||
| 869 | AV *tick = newAV(); | ||
| 870 | av_extend(tick, 2); | ||
| 871 | av_store(tick, 0, newSViv(c->fd)); | ||
| 872 | av_store(tick, 1, newSVuv(c->id)); | ||
| 873 | av_store(tick, 2, newSViv(st->id)); | ||
| 874 | hv_stores(env, "psgix.hyperman.stream", newRV_noinc((SV *)tick)); | ||
| 875 | if (c->proxy_addr[0]) | ||
| 876 | hv_stores(env, "psgix.proxy_addr", newSVpv(c->proxy_addr, 0)); | ||
| 877 | } | ||
| 878 | env_rv = newRV_noinc((SV *)env); | ||
| 879 | { | ||
| 880 | int32_t sid = st->id; | ||
| 881 | st->in_app = 1; | ||
| 882 | resp = hm_call_app(aTHX_ loop, env_rv); | ||
| 883 | hm_count_request(loop); | ||
| 884 | /* An application that waited (a ->get, a drain) pumped this loop, | ||
| 885 | * and the loop read this connection: the peer may have reset the | ||
| 886 | * stream, which freed st. Look it up again rather than trust the | ||
| 887 | * pointer the call went in with. */ | ||
| 888 | st = s->session | ||
| 889 | ? (hm_h2_stream *)nghttp2_session_get_stream_user_data(s->session, | ||
| 890 | sid) | ||
| 891 | : NULL; | ||
| 892 | if (!st) { | ||
| 893 | /* | ||
| 894 | # uncoverable branch false class:ignore_covered_err | ||
| 895 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 896 | if (resp) SvREFCNT_dec(resp); | ||
| 897 | SvREFCNT_dec(env_rv); | ||
| 898 | return; | ||
| 899 | } | ||
| 900 | st->in_app = 0; | ||
| 901 | } | ||
| 902 | |||
| 903 | if (resp && hm_is_awaitable(aTHX_ resp)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 904 | SV *cb = hm_closure(aTHX_ hm_xs_h2park_cb, NULL, NULL, NULL, | ||
| 905 | sv_2mortal(newSViv(st->id)), (IV)c->fd, c->id); | ||
| 906 | st->awaiting = 1; | ||
| 907 | st->env_rv = SvREFCNT_inc(env_rv); | ||
| 908 | st->resp_f = SvREFCNT_inc(resp); /* cancelled on RST_STREAM */ | ||
| 909 | hm_any_on_ready(aTHX_ resp, cb); | ||
| 910 | SvREFCNT_dec(cb); | ||
| 911 | SvREFCNT_dec(resp); | ||
| 912 | SvREFCNT_dec(env_rv); | ||
| 913 | return; | ||
| 914 | } | ||
| 915 | |||
| 916 | /* A stream handle already sent this stream's response - the application | ||
| 917 | * answered through the seam and its return value is the sentinel that | ||
| 918 | * goes with it, the way [101,[],[]] is on the detach path. Submitting it | ||
| 919 | * as well would put two responses on one stream, which is a protocol | ||
| 920 | * error the client sees and the server does not. */ | ||
| 921 | /* | ||
| 922 | # uncoverable branch true | ||
| 923 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 924 | if (st->producing) { | ||
| 925 | /* | ||
| 926 | # uncoverable statement | ||
| 927 | # uncoverable branch true | ||
| 928 | # uncoverable branch false | ||
| 929 | # uncoverable condition left count:1 | ||
| 930 | # uncoverable condition right count:1 */ | ||
| 931 | if (resp) SvREFCNT_dec(resp); | ||
| 932 | SvREFCNT_dec(env_rv); /* # uncoverable statement */ | ||
| 933 | /* An Extended CONNECT stream whose first DATA came in the same read | ||
| 934 | * as its headers: nghttp2 parsed it before this dispatch, with no | ||
| 935 | * handle to take it, so it was buffered as the body. Handed over | ||
| 936 | * now that the handler has returned and has had the chance to wire | ||
| 937 | * its events, as hm_process does for an HTTP/1.1 tunnel opened | ||
| 938 | * inside the handler. */ | ||
| 939 | /* | ||
| 940 | # uncoverable statement | ||
| 941 | # uncoverable branch true | ||
| 942 | # uncoverable branch false | ||
| 943 | # uncoverable condition left count:1 | ||
| 944 | # uncoverable condition right count:1 | ||
| 945 | # uncoverable condition left count:2 | ||
| 946 | # uncoverable condition right count:2 | ||
| 947 | # uncoverable condition left count:3 | ||
| 948 | # uncoverable condition right count:3 */ | ||
| 949 | if (st->tunnel && st->body && SvCUR(st->body)) { | ||
| 950 | SV *b = st->body; /* # uncoverable statement */ | ||
| 951 | st->body = NULL; /* # uncoverable statement */ | ||
| 952 | /* | ||
| 953 | # uncoverable statement | ||
| 954 | # uncoverable branch true | ||
| 955 | # uncoverable branch false | ||
| 956 | # uncoverable condition left count:1 | ||
| 957 | # uncoverable condition right count:1 */ | ||
| 958 | if (!hm_stream_deliver(aTHX_ c->fd, c->id, (int64_t)st->id, | ||
| 959 | SvPVX(b), SvCUR(b), 0)) { /* # uncoverable statement */ | ||
| 960 | st->body = b; /* no read half yet: kept for it */ /* # uncoverable statement */ | ||
| 961 | b = NULL; /* # uncoverable statement */ | ||
| 962 | } | ||
| 963 | /* | ||
| 964 | # uncoverable statement | ||
| 965 | # uncoverable branch true | ||
| 966 | # uncoverable branch false | ||
| 967 | # uncoverable condition left count:1 | ||
| 968 | # uncoverable condition right count:1 */ | ||
| 969 | if (b) SvREFCNT_dec(b); | ||
| 970 | } | ||
| 971 | return; /* # uncoverable statement */ | ||
| 972 | } | ||
| 973 | hm_h2_respond(aTHX_ s, st, resp); | ||
| 974 | if (hm_logging(loop)) | ||
| 975 | hm_access_log(aTHX_ loop, env_rv, st->status, (ssize_t)st->blen); | ||
| 976 | /* | ||
| 977 | # uncoverable branch false class:ignore_covered_err | ||
| 978 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 979 | if (resp) SvREFCNT_dec(resp); | ||
| 980 | SvREFCNT_dec(env_rv); | ||
| 981 | } | ||
| 982 | |||
| 983 | /* ---- nghttp2 callbacks --------------------------------------------------- */ | ||
| 984 | |||
| 985 | static ssize_t hm_h2_cb_send(nghttp2_session *ses, const uint8_t *data, | ||
| 986 | size_t length, int flags, void *ud) { | ||
| 987 | dTHX; | ||
| 988 | hm_h2_sess *s = (hm_h2_sess *)ud; | ||
| 989 | hm_conn *c = s->conn; | ||
| 990 | (void)ses; (void)flags; | ||
| 991 | /* Backpressure. nghttp2 keeps producing for as long as the peer's flow | ||
| 992 | * window allows, and a peer that opens its windows wide and then stops | ||
| 993 | * reading would have every file it asked for read into this buffer. | ||
| 994 | * Past the high-water mark the frame is left with nghttp2; the drain | ||
| 995 | * (hm_flush, backlogged) re-queues the connection and session_send | ||
| 996 | * picks it up where it stopped. */ | ||
| 997 | if (c->wlen - c->woff > HM_WB_HIWAT) { | ||
| 998 | c->backlogged = 1; | ||
| 999 | return NGHTTP2_ERR_WOULDBLOCK; | ||
| 1000 | } | ||
| 1001 | hm_wb_put(c, (const char *)data, length); | ||
| 1002 | return (ssize_t)length; | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | static int hm_h2_cb_begin_headers(nghttp2_session *ses, | ||
| 1006 | const nghttp2_frame *frame, void *ud) { | ||
| 1007 | dTHX; | ||
| 1008 | hm_h2_sess *s = (hm_h2_sess *)ud; | ||
| 1009 | hm_h2_stream *st; | ||
| 1010 | if (frame->hd.type != NGHTTP2_HEADERS /* # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1011 | || frame->headers.cat != NGHTTP2_HCAT_REQUEST) | ||
| 1012 | return 0; | ||
| 1013 | st = (hm_h2_stream *)hm_xcalloc(1, sizeof(hm_h2_stream)); | ||
| 1014 | st->id = frame->hd.stream_id; | ||
| 1015 | st->env = newHV(); | ||
| 1016 | /* pre-size past the ~25 fixed keys plus typical request headers, so no | ||
| 1017 | * request pays the 8 -> 16 -> 32 bucket splits (see hm_build_env) */ | ||
| 1018 | hv_ksplit(st->env, 64); | ||
| 1019 | hm_h2_env_init(aTHX_ s, st->env); | ||
| 1020 | st->next = s->streams; | ||
| 1021 | s->streams = st; | ||
| 1022 | nghttp2_session_set_stream_user_data(ses, st->id, st); | ||
| 1023 | return 0; | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | static int hm_h2_cb_header(nghttp2_session *ses, const nghttp2_frame *frame, | ||
| 1027 | const uint8_t *name, size_t namelen, | ||
| 1028 | const uint8_t *value, size_t valuelen, | ||
| 1029 | uint8_t flags, void *ud) { | ||
| 1030 | dTHX; | ||
| 1031 | hm_h2_stream *st; | ||
| 1032 | (void)ud; (void)flags; | ||
| 1033 | st = (hm_h2_stream *)nghttp2_session_get_stream_user_data( | ||
| 1034 | ses, frame->hd.stream_id); | ||
| 1035 | /* | ||
| 1036 | # uncoverable branch false class:ignore_covered_err | ||
| 1037 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 1038 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 1039 | if (st && st->env) { | ||
| 1040 | /* HPACK expands: a 4KB dynamic-table entry costs one byte on the | ||
| 1041 | * wire each time it is referenced, so the wire size bounds nothing. | ||
| 1042 | * Count the decoded field block and reset the stream past the | ||
| 1043 | * advertised ceiling, before it is joined into $env. */ | ||
| 1044 | st->hdr_bytes += namelen + valuelen + 32; | ||
| 1045 | /* | ||
| 1046 | # uncoverable branch true | ||
| 1047 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1048 | if (st->hdr_bytes > HM_H2_MAX_HEADER_LIST) | ||
| 1049 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; /* # uncoverable statement */ | ||
| 1050 | hm_h2_add_header(aTHX_ st->env, (const char *)name, namelen, | ||
| 1051 | (const char *)value, valuelen); | ||
| 1052 | } | ||
| 1053 | return 0; | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | static int hm_h2_cb_data_chunk(nghttp2_session *ses, uint8_t flags, | ||
| 1057 | int32_t sid, const uint8_t *data, | ||
| 1058 | size_t len, void *ud) { | ||
| 1059 | dTHX; | ||
| 1060 | hm_h2_sess *s = (hm_h2_sess *)ud; | ||
| 1061 | hm_h2_stream *st; | ||
| 1062 | (void)flags; | ||
| 1063 | st = (hm_h2_stream *)nghttp2_session_get_stream_user_data(ses, sid); | ||
| 1064 | if (!st || st->refused) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1065 | /* A stream handle with a read callback takes these bytes instead. An | ||
| 1066 | * Extended CONNECT stream has no end, so buffering it into psgi.input | ||
| 1067 | * would grow without bound and nothing would ever read it. */ | ||
| 1068 | if (hm_stream_deliver(aTHX_ s->conn->fd, s->conn->id, (int64_t)sid, | ||
| 1069 | (const char *)data, (STRLEN)len, 0)) | ||
| 1070 | return 0; | ||
| 1071 | /* max_body, the same ceiling HTTP/1.1 enforces on its read buffer. | ||
| 1072 | * nghttp2 credits the flow window back on its own, so without this a | ||
| 1073 | * client could stream a body for as long as it liked into psgi.input, | ||
| 1074 | * on a hundred streams at once. */ | ||
| 1075 | if ((st->body ? SvCUR(st->body) : 0) + len > s->conn->loop->max_read) { | ||
| 1076 | st->refused = 1; | ||
| 1077 | /* | ||
| 1078 | # uncoverable branch true class:ignore_covered_err | ||
| 1079 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1080 | if (st->body) { SvREFCNT_dec(st->body); st->body = NULL; } | ||
| 1081 | /* | ||
| 1082 | # uncoverable branch false | ||
| 1083 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1084 | if (st->env) { | ||
| 1085 | /* not yet dispatched: answer 413 and never dispatch it */ | ||
| 1086 | SvREFCNT_dec((SV *)st->env); | ||
| 1087 | st->env = NULL; | ||
| 1088 | /* | ||
| 1089 | # uncoverable branch true class:ignore_covered_err | ||
| 1090 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1091 | if (st->resp_body) SvREFCNT_dec(st->resp_body); | ||
| 1092 | st->resp_body = newSVpvs("Payload Too Large"); | ||
| 1093 | st->blen = 17; | ||
| 1094 | hm_h2_submit_status(aTHX_ s, st, 413, "text/plain"); | ||
| 1095 | } else { | ||
| 1096 | /* already with the application (an Extended CONNECT stream | ||
| 1097 | * nothing is reading): nothing to answer, so reset it */ | ||
| 1098 | nghttp2_submit_rst_stream(ses, NGHTTP2_FLAG_NONE, sid, /* # uncoverable statement */ | ||
| 1099 | NGHTTP2_ENHANCE_YOUR_CALM); | ||
| 1100 | } | ||
| 1101 | return 0; | ||
| 1102 | } | ||
| 1103 | if (!st->body) st->body = newSVpvn((const char *)data, len); | ||
| 1104 | else sv_catpvn(st->body, (const char *)data, len); | ||
| 1105 | return 0; | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | /* Does this request's environment carry an Extended CONNECT :protocol? */ | ||
| 1109 | static int hm_h2_is_connect(pTHX_ HV *env) { | ||
| 1110 | SV **p; | ||
| 1111 | /* | ||
| 1112 | # uncoverable branch true | ||
| 1113 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1114 | if (!env) return 0; | ||
| 1115 | p = hv_fetchs(env, "psgix.connect_protocol", 0); | ||
| 1116 | /* | ||
| 1117 | # uncoverable condition right count:2 class:ignore_covered_err | ||
| 1118 | # uncoverable condition right count:3 class:ignore_covered_err | ||
| 1119 | # uncoverable condition right count:4 class:ignore_covered_err */ | ||
| 1120 | return p && *p && SvOK(*p) && SvCUR(*p) > 0; | ||
| 1121 | } | ||
| 1122 | |||
| 1123 | static int hm_h2_cb_frame_recv(nghttp2_session *ses, | ||
| 1124 | const nghttp2_frame *frame, void *ud) { | ||
| 1125 | dTHX; | ||
| 1126 | hm_h2_sess *s = (hm_h2_sess *)ud; | ||
| 1127 | if (frame->hd.type != NGHTTP2_DATA && frame->hd.type != NGHTTP2_HEADERS) | ||
| 1128 | return 0; | ||
| 1129 | { | ||
| 1130 | hm_h2_stream *st = (hm_h2_stream *)nghttp2_session_get_stream_user_data( | ||
| 1131 | ses, frame->hd.stream_id); | ||
| 1132 | if (!st || !st->env) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1133 | if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { | ||
| 1134 | /* NOT dispatched from here. This is a callback inside | ||
| 1135 | * nghttp2_session_mem_recv, where nothing may be sent and the | ||
| 1136 | * connection is not read: an application that wrote a body and | ||
| 1137 | * waited for it to drain was waiting on output that could not | ||
| 1138 | * leave and on window updates that could not arrive. The | ||
| 1139 | * request is marked, and hm_h2_input hands it over as soon as | ||
| 1140 | * the library has returned. */ | ||
| 1141 | st->ready = 1; | ||
| 1142 | return 0; | ||
| 1143 | } | ||
| 1144 | /* An Extended CONNECT stream has no end: the client sends headers | ||
| 1145 | * and then holds the stream open for as long as the session lasts. | ||
| 1146 | * Waiting for END_STREAM here would mean never dispatching it at | ||
| 1147 | * all, so it is marked ready as soon as the headers are complete - | ||
| 1148 | * which is also when the application has everything it needs to | ||
| 1149 | * accept or refuse the upgrade. Marked, not dispatched, for the | ||
| 1150 | * same reason as the END_STREAM case above: this is still inside | ||
| 1151 | * mem_recv, where a handler that waits for its body to drain waits | ||
| 1152 | * on frames that cannot arrive. */ | ||
| 1153 | if (frame->hd.type == NGHTTP2_HEADERS /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 1154 | && (frame->hd.flags & NGHTTP2_FLAG_END_HEADERS) | ||
| 1155 | && hm_h2_is_connect(aTHX_ st->env)) | ||
| 1156 | st->ready = st->tunnel = 1; | ||
| 1157 | } | ||
| 1158 | return 0; | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | static void hm_h2_stream_free(pTHX_ hm_h2_sess *s, hm_h2_stream *st) { | ||
| 1162 | hm_h2_stream **pp = &s->streams; | ||
| 1163 | SV *pending = st->resp_f; | ||
| 1164 | st->resp_f = NULL; | ||
| 1165 | /* | ||
| 1166 | # uncoverable branch false class:ignore_covered_err | ||
| 1167 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 1168 | while (*pp) { if (*pp == st) { *pp = st->next; break; } pp = &(*pp)->next; } | ||
| 1169 | /* The stream went (RST_STREAM, or the connection) while its response | ||
| 1170 | * was still being made: cancel the Future, as hm_close does for an | ||
| 1171 | * HTTP/1 connection, so the work behind it can stop. Without this a | ||
| 1172 | * client opening and resetting streams in a loop keeps unbounded work | ||
| 1173 | * in flight whatever MAX_CONCURRENT_STREAMS says. The continuation it | ||
| 1174 | * fires finds no stream under this id and returns. */ | ||
| 1175 | if (pending) { | ||
| 1176 | /* | ||
| 1177 | # uncoverable branch false class:ignore_covered_err | ||
| 1178 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1179 | if (st->awaiting) { | ||
| 1180 | dSP; | ||
| 1181 | ENTER; SAVETMPS; PUSHMARK(SP); | ||
| 1182 | XPUSHs(pending); | ||
| 1183 | PUTBACK; | ||
| 1184 | call_method("cancel", G_DISCARD | G_EVAL); | ||
| 1185 | FREETMPS; LEAVE; | ||
| 1186 | } | ||
| 1187 | SvREFCNT_dec(pending); | ||
| 1188 | } | ||
| 1189 | if (st->env) SvREFCNT_dec((SV *)st->env); | ||
| 1190 | if (st->env_rv) SvREFCNT_dec(st->env_rv); | ||
| 1191 | if (st->body) SvREFCNT_dec(st->body); | ||
| 1192 | if (st->resp_body) SvREFCNT_dec(st->resp_body); | ||
| 1193 | if (st->resp_headers) SvREFCNT_dec(st->resp_headers); | ||
| 1194 | hm_h2_src_free(aTHX_ st); | ||
| 1195 | free(st); | ||
| 1196 | } | ||
| 1197 | |||
| 1198 | static int hm_h2_cb_stream_close(nghttp2_session *ses, int32_t sid, | ||
| 1199 | uint32_t error_code, void *ud) { | ||
| 1200 | dTHX; | ||
| 1201 | hm_h2_sess *s = (hm_h2_sess *)ud; | ||
| 1202 | hm_h2_stream *st = (hm_h2_stream *)nghttp2_session_get_stream_user_data( | ||
| 1203 | ses, sid); | ||
| 1204 | (void)error_code; | ||
| 1205 | /* A peer can reset one stream out of many; any stream handle still open | ||
| 1206 | * on it has to hear about it before st is freed underneath it. */ | ||
| 1207 | /* | ||
| 1208 | # uncoverable branch false class:ignore_covered_err | ||
| 1209 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1210 | if (s->conn) hm_stream_h2_gone(aTHX_ s->conn->fd, s->conn->id, (int64_t)sid); | ||
| 1211 | /* | ||
| 1212 | # uncoverable branch false class:ignore_covered_err | ||
| 1213 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1214 | if (st) { | ||
| 1215 | nghttp2_session_set_stream_user_data(ses, sid, NULL); | ||
| 1216 | hm_h2_stream_free(aTHX_ s, st); | ||
| 1217 | } | ||
| 1218 | return 0; | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | /* ---- session lifecycle + loop entry points ------------------------------ */ | ||
| 1222 | |||
| 1223 | static int hm_h2_flush_send(pTHX_ hm_h2_sess *s) { | ||
| 1224 | hm_conn *c = s->conn; | ||
| 1225 | /* Inside nghttp2_session_mem_recv (a callback dispatched the app, and | ||
| 1226 | * the app answered): sending now would be re-entering nghttp2, and a | ||
| 1227 | * write error would close the connection and free the session the | ||
| 1228 | * library is still executing on. hm_h2_input flushes once mem_recv | ||
| 1229 | * has returned. */ | ||
| 1230 | /* | ||
| 1231 | # uncoverable branch true class:ignore_covered_err | ||
| 1232 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 1233 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 1234 | if (s->in_recv || s->in_send) return 0; | ||
| 1235 | { | ||
| 1236 | hm_loop *loop = c->loop; | ||
| 1237 | int fd = c->fd, rc; | ||
| 1238 | UV id = c->id; | ||
| 1239 | s->in_send = 1; | ||
| 1240 | rc = nghttp2_session_send(s->session); | ||
| 1241 | s->in_send = 0; | ||
| 1242 | /* | ||
| 1243 | # uncoverable branch true | ||
| 1244 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1245 | if (rc != 0) { | ||
| 1246 | hm_close(aTHX_ loop, c); /* # uncoverable statement */ | ||
| 1247 | return -1; /* # uncoverable statement */ | ||
| 1248 | } | ||
| 1249 | /* | ||
| 1250 | # uncoverable branch true | ||
| 1251 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1252 | if (loop->conns[fd] != c) return -1; | ||
| 1253 | /* a paused stream handle is restarted by hm_flush once what it was | ||
| 1254 | * waiting behind has been written (hm_stream_drained) */ | ||
| 1255 | (void)id; | ||
| 1256 | return hm_flush(aTHX_ c); | ||
| 1257 | } | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | /* Create the nghttp2 server session + callbacks and attach it to the | ||
| 1261 | * connection, without submitting SETTINGS yet (the upgrade path must call | ||
| 1262 | * nghttp2_session_upgrade2 in between). */ | ||
| 1263 | static int hm_h2_new_session(pTHX_ hm_conn *c) { | ||
| 1264 | hm_h2_sess *s = (hm_h2_sess *)hm_xcalloc(1, sizeof(hm_h2_sess)); | ||
| 1265 | nghttp2_session_callbacks *cbs; | ||
| 1266 | /* | ||
| 1267 | # uncoverable branch true | ||
| 1268 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1269 | if (!s) return -1; | ||
| 1270 | s->conn = c; | ||
| 1271 | /* | ||
| 1272 | # uncoverable branch true | ||
| 1273 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1274 | if (nghttp2_session_callbacks_new(&cbs) != 0) { free(s); return -1; } | ||
| 1275 | nghttp2_session_callbacks_set_send_callback(cbs, hm_h2_cb_send); | ||
| 1276 | nghttp2_session_callbacks_set_on_begin_headers_callback(cbs, hm_h2_cb_begin_headers); | ||
| 1277 | nghttp2_session_callbacks_set_on_header_callback(cbs, hm_h2_cb_header); | ||
| 1278 | nghttp2_session_callbacks_set_on_data_chunk_recv_callback(cbs, hm_h2_cb_data_chunk); | ||
| 1279 | nghttp2_session_callbacks_set_on_frame_recv_callback(cbs, hm_h2_cb_frame_recv); | ||
| 1280 | nghttp2_session_callbacks_set_on_stream_close_callback(cbs, hm_h2_cb_stream_close); | ||
| 1281 | { | ||
| 1282 | /* The flood limits nghttp2 grew over its releases, PINNED rather | ||
| 1283 | * than inherited: rapid reset (1.57) and CONTINUATION floods (1.61) | ||
| 1284 | * are answered with GOAWAY whatever the linked library's defaults, | ||
| 1285 | * and a build against an older library gets what it has. The | ||
| 1286 | * numbers are nghttp2's own defaults. */ | ||
| 1287 | nghttp2_option *opt = NULL; | ||
| 1288 | int rv; | ||
| 1289 | /* | ||
| 1290 | # uncoverable branch true class:ignore_covered_err | ||
| 1291 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1292 | if (nghttp2_option_new(&opt) != 0) opt = NULL; | ||
| 1293 | /* | ||
| 1294 | # uncoverable branch false class:ignore_covered_err | ||
| 1295 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1296 | if (opt) { | ||
| 1297 | #if NGHTTP2_VERSION_NUM >= 0x013900 | ||
| 1298 | nghttp2_option_set_stream_reset_rate_limit(opt, 1000, 33); | ||
| 1299 | #endif | ||
| 1300 | #if NGHTTP2_VERSION_NUM >= 0x013d00 | ||
| 1301 | nghttp2_option_set_max_continuations(opt, 8); | ||
| 1302 | #endif | ||
| 1303 | } | ||
| 1304 | rv = opt ? nghttp2_session_server_new2(&s->session, cbs, s, opt) | ||
| 1305 | : nghttp2_session_server_new(&s->session, cbs, s); /* # uncoverable statement class:ignore_covered_err */ | ||
| 1306 | /* | ||
| 1307 | # uncoverable branch false class:ignore_covered_err | ||
| 1308 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1309 | if (opt) nghttp2_option_del(opt); | ||
| 1310 | /* | ||
| 1311 | # uncoverable branch true | ||
| 1312 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1313 | if (rv != 0) { | ||
| 1314 | nghttp2_session_callbacks_del(cbs); /* # uncoverable statement */ | ||
| 1315 | free(s); /* # uncoverable statement */ | ||
| 1316 | return -1; /* # uncoverable statement */ | ||
| 1317 | } | ||
| 1318 | } | ||
| 1319 | nghttp2_session_callbacks_del(cbs); | ||
| 1320 | c->h2 = s; | ||
| 1321 | c->keepalive = 1; /* h2 connections are persistent */ | ||
| 1322 | return 0; | ||
| 1323 | } | ||
| 1324 | |||
| 1325 | static void hm_h2_submit_our_settings(hm_h2_sess *s) { | ||
| 1326 | nghttp2_settings_entry iv[3]; | ||
| 1327 | iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS; | ||
| 1328 | iv[0].value = 100; | ||
| 1329 | /* RFC 8441 Extended CONNECT. Without this a client will not attempt a | ||
| 1330 | * WebSocket over HTTP/2 at all - the setting IS the advertisement, and | ||
| 1331 | * there is no other way to say the server understands :protocol. */ | ||
| 1332 | iv[1].settings_id = NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL; | ||
| 1333 | iv[1].value = 1; | ||
| 1334 | /* Advisory to the peer; hm_h2_cb_header is what enforces it. */ | ||
| 1335 | iv[2].settings_id = NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE; | ||
| 1336 | iv[2].value = HM_H2_MAX_HEADER_LIST; | ||
| 1337 | nghttp2_submit_settings(s->session, NGHTTP2_FLAG_NONE, iv, 3); | ||
| 1338 | } | ||
| 1339 | |||
| 1340 | static int hm_h2_start(pTHX_ hm_conn *c) { | ||
| 1341 | /* | ||
| 1342 | # uncoverable branch true | ||
| 1343 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1344 | if (hm_h2_new_session(aTHX_ c) < 0) return -1; | ||
| 1345 | hm_h2_submit_our_settings((hm_h2_sess *)c->h2); | ||
| 1346 | return 0; | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | /* base64url decode (no padding), RFC 7540's HTTP2-Settings encoding. | ||
| 1350 | * Returns decoded length, or -1 on a bad character / overflow. */ | ||
| 1351 | static int hm_b64url_decode(const char *in, size_t inlen, | ||
| 1352 | unsigned char *out, size_t outcap) { | ||
| 1353 | static const signed char T[256] = { | ||
| 1354 | ['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7, | ||
| 1355 | ['I']=8,['J']=9,['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15, | ||
| 1356 | ['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23, | ||
| 1357 | ['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31, | ||
| 1358 | ['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39, | ||
| 1359 | ['o']=40,['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47, | ||
| 1360 | ['w']=48,['x']=49,['y']=50,['z']=51,['0']=52,['1']=53,['2']=54,['3']=55, | ||
| 1361 | ['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61,['-']=62,['_']=63 | ||
| 1362 | }; | ||
| 1363 | unsigned int acc = 0; int bits = 0; size_t o = 0, i; | ||
| 1364 | for (i = 0; i < inlen; i++) { | ||
| 1365 | unsigned char ch = (unsigned char)in[i]; | ||
| 1366 | signed char v; | ||
| 1367 | /* | ||
| 1368 | # uncoverable branch true | ||
| 1369 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1370 | if (ch == '=' ) break; | ||
| 1371 | v = T[ch]; | ||
| 1372 | /* | ||
| 1373 | # uncoverable branch true | ||
| 1374 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1375 | if (v < 0) { | ||
| 1376 | /* | ||
| 1377 | # uncoverable statement | ||
| 1378 | # uncoverable branch true | ||
| 1379 | # uncoverable branch false | ||
| 1380 | # uncoverable condition left count:1 | ||
| 1381 | # uncoverable condition right count:1 | ||
| 1382 | # uncoverable condition left count:2 | ||
| 1383 | # uncoverable condition right count:2 | ||
| 1384 | # uncoverable condition left count:3 | ||
| 1385 | # uncoverable condition right count:3 | ||
| 1386 | # uncoverable condition left count:4 | ||
| 1387 | # uncoverable condition right count:4 */ | ||
| 1388 | if (ch == '\r' || ch == '\n' || ch == ' ' || ch == '\t') continue; | ||
| 1389 | return -1; /* # uncoverable statement */ | ||
| 1390 | } | ||
| 1391 | acc = (acc << 6) | (unsigned)v; | ||
| 1392 | bits += 6; | ||
| 1393 | if (bits >= 8) { | ||
| 1394 | bits -= 8; | ||
| 1395 | /* | ||
| 1396 | # uncoverable branch true | ||
| 1397 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1398 | if (o >= outcap) return -1; | ||
| 1399 | out[o++] = (unsigned char)((acc >> bits) & 0xff); | ||
| 1400 | } | ||
| 1401 | } | ||
| 1402 | return (int)o; | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | /* HTTP/1.1 Upgrade: h2c. Called with the already-built PSGI env of the | ||
| 1406 | * original request (which carries method/path/headers/body via psgi.input) | ||
| 1407 | * and the raw base64url HTTP2-Settings value. Starts the session with | ||
| 1408 | * nghttp2_session_upgrade2 (the original request becomes stream 1) and | ||
| 1409 | * dispatches it. Caller has already written the 101 to the write buffer. | ||
| 1410 | * Returns 0 on success (connection is now in h2 mode), -1 on failure. */ | ||
| 1411 | static int hm_h2_start_upgrade(pTHX_ hm_conn *c, const char *settings, | ||
| 1412 | size_t slen, HV *env, int is_head) { | ||
| 1413 | unsigned char sbuf[256]; | ||
| 1414 | int n = hm_b64url_decode(settings, slen, sbuf, sizeof(sbuf)); | ||
| 1415 | hm_h2_sess *s; | ||
| 1416 | hm_h2_stream *st; | ||
| 1417 | /* env is ours from here: on the early failures nothing else holds it, | ||
| 1418 | * once it is on the stream the session's teardown frees it */ | ||
| 1419 | /* | ||
| 1420 | # uncoverable branch true | ||
| 1421 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1422 | if (n < 0) { SvREFCNT_dec((SV *)env); return -1; } | ||
| 1423 | /* | ||
| 1424 | # uncoverable branch true | ||
| 1425 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1426 | if (hm_h2_new_session(aTHX_ c) < 0) { SvREFCNT_dec((SV *)env); return -1; } | ||
| 1427 | s = (hm_h2_sess *)c->h2; | ||
| 1428 | st = (hm_h2_stream *)hm_xcalloc(1, sizeof(hm_h2_stream)); | ||
| 1429 | st->id = 1; | ||
| 1430 | st->env = env; | ||
| 1431 | st->next = s->streams; | ||
| 1432 | s->streams = st; | ||
| 1433 | if (nghttp2_session_upgrade2(s->session, sbuf, (size_t)n, is_head, st) != 0) | ||
| 1434 | return -1; | ||
| 1435 | nghttp2_session_set_stream_user_data(s->session, 1, st); | ||
| 1436 | hm_h2_submit_our_settings(s); /* server connection preface */ | ||
| 1437 | /* dispatching: a request that completes while this handler waits is | ||
| 1438 | * left for hm_h2_input, as it is for every other handler */ | ||
| 1439 | s->dispatching = 1; | ||
| 1440 | { | ||
| 1441 | hm_loop *loop = c->loop; | ||
| 1442 | int fd = c->fd; | ||
| 1443 | int lent = hm_read_lend(loop, c); | ||
| 1444 | hm_h2_dispatch(aTHX_ s, st); /* env already carries psgi.input */ | ||
| 1445 | /* | ||
| 1446 | # uncoverable branch true class:ignore_covered_err | ||
| 1447 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 1448 | # uncoverable condition left count:2 class:ignore_covered_err | ||
| 1449 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 1450 | if (lent && loop->conns[fd] == c) hm_read_unlend(loop, c); | ||
| 1451 | } | ||
| 1452 | /* | ||
| 1453 | # uncoverable branch false class:ignore_covered_err | ||
| 1454 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1455 | if (c->h2 == (void *)s) s->dispatching = 0; | ||
| 1456 | return 0; | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | static void hm_h2_free(pTHX_ void *h2) { | ||
| 1460 | hm_h2_sess *s = (hm_h2_sess *)h2; | ||
| 1461 | /* | ||
| 1462 | # uncoverable branch true | ||
| 1463 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1464 | if (!s) return; | ||
| 1465 | /* | ||
| 1466 | # uncoverable branch false class:ignore_covered_err | ||
| 1467 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1468 | if (s->session) nghttp2_session_del(s->session); /* fires stream_close */ | ||
| 1469 | s->session = NULL; | ||
| 1470 | while (s->streams) hm_h2_stream_free(aTHX_ s, s->streams); | ||
| 1471 | free(s); | ||
| 1472 | } | ||
| 1473 | |||
| 1474 | /* Feed buffered bytes to the session, flush output, close if finished. | ||
| 1475 | * | ||
| 1476 | * mem_recv dispatches the application from inside its callbacks, and an | ||
| 1477 | * application that awaits pumps this loop, which can read more of this | ||
| 1478 | * very connection. So, for the duration of the call: the connection is | ||
| 1479 | * in_app (hm_close defers, hm_readable will not grow the buffer nghttp2 | ||
| 1480 | * is reading, and will not call back in here), the session is in_recv | ||
| 1481 | * (hm_h2_flush_send does nothing), and bytes that arrive meanwhile are | ||
| 1482 | * fed by the next turn of the loop below rather than by a second frame. */ | ||
| 1483 | /* Hand every complete request to the application, lowest stream first. | ||
| 1484 | * | ||
| 1485 | * Called with nghttp2 off the stack, which is the point: inside the call the | ||
| 1486 | * session can send, and a handler that waits pumps the loop, which reads this | ||
| 1487 | * connection and comes back into hm_h2_input - so output it is waiting on | ||
| 1488 | * leaves and the window updates it is waiting on arrive. | ||
| 1489 | * | ||
| 1490 | * One at a time. A request that completes while a handler is on the stack is | ||
| 1491 | * left marked and taken by this same loop when that handler returns, exactly | ||
| 1492 | * as it used to wait for the handler ahead of it; nesting them would stack a | ||
| 1493 | * second application call on a first that cannot finish until the second | ||
| 1494 | * does. | ||
| 1495 | * | ||
| 1496 | * Returns -1 when the connection has gone. */ | ||
| 1497 | static int hm_h2_run_ready(pTHX_ hm_h2_sess *s) { | ||
| 1498 | hm_conn *c = s->conn; | ||
| 1499 | hm_loop *loop = c->loop; | ||
| 1500 | int fd = c->fd; | ||
| 1501 | int lent = 0; | ||
| 1502 | if (s->dispatching) return 0; | ||
| 1503 | for (;;) { | ||
| 1504 | hm_h2_stream *st, *pick = NULL; | ||
| 1505 | unsigned char was; | ||
| 1506 | for (st = s->streams; st; st = st->next) | ||
| 1507 | /* | ||
| 1508 | # uncoverable condition right count:2 class:ignore_covered_err | ||
| 1509 | # uncoverable condition right count:3 class:ignore_covered_err | ||
| 1510 | # uncoverable condition right count:4 class:ignore_covered_err */ | ||
| 1511 | if (st->ready && st->env && (!pick || st->id < pick->id)) | ||
| 1512 | pick = st; | ||
| 1513 | if (!pick) { | ||
| 1514 | /* | ||
| 1515 | # uncoverable branch true class:ignore_covered_err | ||
| 1516 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1517 | if (lent) hm_read_unlend(loop, c); | ||
| 1518 | return 0; | ||
| 1519 | } | ||
| 1520 | pick->ready = 0; | ||
| 1521 | /* | ||
| 1522 | # uncoverable branch false class:ignore_covered_err | ||
| 1523 | # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1524 | if (!lent) lent = hm_read_lend(loop, c); | ||
| 1525 | /* in_app: a close asked for while the handler runs is deferred to | ||
| 1526 | * here, because this frame still holds c and s (see hm_close) */ | ||
| 1527 | was = c->in_app; | ||
| 1528 | c->in_app = 1; | ||
| 1529 | s->dispatching = 1; | ||
| 1530 | hm_h2_dispatch(aTHX_ s, pick); | ||
| 1531 | s->dispatching = 0; | ||
| 1532 | c->in_app = was; | ||
| 1533 | /* | ||
| 1534 | # uncoverable branch true | ||
| 1535 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1536 | if (loop->conns[fd] != c) return -1; | ||
| 1537 | if (!was && c->close_deferred) { /* # uncoverable condition right count:1 class:ignore_covered_err */ | ||
| 1538 | c->close_deferred = 0; | ||
| 1539 | hm_close(aTHX_ loop, c); | ||
| 1540 | return -1; | ||
| 1541 | } | ||
| 1542 | hm_read_resume(loop, c); | ||
| 1543 | } | ||
| 1544 | } | ||
| 1545 | |||
| 1546 | /* May this connection's frames be read while an application call for it is | ||
| 1547 | * on the stack? Yes for HTTP/2 once the library has returned: the handler is | ||
| 1548 | * waiting on exactly what those frames carry. Never from inside mem_recv. */ | ||
| 1549 | static int hm_h2_reentrant(hm_conn *c) { | ||
| 1550 | hm_h2_sess *s = (hm_h2_sess *)c->h2; | ||
| 1551 | /* | ||
| 1552 | # uncoverable condition right count:2 class:ignore_covered_err | ||
| 1553 | # uncoverable condition right count:3 class:ignore_covered_err | ||
| 1554 | # uncoverable condition right count:4 class:ignore_covered_err */ | ||
| 1555 | return s && s->session && !s->in_recv && !s->in_send; | ||
| 1556 | } | ||
| 1557 | |||
| 1558 | static void hm_h2_input(pTHX_ hm_conn *c) { | ||
| 1559 | hm_h2_sess *s = (hm_h2_sess *)c->h2; | ||
| 1560 | hm_loop *loop = c->loop; | ||
| 1561 | int fd = c->fd; | ||
| 1562 | /* | ||
| 1563 | # uncoverable branch true | ||
| 1564 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1565 | if (s->in_recv) return; | ||
| 1566 | for (;;) { /* # uncoverable statement class:ignore_covered_err */ | ||
| 1567 | int whole = 0; | ||
| 1568 | if (c->rlen > 0) { | ||
| 1569 | size_t before = c->rlen; | ||
| 1570 | ssize_t rv; | ||
| 1571 | /* was: this may be a handler's own loop reading for it, and its | ||
| 1572 | * frame further up still wants the connection held */ | ||
| 1573 | unsigned char was = c->in_app; | ||
| 1574 | s->in_recv++; | ||
| 1575 | c->in_app = 1; | ||
| 1576 | rv = nghttp2_session_mem_recv(s->session, | ||
| 1577 | (const uint8_t *)c->rbuf, before); | ||
| 1578 | c->in_app = was; | ||
| 1579 | s->in_recv--; | ||
| 1580 | /* | ||
| 1581 | # uncoverable branch true | ||
| 1582 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1583 | if (loop->conns[fd] != c) return; /* gone underneath */ | ||
| 1584 | /* | ||
| 1585 | # uncoverable branch true class:ignore_covered_err | ||
| 1586 | # uncoverable condition left count:1 class:ignore_covered_err | ||
| 1587 | # uncoverable condition left count:2 class:ignore_covered_err */ | ||
| 1588 | if (!was && c->close_deferred) { | ||
| 1589 | c->close_deferred = 0; /* # uncoverable statement */ | ||
| 1590 | hm_close(aTHX_ loop, c); /* # uncoverable statement */ | ||
| 1591 | return; /* # uncoverable statement */ | ||
| 1592 | } | ||
| 1593 | hm_read_resume(loop, c); | ||
| 1594 | /* | ||
| 1595 | # uncoverable branch true | ||
| 1596 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1597 | if (rv < 0) { hm_close(aTHX_ loop, c); return; } | ||
| 1598 | /* consumed BEFORE any application runs: its loop may read more | ||
| 1599 | * into this buffer, and feed it, while the handler waits */ | ||
| 1600 | /* | ||
| 1601 | # uncoverable branch true class:ignore_covered_err | ||
| 1602 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1603 | if ((size_t)rv < c->rlen) | ||
| 1604 | memmove(c->rbuf, c->rbuf + rv, c->rlen - rv); /* # uncoverable statement */ | ||
| 1605 | c->rlen -= (size_t)rv; | ||
| 1606 | whole = ((size_t)rv == before); | ||
| 1607 | } | ||
| 1608 | if (hm_h2_run_ready(aTHX_ s) < 0) return; | ||
| 1609 | /* | ||
| 1610 | # uncoverable branch true | ||
| 1611 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1612 | if (c->close_deferred) return; /* an outer frame closes */ | ||
| 1613 | /* | ||
| 1614 | # uncoverable branch true count:1 class:ignore_covered_err | ||
| 1615 | # uncoverable branch true count:2 class:ignore_covered_err | ||
| 1616 | # uncoverable branch false count:3 class:ignore_covered_err | ||
| 1617 | # uncoverable condition right count:1 class:ignore_covered_err | ||
| 1618 | # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 1619 | if (!whole || c->rlen == 0) break; /* a partial frame remains */ | ||
| 1620 | } | ||
| 1621 | /* | ||
| 1622 | # uncoverable branch true | ||
| 1623 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1624 | if (hm_h2_flush_send(aTHX_ s) < 0) return; | ||
| 1625 | if (c->in_app) return; /* a handler's loop: its own frame decides */ | ||
| 1626 | if (!nghttp2_session_want_read(s->session) /* # uncoverable condition right count:2 class:ignore_covered_err */ | ||
| 1627 | && !nghttp2_session_want_write(s->session)) | ||
| 1628 | hm_close(aTHX_ loop, c); | ||
| 1629 | } | ||
| 1630 | |||
| 1631 | /* On a new connection with http2 enabled: is this the h2 preface? | ||
| 1632 | * 1 = started (input already fed), -1 = matches, need more bytes, 0 = not h2. */ | ||
| 1633 | static int hm_h2_detect(pTHX_ hm_conn *c) { | ||
| 1634 | size_t n = c->rlen < sizeof(HM_H2_PREFACE) | ||
| 1635 | ? c->rlen : sizeof(HM_H2_PREFACE); | ||
| 1636 | if (memcmp(c->rbuf, HM_H2_PREFACE, n) != 0) return 0; | ||
| 1637 | if (n < sizeof(HM_H2_PREFACE)) return -1; | ||
| 1638 | /* | ||
| 1639 | # uncoverable branch true | ||
| 1640 | # uncoverable condition left count:1 class:ignore_covered_err */ | ||
| 1641 | if (hm_h2_start(aTHX_ c) < 0) { hm_close(aTHX_ c->loop, c); return 1; } | ||
| 1642 | hm_h2_input(aTHX_ c); /* nghttp2 validates + consumes the preface */ | ||
| 1643 | return 1; | ||
| 1644 | } | ||
| 1645 | |||
| 1646 | /* hm_readable, with a FULL buffer on a fresh connection: is this an h2 | ||
| 1647 | * preface hm_process has not seen yet? A client that sends the preface, | ||
| 1648 | * its frames and a whole body in one burst (curl 8.2x does) fills the | ||
| 1649 | * buffer before hm_process runs once, and the HTTP/1 ceiling would then | ||
| 1650 | * answer a plain-text 413 to a client waiting for SETTINGS (CPAN Testers, | ||
| 1651 | * 0.56, FreeBSD: curl reported no status at all). */ | ||
| 1652 | static int hm_h2_preface_pending(hm_conn *c) { | ||
| 1653 | return c->lst && c->lst->http2 && c->nreqs == 0 && !c->h2 | ||
| 1654 | && c->rlen >= sizeof(HM_H2_PREFACE) | ||
| 1655 | && memcmp(c->rbuf, HM_H2_PREFACE, sizeof(HM_H2_PREFACE)) == 0; | ||
| 1656 | } | ||
| 1657 | |||
| 1658 | static int hm_h2_available(void) { return 1; } | ||
| 1659 | |||
| 1660 | #else /* !HM_HAVE_NGHTTP2 */ | ||
| 1661 | |||
| 1662 | 2 | static int hm_h2_reentrant(hm_conn *c) { (void)c; return 0; } | |
| 1663 | 0 | static int hm_h2_detect(pTHX_ hm_conn *c) { (void)c; return 0; } | |
| 1664 | 0 | static void hm_h2_input(pTHX_ hm_conn *c) { (void)c; } | |
| 1665 | 0 | static void hm_h2_free(pTHX_ void *h2) { (void)h2; } | |
| 1666 | 0 | static int hm_h2_writer_write(pTHX_ int fd, UV gen, int32_t sid, SV *d) | |
| 1667 | 0 | { (void)fd; (void)gen; (void)sid; (void)d; return -1; } | |
| 1668 | 0 | static void hm_h2_writer_close(pTHX_ int fd, UV gen, int32_t sid) | |
| 1669 | 0 | { (void)fd; (void)gen; (void)sid; } | |
| 1670 | 40 | static int hm_h2_preface_pending(hm_conn *c) { (void)c; return 0; } | |
| 1671 | 11 | static int hm_h2_available(void) { return 0; } | |
| 1672 | |||
| 1673 | #endif /* HM_HAVE_NGHTTP2 */ | ||
| 1674 | |||
| 1675 | #endif /* HM_HTTP2_H */ |