| 1 | #ifndef FT_H2_H | ||
| 2 | #define FT_H2_H | ||
| 3 | |||
| 4 | /* HTTP/2 client via nghttp2: one connection carries one request stream (for | ||
| 5 | * now - multiplexing across a pooled connection comes later). nghttp2 owns | ||
| 6 | * framing, HPACK and flow control; we feed it received bytes and flush its | ||
| 7 | * output through the same (possibly TLS) socket. The response resolves the | ||
| 8 | * connection's Fetch::Future with a Fetch::Response. | ||
| 9 | * | ||
| 10 | * Included from ft_http.h after ft_recv/ft_send/ft_conn_finish/ft_conn_fail. | ||
| 11 | * Compiles to stubs without -DFT_HAVE_NGHTTP2. */ | ||
| 12 | |||
| 13 | #ifdef FT_HAVE_NGHTTP2 | ||
| 14 | |||
| 15 | #include <nghttp2/nghttp2.h> | ||
| 16 | |||
| 17 | /* nghttp2 wants our socket writes here; return bytes sent or a would-block. */ | ||
| 18 | static ssize_t ft_h2_send_cb(nghttp2_session *s, const uint8_t *data, | ||
| 19 | size_t length, int flags, void *user) { | ||
| 20 | ft_conn *c = (ft_conn *)user; | ||
| 21 | int want = 0; | ||
| 22 | ssize_t n; | ||
| 23 | dTHX; | ||
| 24 | (void)s; (void)flags; | ||
| 25 | n = ft_send(c, data, length, &want); | ||
| 26 | if (n > 0) return n; | ||
| 27 | if (n < 0 && want) return NGHTTP2_ERR_WOULDBLOCK; | ||
| 28 | return NGHTTP2_ERR_CALLBACK_FAILURE; | ||
| 29 | } | ||
| 30 | |||
| 31 | static int ft_h2_header_cb(nghttp2_session *s, const nghttp2_frame *frame, | ||
| 32 | const uint8_t *name, size_t namelen, | ||
| 33 | const uint8_t *value, size_t valuelen, | ||
| 34 | uint8_t flags, void *user) { | ||
| 35 | ft_conn *c = (ft_conn *)user; | ||
| 36 | dTHX; | ||
| 37 | (void)s; (void)flags; | ||
| 38 | if (frame->hd.type != NGHTTP2_HEADERS) return 0; | ||
| 39 | |||
| 40 | /* TRAILERS: a second HEADERS frame, after the DATA. | ||
| 41 | * | ||
| 42 | * These used to be discarded along with everything that was not | ||
| 43 | * HCAT_RESPONSE, which is fine for ordinary HTTP - almost nothing uses | ||
| 44 | * trailers - and fatal for gRPC, where the CALL STATUS lives in them and | ||
| 45 | * nowhere else. A gRPC response is HTTP 200 whether it succeeded or | ||
| 46 | * failed; a client that cannot read trailers can only ever report | ||
| 47 | * success. Hence a separate list rather than merging them into the | ||
| 48 | * response headers: a trailer arrived after the body and a consumer that | ||
| 49 | * cares about the difference must be able to tell. */ | ||
| 50 | if (frame->headers.cat == NGHTTP2_HCAT_HEADERS) { | ||
| 51 | if (namelen && name[0] != ':') { | ||
| 52 | if (!c->trailers) c->trailers = newAV(); | ||
| 53 | av_push(c->trailers, newSVpvn((const char *)name, namelen)); | ||
| 54 | av_push(c->trailers, newSVpvn((const char *)value, valuelen)); | ||
| 55 | } | ||
| 56 | return 0; | ||
| 57 | } | ||
| 58 | if (frame->headers.cat != NGHTTP2_HCAT_RESPONSE) return 0; | ||
| 59 | |||
| 60 | if (namelen == 7 && memcmp(name, ":status", 7) == 0) { | ||
| 61 | c->status = (int)strtol((const char *)value, NULL, 10); | ||
| 62 | } else if (namelen && name[0] != ':') { /* skip other pseudo-headers */ | ||
| 63 | if (!c->headers) c->headers = newAV(); | ||
| 64 | av_push(c->headers, newSVpvn((const char *)name, namelen)); | ||
| 65 | av_push(c->headers, newSVpvn((const char *)value, valuelen)); | ||
| 66 | } | ||
| 67 | return 0; | ||
| 68 | } | ||
| 69 | |||
| 70 | static int ft_h2_data_cb(nghttp2_session *s, uint8_t flags, int32_t stream_id, | ||
| 71 | const uint8_t *data, size_t len, void *user) { | ||
| 72 | ft_conn *c = (ft_conn *)user; | ||
| 73 | dTHX; | ||
| 74 | (void)s; (void)flags; (void)stream_id; | ||
| 75 | if (c->on_body) { /* stream: emit, do not buffer */ | ||
| 76 | ft_emit_body(aTHX_ c, (const char *)data, len); | ||
| 77 | c->body_recv += len; | ||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | if (c->dblen + len > c->dbcap) { | ||
| 81 | c->dbcap = (c->dblen + len) * 2 + 64; | ||
| 82 | Renew(c->dbody, c->dbcap, char); | ||
| 83 | } | ||
| 84 | memcpy(c->dbody + c->dblen, data, len); | ||
| 85 | c->dblen += len; | ||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | |||
| 89 | static int ft_h2_stream_close_cb(nghttp2_session *s, int32_t stream_id, | ||
| 90 | uint32_t error_code, void *user) { | ||
| 91 | ft_conn *c = (ft_conn *)user; | ||
| 92 | (void)s; (void)stream_id; (void)error_code; | ||
| 93 | c->h2_done = 1; | ||
| 94 | return 0; | ||
| 95 | } | ||
| 96 | |||
| 97 | /* request-body data provider for POST/PUT over h2 */ | ||
| 98 | static ssize_t ft_h2_body_read(nghttp2_session *s, int32_t stream_id, | ||
| 99 | uint8_t *buf, size_t length, uint32_t *data_flags, | ||
| 100 | nghttp2_data_source *source, void *user) { | ||
| 101 | ft_conn *c = (ft_conn *)user; | ||
| 102 | STRLEN blen = 0; | ||
| 103 | const char *b; | ||
| 104 | size_t take; | ||
| 105 | dTHX; | ||
| 106 | (void)s; (void)stream_id; (void)source; | ||
| 107 | if (!c->rq_body) { *data_flags |= NGHTTP2_DATA_FLAG_EOF; return 0; } | ||
| 108 | b = SvPV(c->rq_body, blen); | ||
| 109 | take = (blen - c->rq_body_off < length) ? (blen - c->rq_body_off) : length; | ||
| 110 | memcpy(buf, b + c->rq_body_off, take); | ||
| 111 | c->rq_body_off += take; | ||
| 112 | if (c->rq_body_off >= blen) *data_flags |= NGHTTP2_DATA_FLAG_EOF; | ||
| 113 | return (ssize_t)take; | ||
| 114 | } | ||
| 115 | |||
| 116 | #define FT_NV(NAME, VAL, VLEN) \ | ||
| 117 | { (uint8_t *)(NAME), (uint8_t *)(VAL), sizeof(NAME) - 1, (VLEN), \ | ||
| 118 | NGHTTP2_NV_FLAG_NONE } | ||
| 119 | |||
| 120 | /* Initialise the h2 session and submit the request. Returns 0 / -1. */ | ||
| 121 | static int ft_h2_init(pTHX_ ft_conn *c) { | ||
| 122 | nghttp2_session_callbacks *cbs; | ||
| 123 | nghttp2_session *sess; | ||
| 124 | nghttp2_nv *nva; | ||
| 125 | int nvn = 0, i, rv; | ||
| 126 | STRLEN ml, sl, al, pl; | ||
| 127 | const char *m = SvPV(c->rq_method, ml); | ||
| 128 | const char *sc= SvPV(c->rq_scheme, sl); | ||
| 129 | const char *au= SvPV(c->rq_authority, al); | ||
| 130 | const char *pa= SvPV(c->rq_path, pl); | ||
| 131 | SSize_t extra = c->rq_headers ? (av_len(c->rq_headers) + 1) / 2 : 0; | ||
| 132 | int32_t sid; | ||
| 133 | nghttp2_data_provider prd, *prdp = NULL; | ||
| 134 | |||
| 135 | nghttp2_session_callbacks_new(&cbs); | ||
| 136 | nghttp2_session_callbacks_set_send_callback(cbs, ft_h2_send_cb); | ||
| 137 | nghttp2_session_callbacks_set_on_header_callback(cbs, ft_h2_header_cb); | ||
| 138 | nghttp2_session_callbacks_set_on_data_chunk_recv_callback(cbs, ft_h2_data_cb); | ||
| 139 | nghttp2_session_callbacks_set_on_stream_close_callback(cbs, ft_h2_stream_close_cb); | ||
| 140 | rv = nghttp2_session_client_new(&sess, cbs, c); | ||
| 141 | nghttp2_session_callbacks_del(cbs); | ||
| 142 | if (rv != 0) return -1; | ||
| 143 | c->h2 = sess; | ||
| 144 | |||
| 145 | /* The client connection preface is the 24-byte magic (nghttp2 emits it | ||
| 146 | * automatically) followed by a SETTINGS frame, which we must submit | ||
| 147 | * ourselves - without it the peer sees magic then HEADERS and aborts | ||
| 148 | * with "SETTINGS expected". */ | ||
| 149 | { | ||
| 150 | nghttp2_settings_entry iv[1] = { | ||
| 151 | { NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100 } | ||
| 152 | }; | ||
| 153 | if (nghttp2_submit_settings(sess, NGHTTP2_FLAG_NONE, iv, 1) != 0) | ||
| 154 | return -1; | ||
| 155 | } | ||
| 156 | |||
| 157 | Newx(nva, 4 + extra, nghttp2_nv); | ||
| 158 | { nghttp2_nv a = FT_NV(":method", m, ml); nva[nvn++] = a; } | ||
| 159 | { nghttp2_nv a = FT_NV(":scheme", sc, sl); nva[nvn++] = a; } | ||
| 160 | { nghttp2_nv a = FT_NV(":authority", au, al); nva[nvn++] = a; } | ||
| 161 | { nghttp2_nv a = FT_NV(":path", pa, pl); nva[nvn++] = a; } | ||
| 162 | if (c->rq_headers) { | ||
| 163 | SSize_t j; | ||
| 164 | for (j = 0; j + 1 <= av_len(c->rq_headers); j += 2) { | ||
| 165 | SV **ks = av_fetch(c->rq_headers, j, 0); | ||
| 166 | SV **vs = av_fetch(c->rq_headers, j + 1, 0); | ||
| 167 | STRLEN kl, vl; | ||
| 168 | char *k = ks ? SvPV(*ks, kl) : (char *)""; | ||
| 169 | char *v = vs ? SvPV(*vs, vl) : (char *)""; | ||
| 170 | if (!ks) kl = 0; if (!vs) vl = 0; | ||
| 171 | nva[nvn].name = (uint8_t *)k; nva[nvn].namelen = kl; | ||
| 172 | nva[nvn].value = (uint8_t *)v; nva[nvn].valuelen = vl; | ||
| 173 | nva[nvn].flags = NGHTTP2_NV_FLAG_NONE; | ||
| 174 | nvn++; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | if (c->rq_body && SvOK(c->rq_body) && SvCUR(c->rq_body)) { | ||
| 178 | prd.source.ptr = NULL; | ||
| 179 | prd.read_callback = ft_h2_body_read; | ||
| 180 | prdp = &prd; | ||
| 181 | } | ||
| 182 | sid = nghttp2_submit_request(sess, NULL, nva, nvn, prdp, c); | ||
| 183 | Safefree(nva); | ||
| 184 | (void)i; | ||
| 185 | if (sid < 0) return -1; | ||
| 186 | return 0; | ||
| 187 | } | ||
| 188 | |||
| 189 | /* Flush pending frames; arm the loop for what nghttp2 wants next. Returns | ||
| 190 | * 0 keep going, -1 fatal. */ | ||
| 191 | static int ft_h2_flush(pTHX_ ft_conn *c) { | ||
| 192 | nghttp2_session *s = (nghttp2_session *)c->h2; | ||
| 193 | int rv = nghttp2_session_send(s); | ||
| 194 | if (rv != 0) return -1; | ||
| 195 | { | ||
| 196 | int mask = 0; | ||
| 197 | if (nghttp2_session_want_read(s)) mask |= HM_EV_READ; | ||
| 198 | if (nghttp2_session_want_write(s)) mask |= HM_EV_WRITE; | ||
| 199 | if (!mask) mask = HM_EV_READ; /* awaiting the response */ | ||
| 200 | ft_arm(aTHX_ c, mask); | ||
| 201 | } | ||
| 202 | return 0; | ||
| 203 | } | ||
| 204 | |||
| 205 | /* Drive one readiness pass in h2 mode. */ | ||
| 206 | static void ft_h2_step(pTHX_ ft_conn *c) { | ||
| 207 | /* write out anything pending first (also flushes the request) */ | ||
| 208 | if (ft_h2_flush(aTHX_ c) < 0) { ft_conn_fail(aTHX_ c, "http/2 send failed"); return; } | ||
| 209 | for (;;) { | ||
| 210 | int want = 0; | ||
| 211 | ssize_t n; | ||
| 212 | unsigned char buf[65536]; | ||
| 213 | n = ft_recv(c, buf, sizeof(buf), &want); | ||
| 214 | if (n > 0) { | ||
| 215 | ssize_t rd = nghttp2_session_mem_recv((nghttp2_session *)c->h2, | ||
| 216 | buf, (size_t)n); | ||
| 217 | if (rd < 0) { ft_conn_fail(aTHX_ c, "http/2 protocol error"); return; } | ||
| 218 | if (ft_h2_flush(aTHX_ c) < 0) { ft_conn_fail(aTHX_ c, "http/2 send failed"); return; } | ||
| 219 | if (c->h2_done) { ft_conn_finish(aTHX_ c); return; } | ||
| 220 | continue; | ||
| 221 | } | ||
| 222 | if (n == 0) { | ||
| 223 | if (c->h2_done) ft_conn_finish(aTHX_ c); | ||
| 224 | else ft_conn_fail(aTHX_ c, "connection closed before the h2 stream completed"); | ||
| 225 | return; | ||
| 226 | } | ||
| 227 | if (want) { ft_arm(aTHX_ c, want); return; } | ||
| 228 | if (errno == EINTR) continue; | ||
| 229 | ft_conn_fail(aTHX_ c, strerror(errno)); | ||
| 230 | return; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | static void ft_h2_free(ft_conn *c) { | ||
| 235 | if (c->h2) { nghttp2_session_del((nghttp2_session *)c->h2); c->h2 = NULL; } | ||
| 236 | } | ||
| 237 | |||
| 238 | #define FT_H2_AVAILABLE 1 | ||
| 239 | |||
| 240 | #else /* stubs */ | ||
| 241 | |||
| 242 | 0 | static int ft_h2_init(pTHX_ ft_conn *c) { (void)c; return -1; } | |
| 243 | 0 | static void ft_h2_step(pTHX_ ft_conn *c) { | |
| 244 | 0 | ft_conn_fail(aTHX_ c, "HTTP/2 not built (need nghttp2)"); } | |
| 245 | 119 | static void ft_h2_free(ft_conn *c) { (void)c; } | |
| 246 | #define FT_H2_AVAILABLE 0 | ||
| 247 | |||
| 248 | #endif | ||
| 249 | |||
| 250 | #endif /* FT_H2_H */ |