| 1 | #ifndef HM_HTTP3_H | ||
| 2 | #define HM_HTTP3_H | ||
| 3 | |||
| 4 | /* HTTP/3 (nghttp3) over the QUIC transport in hm_quic.h. Built under the | ||
| 5 | * same -DHM_HAVE_HTTP3 as the transport, because neither is useful alone. | ||
| 6 | * | ||
| 7 | * Included from hm_core.h after hm_quic.h. Requests arriving here resolve | ||
| 8 | * through the same sync / Future / streaming paths as HTTP/1.1 and HTTP/2: | ||
| 9 | * nghttp3 owns framing and QPACK, ngtcp2 owns loss recovery and flow | ||
| 10 | * control, and the application sees a PSGI env either way. | ||
| 11 | * | ||
| 12 | * hm_http2.h is the template throughout, and where a piece of it is already | ||
| 13 | * transport-independent this file CALLS it rather than copying it: | ||
| 14 | * hm_h2_add_header (retargeted onto an HV so both can use it), | ||
| 15 | * hm_h2_hopbyhop, hm_h2_body_sv. Two implementations of header mangling | ||
| 16 | * would be two implementations to keep in step. | ||
| 17 | * | ||
| 18 | * Two things here are genuinely not h2 transliterations: | ||
| 19 | * | ||
| 20 | * - The send half is NOT here. QUIC pulls rather than pushes, so what | ||
| 21 | * would be hm_h2_flush_send's counterpart lives in hm_quic.h with the | ||
| 22 | * socket - see the header comment there. | ||
| 23 | * - The data provider fills an IOVEC ARRAY and the buffers it points at | ||
| 24 | * must stay alive until acked_stream_data. nghttp2's model, a memcpy | ||
| 25 | * into a window ngtcp2 owns, does not port. */ | ||
| 26 | |||
| 27 | #ifdef HM_HAVE_HTTP3 | ||
| 28 | |||
| 29 | typedef struct hm_h3_stream { | ||
| 30 | int64_t id; | ||
| 31 | HV *env; /* being built (moved out at dispatch) */ | ||
| 32 | SV *env_rv; /* kept while awaiting, for access_log */ | ||
| 33 | SV *body; /* accumulating request body, or NULL */ | ||
| 34 | SV *resp_body; /* response bytes the data reader feeds */ | ||
| 35 | size_t resp_off; /* how much has been HANDED to nghttp3 */ | ||
| 36 | size_t resp_acked; /* ...and how much it has finished with */ | ||
| 37 | SV *src_sv; /* streamed file body: owned, or NULL */ | ||
| 38 | int src_fd; | ||
| 39 | off_t src_off; | ||
| 40 | UV src_rem; | ||
| 41 | char *src_buf; /* bounce buffer for the file source */ | ||
| 42 | size_t src_buflen; | ||
| 43 | size_t blen; /* response body length (logging) */ | ||
| 44 | int status; | ||
| 45 | int resp_status; /* streaming: stashed until close */ | ||
| 46 | SV *resp_headers; | ||
| 47 | int awaiting; /* parked on a Future */ | ||
| 48 | SV *resp_f; /* that Future: cancelled if the stream is | ||
| 49 | * reset before it settles */ | ||
| 50 | size_t hdr_bytes; /* decoded request header octets so far */ | ||
| 51 | unsigned char refused; /* body over max_body: 413 sent, the rest | ||
| 52 | * of the stream is dropped unread */ | ||
| 53 | unsigned char fin; /* the request stream ended */ | ||
| 54 | unsigned char dispatched; | ||
| 55 | unsigned char ready; /* complete: dispatched after read_pkt */ | ||
| 56 | unsigned char responded; /* a response was submitted: nghttp3 now | ||
| 57 | * points into resp_body, so nothing may | ||
| 58 | * append to it, free it or submit again */ | ||
| 59 | struct hm_h3_stream *next; | ||
| 60 | } hm_h3_stream; | ||
| 61 | |||
| 62 | static void hm_h3_respond(pTHX_ hm_qconn *qc, hm_h3_stream *st, SV *resp); | ||
| 63 | static int hm_h3_resume(pTHX_ hm_qconn *qc, int64_t sid); | ||
| 64 | static void hm_h3_dispatch(pTHX_ hm_qconn *qc, hm_h3_stream *st); | ||
| 65 | static void hm_h3_submit_status(pTHX_ hm_qconn *qc, hm_h3_stream *st, | ||
| 66 | int status, const char *ctype); | ||
| 67 | |||
| 68 | static int hm_h3_available(void) { return 1; } | ||
| 69 | |||
| 70 | /* ---- streams ------------------------------------------------------------- */ | ||
| 71 | |||
| 72 | static hm_h3_stream *hm_h3_stream_new(pTHX_ hm_qconn *qc, int64_t id) { | ||
| 73 | hm_h3_stream *st = (hm_h3_stream *)hm_xcalloc(1, sizeof(hm_h3_stream)); | ||
| 74 | st->id = id; | ||
| 75 | st->env = newHV(); | ||
| 76 | /* Same reason as the h2 path: a request env holds a few dozen keys and | ||
| 77 | * growing the hash one rehash at a time is measurable per request. */ | ||
| 78 | hv_ksplit(st->env, 64); | ||
| 79 | st->next = (hm_h3_stream *)qc->h3_streams; | ||
| 80 | qc->h3_streams = st; | ||
| 81 | return st; | ||
| 82 | } | ||
| 83 | |||
| 84 | static hm_h3_stream *hm_h3_stream_find(hm_qconn *qc, int64_t id) { | ||
| 85 | hm_h3_stream *st; | ||
| 86 | for (st = (hm_h3_stream *)qc->h3_streams; st; st = st->next) | ||
| 87 | if (st->id == id) return st; | ||
| 88 | return NULL; | ||
| 89 | } | ||
| 90 | |||
| 91 | static void hm_h3_src_free(pTHX_ hm_h3_stream *st) { | ||
| 92 | hm_bsrc bs; | ||
| 93 | if (st->src_buf) { free(st->src_buf); st->src_buf = NULL; st->src_buflen = 0; } | ||
| 94 | if (!st->src_sv) return; | ||
| 95 | memset(&bs, 0, sizeof bs); | ||
| 96 | bs.kind = 1; | ||
| 97 | bs.fd = st->src_fd; | ||
| 98 | bs.sv = st->src_sv; | ||
| 99 | hm_bsrc_release(aTHX_ &bs); | ||
| 100 | st->src_sv = NULL; | ||
| 101 | st->src_rem = 0; | ||
| 102 | } | ||
| 103 | |||
| 104 | static void hm_h3_stream_free(pTHX_ hm_qconn *qc, hm_h3_stream *st) { | ||
| 105 | hm_h3_stream **pp = (hm_h3_stream **)&qc->h3_streams; | ||
| 106 | SV *pending = st->resp_f; | ||
| 107 | st->resp_f = NULL; | ||
| 108 | while (*pp) { if (*pp == st) { *pp = st->next; break; } pp = &(*pp)->next; } | ||
| 109 | /* Reset while its response was still being made: cancel the Future, | ||
| 110 | * as the h2 and HTTP/1 paths do, so the work behind it can stop. */ | ||
| 111 | if (pending) { | ||
| 112 | if (st->awaiting) { /* # uncoverable statement */ | ||
| 113 | dSP; /* # uncoverable statement */ | ||
| 114 | ENTER; SAVETMPS; PUSHMARK(SP); /* # uncoverable statement */ | ||
| 115 | XPUSHs(pending); /* # uncoverable statement */ | ||
| 116 | PUTBACK; /* # uncoverable statement */ | ||
| 117 | call_method("cancel", G_DISCARD | G_EVAL); /* # uncoverable statement */ | ||
| 118 | FREETMPS; LEAVE; /* # uncoverable statement */ | ||
| 119 | } | ||
| 120 | SvREFCNT_dec(pending); /* # uncoverable statement */ | ||
| 121 | } | ||
| 122 | if (st->env) SvREFCNT_dec((SV *)st->env); /* # uncoverable statement */ | ||
| 123 | if (st->env_rv) SvREFCNT_dec(st->env_rv); | ||
| 124 | if (st->body) SvREFCNT_dec(st->body); | ||
| 125 | if (st->resp_body) SvREFCNT_dec(st->resp_body); | ||
| 126 | if (st->resp_headers) SvREFCNT_dec(st->resp_headers); | ||
| 127 | hm_h3_src_free(aTHX_ st); | ||
| 128 | free(st); | ||
| 129 | } | ||
| 130 | |||
| 131 | /* ---- request: headers -> $env -------------------------------------------- */ | ||
| 132 | |||
| 133 | static void hm_h3_env_init(pTHX_ hm_qconn *qc, HV *env) { | ||
| 134 | hm_loop *loop = qc->srv->loop; | ||
| 135 | hm_listener *lst = qc->srv->lst; | ||
| 136 | AV *ver = newAV(); | ||
| 137 | /* Fills the shared header-key table. A process that only ever speaks | ||
| 138 | * HTTP/3 has to do this from here or hm_hdrk_lookup never engages - it | ||
| 139 | * would fall back safely, just without the point of it. */ | ||
| 140 | hm_env_init(aTHX); | ||
| 141 | av_push(ver, newSViv(1)); av_push(ver, newSViv(1)); | ||
| 142 | hv_stores(env, "SCRIPT_NAME", newSVpvs("")); | ||
| 143 | /* "HTTP/3", matching h2's "HTTP/2" rather than a dotted form. */ | ||
| 144 | hv_stores(env, "SERVER_PROTOCOL", newSVpvs("HTTP/3")); | ||
| 145 | hv_stores(env, "SERVER_NAME", newSVpv(lst && lst->host ? lst->host : "0.0.0.0", 0)); | ||
| 146 | hv_stores(env, "SERVER_PORT", newSViv(lst ? lst->port : 0)); | ||
| 147 | if (qc->peer_str[0]) { | ||
| 148 | hv_stores(env, "REMOTE_ADDR", newSVpv(qc->peer_str, 0)); | ||
| 149 | hv_stores(env, "REMOTE_HOST", newSVpv(qc->peer_str, 0)); | ||
| 150 | } | ||
| 151 | if (qc->peer_port) hv_stores(env, "REMOTE_PORT", newSViv(qc->peer_port)); | ||
| 152 | hv_stores(env, "QUERY_STRING", newSVpvs("")); | ||
| 153 | hv_stores(env, "psgi.version", newRV_noinc((SV *)ver)); | ||
| 154 | /* QUIC has no cleartext mode, so this is https before :scheme is even | ||
| 155 | * read - and :scheme may still override it, as on h2. */ | ||
| 156 | hv_stores(env, "psgi.url_scheme", newSVpvs("https")); | ||
| 157 | hv_stores(env, "psgi.multithread", newSViv(0)); | ||
| 158 | hv_stores(env, "psgi.multiprocess", newSViv(1)); | ||
| 159 | hv_stores(env, "psgi.run_once", newSViv(0)); | ||
| 160 | hv_stores(env, "psgi.streaming", newSViv(1)); | ||
| 161 | hv_stores(env, "psgi.nonblocking", newSViv(1)); | ||
| 162 | hv_stores(env, "psgi.errors", newRV_inc((SV *)PL_stderrgv)); | ||
| 163 | /* Honest here: dispatch is on stream end, so the whole body is in hand | ||
| 164 | * before the application sees it. Without this an application cannot | ||
| 165 | * tell that a bodied request with no content-length - which is what h3 | ||
| 166 | * sends for a streamed upload - can be read to EOF safely. */ | ||
| 167 | hv_stores(env, "psgix.input.buffered", newSViv(1)); | ||
| 168 | /* HTTPS, SSL_PROTOCOL, SSL_CIPHER and the SSL_CLIENT_* family, through the | ||
| 169 | * same function the TCP side uses - so mTLS reports identically on every | ||
| 170 | * transport instead of working on one and silently not on the other. | ||
| 171 | * SSL_KTLS is passed 0 because there is no kernel record layer for QUIC, | ||
| 172 | * which is a fact about the transport and not an absent value. */ | ||
| 173 | hm_tls_env_from(aTHX_ env, qc->tls_proto, qc->tls_cipher, qc->tls_peer, 0); | ||
| 174 | /* psgix.hyperman.stream, the ticket ABI v6's stream_open takes. Not | ||
| 175 | * psgix.hyperman.conn: that names a descriptor an application may take | ||
| 176 | * over, and HTTP/3 has nothing to put behind it - which is what makes | ||
| 177 | * Hyperman::detach croak here, as it already does on HTTP/2. */ | ||
| 178 | hv_stores(env, "psgix.loop", hm_loop_to_sv(aTHX_ loop)); | ||
| 179 | if (loop->log_wants_t0) | ||
| 180 | hv_stores(env, "psgix.request_start", newSVnv((NV)hm_now_ns() / 1e9)); /* # uncoverable statement */ | ||
| 181 | } | ||
| 182 | |||
| 183 | /* ---- nghttp3 callbacks --------------------------------------------------- */ | ||
| 184 | |||
| 185 | static int hm_h3_cb_begin_headers(nghttp3_conn *conn, int64_t stream_id, | ||
| 186 | void *conn_user_data, void *stream_user_data) { | ||
| 187 | dTHX; | ||
| 188 | hm_qconn *qc = (hm_qconn *)conn_user_data; | ||
| 189 | hm_h3_stream *st; | ||
| 190 | (void)stream_user_data; | ||
| 191 | st = hm_h3_stream_find(qc, stream_id); | ||
| 192 | if (!st) st = hm_h3_stream_new(aTHX_ qc, stream_id); | ||
| 193 | hm_h3_env_init(aTHX_ qc, st->env); | ||
| 194 | nghttp3_conn_set_stream_user_data(conn, stream_id, st); | ||
| 195 | return 0; | ||
| 196 | } | ||
| 197 | |||
| 198 | static int hm_h3_cb_recv_header(nghttp3_conn *conn, int64_t stream_id, | ||
| 199 | int32_t token, nghttp3_rcbuf *name, | ||
| 200 | nghttp3_rcbuf *value, uint8_t flags, | ||
| 201 | void *conn_user_data, void *stream_user_data) { | ||
| 202 | dTHX; | ||
| 203 | hm_h3_stream *st = (hm_h3_stream *)stream_user_data; | ||
| 204 | nghttp3_vec n, v; | ||
| 205 | (void)conn; (void)stream_id; (void)token; (void)flags; (void)conn_user_data; | ||
| 206 | if (!st || !st->env) return 0; | ||
| 207 | n = nghttp3_rcbuf_get_buf(name); | ||
| 208 | v = nghttp3_rcbuf_get_buf(value); | ||
| 209 | /* The decoded block is bounded here as on h2 (hm_h2_cb_header): the | ||
| 210 | * nghttp3 setting advertises the ceiling, this enforces it. */ | ||
| 211 | st->hdr_bytes += n.len + v.len + 32; | ||
| 212 | if (st->hdr_bytes > HM_H2_MAX_HEADER_LIST) /* # uncoverable branch true */ | ||
| 213 | return NGHTTP3_ERR_CALLBACK_FAILURE; /* # uncoverable statement */ | ||
| 214 | /* The h2 implementation, shared rather than copied: HTTP/3's | ||
| 215 | * pseudo-headers are HTTP/2's and QPACK yields the same lowercase pairs. */ | ||
| 216 | hm_h2_add_header(aTHX_ st->env, (const char *)n.base, n.len, | ||
| 217 | (const char *)v.base, v.len); | ||
| 218 | return 0; | ||
| 219 | } | ||
| 220 | |||
| 221 | /* The h3 counterpart of the HTTP/2 rule: an Extended CONNECT stream is | ||
| 222 | * dispatched when its headers are complete, because it has no end for | ||
| 223 | * end_stream to fire on. */ | ||
| 224 | static int hm_h3_cb_end_headers(nghttp3_conn *conn, int64_t stream_id, | ||
| 225 | int fin, void *conn_user_data, | ||
| 226 | void *stream_user_data) { | ||
| 227 | dTHX; | ||
| 228 | hm_qconn *qc = (hm_qconn *)conn_user_data; | ||
| 229 | hm_h3_stream *st = (hm_h3_stream *)stream_user_data; | ||
| 230 | (void)conn; (void)stream_id; (void)fin; | ||
| 231 | if (!st || st->dispatched || !st->env) return 0; | ||
| 232 | if (!hm_h2_is_connect(aTHX_ st->env)) return 0; /* # uncoverable branch false */ | ||
| 233 | st->ready = 1; /* hm_h3_run_ready, after read_pkt returns */ /* # uncoverable statement */ | ||
| 234 | return 0; /* # uncoverable statement */ | ||
| 235 | } | ||
| 236 | |||
| 237 | static int hm_h3_cb_recv_data(nghttp3_conn *conn, int64_t stream_id, | ||
| 238 | const uint8_t *data, size_t datalen, | ||
| 239 | void *conn_user_data, void *stream_user_data) { | ||
| 240 | dTHX; | ||
| 241 | hm_qconn *qc = (hm_qconn *)conn_user_data; | ||
| 242 | hm_h3_stream *st = (hm_h3_stream *)stream_user_data; | ||
| 243 | (void)conn; (void)stream_id; | ||
| 244 | /* Same rule as the h2 path: a handle with a read callback takes these | ||
| 245 | * bytes, because an Extended CONNECT stream never ends and buffering it | ||
| 246 | * would grow forever. */ | ||
| 247 | if (!hm_stream_deliver(aTHX_ qc->srv->lst->udp_fd, qc->id, stream_id, | ||
| 248 | (const char *)data, (STRLEN)datalen, 0) | ||
| 249 | && st && !st->refused) { | ||
| 250 | /* max_body, as on the other two transports. The window below is | ||
| 251 | * credited back whatever happens, so this is the only ceiling. */ | ||
| 252 | if ((st->body ? SvCUR(st->body) : 0) + datalen /* # uncoverable branch true */ | ||
| 253 | > qc->srv->loop->max_read) { | ||
| 254 | st->refused = 1; /* # uncoverable statement */ | ||
| 255 | if (st->body) { SvREFCNT_dec(st->body); st->body = NULL; } /* # uncoverable statement */ | ||
| 256 | if (st->env && !st->dispatched) { /* # uncoverable statement */ | ||
| 257 | SvREFCNT_dec((SV *)st->env); /* # uncoverable statement */ | ||
| 258 | st->env = NULL; /* # uncoverable statement */ | ||
| 259 | if (st->resp_body) SvREFCNT_dec(st->resp_body); /* # uncoverable statement */ | ||
| 260 | st->resp_body = newSVpvs("Payload Too Large"); /* # uncoverable statement */ | ||
| 261 | st->blen = 17; /* # uncoverable statement */ | ||
| 262 | hm_h3_submit_status(aTHX_ qc, st, 413, "text/plain"); /* # uncoverable statement */ | ||
| 263 | } | ||
| 264 | nghttp3_conn_shutdown_stream_read(conn, stream_id); /* # uncoverable statement */ | ||
| 265 | } else { | ||
| 266 | if (!st->body) st->body = newSVpvs(""); | ||
| 267 | sv_catpvn(st->body, (const char *)data, datalen); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | /* Flow control is ngtcp2's here, not nghttp3's - the opposite of h2, | ||
| 271 | * where nghttp2 owned it and there was nothing to do. Forgetting this | ||
| 272 | * stalls any upload larger than the initial window. */ | ||
| 273 | ngtcp2_conn_extend_max_stream_offset(qc->conn, stream_id, datalen); | ||
| 274 | ngtcp2_conn_extend_max_offset(qc->conn, datalen); | ||
| 275 | return 0; | ||
| 276 | } | ||
| 277 | |||
| 278 | /* Bytes nghttp3 consumed off a stream without handing them to recv_data - | ||
| 279 | * QPACK and control-stream traffic. The window has to be reopened for those | ||
| 280 | * too, or the peer's encoder stream stalls and every later request blocks. */ | ||
| 281 | /* | ||
| 282 | # uncoverable statement | ||
| 283 | # uncoverable subroutine */ | ||
| 284 | static int hm_h3_cb_deferred_consume(nghttp3_conn *conn, int64_t stream_id, | ||
| 285 | size_t consumed, void *conn_user_data, | ||
| 286 | void *stream_user_data) { | ||
| 287 | hm_qconn *qc = (hm_qconn *)conn_user_data; /* # uncoverable statement */ | ||
| 288 | (void)conn; (void)stream_user_data; /* # uncoverable statement */ | ||
| 289 | ngtcp2_conn_extend_max_stream_offset(qc->conn, stream_id, consumed); /* # uncoverable statement */ | ||
| 290 | ngtcp2_conn_extend_max_offset(qc->conn, consumed); /* # uncoverable statement */ | ||
| 291 | return 0; /* # uncoverable statement */ | ||
| 292 | } | ||
| 293 | |||
| 294 | static int hm_h3_cb_end_stream(nghttp3_conn *conn, int64_t stream_id, | ||
| 295 | void *conn_user_data, void *stream_user_data) { | ||
| 296 | dTHX; | ||
| 297 | hm_qconn *qc = (hm_qconn *)conn_user_data; | ||
| 298 | hm_h3_stream *st = (hm_h3_stream *)stream_user_data; | ||
| 299 | (void)conn; (void)stream_id; | ||
| 300 | if (!st || st->dispatched || st->refused) return 0; | ||
| 301 | st->fin = 1; | ||
| 302 | /* NOT dispatched from here: this is a callback inside | ||
| 303 | * ngtcp2_conn_read_pkt, and a handler that waited pumped a loop whose | ||
| 304 | * expiry sweep freed this connection under the frame (then the | ||
| 305 | * response was written into freed memory). hm_h3_run_ready hands it | ||
| 306 | * over once the library has returned. */ | ||
| 307 | st->ready = 1; | ||
| 308 | return 0; | ||
| 309 | } | ||
| 310 | |||
| 311 | static int hm_h3_cb_stream_close(nghttp3_conn *conn, int64_t stream_id, | ||
| 312 | uint64_t app_error_code, | ||
| 313 | void *conn_user_data, void *stream_user_data) { | ||
| 314 | dTHX; | ||
| 315 | hm_qconn *qc = (hm_qconn *)conn_user_data; | ||
| 316 | hm_h3_stream *st = (hm_h3_stream *)stream_user_data; | ||
| 317 | (void)app_error_code; | ||
| 318 | /* A peer can reset one stream out of many; a stream handle open on it | ||
| 319 | * has to hear about that before st is freed underneath it. */ | ||
| 320 | hm_stream_h2_gone(aTHX_ qc->srv->lst->udp_fd, qc->id, stream_id); | ||
| 321 | if (st) { | ||
| 322 | nghttp3_conn_set_stream_user_data(conn, stream_id, NULL); | ||
| 323 | hm_h3_stream_free(aTHX_ qc, st); | ||
| 324 | } | ||
| 325 | return 0; | ||
| 326 | } | ||
| 327 | |||
| 328 | /* nghttp3 has finished with the first n bytes it was handed: they can be | ||
| 329 | * released. This is the half nghttp2 did not need, because there the bytes | ||
| 330 | * were memcpy'd into a window the library owned. */ | ||
| 331 | static int hm_h3_cb_acked_stream_data(nghttp3_conn *conn, int64_t stream_id, | ||
| 332 | uint64_t datalen, void *conn_user_data, | ||
| 333 | void *stream_user_data) { | ||
| 334 | hm_h3_stream *st = (hm_h3_stream *)stream_user_data; | ||
| 335 | (void)conn; (void)stream_id; (void)conn_user_data; | ||
| 336 | if (st) st->resp_acked += (size_t)datalen; | ||
| 337 | return 0; | ||
| 338 | } | ||
| 339 | |||
| 340 | /* | ||
| 341 | # uncoverable statement | ||
| 342 | # uncoverable subroutine */ | ||
| 343 | static int hm_h3_cb_stop_sending(nghttp3_conn *conn, int64_t stream_id, | ||
| 344 | uint64_t app_error_code, void *conn_user_data, | ||
| 345 | void *stream_user_data) { | ||
| 346 | hm_qconn *qc = (hm_qconn *)conn_user_data; /* # uncoverable statement */ | ||
| 347 | (void)conn; (void)stream_user_data; /* # uncoverable statement */ | ||
| 348 | ngtcp2_conn_shutdown_stream_read(qc->conn, 0, stream_id, app_error_code); /* # uncoverable statement */ | ||
| 349 | return 0; /* # uncoverable statement */ | ||
| 350 | } | ||
| 351 | |||
| 352 | /* | ||
| 353 | # uncoverable statement | ||
| 354 | # uncoverable subroutine */ | ||
| 355 | static int hm_h3_cb_reset_stream(nghttp3_conn *conn, int64_t stream_id, | ||
| 356 | uint64_t app_error_code, void *conn_user_data, | ||
| 357 | void *stream_user_data) { | ||
| 358 | hm_qconn *qc = (hm_qconn *)conn_user_data; /* # uncoverable statement */ | ||
| 359 | (void)conn; (void)stream_user_data; /* # uncoverable statement */ | ||
| 360 | ngtcp2_conn_shutdown_stream_write(qc->conn, 0, stream_id, app_error_code); /* # uncoverable statement */ | ||
| 361 | return 0; /* # uncoverable statement */ | ||
| 362 | } | ||
| 363 | |||
| 364 | static void hm_h3_cb_rand(uint8_t *dest, size_t destlen) { | ||
| 365 | hm_quic_rand_bytes(dest, destlen); | ||
| 366 | } | ||
| 367 | |||
| 368 | /* ---- response ------------------------------------------------------------ */ | ||
| 369 | |||
| 370 | /* The data reader. It hands back POINTERS, and everything they point at must | ||
| 371 | * stay valid until acked_stream_data says otherwise - which is why the SV is | ||
| 372 | * held with an explicit reference and the file source has a bounce buffer | ||
| 373 | * that is not refilled until the previous fill is acked. | ||
| 374 | * | ||
| 375 | * The in-memory case points straight into the SV: zero-copy, and strictly | ||
| 376 | * better than the h2 path's memcpy. It is safe because a Writer buffers to | ||
| 377 | * close and never appends to a body with bytes in flight. | ||
| 378 | * | ||
| 379 | * sendfile cannot be used anywhere here: these bytes have to be encrypted | ||
| 380 | * into QUIC packets in userspace, so there is no kernel path for them. */ | ||
| 381 | static nghttp3_ssize hm_h3_read_data(nghttp3_conn *conn, int64_t stream_id, | ||
| 382 | nghttp3_vec *vec, size_t veccnt, | ||
| 383 | uint32_t *pflags, void *conn_user_data, | ||
| 384 | void *stream_user_data) { | ||
| 385 | dTHX; | ||
| 386 | hm_h3_stream *st = (hm_h3_stream *)stream_user_data; | ||
| 387 | (void)conn; (void)stream_id; (void)conn_user_data; | ||
| 388 | if (!st || veccnt == 0) { *pflags |= NGHTTP3_DATA_FLAG_EOF; return 0; } | ||
| 389 | |||
| 390 | if (st->src_sv) { /* a file body */ | ||
| 391 | size_t want; | ||
| 392 | ssize_t got; | ||
| 393 | /* Not refilled while the previous fill is unacked: nghttp3 still | ||
| 394 | * holds a pointer into this buffer. */ | ||
| 395 | if (st->resp_off > st->resp_acked) return NGHTTP3_ERR_WOULDBLOCK; | ||
| 396 | if (st->src_rem == 0) { *pflags |= NGHTTP3_DATA_FLAG_EOF; return 0; } /* # uncoverable branch true */ | ||
| 397 | if (!st->src_buf) { | ||
| 398 | st->src_buflen = HM_BSRC_CHUNK; | ||
| 399 | st->src_buf = (char *)hm_xmalloc(st->src_buflen); | ||
| 400 | } | ||
| 401 | want = st->src_rem < st->src_buflen ? (size_t)st->src_rem | ||
| 402 | : st->src_buflen; | ||
| 403 | got = hm_os_pread(st->src_fd, st->src_buf, want, st->src_off); | ||
| 404 | if (got <= 0) { /* truncated under us */ /* # uncoverable branch true */ | ||
| 405 | st->src_rem = 0; /* # uncoverable statement */ | ||
| 406 | *pflags |= NGHTTP3_DATA_FLAG_EOF; /* # uncoverable statement */ | ||
| 407 | return 0; /* # uncoverable statement */ | ||
| 408 | } | ||
| 409 | st->src_off += got; | ||
| 410 | st->src_rem -= (UV)got; | ||
| 411 | st->resp_off += (size_t)got; | ||
| 412 | vec[0].base = (uint8_t *)st->src_buf; | ||
| 413 | vec[0].len = (size_t)got; | ||
| 414 | if (st->src_rem == 0) *pflags |= NGHTTP3_DATA_FLAG_EOF; | ||
| 415 | return 1; | ||
| 416 | } | ||
| 417 | |||
| 418 | { | ||
| 419 | STRLEN blen = 0; | ||
| 420 | const char *bp = ""; | ||
| 421 | if (st->resp_body) bp = SvPV(st->resp_body, blen); | ||
| 422 | if (st->resp_off >= blen) { *pflags |= NGHTTP3_DATA_FLAG_EOF; return 0; } /* # uncoverable branch true */ | ||
| 423 | vec[0].base = (uint8_t *)(bp + st->resp_off); | ||
| 424 | vec[0].len = blen - st->resp_off; | ||
| 425 | st->resp_off = blen; | ||
| 426 | *pflags |= NGHTTP3_DATA_FLAG_EOF; | ||
| 427 | return 1; | ||
| 428 | } | ||
| 429 | } | ||
| 430 | |||
| 431 | static void hm_h3_submit_response(pTHX_ hm_qconn *qc, hm_h3_stream *st, | ||
| 432 | int status, AV *hav) { | ||
| 433 | SSize_t hn = hav ? av_len(hav) + 1 : 0; | ||
| 434 | /* :status, every pair, and Date: the bound on the array the loop | ||
| 435 | * fills, grown with every unconditional header (see hm_http2.h) */ | ||
| 436 | size_t maxnv = 2 + (size_t)(hn / 2); | ||
| 437 | nghttp3_nv *nva = (nghttp3_nv *)hm_xmalloc(maxnv * sizeof(nghttp3_nv)); | ||
| 438 | char **freelist = (char **)hm_xmalloc(maxnv * sizeof(char *)); | ||
| 439 | size_t nfree = 0, n = 0; | ||
| 440 | char sbuf[8]; | ||
| 441 | int sl, has_date = 0; | ||
| 442 | nghttp3_data_reader dr; | ||
| 443 | SSize_t i; | ||
| 444 | |||
| 445 | if (status < 100 || status > 999) status = 500; /* sbuf is 8 bytes */ | ||
| 446 | sl = snprintf(sbuf, sizeof(sbuf), "%d", status); | ||
| 447 | nva[n].name = (uint8_t *)":status"; nva[n].namelen = 7; | ||
| 448 | nva[n].value = (uint8_t *)sbuf; nva[n].valuelen = (size_t)sl; | ||
| 449 | nva[n].flags = NGHTTP3_NV_FLAG_NONE; n++; | ||
| 450 | |||
| 451 | for (i = 0; i + 1 < hn; i += 2) { | ||
| 452 | SV **k = av_fetch(hav, i, 0); | ||
| 453 | SV **v = av_fetch(hav, i + 1, 0); | ||
| 454 | STRLEN kl, vl; const char *ks, *vs; | ||
| 455 | char *lname; STRLEN j; | ||
| 456 | if (!k || !*k || !v || !*v) continue; | ||
| 457 | ks = SvPV(*k, kl); vs = SvPV(*v, vl); | ||
| 458 | lname = (char *)hm_xmalloc(kl ? kl : 1); | ||
| 459 | for (j = 0; j < kl; j++) { | ||
| 460 | unsigned char ch = (unsigned char)ks[j]; | ||
| 461 | lname[j] = (ch >= 'A' && ch <= 'Z') ? (char)(ch + 32) : (char)ch; | ||
| 462 | } | ||
| 463 | /* HTTP/3 forbids the same set HTTP/2 does, so this is hm_http2.h's | ||
| 464 | * predicate rather than a second copy of the list. */ | ||
| 465 | if (hm_h2_hopbyhop(lname, kl)) { free(lname); continue; } /* # uncoverable branch true */ | ||
| 466 | if (kl == 4 && memcmp(lname, "date", 4) == 0) has_date = 1; | ||
| 467 | freelist[nfree++] = lname; | ||
| 468 | nva[n].name = (uint8_t *)lname; nva[n].namelen = kl; | ||
| 469 | nva[n].value = (uint8_t *)vs; nva[n].valuelen = vl; | ||
| 470 | nva[n].flags = NGHTTP3_NV_FLAG_NONE; n++; | ||
| 471 | } | ||
| 472 | if (!has_date) { | ||
| 473 | const char *dh = hm_date_header(qc->srv->loop); | ||
| 474 | if (dh) { | ||
| 475 | nva[n].name = (uint8_t *)"date"; nva[n].namelen = 4; | ||
| 476 | nva[n].value = (uint8_t *)(dh + 6); | ||
| 477 | nva[n].valuelen = HM_DATE_LEN - 8; | ||
| 478 | nva[n].flags = NGHTTP3_NV_FLAG_NONE; n++; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | st->status = status; | ||
| 483 | st->responded = 1; | ||
| 484 | st->resp_off = 0; | ||
| 485 | st->resp_acked = 0; | ||
| 486 | dr.read_data = hm_h3_read_data; | ||
| 487 | /* nva is copied by nghttp3 unless NO_COPY is asked for, so the freelist | ||
| 488 | * below is safe - the same contract hm_h2_submit_response relies on. */ | ||
| 489 | if (nghttp3_conn_submit_response((nghttp3_conn *)qc->h3, st->id, | ||
| 490 | nva, n, &dr) != 0) | ||
| 491 | /* refused: the stream is reset rather than left silent */ | ||
| 492 | (void)nghttp3_conn_close_stream((nghttp3_conn *)qc->h3, st->id, /* # uncoverable statement */ | ||
| 493 | NGHTTP3_H3_INTERNAL_ERROR); | ||
| 494 | /* NOT counted here. The h2 path adds the body length at submit | ||
| 495 | * because that is its only chance, but on QUIC every datagram is | ||
| 496 | * counted as it is actually sent - adding it here as well counts | ||
| 497 | * the same bytes twice. */ | ||
| 498 | |||
| 499 | for (n = 0; n < nfree; n++) free(freelist[n]); | ||
| 500 | free(freelist); | ||
| 501 | free(nva); | ||
| 502 | } | ||
| 503 | |||
| 504 | /* | ||
| 505 | # uncoverable statement | ||
| 506 | # uncoverable subroutine */ | ||
| 507 | static void hm_h3_submit_status(pTHX_ hm_qconn *qc, hm_h3_stream *st, | ||
| 508 | int status, const char *ctype) { | ||
| 509 | AV *hav = newAV(); /* # uncoverable statement */ | ||
| 510 | av_push(hav, newSVpvs("content-type")); /* # uncoverable statement */ | ||
| 511 | av_push(hav, newSVpv(ctype, 0)); /* # uncoverable statement */ | ||
| 512 | hm_h3_submit_response(aTHX_ qc, st, status, hav); /* # uncoverable statement */ | ||
| 513 | SvREFCNT_dec((SV *)hav); /* # uncoverable statement */ | ||
| 514 | } | ||
| 515 | |||
| 516 | /* A file body onto the stream, read per window rather than slurped. */ | ||
| 517 | static int hm_h3_body_src(pTHX_ hm_h3_stream *st, SV **s2, AV *hav) { | ||
| 518 | hm_bsrc bs; | ||
| 519 | UV clv = 0; | ||
| 520 | int cl_seen; | ||
| 521 | if (!(s2 && SvROK(*s2)) || SvTYPE(SvRV(*s2)) == SVt_PVAV) return 0; | ||
| 522 | cl_seen = hm_hav_clen(aTHX_ hav, &clv); | ||
| 523 | memset(&bs, 0, sizeof bs); | ||
| 524 | if (!hm_bsrc_lift(aTHX_ *s2, &bs, cl_seen, clv)) return 0; /* # uncoverable branch true */ | ||
| 525 | if (bs.kind != 1) { hm_bsrc_release(aTHX_ &bs); return 0; } /* # uncoverable branch true */ | ||
| 526 | st->src_sv = bs.sv; | ||
| 527 | st->src_fd = bs.fd; | ||
| 528 | st->src_off = bs.off; | ||
| 529 | st->src_rem = bs.remaining; | ||
| 530 | st->blen = (size_t)bs.remaining; | ||
| 531 | return 1; | ||
| 532 | } | ||
| 533 | |||
| 534 | /* ---- the streaming writer ------------------------------------------------ */ | ||
| 535 | |||
| 536 | static hm_qconn *hm_h3_qconn(int fd, UV qid) { | ||
| 537 | hm_loop *loop = hm_cur_loop; | ||
| 538 | hm_listener *lst = loop ? hm_listener_for_udp(loop, fd) : NULL; | ||
| 539 | hm_quic_srv *srv = lst ? (hm_quic_srv *)lst->quic : NULL; | ||
| 540 | return srv ? hm_quic_conn_by_id(srv, qid) : NULL; | ||
| 541 | } | ||
| 542 | |||
| 543 | /* 1 buffered, -1 stale. */ | ||
| 544 | static int hm_h3_writer_write(pTHX_ int fd, UV qid, int64_t sid, SV *data) { | ||
| 545 | hm_qconn *qc = hm_h3_qconn(fd, qid); | ||
| 546 | hm_h3_stream *st = qc ? hm_h3_stream_find(qc, sid) : NULL; | ||
| 547 | /* After close the reader has handed nghttp3 a pointer into resp_body: | ||
| 548 | * an append could move it, and a retransmission would then read freed | ||
| 549 | * memory out to the peer. */ | ||
| 550 | if (!st || st->responded) return -1; | ||
| 551 | if (!st->resp_body) st->resp_body = newSVpvs(""); | ||
| 552 | sv_catsv(st->resp_body, data); | ||
| 553 | return 1; | ||
| 554 | } | ||
| 555 | |||
| 556 | static void hm_h3_writer_close(pTHX_ int fd, UV qid, int64_t sid) { | ||
| 557 | hm_qconn *qc = hm_h3_qconn(fd, qid); | ||
| 558 | hm_h3_stream *st = qc ? hm_h3_stream_find(qc, sid) : NULL; | ||
| 559 | AV *hav; | ||
| 560 | if (!st || st->responded) return; /* a second close: no-op */ | ||
| 561 | { STRLEN l; if (st->resp_body) { (void)SvPV(st->resp_body, l); st->blen = l; } } | ||
| 562 | hav = (st->resp_headers && SvROK(st->resp_headers)) | ||
| 563 | ? (AV *)SvRV(st->resp_headers) : NULL; | ||
| 564 | hm_h3_submit_response(aTHX_ qc, st, st->resp_status ? st->resp_status : 200, | ||
| 565 | hav); | ||
| 566 | hm_quic_write(aTHX_ qc); | ||
| 567 | } | ||
| 568 | |||
| 569 | /* psgi.streaming responder for h3: 3-arg delivers, 2-arg buffers via a | ||
| 570 | * Hyperman::Writer (h3 form) that submits at close. */ | ||
| 571 | XS_INTERNAL(hm_xs_h3responder); | ||
| 572 | XS_INTERNAL(hm_xs_h3responder) { | ||
| 573 | dXSARGS; | ||
| 574 | hm_clos *cl = hm_clos_of(aTHX_ cv); | ||
| 575 | hm_qconn *qc; | ||
| 576 | hm_h3_stream *st; | ||
| 577 | AV *rav, *hav = NULL; | ||
| 578 | SSize_t n; | ||
| 579 | SV **s0, **s1; | ||
| 580 | int fd, status; | ||
| 581 | UV qid; | ||
| 582 | int64_t sid; | ||
| 583 | if (!cl) XSRETURN_EMPTY; | ||
| 584 | fd = (int)cl->i; qid = cl->u; sid = (int64_t)SvIV(cl->d); | ||
| 585 | qc = hm_h3_qconn(fd, qid); | ||
| 586 | st = qc ? hm_h3_stream_find(qc, sid) : NULL; | ||
| 587 | if (!st) XSRETURN_EMPTY; | ||
| 588 | /* Answered already (a second responder call): a second submit would | ||
| 589 | * reset the reader's offsets under bytes nghttp3 still holds. */ | ||
| 590 | if (st->responded) XSRETURN_EMPTY; /* # uncoverable statement */ | ||
| 591 | if (items < 1 || !(SvROK(ST(0)) && SvTYPE(SvRV(ST(0))) == SVt_PVAV)) | ||
| 592 | croak("Hyperman responder expects an array reference"); /* # uncoverable statement */ | ||
| 593 | rav = (AV *)SvRV(ST(0)); | ||
| 594 | n = av_len(rav) + 1; | ||
| 595 | s0 = av_fetch(rav, 0, 0); | ||
| 596 | s1 = av_fetch(rav, 1, 0); | ||
| 597 | status = s0 ? (int)SvIV(*s0) : 200; | ||
| 598 | if (s1 && SvROK(*s1) && SvTYPE(SvRV(*s1)) == SVt_PVAV) hav = (AV *)SvRV(*s1); | ||
| 599 | |||
| 600 | if (n >= 3) { | ||
| 601 | SV **s2 = av_fetch(rav, 2, 0); /* # uncoverable statement */ | ||
| 602 | if (st->resp_body) { SvREFCNT_dec(st->resp_body); st->resp_body = NULL; } /* # uncoverable statement */ | ||
| 603 | hm_h3_src_free(aTHX_ st); /* # uncoverable statement */ | ||
| 604 | if (!hm_h3_body_src(aTHX_ st, s2, hav)) /* # uncoverable statement */ | ||
| 605 | st->resp_body = hm_h2_body_sv(aTHX_ s2, &st->blen); /* # uncoverable statement */ | ||
| 606 | hm_h3_submit_response(aTHX_ qc, st, status, hav); /* # uncoverable statement */ | ||
| 607 | hm_quic_write(aTHX_ qc); /* # uncoverable statement */ | ||
| 608 | XSRETURN_EMPTY; /* # uncoverable statement */ | ||
| 609 | } | ||
| 610 | st->resp_status = status; | ||
| 611 | if (hav) st->resp_headers = newRV_inc((SV *)hav); | ||
| 612 | if (!st->resp_body) st->resp_body = newSVpvs(""); | ||
| 613 | { | ||
| 614 | AV *w = newAV(); | ||
| 615 | SV *wrv; | ||
| 616 | av_push(w, newSViv(fd)); | ||
| 617 | av_push(w, newSVuv(qid)); | ||
| 618 | av_push(w, newSViv((IV)sid)); | ||
| 619 | av_push(w, newSVpvs("h3")); | ||
| 620 | wrv = newRV_noinc((SV *)w); | ||
| 621 | sv_bless(wrv, gv_stashpv("Hyperman::Writer", GV_ADD)); | ||
| 622 | ST(0) = sv_2mortal(wrv); | ||
| 623 | XSRETURN(1); | ||
| 624 | } | ||
| 625 | } | ||
| 626 | |||
| 627 | static void hm_h3_run_delayed(pTHX_ hm_qconn *qc, hm_h3_stream *st, SV *code) { | ||
| 628 | SV *responder = hm_closure(aTHX_ hm_xs_h3responder, NULL, NULL, NULL, | ||
| 629 | sv_2mortal(newSViv((IV)st->id)), | ||
| 630 | (IV)qc->srv->lst->udp_fd, qc->id); | ||
| 631 | int64_t sid = st->id; | ||
| 632 | dSP; | ||
| 633 | ENTER; SAVETMPS; | ||
| 634 | PUSHMARK(SP); | ||
| 635 | XPUSHs(responder); | ||
| 636 | PUTBACK; | ||
| 637 | call_sv(code, G_DISCARD | G_EVAL); | ||
| 638 | /* The coderef may have waited, and the stream may have been reset | ||
| 639 | * meanwhile: look it up again. A 500 only for a stream that has not | ||
| 640 | * answered - after the responder ran, the body nghttp3 holds is the | ||
| 641 | * response, and a die after that is logged, not served. */ | ||
| 642 | st = hm_h3_stream_find(qc, sid); | ||
| 643 | if (SvTRUE(ERRSV) && st && !st->responded) { | ||
| 644 | if (st->resp_body) SvREFCNT_dec(st->resp_body); /* # uncoverable statement */ | ||
| 645 | st->resp_body = newSVpvs("Internal Server Error"); /* # uncoverable statement */ | ||
| 646 | st->blen = 21; /* # uncoverable statement */ | ||
| 647 | hm_h3_submit_status(aTHX_ qc, st, 500, "text/plain"); /* # uncoverable statement */ | ||
| 648 | } | ||
| 649 | FREETMPS; LEAVE; /* # uncoverable statement */ | ||
| 650 | SvREFCNT_dec(responder); | ||
| 651 | } | ||
| 652 | |||
| 653 | /* sync array / streaming CV / invalid -> a response */ | ||
| 654 | static void hm_h3_respond(pTHX_ hm_qconn *qc, hm_h3_stream *st, SV *resp) { | ||
| 655 | if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV) { | ||
| 656 | AV *rav = (AV *)SvRV(resp); | ||
| 657 | SV **s0 = av_fetch(rav, 0, 0); | ||
| 658 | SV **s1 = av_fetch(rav, 1, 0); | ||
| 659 | SV **s2 = av_fetch(rav, 2, 0); | ||
| 660 | int status = s0 ? (int)SvIV(*s0) : 200; | ||
| 661 | AV *hav = (s1 && SvROK(*s1) && SvTYPE(SvRV(*s1)) == SVt_PVAV) | ||
| 662 | ? (AV *)SvRV(*s1) : NULL; | ||
| 663 | if (st->resp_body) { SvREFCNT_dec(st->resp_body); st->resp_body = NULL; } | ||
| 664 | hm_h3_src_free(aTHX_ st); | ||
| 665 | if (!hm_h3_body_src(aTHX_ st, s2, hav)) | ||
| 666 | st->resp_body = hm_h2_body_sv(aTHX_ s2, &st->blen); | ||
| 667 | hm_h3_submit_response(aTHX_ qc, st, status, hav); | ||
| 668 | } else if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVCV) { | ||
| 669 | hm_h3_run_delayed(aTHX_ qc, st, resp); | ||
| 670 | } else { | ||
| 671 | if (st->resp_body) SvREFCNT_dec(st->resp_body); /* # uncoverable statement */ | ||
| 672 | st->resp_body = newSVpvs("Internal Server Error"); /* # uncoverable statement */ | ||
| 673 | st->blen = 21; /* # uncoverable statement */ | ||
| 674 | hm_h3_submit_status(aTHX_ qc, st, 500, "text/plain"); /* # uncoverable statement */ | ||
| 675 | } | ||
| 676 | } | ||
| 677 | |||
| 678 | /* Deliver a parked response once its Future settles. */ | ||
| 679 | XS_INTERNAL(hm_xs_h3park_cb); | ||
| 680 | XS_INTERNAL(hm_xs_h3park_cb) { | ||
| 681 | dXSARGS; | ||
| 682 | hm_clos *cl = hm_clos_of(aTHX_ cv); | ||
| 683 | hm_qconn *qc; | ||
| 684 | hm_h3_stream *st; | ||
| 685 | SV *f, *resp = NULL; | ||
| 686 | IV state; | ||
| 687 | int fd; | ||
| 688 | UV qid; | ||
| 689 | int64_t sid; | ||
| 690 | if (!cl || items < 1) XSRETURN_EMPTY; | ||
| 691 | f = ST(0); | ||
| 692 | fd = (int)cl->i; qid = cl->u; sid = (int64_t)SvIV(cl->d); | ||
| 693 | qc = hm_h3_qconn(fd, qid); | ||
| 694 | st = qc ? hm_h3_stream_find(qc, sid) : NULL; | ||
| 695 | if (!st) XSRETURN_EMPTY; | ||
| 696 | state = hm_any_state(aTHX_ f); | ||
| 697 | if (state == HMF_DONE) { | ||
| 698 | AV *vals = newAV(); | ||
| 699 | SV **e; | ||
| 700 | hm_any_values(aTHX_ f, state, vals); | ||
| 701 | e = av_len(vals) >= 0 ? av_fetch(vals, 0, 0) : NULL; | ||
| 702 | if (e) resp = sv_mortalcopy(*e); | ||
| 703 | SvREFCNT_dec((SV *)vals); | ||
| 704 | } | ||
| 705 | st->awaiting = 0; | ||
| 706 | if (st->resp_f) { SvREFCNT_dec(st->resp_f); st->resp_f = NULL; } | ||
| 707 | if (st->responded) XSRETURN_EMPTY; /* a handle answered meanwhile */ | ||
| 708 | hm_h3_respond(aTHX_ qc, st, resp); | ||
| 709 | if (hm_logging(qc->srv->loop) && st->env_rv) | ||
| 710 | hm_access_log(aTHX_ qc->srv->loop, st->env_rv, st->status, /* # uncoverable statement */ | ||
| 711 | (ssize_t)st->blen); /* # uncoverable statement */ | ||
| 712 | hm_quic_write(aTHX_ qc); | ||
| 713 | XSRETURN_EMPTY; | ||
| 714 | } | ||
| 715 | |||
| 716 | static void hm_h3_dispatch(pTHX_ hm_qconn *qc, hm_h3_stream *st) { | ||
| 717 | hm_loop *loop = qc->srv->loop; | ||
| 718 | HV *env = st->env; | ||
| 719 | SV *env_rv, *resp; | ||
| 720 | int64_t sid = st->id; | ||
| 721 | st->dispatched = 1; | ||
| 722 | st->env = NULL; | ||
| 723 | if (!hv_exists(env, "psgi.input", 10)) { | ||
| 724 | if (st->body) | ||
| 725 | hv_stores(env, "psgi.input", | ||
| 726 | hm_new_input(aTHX_ SvPVX(st->body), SvCUR(st->body))); | ||
| 727 | else | ||
| 728 | hv_stores(env, "psgi.input", hm_empty_input_sv(aTHX)); | ||
| 729 | } | ||
| 730 | /* The stream ticket, built here because it needs the stream id. */ | ||
| 731 | { | ||
| 732 | AV *tick = newAV(); | ||
| 733 | av_extend(tick, 2); | ||
| 734 | av_store(tick, 0, newSViv(qc->srv->lst->udp_fd)); | ||
| 735 | av_store(tick, 1, newSVuv(qc->id)); | ||
| 736 | av_store(tick, 2, newSViv((IV)st->id)); | ||
| 737 | hv_stores(env, "psgix.hyperman.stream", newRV_noinc((SV *)tick)); | ||
| 738 | } | ||
| 739 | env_rv = newRV_noinc((SV *)env); | ||
| 740 | resp = hm_call_app(aTHX_ loop, env_rv); | ||
| 741 | hm_count_request(loop); | ||
| 742 | loop->h3_requests++; | ||
| 743 | /* An application that waited pumped this loop, which read this | ||
| 744 | * connection: the peer may have reset the stream, which freed st. Look | ||
| 745 | * it up again rather than trust the pointer the call went in with (the | ||
| 746 | * connection itself cannot have gone: hm_quic_conn_free defers while | ||
| 747 | * in_app). */ | ||
| 748 | st = hm_h3_stream_find(qc, sid); | ||
| 749 | if (!st) { /* # uncoverable branch true */ | ||
| 750 | if (resp) SvREFCNT_dec(resp); /* # uncoverable statement */ | ||
| 751 | SvREFCNT_dec(env_rv); /* # uncoverable statement */ | ||
| 752 | return; /* # uncoverable statement */ | ||
| 753 | } | ||
| 754 | |||
| 755 | if (resp && hm_is_awaitable(aTHX_ resp)) { | ||
| 756 | SV *cb = hm_closure(aTHX_ hm_xs_h3park_cb, NULL, NULL, NULL, | ||
| 757 | sv_2mortal(newSViv((IV)st->id)), | ||
| 758 | (IV)qc->srv->lst->udp_fd, qc->id); | ||
| 759 | st->awaiting = 1; | ||
| 760 | st->env_rv = SvREFCNT_inc(env_rv); | ||
| 761 | st->resp_f = SvREFCNT_inc(resp); /* cancelled on a reset */ | ||
| 762 | hm_any_on_ready(aTHX_ resp, cb); | ||
| 763 | SvREFCNT_dec(cb); | ||
| 764 | SvREFCNT_dec(resp); | ||
| 765 | SvREFCNT_dec(env_rv); | ||
| 766 | return; | ||
| 767 | } | ||
| 768 | |||
| 769 | /* Same rule as the h2 path: a stream handle already answered, so the | ||
| 770 | * application's return value is a sentinel and submitting it would be a | ||
| 771 | * second response on one stream. */ | ||
| 772 | if (st->status) { /* # uncoverable branch true */ | ||
| 773 | if (resp) SvREFCNT_dec(resp); /* # uncoverable statement */ | ||
| 774 | SvREFCNT_dec(env_rv); /* # uncoverable statement */ | ||
| 775 | return; /* # uncoverable statement */ | ||
| 776 | } | ||
| 777 | hm_h3_respond(aTHX_ qc, st, resp); | ||
| 778 | if (hm_logging(loop)) | ||
| 779 | hm_access_log(aTHX_ loop, env_rv, st->status, (ssize_t)st->blen); /* # uncoverable statement */ | ||
| 780 | if (resp) SvREFCNT_dec(resp); | ||
| 781 | SvREFCNT_dec(env_rv); | ||
| 782 | } | ||
| 783 | |||
| 784 | /* Hand every completed request to the application, lowest stream first, | ||
| 785 | * now that ngtcp2 has returned (hm_quic_packet). The h3 counterpart of | ||
| 786 | * hm_h2_run_ready: one handler at a time per connection, the connection | ||
| 787 | * held (in_app) so a free asked for under the handler waits for it, and out | ||
| 788 | * of the expiry heap until the handler returns. -1 if the connection was | ||
| 789 | * freed here. */ | ||
| 790 | static int hm_h3_run_ready(pTHX_ hm_qconn *qc) { | ||
| 791 | if (qc->dispatching || !qc->h3) return 0; | ||
| 792 | for (;;) { | ||
| 793 | hm_h3_stream *st, *pick = NULL; | ||
| 794 | for (st = (hm_h3_stream *)qc->h3_streams; st; st = st->next) | ||
| 795 | if (st->ready && st->env && !st->dispatched | ||
| 796 | && (!pick || st->id < pick->id)) | ||
| 797 | pick = st; | ||
| 798 | if (!pick) return 0; | ||
| 799 | pick->ready = 0; | ||
| 800 | qc->dispatching = 1; | ||
| 801 | qc->in_app = 1; | ||
| 802 | hm_qheap_remove(&qc->srv->loop->qheap, qc); | ||
| 803 | hm_h3_dispatch(aTHX_ qc, pick); | ||
| 804 | qc->in_app = 0; | ||
| 805 | qc->dispatching = 0; | ||
| 806 | if (qc->close_deferred) { /* # uncoverable branch true */ | ||
| 807 | qc->close_deferred = 0; /* # uncoverable statement */ | ||
| 808 | hm_quic_conn_free(aTHX_ qc); /* # uncoverable statement */ | ||
| 809 | return -1; /* # uncoverable statement */ | ||
| 810 | } | ||
| 811 | hm_quic_rearm(qc); | ||
| 812 | } | ||
| 813 | } | ||
| 814 | |||
| 815 | /* ---- session ------------------------------------------------------------- */ | ||
| 816 | |||
| 817 | /* Built from the QUIC handshake_completed callback: the control and QPACK | ||
| 818 | * streams cannot be opened before there is a connection to open them on. */ | ||
| 819 | static int hm_h3_session_start(pTHX_ hm_qconn *qc) { | ||
| 820 | nghttp3_callbacks cbs; | ||
| 821 | nghttp3_settings settings; | ||
| 822 | nghttp3_conn *h3 = NULL; | ||
| 823 | int64_t ctrl = -1, qenc = -1, qdec = -1; | ||
| 824 | |||
| 825 | memset(&cbs, 0, sizeof(cbs)); | ||
| 826 | cbs.begin_headers = hm_h3_cb_begin_headers; | ||
| 827 | cbs.recv_header = hm_h3_cb_recv_header; | ||
| 828 | cbs.end_headers = hm_h3_cb_end_headers; | ||
| 829 | cbs.recv_data = hm_h3_cb_recv_data; | ||
| 830 | cbs.deferred_consume = hm_h3_cb_deferred_consume; | ||
| 831 | cbs.end_stream = hm_h3_cb_end_stream; | ||
| 832 | cbs.stream_close = hm_h3_cb_stream_close; | ||
| 833 | cbs.acked_stream_data = hm_h3_cb_acked_stream_data; | ||
| 834 | cbs.stop_sending = hm_h3_cb_stop_sending; | ||
| 835 | cbs.reset_stream = hm_h3_cb_reset_stream; | ||
| 836 | cbs.rand = hm_h3_cb_rand; | ||
| 837 | |||
| 838 | nghttp3_settings_default(&settings); | ||
| 839 | settings.qpack_max_dtable_capacity = 4096; | ||
| 840 | settings.qpack_blocked_streams = 100; | ||
| 841 | /* The default is 2^62-1, which is no bound at all against QPACK | ||
| 842 | * expansion; the same 64KB HTTP/1.1 and h2 allow. */ | ||
| 843 | settings.max_field_section_size = HM_H2_MAX_HEADER_LIST; | ||
| 844 | /* RFC 9220 Extended CONNECT, the HTTP/3 spelling of the HTTP/2 setting | ||
| 845 | * in hm_h2_submit_our_settings. Same purpose: a client will not try a | ||
| 846 | * WebSocket over h3 unless the server has said it understands | ||
| 847 | * :protocol. */ | ||
| 848 | settings.enable_connect_protocol = 1; | ||
| 849 | |||
| 850 | if (nghttp3_conn_server_new(&h3, &cbs, &settings, NULL, qc) != 0) return -1; /* # uncoverable branch true */ | ||
| 851 | |||
| 852 | if (ngtcp2_conn_open_uni_stream(qc->conn, &ctrl, NULL) != 0 | ||
| 853 | || nghttp3_conn_bind_control_stream(h3, ctrl) != 0 | ||
| 854 | || ngtcp2_conn_open_uni_stream(qc->conn, &qenc, NULL) != 0 | ||
| 855 | || ngtcp2_conn_open_uni_stream(qc->conn, &qdec, NULL) != 0 | ||
| 856 | || nghttp3_conn_bind_qpack_streams(h3, qenc, qdec) != 0) { | ||
| 857 | nghttp3_conn_del(h3); /* # uncoverable statement */ | ||
| 858 | return -1; /* # uncoverable statement */ | ||
| 859 | } | ||
| 860 | qc->h3 = h3; | ||
| 861 | return 0; | ||
| 862 | } | ||
| 863 | |||
| 864 | static void hm_h3_session_free(pTHX_ hm_qconn *qc) { | ||
| 865 | if (!qc->h3) return; | ||
| 866 | while (qc->h3_streams) | ||
| 867 | hm_h3_stream_free(aTHX_ qc, (hm_h3_stream *)qc->h3_streams); | ||
| 868 | nghttp3_conn_del((nghttp3_conn *)qc->h3); | ||
| 869 | qc->h3 = NULL; | ||
| 870 | } | ||
| 871 | |||
| 872 | /* ---- the seams the transport reaches up through ------------------------- */ | ||
| 873 | |||
| 874 | /* Stream bytes off ngtcp2 into nghttp3. The window is credited in three | ||
| 875 | * parts, and every part is needed: recv_data credits the DATA payload, | ||
| 876 | * deferred_consume what QPACK held back, and the RETURN VALUE here is | ||
| 877 | * everything else - HEADERS frames, frame headers, control and QPACK | ||
| 878 | * stream bytes. That last part was discarded, so a connection that had | ||
| 879 | * carried a megabyte of request headers (a browser tab left open) ran out | ||
| 880 | * of connection window and hung, and the control and encoder streams | ||
| 881 | * blocked for good at 256KB. 0 to carry on, -1 to kill the connection. */ | ||
| 882 | static int hm_h3_recv_stream(pTHX_ hm_qconn *qc, int64_t sid, | ||
| 883 | const uint8_t *data, size_t datalen, int fin) { | ||
| 884 | nghttp3_ssize n; | ||
| 885 | PERL_UNUSED_CONTEXT; | ||
| 886 | if (!qc->h3) return 0; /* # uncoverable branch true */ | ||
| 887 | n = nghttp3_conn_read_stream((nghttp3_conn *)qc->h3, sid, data, datalen, fin); | ||
| 888 | if (n < 0) return -1; /* # uncoverable branch true */ | ||
| 889 | if (n > 0) { | ||
| 890 | ngtcp2_conn_extend_max_stream_offset(qc->conn, sid, (uint64_t)n); | ||
| 891 | ngtcp2_conn_extend_max_offset(qc->conn, (uint64_t)n); | ||
| 892 | } | ||
| 893 | return 0; | ||
| 894 | } | ||
| 895 | |||
| 896 | /* ngtcp2 says a stream is finished. nghttp3 has to be told, or its own | ||
| 897 | * bookkeeping keeps the stream alive forever. */ | ||
| 898 | static int hm_h3_stream_gone(pTHX_ hm_qconn *qc, int64_t sid, | ||
| 899 | uint64_t app_error_code) { | ||
| 900 | PERL_UNUSED_CONTEXT; | ||
| 901 | if (!qc->h3) return 0; /* # uncoverable branch true */ | ||
| 902 | if (nghttp3_conn_close_stream((nghttp3_conn *)qc->h3, sid, /* # uncoverable branch true */ | ||
| 903 | app_error_code) != 0) | ||
| 904 | return -1; /* # uncoverable statement */ | ||
| 905 | return 0; | ||
| 906 | } | ||
| 907 | |||
| 908 | static int hm_h3_acked(pTHX_ hm_qconn *qc, int64_t sid, uint64_t n) { | ||
| 909 | PERL_UNUSED_CONTEXT; | ||
| 910 | if (!qc->h3) return 0; /* # uncoverable branch true */ | ||
| 911 | if (nghttp3_conn_add_ack_offset((nghttp3_conn *)qc->h3, sid, n) != 0) /* # uncoverable branch true */ | ||
| 912 | return -1; /* # uncoverable statement */ | ||
| 913 | /* The bounce buffer a file source handed out is free again, so a reader | ||
| 914 | * that deferred on it can be asked once more. */ | ||
| 915 | return hm_h3_resume(aTHX_ qc, sid); | ||
| 916 | } | ||
| 917 | |||
| 918 | /* What HTTP/3 wants sent next, as an iovec the transport hands to ngtcp2. | ||
| 919 | * This is the pull the h2 template has no equivalent of. */ | ||
| 920 | static nghttp3_ssize hm_h3_pull(pTHX_ hm_qconn *qc, int64_t *psid, int *pfin, | ||
| 921 | nghttp3_vec *vec, size_t veccnt) { | ||
| 922 | nghttp3_ssize n; | ||
| 923 | PERL_UNUSED_CONTEXT; | ||
| 924 | *psid = -1; *pfin = 0; | ||
| 925 | if (!qc->h3) return 0; /* # uncoverable branch true */ | ||
| 926 | n = nghttp3_conn_writev_stream((nghttp3_conn *)qc->h3, psid, pfin, | ||
| 927 | vec, veccnt); | ||
| 928 | if (n < 0) { *psid = -1; return -1; } /* # uncoverable branch true */ | ||
| 929 | return n; | ||
| 930 | } | ||
| 931 | |||
| 932 | /* The peer granted more room on a stream that had run out. Without this a | ||
| 933 | * stream blocked once by flow control is blocked FOREVER: nghttp3 stops | ||
| 934 | * offering it and nothing ever puts it back. It shows up as a hang only | ||
| 935 | * under concurrency, when the connection window is exhausted - a single | ||
| 936 | * large response never reaches it. */ | ||
| 937 | static int hm_h3_unblock(pTHX_ hm_qconn *qc, int64_t sid) { | ||
| 938 | PERL_UNUSED_CONTEXT; | ||
| 939 | if (!qc->h3) return 0; /* # uncoverable branch true */ | ||
| 940 | return nghttp3_conn_unblock_stream((nghttp3_conn *)qc->h3, sid) == 0 | ||
| 941 | ? 0 : -1; | ||
| 942 | } | ||
| 943 | |||
| 944 | /* A deferred data reader can be asked again. The file source returns | ||
| 945 | * WOULDBLOCK while its bounce buffer is still unacked, so an ack is exactly | ||
| 946 | * when it becomes worth retrying. */ | ||
| 947 | static int hm_h3_resume(pTHX_ hm_qconn *qc, int64_t sid) { | ||
| 948 | PERL_UNUSED_CONTEXT; | ||
| 949 | if (!qc->h3) return 0; /* # uncoverable branch true */ | ||
| 950 | return nghttp3_conn_resume_stream((nghttp3_conn *)qc->h3, sid) == 0 | ||
| 951 | ? 0 : -1; | ||
| 952 | } | ||
| 953 | |||
| 954 | /* How much of what was pulled the transport actually took. */ | ||
| 955 | static int hm_h3_wrote(pTHX_ hm_qconn *qc, int64_t sid, size_t n) { | ||
| 956 | PERL_UNUSED_CONTEXT; | ||
| 957 | if (!qc->h3 || sid < 0) return 0; | ||
| 958 | return nghttp3_conn_add_write_offset((nghttp3_conn *)qc->h3, sid, n) == 0 | ||
| 959 | ? 0 : -1; | ||
| 960 | } | ||
| 961 | |||
| 962 | #else /* !HM_HAVE_HTTP3 */ | ||
| 963 | |||
| 964 | 12 | static int hm_h3_available(void) { return 0; } | |
| 965 | /* | ||
| 966 | # uncoverable statement | ||
| 967 | # uncoverable subroutine */ | ||
| 968 | 0 | static int hm_h3_writer_write(pTHX_ int fd, UV qid, int64_t sid, SV *d) | |
| 969 | 0 | { PERL_UNUSED_CONTEXT; (void)fd; (void)qid; (void)sid; (void)d; return -1; } /* # uncoverable statement */ | |
| 970 | /* | ||
| 971 | # uncoverable statement | ||
| 972 | # uncoverable subroutine */ | ||
| 973 | 0 | static void hm_h3_writer_close(pTHX_ int fd, UV qid, int64_t sid) | |
| 974 | 0 | { PERL_UNUSED_CONTEXT; (void)fd; (void)qid; (void)sid; } /* # uncoverable statement */ | |
| 975 | |||
| 976 | #endif /* HM_HAVE_HTTP3 */ | ||
| 977 | |||
| 978 | #endif /* HM_HTTP3_H */ |