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