diff --git a/deps/ngtcp2/nghttp3/lib/includes/nghttp3/nghttp3.h b/deps/ngtcp2/nghttp3/lib/includes/nghttp3/nghttp3.h index 77ae851ae594..4fddcf7af52e 100644 --- a/deps/ngtcp2/nghttp3/lib/includes/nghttp3/nghttp3.h +++ b/deps/ngtcp2/nghttp3/lib/includes/nghttp3/nghttp3.h @@ -243,6 +243,13 @@ typedef uint64_t nghttp3_duration; * of stream data is too long, and causes overflow. */ #define NGHTTP3_ERR_STREAM_DATA_OVERFLOW -112 +/** + * @macro + * + * :macro:`NGHTTP3_ERR_MAX_FIELD_SECTION_SIZE_EXCEEDED` indicates that + * the incoming field block exceeds max_field_section_size limit. + */ +#define NGHTTP3_ERR_MAX_FIELD_SECTION_SIZE_EXCEEDED -113 /** * @macro * @@ -335,6 +342,27 @@ typedef uint64_t nghttp3_duration; * that might generating excessive load. */ #define NGHTTP3_ERR_H3_EXCESSIVE_LOAD -610 +/** + * @macro + * + * :macro:`NGHTTP3_ERR_H3_MESSAGE_ERROR` indicates that HTTP message + * was malformed. + */ +#define NGHTTP3_ERR_H3_MESSAGE_ERROR -611 +/** + * @macro + * + * :macro:`NGHTTP3_ERR_WT_SESSION_GONE` indicates that WebTransport + * session was terminated or rejected. + */ +#define NGHTTP3_ERR_WT_SESSION_GONE -612 +/** + * @macro + * + * :macro:`NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED` indicates that + * buffering WebTransport data stream was rejected. + */ +#define NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED -613 /** * @macro * @@ -503,6 +531,38 @@ typedef uint64_t nghttp3_duration; * error code ``QPACK_DECODER_STREAM_ERROR``. */ #define NGHTTP3_QPACK_DECODER_STREAM_ERROR 0x0202 +/** + * @macro + * + * :macro:`NGHTTP3_WT_BUFFERED_STREAM_REJECTED` is WebTransport error + * code ``WT_BUFFERED_STREAM_REJECTED``. + */ +#define NGHTTP3_WT_BUFFERED_STREAM_REJECTED 0x3994BD84 +/** + * @macro + * + * :macro:`NGHTTP3_WT_SESSION_GONE` is WebTransport error code + * ``WT_SESSION_GONE``. + */ +#define NGHTTP3_WT_SESSION_GONE 0x170D7B68 +/** + * @macro + * + * :macro:`NGHTTP3_WT_ALPN_ERROR` is WebTransport error code + * ``WT_ALPN_ERROR``. + * + * https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-15 + */ +#define NGHTTP3_WT_ALPN_ERROR 0x0817B3DD +/** + * @macro + * + * :macro:`NGHTTP3_WT_REQUIREMENTS_NOT_MET` is WebTransport error code + * ``WT_REQUIREMENTS_NOT_MET``. + * + * https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-15 + */ +#define NGHTTP3_WT_REQUIREMENTS_NOT_MET 0x212C0D48 /** * @functypedef @@ -719,8 +779,9 @@ NGHTTP3_EXTERN void nghttp3_buf_init(nghttp3_buf *buf); * @function * * `nghttp3_buf_free` frees resources allocated for |buf| using |mem| - * as memory allocator. :member:`buf->begin ` must - * be a heap buffer allocated by |mem|. + * as memory allocator. If :member:`buf->begin ` + * points to any memory that is not initialized by `nghttp3_buf_init`, + * it must be a heap buffer allocated by |mem|. */ NGHTTP3_EXTERN void nghttp3_buf_free(nghttp3_buf *buf, const nghttp3_mem *mem); @@ -1274,13 +1335,15 @@ NGHTTP3_EXTERN void nghttp3_qpack_encoder_del(nghttp3_qpack_encoder *encoder); * written. An application should write |pbuf| and |rbuf| to the * request stream in this order. * - * The buffer pointed by |pbuf|, |rbuf|, and |ebuf| can be empty - * buffer. It is fine to pass a buffer initialized by - * `nghttp3_buf_init(buf) `. This function - * allocates memory for these buffers as necessary. In particular, it - * frees and expands buffer if the current capacity of buffer is not - * enough. If :member:`nghttp3_buf.begin` of any buffer is not NULL, - * it must be allocated by the same memory allocator passed to + * The buffer pointed by |pbuf|, |rbuf|, and |ebuf| must be + * initialized by `nghttp3_buf_init`. Avoid initializing it with zero + * values. Any caller-supplied backing storage must come from the + * allocator passed to `nghttp3_qpack_encoder_new`. This function + * allocates memory for these buffers as necessary if it needs more + * space in the buffer. In particular, it frees and expands buffer if + * the current capacity of buffer is not enough. Any dynamically + * allocated memory pointed by :member:`nghttp3_buf.begin` must be + * allocated by the same memory allocator passed to * `nghttp3_qpack_encoder_new`. * * This function returns 0 if it succeeds, or one of the following @@ -1387,6 +1450,20 @@ nghttp3_qpack_encoder_get_num_blocked_streams(nghttp3_qpack_encoder *encoder); NGHTTP3_EXTERN size_t nghttp3_qpack_encoder_get_num_blocked_streams2( const nghttp3_qpack_encoder *encoder); +/** + * @function + * + * `nghttp3_qpack_encoder_set_max_concurrent_streams` tells |encoder| + * the maximum number of concurrent request streams that |encoder| + * encodes QPACK field block concurrently. This limits the number of + * streams that waits for acknowledgement from the decoder. + * + * .. version-added:: 1.20.0 + */ +NGHTTP3_EXTERN void +nghttp3_qpack_encoder_set_max_concurrent_streams(nghttp3_qpack_encoder *encoder, + size_t max_concurrent_streams); + /** * @struct * @@ -1698,10 +1775,9 @@ nghttp3_qpack_decoder_set_max_dtable_capacity(nghttp3_qpack_decoder *decoder, * @function * * `nghttp3_qpack_decoder_set_max_concurrent_streams` tells |decoder| - * the maximum number of concurrent streams that a remote endpoint can - * open, including both bidirectional and unidirectional streams which - * potentially receive QPACK encoded HEADERS frame. This value is - * used as a hint to limit the length of decoder stream. + * the maximum number of concurrent request streams that |decoder| + * decodes QPACK field block concurrently. This value is used as a + * hint to limit the length of decoder stream. */ NGHTTP3_EXTERN void nghttp3_qpack_decoder_set_max_concurrent_streams(nghttp3_qpack_decoder *decoder, @@ -1808,8 +1884,7 @@ typedef struct nghttp3_conn nghttp3_conn; typedef struct nghttp3_settings { /** * :member:`max_field_section_size` specifies the maximum header - * section (block) size. nghttp3 library does not enforce this - * limit. Applications are responsible for imposing their own + * section (block) size. Applications should also impose their own * limits to protect against resource exhaustion. See * https://datatracker.ietf.org/doc/html/rfc9114#section-4.2.2 for * details. @@ -1900,6 +1975,16 @@ typedef struct nghttp3_settings { * .. version-added:: 1.13.0 */ nghttp3_qpack_indexing_strat qpack_indexing_strat; + /** + * :member:`wt_enabled`, if set to nonzero, enables WebTransport. + * + * https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-15 + * + * TODO For client, it might be better to always enable + * WebTransport. Only draft version of client needs to send + * SETTINGS_WT_ENABLED. + */ + uint8_t wt_enabled; } nghttp3_settings; #define NGHTTP3_PROTO_SETTINGS_V1 1 @@ -1940,6 +2025,12 @@ typedef struct nghttp3_proto_settings { * Datagrams (see :rfc:`9297`). */ uint8_t h3_datagram; + /** + * :member:`wt_enabled`, if set to nonzero, enables WebTransport. + * + * https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-15 + */ + uint8_t wt_enabled; } nghttp3_proto_settings; /** @@ -2313,6 +2404,69 @@ typedef int (*nghttp3_stream_close2)(nghttp3_conn *conn, uint32_t flags, void *conn_user_data, void *stream_user_data); +/** + * @functypedef + * + * :type:`nghttp3_recv_wt_data` is a callback function which is + * invoked when data is received on WebTransport data stream. + * |session_id| is the WebTransport session ID. |stream_id| is the + * stream ID of the WebTransport data stream. |data| points to the + * received data, and its length is |datalen|. + * + * The application is responsible for increasing flow control credit + * (say, increasing by |datalen| bytes). + * + * The implementation of this callback must return 0 if it succeeds. + * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the + * caller immediately. Any values other than 0 is treated as + * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. + */ +typedef int (*nghttp3_recv_wt_data)(nghttp3_conn *conn, int64_t session_id, + int64_t stream_id, const uint8_t *data, + size_t datalen, void *conn_user_data, + void *stream_user_data); + +/** + * @functypedef + * + * :type:`nghttp3_wt_data_stream_open` is a callback function which is + * invoked when a remote stream denoted by |stream_id| is identified + * as WebTransport data stream that belongs to WebTransport session + * identified by |session_id|. This callback function is called after + * WebTransport session is confirmed. + * + * The implementation of this callback must return 0 if it succeeds. + * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the + * caller immediately. Any values other than 0 is treated as + * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. + */ +typedef int (*nghttp3_wt_data_stream_open)(nghttp3_conn *conn, + int64_t session_id, + int64_t stream_id, + void *conn_user_data, + void *stream_user_data); + +/** + * @functypedef + * + * :type:`nghttp3_recv_wt_close_session` is a callback function which + * is invoked when WT_CLOSE_SESSION Capsule is received. The + * WebTransport session is identified by |session_id|. + * |wt_error_code| is Application Error Code. The buffer pointed by + * |msg| of length |msglen| contains Application Error Message. + * + * The implementation of this callback must return 0 if it succeeds. + * Returning :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` will return to the + * caller immediately. Any values other than 0 is treated as + * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE`. + */ +typedef int (*nghttp3_recv_wt_close_session)(nghttp3_conn *conn, + int64_t session_id, + uint32_t wt_error_code, + const uint8_t *msg, size_t msglen, + void *conn_user_data, + void *stream_user_data); + #define NGHTTP3_CALLBACKS_V1 1 #define NGHTTP3_CALLBACKS_V2 2 #define NGHTTP3_CALLBACKS_V3 3 @@ -2463,6 +2617,22 @@ typedef struct nghttp3_callbacks { * .. version-added:: 1.18.0 */ nghttp3_stream_close2 stream_close2; + /** + * :member:`recv_wt_data` is a callback function which is invoked + * when data on WebTransport data stream is received. + */ + nghttp3_recv_wt_data recv_wt_data; + /** + * :member:`wt_data_stream_open` is a callback function which is + * invoked when a remote stream is identified as WebTransport data + * stream. + */ + nghttp3_wt_data_stream_open wt_data_stream_open; + /** + * :member:`recv_wt_close_session` is a callback function which is + * invoked when WT_CLOSE_SESSION Capsule is received. + */ + nghttp3_recv_wt_close_session recv_wt_close_session; } nghttp3_callbacks; /** @@ -2923,6 +3093,8 @@ NGHTTP3_EXTERN int nghttp3_conn_resume_stream(nghttp3_conn *conn, * Stream not found. * :macro:`NGHTTP3_ERR_H3_CLOSED_CRITICAL_STREAM` * A critical stream is closed. + * :macro:`NGHTTP3_ERR_NOMEM` + * Out of memory. * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` * User callback failed * @@ -2956,6 +3128,8 @@ NGHTTP3_EXTERN int nghttp3_conn_close_stream(nghttp3_conn *conn, * Stream not found. * :macro:`NGHTTP3_ERR_H3_CLOSED_CRITICAL_STREAM` * A critical stream is closed. + * :macro:`NGHTTP3_ERR_NOMEM` + * Out of memory. * :macro:`NGHTTP3_ERR_CALLBACK_FAILURE` * User callback failed * @@ -3015,10 +3189,9 @@ nghttp3_conn_set_max_client_streams_bidi(nghttp3_conn *conn, * @function * * `nghttp3_conn_set_max_concurrent_streams` tells |conn| the maximum - * number of concurrent streams that a remote endpoint can open, - * including both bidirectional and unidirectional streams which - * potentially receive QPACK encoded HEADERS frame. This value is - * used as a hint to limit the internal resource consumption. + * number of concurrent bidirectional streams that a client can open. + * This value is used as a hint to limit the internal resource + * consumption. */ NGHTTP3_EXTERN void nghttp3_conn_set_max_concurrent_streams(nghttp3_conn *conn, @@ -3091,7 +3264,8 @@ typedef struct nghttp3_data_reader { * :macro:`NGHTTP3_ERR_NOMEM` * Out of memory. * :macro:`NGHTTP3_ERR_INVALID_ARGUMENT` - * The required memory for |nva| of length |nvlen| is too large. + * The required memory for |nva| of length |nvlen| is too large; + * or |nvlen| == 0. */ NGHTTP3_EXTERN int nghttp3_conn_submit_request( nghttp3_conn *conn, int64_t stream_id, const nghttp3_nv *nva, size_t nvlen, @@ -3136,7 +3310,8 @@ NGHTTP3_EXTERN int nghttp3_conn_submit_info(nghttp3_conn *conn, * :macro:`NGHTTP3_ERR_NOMEM` * Out of memory. * :macro:`NGHTTP3_ERR_INVALID_ARGUMENT` - * The required memory for |nva| of length |nvlen| is too large. + * The required memory for |nva| of length |nvlen| is too large; + * or |nvlen| == 0. */ NGHTTP3_EXTERN int nghttp3_conn_submit_response(nghttp3_conn *conn, int64_t stream_id, @@ -3176,6 +3351,9 @@ NGHTTP3_EXTERN int nghttp3_conn_submit_trailers(nghttp3_conn *conn, * to stop creating new stream. After a couple of RTTs later, call * `nghttp3_conn_shutdown` to start graceful shutdown. * + * This function does nothing if `nghttp3_conn_shutdown` has been + * called. + * * This function returns 0 if it succeeds, or one of the following * negative error codes: * @@ -3193,6 +3371,8 @@ NGHTTP3_EXTERN int nghttp3_conn_submit_shutdown_notice(nghttp3_conn *conn); * rejecting new incoming streams. The existing streams are processed * normally. See also `nghttp3_conn_is_drained`. * + * Calling this function more than once does nothing, and returns 0. + * * This function returns 0 if it succeeds, or one of the following * negative error codes: * @@ -3485,6 +3665,165 @@ NGHTTP3_EXTERN int nghttp3_conn_is_drained(nghttp3_conn *conn); */ NGHTTP3_EXTERN int nghttp3_conn_is_drained2(const nghttp3_conn *conn); +/** + * @function + * + * `nghttp3_conn_submit_wt_request` works like + * `nghttp3_conn_submit_request`, but it is specifically tailored for + * WebTransport session establishment. |nva| of length |nvlen| + * specifies HTTP request header fields. They must contain at least + * the following fields: + * + * - :method = "CONNECT" + * - :scheme = "https" + * - :protocol = "webtransport-h3" + * - :authority + * - :path + * + * The application must also set the following settings: + * + * - :member:`nghttp3_settings.h3_datagram` = 1 + * - :member:`nghttp3_settings.wt_enabled` = 1 + * + * It also must send the following QUIC transport parameters: + * + * - max_datagram_frame_size > 0 + * - reset_stream_at + * + * The application should wait for SETTINGS frame from server and make + * sure that it satisfies server-side requirements for WebTransport. + * + * After receiving 2xx response from server, WebTransport session is + * established. `nghttp3_conn_open_wt_data_stream` is used to open + * WebTransport data streams. + * + * This function returns 0 if it succeeds, or one of the following + * negative error codes: + * + * TBD + */ +NGHTTP3_EXTERN int nghttp3_conn_submit_wt_request(nghttp3_conn *conn, + int64_t stream_id, + const nghttp3_nv *nva, + size_t nvlen, + void *stream_user_data); + +/** + * @function + * + * `nghttp3_conn_submit_wt_response` works like + * `nghttp3_conn_submit_response`, but it is specifically tailored for + * WebTransport session establishment. |nva| of length |nvlen| + * specifies HTTP response header fields. It must contain 2xx status + * code in :status field. + * + * The application should make sure that the stream denoted by + * |stream_id| is a request stream that requests WebTransport session + * establishment. If this function is called inside + * :member:`nghttp3_callbacks.end_headers` callback, + * `nghttp3_conn_server_confirm_wt_session` is called internally, and + * it establishes WebTransport session. If this function is called + * outside of the callback, the application must call + * `nghttp3_conn_server_confirm_wt_session` after calling this + * function. + * + * If `nghttp3_conn_submit_response` is used against the WebTransport + * upgrade request, it means refusal of the request regardless of HTTP + * status code. The application is responsible to set non-2xx status + * code when `nghttp3_conn_submit_response` is used. If + * `nghttp3_conn_submit_response` is called from + * :member:`nghttp3_callbacks.end_headers` callback, + * :member:`nghttp3_callbacks.stop_sending` callback is automatically + * called. If `nghttp3_conn_submit_response` is called outside of the + * :member:`nghttp3_callbacks.end_headers` callback, + * :member:`nghttp3_callbacks.stop_sending` is not called + * automatically. The application should tell QUIC stack to send + * STOP_SENDING frame to this stream. + * + * This function returns 0 if it succeeds, or one of the following + * negative error codes: + * + * TBD + */ +NGHTTP3_EXTERN int nghttp3_conn_submit_wt_response(nghttp3_conn *conn, + int64_t stream_id, + const nghttp3_nv *nva, + size_t nvlen); + +/** + * @function + * + * `nghttp3_conn_server_confirm_wt_session` establishes WebTransport + * session. This should be called after + * `nghttp3_conn_submit_wt_response` call if it is not called inside + * `nghttp3_callbacks.end_headers` callback. + * + * Only server can call this function. + * + * This function returns 0 if it succeeds, or one of the following + * negative error codes: + * + * TBD + */ +NGHTTP3_EXTERN int nghttp3_conn_server_confirm_wt_session(nghttp3_conn *conn, + int64_t session_id, + nghttp3_tstamp ts); + +/** + * @function + * + * `nghttp3_conn_open_wt_data_stream` opens WebTransport data stream. + * |session_id| is the stream ID that established WebTransport + * session. |stream_id| is the stream ID to write data, and it can be + * both bidirectional and unidirectional. |dr| must not be NULL, and + * it must have non-NULL callback. + * + * This function can be also used to start writing to the + * bidirectional stream initiated by the remote endpoint. + * + * This function returns 0 if it succeeds, or one of the following + * negative error codes: + * + * TBD + */ +NGHTTP3_EXTERN int nghttp3_conn_open_wt_data_stream( + nghttp3_conn *conn, int64_t session_id, int64_t stream_id, + const nghttp3_data_reader *dr, void *stream_user_data); + +/** + * @function + * + * `nghttp3_conn_close_wt_session` closes WebTransport session denoted + * by |session_id| which is the stream ID that established + * WebTransport session. |wt_error_code| is WebTransport error code. + * Upon calling this function, all existing WebTransport data streams + * are shutdown. |msg| of |msglen| bytes is the application error + * message, which is optional. |msglen| must be less than or equal to + * 1024. + * + * This function returns 0 if it succeeds, or one of the following + * negative error codes: + * + * TBD + */ +NGHTTP3_EXTERN int nghttp3_conn_close_wt_session(nghttp3_conn *conn, + int64_t session_id, + uint32_t wt_error_code, + const uint8_t *msg, + size_t msglen); + +/** + * @function + * + * `nghttp3_conn_get_stream_wt_session_id` returns the WebTransport + * session ID of a stream denoted by |stream_id| if it is WebTransport + * data stream. If the stream is not found, it is not a WebTransport + * data stream, or it is unable to get session ID, this function + * returns -1. + */ +NGHTTP3_EXTERN int64_t nghttp3_conn_get_stream_wt_session_id( + const nghttp3_conn *conn, int64_t stream_id); + /** * @function * diff --git a/deps/ngtcp2/nghttp3/lib/includes/nghttp3/version.h b/deps/ngtcp2/nghttp3/lib/includes/nghttp3/version.h index f362bd4f6ba8..e5c94deb1849 100644 --- a/deps/ngtcp2/nghttp3/lib/includes/nghttp3/version.h +++ b/deps/ngtcp2/nghttp3/lib/includes/nghttp3/version.h @@ -31,7 +31,7 @@ * * Version number of the nghttp3 library release. */ -#define NGHTTP3_VERSION "1.19.0" +#define NGHTTP3_VERSION "1.20.0-DEV" /** * @macro @@ -41,6 +41,6 @@ * number, 8 bits for minor and 8 bits for patch. Version 1.2.3 * becomes 0x010203. */ -#define NGHTTP3_VERSION_NUM 0x011300 +#define NGHTTP3_VERSION_NUM 0x011400 #endif /* !defined(NGHTTP3_VERSION_H) */ diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_balloc.c b/deps/ngtcp2/nghttp3/lib/nghttp3_balloc.c index 7750263242ec..addb0b00b6f2 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_balloc.c +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_balloc.c @@ -36,7 +36,7 @@ void nghttp3_balloc_init(nghttp3_balloc *balloc, size_t blklen, balloc->mem = mem; balloc->blklen = blklen; balloc->head = NULL; - nghttp3_buf_wrap_init(&balloc->buf, (void *)"", 0); + nghttp3_buf_init(&balloc->buf); } void nghttp3_balloc_free(nghttp3_balloc *balloc) { @@ -56,7 +56,7 @@ void nghttp3_balloc_clear(nghttp3_balloc *balloc) { } balloc->head = NULL; - nghttp3_buf_wrap_init(&balloc->buf, (void *)"", 0); + nghttp3_buf_init(&balloc->buf); } int nghttp3_balloc_get(nghttp3_balloc *balloc, void **pbuf, size_t n) { diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_buf.c b/deps/ngtcp2/nghttp3/lib/nghttp3_buf.c index 7c3296cd1efb..736b416e53c1 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_buf.c +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_buf.c @@ -25,7 +25,16 @@ */ #include "nghttp3_buf.h" -void nghttp3_buf_init(nghttp3_buf *buf) { *buf = (nghttp3_buf){0}; } +static const uint8_t inibuf; + +void nghttp3_buf_init(nghttp3_buf *buf) { + *buf = (nghttp3_buf){ + .begin = (uint8_t *)&inibuf, + .end = (uint8_t *)&inibuf, + .pos = (uint8_t *)&inibuf, + .last = (uint8_t *)&inibuf, + }; +} void nghttp3_buf_wrap_init(nghttp3_buf *buf, uint8_t *src, size_t len) { *buf = (nghttp3_buf){ @@ -37,6 +46,10 @@ void nghttp3_buf_wrap_init(nghttp3_buf *buf, uint8_t *src, size_t len) { } void nghttp3_buf_free(nghttp3_buf *buf, const nghttp3_mem *mem) { + if (buf->begin == &inibuf) { + return; + } + nghttp3_mem_free(mem, buf->begin); } @@ -69,7 +82,7 @@ int nghttp3_buf_reserve(nghttp3_buf *buf, size_t size, const nghttp3_mem *mem) { pos_offset = buf->pos - buf->begin; last_offset = buf->last - buf->begin; - p = nghttp3_mem_realloc(mem, buf->begin, size); + p = nghttp3_mem_realloc(mem, buf->begin == &inibuf ? NULL : buf->begin, size); if (p == NULL) { return NGHTTP3_ERR_NOMEM; } diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_conn.c b/deps/ngtcp2/nghttp3/lib/nghttp3_conn.c index 8b15fb743bd9..92c405a26b33 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_conn.c +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_conn.c @@ -27,6 +27,11 @@ #include #include #include +#include + +#ifdef HAVE_WINDOWS_H +# include +#endif /* defined(HAVE_WINDOWS_H) */ #include "nghttp3_mem.h" #include "nghttp3_macro.h" @@ -36,9 +41,22 @@ #include "nghttp3_unreachable.h" #include "nghttp3_settings.h" #include "nghttp3_callbacks.h" +#include "nghttp3_wt.h" nghttp3_objalloc_def(chunk, nghttp3_chunk, oplent) +/* + * conn_remote_stream returns nonzero if |stream_id| is a remote + * stream ID. + */ +static int conn_remote_stream(const nghttp3_conn *conn, int64_t stream_id) { + if (conn->server) { + return !(stream_id & 0x1); + } + + return stream_id & 0x1; +} + /* * conn_remote_stream_uni returns nonzero if |stream_id| is remote * unidirectional stream ID. @@ -50,6 +68,31 @@ static int conn_remote_stream_uni(const nghttp3_conn *conn, int64_t stream_id) { return (stream_id & 0x03) == 0x03; } +static int conn_wt_enabled(const nghttp3_conn *conn) { + const nghttp3_settings *local_settings = &conn->local.settings; + const nghttp3_proto_settings *remote_settings = &conn->remote.settings; + + if (!local_settings->wt_enabled || !local_settings->h3_datagram) { + return 0; + } + + if (conn->server) { + return (!(conn->flags & NGHTTP3_CONN_FLAG_SETTINGS_RECVED) || + /* TODO client sends SETTINGS_WT_ENABLED for draft + versions only. But some client implementations do not + send it. For interop purpose, do not require this + remote setting for now. */ + (/* remote_settings->wt_enabled && */ remote_settings + ->h3_datagram)) && + local_settings->enable_connect_protocol; + } + + return (conn->flags & NGHTTP3_CONN_FLAG_SETTINGS_RECVED) && + remote_settings->wt_enabled && + remote_settings->enable_connect_protocol && + remote_settings->h3_datagram; +} + static int conn_call_begin_headers(nghttp3_conn *conn, nghttp3_stream *stream) { int rv; @@ -251,6 +294,70 @@ static int conn_call_end_origin(nghttp3_conn *conn) { return 0; } +static int conn_call_recv_data(nghttp3_conn *conn, const nghttp3_stream *stream, + const uint8_t *data, size_t datalen) { + int rv; + + if (!conn->callbacks.recv_data) { + return 0; + } + + rv = conn->callbacks.recv_data(conn, stream->node.id, data, datalen, + conn->user_data, stream->user_data); + if (rv != 0) { + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + + return 0; +} + +static int conn_call_recv_wt_data(nghttp3_conn *conn, + const nghttp3_stream *stream, + const uint8_t *data, size_t datalen) { + nghttp3_wt_session *wt_session; + int rv; + + if (!conn->callbacks.recv_wt_data) { + return 0; + } + + wt_session = stream->wt.session; + + assert(wt_session); + + rv = conn->callbacks.recv_wt_data(conn, wt_session->session_id, + stream->node.id, data, datalen, + conn->user_data, stream->user_data); + if (rv != 0) { + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + + return 0; +} + +static int conn_call_wt_data_stream_open(nghttp3_conn *conn, + const nghttp3_stream *stream) { + nghttp3_wt_session *wt_session; + int rv; + + if (!conn->callbacks.wt_data_stream_open) { + return 0; + } + + wt_session = stream->wt.session; + + assert(wt_session); + + rv = conn->callbacks.wt_data_stream_open(conn, wt_session->session_id, + stream->node.id, conn->user_data, + stream->user_data); + if (rv != 0) { + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + + return 0; +} + static int conn_glitch_ratelim_drain(nghttp3_conn *conn, uint64_t n, nghttp3_tstamp ts) { if (ts == UINT64_MAX) { @@ -282,6 +389,45 @@ static int cycle_less(const nghttp3_pq_entry *lhsx, return rhs->cycle - lhs->cycle <= NGHTTP3_TNODE_MAX_CYCLE_GAP; } +static uint64_t internal_seed_fallback(void) { + time_t t; + + t = time(NULL); + if (t == -1) { + return 0; + } + + return (uint64_t)t * 1000000000; +} + +#if defined(HAVE_QUERYPERFORMANCECOUNTER) +static uint64_t internal_seed(void) { + LARGE_INTEGER ticks; + + if (!QueryPerformanceCounter(&ticks)) { + return internal_seed_fallback(); + } + + return (uint64_t)ticks.QuadPart; +} +#elif defined(HAVE_CLOCK_GETTIME) && HAVE_DECL_CLOCK_MONOTONIC +static uint64_t internal_seed(void) { + struct timespec tp; + int rv; + + rv = clock_gettime(CLOCK_MONOTONIC, &tp); + if (rv == -1) { + return internal_seed_fallback(); + } + + return (uint64_t)tp.tv_sec * 1000000000 + (uint64_t)tp.tv_nsec; +} +#else /* !defined(HAVE_QUERYPERFORMANCECOUNTER) && \ + (!defined(HAVE_CLOCK_GETTIME) || !HAVE_DECL_CLOCK_MONOTONIC) */ +static uint64_t internal_seed(void) { return internal_seed_fallback(); } +#endif /* !defined(HAVE_QUERYPERFORMANCECOUNTER) && \ + (!defined(HAVE_CLOCK_GETTIME) || !HAVE_DECL_CLOCK_MONOTONIC) */ + static int conn_new(nghttp3_conn **pconn, int server, int callbacks_version, const nghttp3_callbacks *callbacks, int settings_version, const nghttp3_settings *settings, const nghttp3_mem *mem, @@ -319,7 +465,7 @@ static int conn_new(nghttp3_conn **pconn, int server, int callbacks_version, if (callbacks->rand) { callbacks->rand((uint8_t *)&map_seed, sizeof(map_seed)); } else { - map_seed = 0; + map_seed = internal_seed(); } nghttp3_map_init(&conn->streams, map_seed, mem); @@ -355,12 +501,15 @@ static int conn_new(nghttp3_conn **pconn, int server, int callbacks_version, conn->local.settings.origin_list = NULL; } conn->remote.settings.max_field_section_size = NGHTTP3_VARINT_MAX; + conn->remote.bidi.max_concurrent_streams = SIZE_MAX; conn->mem = mem; conn->user_data = user_data; conn->server = server; conn->rx.goaway_id = NGHTTP3_VARINT_MAX + 1; conn->tx.goaway_id = NGHTTP3_VARINT_MAX + 1; conn->rx.max_stream_id_bidi = -4; + nghttp3_buf_init(&conn->tx.qpack.rbuf); + nghttp3_buf_init(&conn->tx.qpack.ebuf); *pconn = conn; @@ -401,11 +550,27 @@ int nghttp3_conn_server_new_versioned(nghttp3_conn **pconn, return 0; } +static void remove_wt_session_ref(nghttp3_wt_session *wt_session) { + nghttp3_stream *stream; + + for (stream = wt_session->head; stream; stream = stream->wt.next) { + assert(stream->wt.session == wt_session); + + stream->wt.session = NULL; + } +} + static int free_stream(void *data, void *ptr) { nghttp3_stream *stream = data; (void)ptr; + if (nghttp3_stream_wt_ctrl(stream)) { + remove_wt_session_ref(stream->wt.session); + } else if (nghttp3_stream_wt_data(stream)) { + nghttp3_wt_session_remove_stream(stream->wt.session, stream); + } + nghttp3_stream_del(stream); return 0; @@ -501,8 +666,19 @@ nghttp3_ssize nghttp3_conn_read_stream2(nghttp3_conn *conn, int64_t stream_id, return rv; } + if (conn_wt_enabled(conn)) { + stream->flags |= NGHTTP3_STREAM_FLAG_MAYBE_WT_DATA; + } + if ((conn->flags & NGHTTP3_CONN_FLAG_GOAWAY_QUEUED) && conn->tx.goaway_id <= stream_id) { + /* A few requests might have been sent before a remote + endpoint received GOAWAY, but receiving too many of them + is suspicious. */ + if (conn_glitch_ratelim_drain(conn, 1, ts) != 0) { + return NGHTTP3_ERR_H3_EXCESSIVE_LOAD; + } + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; rv = nghttp3_conn_reject_stream(conn, stream); @@ -517,6 +693,12 @@ nghttp3_ssize nghttp3_conn_read_stream2(nghttp3_conn *conn, int64_t stream_id, } else { /* unidirectional stream */ if (srclen == 0 && fin) { + /* Receiving too frequent 0 length unidirectional stream is + suspicious. */ + if (conn_glitch_ratelim_drain(conn, 1, ts) != 0) { + return NGHTTP3_ERR_H3_EXCESSIVE_LOAD; + } + return 0; } @@ -527,8 +709,16 @@ nghttp3_ssize nghttp3_conn_read_stream2(nghttp3_conn *conn, int64_t stream_id, } stream->rx.hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL; - } else if (nghttp3_server_stream_uni(stream_id)) { + } else if (nghttp3_server_stream_uni(stream_id) || + (conn_wt_enabled(conn) && + nghttp3_server_stream_bidi(stream_id))) { if (srclen == 0 && fin) { + /* Receiving too frequent 0 length unidirectional stream is + suspicious. */ + if (conn_glitch_ratelim_drain(conn, 1, ts) != 0) { + return NGHTTP3_ERR_H3_EXCESSIVE_LOAD; + } + return 0; } @@ -537,6 +727,10 @@ nghttp3_ssize nghttp3_conn_read_stream2(nghttp3_conn *conn, int64_t stream_id, return rv; } + if (!(stream_id & 0x2) && conn_wt_enabled(conn)) { + stream->flags |= NGHTTP3_STREAM_FLAG_MAYBE_WT_DATA; + } + stream->rx.hstate = NGHTTP3_HTTP_STATE_RESP_INITIAL; } else { /* client doesn't expect to receive new bidirectional stream or @@ -545,10 +739,12 @@ nghttp3_ssize nghttp3_conn_read_stream2(nghttp3_conn *conn, int64_t stream_id, } } else if (conn->server) { assert(nghttp3_client_stream_bidi(stream_id) || - nghttp3_client_stream_uni(stream_id)); + nghttp3_client_stream_uni(stream_id) || + (conn_wt_enabled(conn) && nghttp3_server_stream_bidi(stream_id))); } else { assert(nghttp3_client_stream_bidi(stream_id) || - nghttp3_server_stream_uni(stream_id)); + nghttp3_server_stream_uni(stream_id) || + (conn_wt_enabled(conn) && nghttp3_server_stream_bidi(stream_id))); } if (srclen == 0 && !fin) { @@ -614,6 +810,12 @@ static nghttp3_ssize conn_read_type(nghttp3_conn *conn, nghttp3_stream *stream, conn->flags |= NGHTTP3_CONN_FLAG_QPACK_DECODER_OPENED; stream->type = NGHTTP3_STREAM_TYPE_QPACK_DECODER; break; + case NGHTTP3_STREAM_TYPE_WT_STREAM: + if (!conn_wt_enabled(conn)) { + return NGHTTP3_ERR_H3_STREAM_CREATION_ERROR; + } + stream->type = NGHTTP3_STREAM_TYPE_WT_STREAM; + break; default: stream->type = NGHTTP3_STREAM_TYPE_UNKNOWN; break; @@ -682,7 +884,8 @@ nghttp3_ssize nghttp3_conn_read_uni(nghttp3_conn *conn, nghttp3_stream *stream, } } - if (srclen == 0) { + if (srclen == 0 && + (!fin || stream->type != NGHTTP3_STREAM_TYPE_WT_STREAM)) { return nread; } } @@ -706,6 +909,10 @@ nghttp3_ssize nghttp3_conn_read_uni(nghttp3_conn *conn, nghttp3_stream *stream, } nconsumed = nghttp3_conn_read_qpack_decoder(conn, src, srclen); break; + case NGHTTP3_STREAM_TYPE_WT_STREAM: + nconsumed = + nghttp3_conn_read_wt_stream_uni(conn, stream, src, srclen, fin, ts); + break; case NGHTTP3_STREAM_TYPE_UNKNOWN: nconsumed = (nghttp3_ssize)srclen; break; @@ -789,7 +996,6 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn, if (rstate->fr.hd.type != NGHTTP3_FRAME_SETTINGS) { return NGHTTP3_ERR_H3_MISSING_SETTINGS; } - conn->flags |= NGHTTP3_CONN_FLAG_SETTINGS_RECVED; } else if (rstate->fr.hd.type == NGHTTP3_FRAME_SETTINGS) { return NGHTTP3_ERR_H3_FRAME_UNEXPECTED; } @@ -798,7 +1004,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn, case NGHTTP3_FRAME_SETTINGS: /* SETTINGS frame might be empty. */ if (rstate->left == 0) { - rv = conn_call_recv_settings(conn); + rv = nghttp3_conn_on_settings_received(conn); if (rv != 0) { return rv; } @@ -896,7 +1102,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn, case NGHTTP3_CTRL_STREAM_STATE_SETTINGS: for (;;) { if (rstate->left == 0) { - rv = conn_call_recv_settings(conn); + rv = nghttp3_conn_on_settings_received(conn); if (rv != 0) { return rv; } @@ -1014,7 +1220,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn, break; } - rv = conn_call_recv_settings(conn); + rv = nghttp3_conn_on_settings_received(conn); if (rv != 0) { return rv; } @@ -1043,8 +1249,10 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn, return NGHTTP3_ERR_H3_ID_ERROR; } - /* Receiving same GOAWAY ID is suspicious. */ - if (conn->rx.goaway_id == (int64_t)rvint->acc && + /* Receiving GOAWAY more than once is suspicious regardless of + its ID. Lowering ID in each frame must not evade the glitch + rate limiter. The first GOAWAY is not counted. */ + if ((conn->flags & NGHTTP3_CONN_FLAG_GOAWAY_RECVED) && conn_glitch_ratelim_drain(conn, 1, ts) != 0) { return NGHTTP3_ERR_H3_EXCESSIVE_LOAD; } @@ -1083,8 +1291,11 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn, return NGHTTP3_ERR_H3_FRAME_ERROR; } - /* Receiving same MAX_PUSH_ID is suspicious. */ - if (conn->local.uni.max_pushes == rvint->acc + 1 && + /* Receiving MAX_PUSH_ID more than once is suspicious regardless + of its value. Raising the value in each frame must not evade + the glitch rate limiter. The first MAX_PUSH_ID is not + counted; max_pushes is nonzero once it has been received. */ + if (conn->local.uni.max_pushes && conn_glitch_ratelim_drain(conn, 1, ts) != 0) { return NGHTTP3_ERR_H3_EXCESSIVE_LOAD; } @@ -1224,6 +1435,12 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn, return NGHTTP3_ERR_H3_FRAME_ERROR; } + /* ORIGIN frame can carry any number of origins. We do not + expect too many origins either. */ + if (conn_glitch_ratelim_drain(conn, 1, ts) != 0) { + return NGHTTP3_ERR_H3_EXCESSIVE_LOAD; + } + if (len < conn->rx.originlen) { /* ASCII-Origin does not fit into this buffer. Needs buffering. */ @@ -1336,6 +1553,35 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn, return (nghttp3_ssize)nconsumed; } +static int conn_unlink_wt_session(nghttp3_conn *conn, + nghttp3_stream *wt_ctrl_stream) { + nghttp3_wt_session *wt_session = wt_ctrl_stream->wt.session; + nghttp3_stream *stream; + int rv; + + for (; wt_session->head;) { + stream = wt_session->head; + + assert(stream->wt.session); + + wt_session->head = stream->wt.next; + if (wt_session->head) { + wt_session->head->wt.prev = NULL; + } + + stream->wt.session = NULL; + stream->wt.prev = stream->wt.next = NULL; + + rv = nghttp3_conn_shutdown_wt_data_stream(conn, stream, + NGHTTP3_WT_SESSION_GONE); + if (rv != 0) { + return rv; + } + } + + return 0; +} + static int conn_delete_stream(nghttp3_conn *conn, nghttp3_stream *stream, uint32_t flags, uint64_t rx_app_error_code, uint64_t tx_app_error_code) { @@ -1351,6 +1597,12 @@ static int conn_delete_stream(nghttp3_conn *conn, nghttp3_stream *stream, if (stream->qpack_blocked_pe.index != NGHTTP3_PQ_BAD_INDEX) { nghttp3_conn_qpack_blocked_streams_remove(conn, stream); + if (stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD) { + assert(conn->qpack_blocked_streams_cancelled > 0); + + --conn->qpack_blocked_streams_cancelled; + } + rv = nghttp3_qpack_decoder_cancel_stream(&conn->qdec, stream->node.id); if (rv != 0) { return rv; @@ -1383,6 +1635,21 @@ static int conn_delete_stream(nghttp3_conn *conn, nghttp3_stream *stream, } } + /* The stream is still visible to an application during the + callbacks above, and they might have scheduled it again (e.g., by + calling nghttp3_conn_resume_stream). Unschedule it here, so that + the scheduler never refers to the deleted stream. */ + nghttp3_conn_unschedule_stream(conn, stream); + + if (nghttp3_stream_wt_ctrl(stream)) { + rv = conn_unlink_wt_session(conn, stream); + if (rv != 0) { + return rv; + } + } else if (nghttp3_stream_wt_data(stream)) { + nghttp3_wt_session_remove_stream(stream->wt.session, stream); + } + if (conn->server && nghttp3_client_stream_bidi(stream->node.id)) { assert(conn->remote.bidi.num_streams > 0); @@ -1410,38 +1677,62 @@ static int conn_process_blocked_stream_data(nghttp3_conn *conn, assert(nghttp3_client_stream_bidi(stream->node.id)); - for (;;) { - len = nghttp3_ringbuf_len(&stream->inq); - if (len == 0) { - break; - } - - buf = nghttp3_ringbuf_get(&stream->inq, 0); - + if (nghttp3_ringbuf_len(&stream->inq) == 0) { + /* No stream data was buffered while stream was blocked. We still + need to resume stream because HEADERS frame that ends with the + field section prefix must be completed, and fin might have been + received. */ nconsumed = nghttp3_conn_read_bidi( - conn, &nproc, stream, buf->pos, nghttp3_buf_len(buf), - len == 1 && (stream->flags & NGHTTP3_STREAM_FLAG_READ_EOF), ts); + conn, &nproc, stream, NULL, 0, + (stream->flags & NGHTTP3_STREAM_FLAG_READ_EOF) != 0, ts); if (nconsumed < 0) { return (int)nconsumed; } - buf->pos += nproc; + assert(0 == nconsumed); + } else { + for (;;) { + len = nghttp3_ringbuf_len(&stream->inq); + if (len == 0) { + break; + } + + buf = nghttp3_ringbuf_get(&stream->inq, 0); - rv = conn_call_deferred_consume(conn, stream, (size_t)nconsumed); - if (rv != 0) { - return rv; - } + nconsumed = nghttp3_conn_read_bidi( + conn, &nproc, stream, buf->pos, nghttp3_buf_len(buf), + len == 1 && (stream->flags & NGHTTP3_STREAM_FLAG_READ_EOF), ts); + if (nconsumed < 0) { + return (int)nconsumed; + } - if (nghttp3_buf_len(buf) == 0) { - nghttp3_buf_free(buf, stream->mem); - nghttp3_ringbuf_pop_front(&stream->inq); - } + buf->pos += nproc; - if (stream->flags & NGHTTP3_STREAM_FLAG_QPACK_DECODE_BLOCKED) { - break; + rv = conn_call_deferred_consume(conn, stream, (size_t)nconsumed); + if (rv != 0) { + return rv; + } + + if (nghttp3_buf_len(buf) == 0) { + nghttp3_buf_free(buf, stream->mem); + nghttp3_ringbuf_pop_front(&stream->inq); + } + + if (stream->flags & NGHTTP3_STREAM_FLAG_QPACK_DECODE_BLOCKED) { + break; + } } } + if (!(stream->flags & NGHTTP3_STREAM_FLAG_QPACK_DECODE_BLOCKED) && + (stream->flags & NGHTTP3_STREAM_FLAG_CLOSED)) { + assert(stream->qpack_blocked_pe.index == NGHTTP3_PQ_BAD_INDEX); + + return conn_delete_stream(conn, stream, stream->close_flags, + stream->close_rx_app_error_code, + stream->close_tx_app_error_code); + } + return 0; } @@ -1469,6 +1760,12 @@ nghttp3_ssize nghttp3_conn_read_qpack_encoder(nghttp3_conn *conn, stream->qpack_blocked_pe.index = NGHTTP3_PQ_BAD_INDEX; stream->flags &= (uint16_t)~NGHTTP3_STREAM_FLAG_QPACK_DECODE_BLOCKED; + if (stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD) { + assert(conn->qpack_blocked_streams_cancelled > 0); + + --conn->qpack_blocked_streams_cancelled; + } + rv = conn_process_blocked_stream_data(conn, stream, ts); if (rv != 0) { return rv; @@ -1512,24 +1809,55 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, nghttp3_stream *stream, const uint8_t *src, size_t srclen, int fin, nghttp3_tstamp ts) { - const uint8_t *p = src, *end = src ? src + srclen : src; + const uint8_t *p, *end; int rv; nghttp3_stream_read_state *rstate = &stream->rstate; nghttp3_varint_read_state *rvint = &rstate->rvint; + nghttp3_stream *wt_ctrl_stream; nghttp3_ssize nread; size_t nconsumed = 0; - int busy = 0; + int busy; size_t len; + if (srclen) { + p = src; + end = p + srclen; + } else { + src = (const uint8_t *)""; + p = src; + end = p; + } + if (stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD) { *pnproc = srclen; return (nghttp3_ssize)srclen; } - if (stream->flags & NGHTTP3_STREAM_FLAG_QPACK_DECODE_BLOCKED) { + if (stream->flags & (NGHTTP3_STREAM_FLAG_QPACK_DECODE_BLOCKED | + NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED)) { *pnproc = 0; + /* A data stream's FIN is delivered after session confirmation. */ + if (fin && !(stream->flags & NGHTTP3_STREAM_FLAG_WT_DATA)) { + if (nghttp3_stream_wt_ctrl(stream)) { + if (!(stream->wt.session->flags & NGHTTP3_WT_SESSION_FLAG_CLOSED)) { + rv = nghttp3_conn_shutdown_wt_session(conn, stream, + NGHTTP3_WT_SESSION_GONE); + if (rv != 0) { + return rv; + } + } + } else if (stream->flags & NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED) { + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + + rv = nghttp3_conn_abort_stream(conn, stream, NGHTTP3_WT_SESSION_GONE); + if (rv != 0) { + return rv; + } + } + } + if (srclen == 0) { return 0; } @@ -1541,6 +1869,14 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, return 0; } + /* If stream was blocked by QPACK decoder at the end of HEADERS + frame, we have no more frame payload to read, but the field + section is not completed yet. Complete it without waiting for + the further stream data. */ + busy = + (rstate->state == NGHTTP3_REQ_STREAM_STATE_HEADERS && rstate->left == 0) || + rstate->state == NGHTTP3_REQ_STREAM_STATE_BEFORE_WT_DATA; + for (; p != end || busy;) { busy = 0; switch (rstate->state) { @@ -1635,7 +1971,51 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, return rv; } + if (stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD) { + rstate->state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + busy = 1; + break; + } + rstate->state = NGHTTP3_REQ_STREAM_STATE_HEADERS; + break; + case NGHTTP3_EXFR_WT_STREAM_BIDI: + if (!nghttp3_stream_wt_data(stream) && + !(stream->flags & NGHTTP3_STREAM_FLAG_MAYBE_WT_DATA)) { + return NGHTTP3_ERR_H3_FRAME_UNEXPECTED; + } + + stream->flags &= (uint16_t)~NGHTTP3_STREAM_FLAG_MAYBE_WT_DATA; + + if (conn->server) { + if (stream->rx.hstate != NGHTTP3_HTTP_STATE_REQ_INITIAL) { + return NGHTTP3_ERR_H3_FRAME_UNEXPECTED; + } + } else if (stream->rx.hstate != NGHTTP3_HTTP_STATE_RESP_INITIAL) { + return NGHTTP3_ERR_H3_FRAME_UNEXPECTED; + } + + /* rstate->left is Session ID */ + rv = nghttp3_conn_on_wt_stream(conn, stream, (int64_t)rstate->left); + if (rv != 0) { + if (!nghttp3_err_is_wt(rv)) { + return rv; + } + + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + + rv = nghttp3_conn_abort_stream( + conn, stream, nghttp3_err_infer_quic_app_error_code(rv)); + if (rv != 0) { + return rv; + } + + break; + } + + rstate->state = NGHTTP3_REQ_STREAM_STATE_BEFORE_WT_DATA; + busy = 1; + break; case NGHTTP3_FRAME_PUSH_PROMISE: /* We do not support push */ case NGHTTP3_FRAME_CANCEL_PUSH: @@ -1655,6 +2035,8 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, return NGHTTP3_ERR_H3_EXCESSIVE_LOAD; } + stream->flags &= (uint16_t)~NGHTTP3_STREAM_FLAG_MAYBE_WT_DATA; + /* TODO Handle reserved frame type */ busy = 1; rstate->state = NGHTTP3_REQ_STREAM_STATE_IGN_FRAME; @@ -1663,13 +2045,37 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, break; case NGHTTP3_REQ_STREAM_STATE_DATA: len = (size_t)nghttp3_min(rstate->left, (uint64_t)(end - p)); - rv = nghttp3_conn_on_data(conn, stream, p, len); - if (rv != 0) { - return rv; + nread = nghttp3_conn_on_data(conn, stream, p, len); + if (nread < 0) { + if (!nghttp3_err_is_wt((int)nread)) { + return nread; + } + + rv = nghttp3_conn_shutdown_wt_session( + conn, stream, nghttp3_err_infer_quic_app_error_code((int)nread)); + if (rv != 0) { + return rv; + } + + /* Now that the stream is in + NGHTTP3_REQ_STREAM_STATE_IGN_REST, end_stream callback is + not called. */ + + /* Pretend that all stream data have been consumed */ + nconsumed += len; + + goto almost_done; } p += len; + nconsumed += (size_t)nread; rstate->left -= len; + if (stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD) { + rstate->state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + busy = 1; + break; + } + if (rstate->left) { goto almost_done; } @@ -1683,8 +2089,20 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, case NGHTTP3_REQ_STREAM_STATE_HEADERS: len = (size_t)nghttp3_min(rstate->left, (uint64_t)(end - p)); nread = - nghttp3_conn_on_headers(conn, stream, p, len, len == rstate->left); + nghttp3_conn_on_headers(conn, stream, p, len, len == rstate->left, ts); if (nread < 0) { + if (nread == NGHTTP3_ERR_MAX_FIELD_SECTION_SIZE_EXCEEDED) { + /* We do not expect too frequent field sections that exceed + the limit we have advertised. */ + if (conn_glitch_ratelim_drain(conn, 1, ts) != 0) { + return NGHTTP3_ERR_H3_EXCESSIVE_LOAD; + } + + rstate->state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + busy = 1; + break; + } + return nread; } @@ -1692,6 +2110,12 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, nconsumed += (size_t)nread; rstate->left -= (uint64_t)nread; + if (stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD) { + rstate->state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + busy = 1; + break; + } + if (stream->flags & NGHTTP3_STREAM_FLAG_QPACK_DECODE_BLOCKED) { if (p != end && nghttp3_stream_get_buffered_datalen(stream) == 0) { rv = nghttp3_stream_buffer_data(stream, p, (size_t)(end - p)); @@ -1739,7 +2163,9 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, return rv; } } - /* fall through */ + + rv = conn_call_end_headers(conn, stream, p == end && fin); + break; case NGHTTP3_HTTP_STATE_RESP_HEADERS_BEGIN: rv = conn_call_end_headers(conn, stream, p == end && fin); break; @@ -1761,19 +2187,141 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, nghttp3_stream_read_state_reset(rstate); - break; - case NGHTTP3_REQ_STREAM_STATE_IGN_FRAME: - len = (size_t)nghttp3_min(rstate->left, (uint64_t)(end - p)); - p += len; - nconsumed += len; - rstate->left -= len; - - if (rstate->left) { - goto almost_done; + if (stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD) { + rstate->state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + busy = 1; + break; + } + + if (conn->server) { + if (stream->rx.hstate == NGHTTP3_HTTP_STATE_REQ_HEADERS_END) { + if (stream->wt.session && (stream->wt.session->flags & + NGHTTP3_WT_SESSION_FLAG_RESP_SUBMITTED)) { + /* Server has submitted WebTransport session. */ + rv = nghttp3_conn_on_wt_session_confirmed(conn, stream, ts); + if (rv != 0) { + return rv; + } + } else if (stream->rx.http.flags & NGHTTP3_HTTP_FLAG_WEBTRANSPORT) { + if (stream->flags & NGHTTP3_STREAM_FLAG_RESP_SUBMITTED) { + /* Server refused WebTransport upgrade request */ + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + + rv = conn_call_stop_sending(conn, stream, NGHTTP3_H3_NO_ERROR); + if (rv != 0) { + return rv; + } + } else if (fin) { + if (nghttp3_stream_wt_ctrl(stream)) { + if (!(stream->wt.session->flags & + NGHTTP3_WT_SESSION_FLAG_CLOSED)) { + rv = nghttp3_conn_shutdown_wt_session( + conn, stream, NGHTTP3_WT_SESSION_GONE); + if (rv != 0) { + return rv; + } + } + } else { + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + + rv = nghttp3_conn_abort_stream(conn, stream, + NGHTTP3_WT_SESSION_GONE); + if (rv != 0) { + return rv; + } + } + + *pnproc = (size_t)(p - src); + + return (nghttp3_ssize)nconsumed; + } else { + /* Server has not submitted response */ + stream->flags |= NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED; + if (p != end) { + rv = nghttp3_stream_buffer_data(stream, p, (size_t)(end - p)); + if (rv != 0) { + return rv; + } + } + + *pnproc = (size_t)(p - src); + + return (nghttp3_ssize)nconsumed; + } + } + } + } else if (stream->rx.hstate == NGHTTP3_HTTP_STATE_RESP_HEADERS_END && + stream->wt.session) { + if (stream->rx.http.status_code == -1) { + /* Non-final response was received. Wait for final + response. */ + } else if (stream->rx.http.status_code / 100 == 2) { + rv = nghttp3_conn_on_wt_session_confirmed(conn, stream, ts); + if (rv != 0) { + return rv; + } + } else { + /* Server refused WebTransport negotiation. Reset the session + stream. This could be a redirect, but client is instructed + not to follow the redirect automatically. Most of the + case, we cannot do anything but just close the stream. */ + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + + rv = nghttp3_conn_abort_stream(conn, stream, + NGHTTP3_H3_REQUEST_CANCELLED); + if (rv != 0) { + return rv; + } + } + } + + break; + case NGHTTP3_REQ_STREAM_STATE_IGN_FRAME: + len = (size_t)nghttp3_min(rstate->left, (uint64_t)(end - p)); + p += len; + nconsumed += len; + rstate->left -= len; + + if (rstate->left) { + goto almost_done; + } + + nghttp3_stream_read_state_reset(rstate); + break; + case NGHTTP3_REQ_STREAM_STATE_BEFORE_WT_DATA: + rstate->state = NGHTTP3_REQ_STREAM_STATE_WT_DATA; + + assert(stream->wt.session); + + wt_ctrl_stream = + nghttp3_conn_find_stream(conn, stream->wt.session->session_id); + + if (!(wt_ctrl_stream->wt.session->flags & + NGHTTP3_WT_SESSION_FLAG_CONFIRMED)) { + stream->flags |= NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED; + + if (p != end) { + rv = nghttp3_stream_buffer_data(stream, p, (size_t)(end - p)); + if (rv != 0) { + return rv; + } + } + + *pnproc = (size_t)(p - src); + + return (nghttp3_ssize)nconsumed; } - nghttp3_stream_read_state_reset(rstate); break; + case NGHTTP3_REQ_STREAM_STATE_WT_DATA: + rv = conn_call_recv_wt_data(conn, stream, p, (size_t)(end - p)); + if (rv != 0) { + return rv; + } + + p = end; + + goto almost_done; case NGHTTP3_REQ_STREAM_STATE_IGN_REST: nconsumed += (size_t)(end - p); *pnproc = (size_t)(end - src); @@ -1793,10 +2341,31 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, if (rv != 0) { return rv; } + + if (nghttp3_stream_wt_ctrl(stream) && + !(stream->wt.session->flags & NGHTTP3_WT_SESSION_FLAG_CLOSED)) { + rv = nghttp3_conn_shutdown_wt_session(conn, stream, + NGHTTP3_WT_SESSION_GONE); + if (rv != 0) { + return rv; + } + } + + rv = conn_call_end_stream(conn, stream); + if (rv != 0) { + return rv; + } + + break; + case NGHTTP3_REQ_STREAM_STATE_BEFORE_WT_DATA: + /* When a stream is closed without any data */ + + case NGHTTP3_REQ_STREAM_STATE_WT_DATA: rv = conn_call_end_stream(conn, stream); if (rv != 0) { return rv; } + break; case NGHTTP3_REQ_STREAM_STATE_IGN_REST: break; @@ -1809,8 +2378,8 @@ nghttp3_ssize nghttp3_conn_read_bidi(nghttp3_conn *conn, size_t *pnproc, return (nghttp3_ssize)nconsumed; } -int nghttp3_conn_on_data(nghttp3_conn *conn, nghttp3_stream *stream, - const uint8_t *data, size_t datalen) { +nghttp3_ssize nghttp3_conn_on_data(nghttp3_conn *conn, nghttp3_stream *stream, + const uint8_t *data, size_t datalen) { int rv; rv = nghttp3_http_on_data_chunk(stream, datalen); @@ -1818,17 +2387,30 @@ int nghttp3_conn_on_data(nghttp3_conn *conn, nghttp3_stream *stream, return rv; } - if (!conn->callbacks.recv_data) { - return 0; + if (!stream->wt.session) { + return conn_call_recv_data(conn, stream, data, datalen); } - rv = conn->callbacks.recv_data(conn, stream->node.id, data, datalen, - conn->user_data, stream->user_data); + /* The stream data must be buffered until WebTransport session has + been confirmed. */ + assert(stream->wt.session->flags & NGHTTP3_WT_SESSION_FLAG_CONFIRMED); + + rv = nghttp3_conn_read_wt_ctrl_stream(conn, stream, data, datalen); if (rv != 0) { - return NGHTTP3_ERR_CALLBACK_FAILURE; + return rv; } - return 0; + /* WebTransport control stream has consumed all data */ + return (nghttp3_ssize)datalen; +} + +/* + * conn_qpack_blocked_streams returns the number of the blocked + * streams by QPACK decoder. + */ +static size_t conn_qpack_blocked_streams(const nghttp3_conn *conn) { + return nghttp3_pq_size(&conn->qpack_blocked_streams) - + conn->qpack_blocked_streams_cancelled; } static nghttp3_pq *conn_get_sched_pq(nghttp3_conn *conn, nghttp3_tnode *tnode) { @@ -1840,7 +2422,7 @@ static nghttp3_pq *conn_get_sched_pq(nghttp3_conn *conn, nghttp3_tnode *tnode) { static nghttp3_ssize conn_decode_headers(nghttp3_conn *conn, nghttp3_stream *stream, const uint8_t *src, size_t srclen, - int fin) { + int fin, nghttp3_tstamp ts) { nghttp3_ssize nread; int rv; nghttp3_qpack_decoder *qdec = &conn->qdec; @@ -1849,6 +2431,7 @@ static nghttp3_ssize conn_decode_headers(nghttp3_conn *conn, nghttp3_buf buf; nghttp3_recv_header recv_header = NULL; nghttp3_http_state *http; + size_t fieldlen; int request = 0; int trailers = 0; @@ -1871,6 +2454,10 @@ static nghttp3_ssize conn_decode_headers(nghttp3_conn *conn, } http = &stream->rx.http; + if (srclen == 0) { + src = (const uint8_t *)""; + } + nghttp3_buf_wrap_init(&buf, (uint8_t *)src, srclen); buf.last = buf.end; @@ -1886,8 +2473,10 @@ static nghttp3_ssize conn_decode_headers(nghttp3_conn *conn, buf.pos += nread; if (flags & NGHTTP3_QPACK_DECODE_FLAG_BLOCKED) { + /* A remote endpoint does not count the streams for which + Stream Cancellation has been sent as blocked streams. */ if (conn->local.settings.qpack_blocked_streams <= - nghttp3_pq_size(&conn->qpack_blocked_streams)) { + conn_qpack_blocked_streams(conn)) { return NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED; } @@ -1896,11 +2485,20 @@ static nghttp3_ssize conn_decode_headers(nghttp3_conn *conn, if (rv != 0) { return rv; } + + if (stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD) { + /* Read-side stream has been shut down in the middle of + processing the stream data, and Stream Cancellation has + already been sent. */ + ++conn->qpack_blocked_streams_cancelled; + } + break; } if (flags & NGHTTP3_QPACK_DECODE_FLAG_FINAL) { nghttp3_qpack_stream_context_reset(&stream->qpack_sctx); + stream->field_blocklen = 0; break; } @@ -1909,6 +2507,29 @@ static nghttp3_ssize conn_decode_headers(nghttp3_conn *conn, } if (flags & NGHTTP3_QPACK_DECODE_FLAG_EMIT) { + fieldlen = nv.name->len + nv.value->len + 32; + + if (fieldlen > conn->local.settings.max_field_section_size || + stream->field_blocklen > + conn->local.settings.max_field_section_size - fieldlen) { + nghttp3_rcbuf_decref(nv.name); + nghttp3_rcbuf_decref(nv.value); + + rv = conn_call_stop_sending(conn, stream, NGHTTP3_H3_EXCESSIVE_LOAD); + if (rv != 0) { + return rv; + } + + rv = conn_call_reset_stream(conn, stream, NGHTTP3_H3_EXCESSIVE_LOAD); + if (rv != 0) { + return rv; + } + + return NGHTTP3_ERR_MAX_FIELD_SECTION_SIZE_EXCEEDED; + } + + stream->field_blocklen += fieldlen; + rv = nghttp3_http_on_header( http, &nv, request, trailers, conn->server && conn->local.settings.enable_connect_protocol); @@ -1916,6 +2537,13 @@ static nghttp3_ssize conn_decode_headers(nghttp3_conn *conn, case NGHTTP3_ERR_MALFORMED_HTTP_HEADER: break; case NGHTTP3_ERR_REMOVE_HTTP_HEADER: + /* The field is dropped without notifying application. We do + not expect too frequent invalid fields. */ + if (conn_glitch_ratelim_drain(conn, 1, ts) != 0) { + rv = NGHTTP3_ERR_H3_EXCESSIVE_LOAD; + break; + } + rv = 0; break; case 0: @@ -1937,6 +2565,15 @@ static nghttp3_ssize conn_decode_headers(nghttp3_conn *conn, if (rv != 0) { return rv; } + + if (stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD) { + /* Application called nghttp3_conn_shutdown_stream_read from + the callback, and Stream Cancellation has been written. + Stop decoding here; otherwise Section Acknowledgment for + the cancelled stream is written when the decoder reaches + the end of the field section. */ + break; + } } } @@ -1946,12 +2583,12 @@ static nghttp3_ssize conn_decode_headers(nghttp3_conn *conn, nghttp3_ssize nghttp3_conn_on_headers(nghttp3_conn *conn, nghttp3_stream *stream, const uint8_t *src, size_t srclen, - int fin) { + int fin, nghttp3_tstamp ts) { if (srclen == 0 && !fin) { return 0; } - return conn_decode_headers(conn, stream, src, srclen, fin); + return conn_decode_headers(conn, stream, src, srclen, fin, ts); } int nghttp3_conn_on_settings_entry_received(nghttp3_conn *conn, @@ -2035,6 +2672,23 @@ int nghttp3_conn_on_settings_entry_received(nghttp3_conn *conn, dest->h3_datagram = (uint8_t)ent->value; break; + case NGHTTP3_SETTINGS_ID_WT_ENABLED: + /* compat for pre draft-17 */ + case NGHTTP3_SETTINGS_ID_WT_ENABLED_DRAFT15: + if (ent->value > 1) { + return NGHTTP3_ERR_H3_SETTINGS_ERROR; + } + + dest->wt_enabled = 1; + + break; + /* compat for pre draft-15 */ + case NGHTTP3_SETTINGS_ID_WT_MAX_SESSIONS: + case NGHTTP3_SETTINGS_ID_WT_MAX_SESSIONS_DRAFT7: + /* compat for ancient draft */ + case NGHTTP3_SETTINGS_ID_ENABLE_WEBTRANSPORT_DRAFT2: + dest->wt_enabled = ent->value != 0; + break; case NGHTTP3_H2_SETTINGS_ID_ENABLE_PUSH: case NGHTTP3_H2_SETTINGS_ID_MAX_CONCURRENT_STREAMS: case NGHTTP3_H2_SETTINGS_ID_INITIAL_WINDOW_SIZE: @@ -2048,6 +2702,37 @@ int nghttp3_conn_on_settings_entry_received(nghttp3_conn *conn, return 0; } +static int abort_wt_session(void *data, void *ptr) { + nghttp3_conn *conn = ptr; + nghttp3_stream *stream = data; + + if (!nghttp3_stream_wt_ctrl(stream)) { + return 0; + } + + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + + return nghttp3_conn_abort_stream(conn, stream, + NGHTTP3_H3_GENERAL_PROTOCOL_ERROR); +} + +int nghttp3_conn_on_settings_received(nghttp3_conn *conn) { + int rv; + + conn->flags |= NGHTTP3_CONN_FLAG_SETTINGS_RECVED; + + rv = conn_call_recv_settings(conn); + if (rv != 0) { + return rv; + } + + if (!conn->local.settings.wt_enabled || conn_wt_enabled(conn)) { + return 0; + } + + return nghttp3_map_each(&conn->streams, abort_wt_session, conn); +} + static int conn_on_priority_update_stream(nghttp3_conn *conn, const nghttp3_frame_priority_update *fr) { @@ -2068,6 +2753,13 @@ conn_on_priority_update_stream(nghttp3_conn *conn, return 0; } + /* Limiting streams under max_concurrent_streams is a good + defence. */ + if (conn->remote.bidi.num_streams >= + conn->remote.bidi.max_concurrent_streams) { + return 0; + } + rv = conn_bidi_idtr_open(conn, stream_id); if (rv != 0) { if (nghttp3_err_is_fatal(rv)) { @@ -2089,6 +2781,11 @@ conn_on_priority_update_stream(nghttp3_conn *conn, stream->node.pri = fr->pri; stream->flags |= NGHTTP3_STREAM_FLAG_PRIORITY_UPDATE_RECVED; + + if (conn_wt_enabled(conn)) { + stream->flags |= NGHTTP3_STREAM_FLAG_MAYBE_WT_DATA; + } + stream->rx.hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL; return 0; @@ -2357,7 +3054,7 @@ nghttp3_ssize nghttp3_conn_writev_stream(nghttp3_conn *conn, return ncnt; } - if (nghttp3_client_stream_bidi(stream->node.id) && + if (nghttp3_stream_schedulable(stream) && !nghttp3_stream_require_schedule(stream)) { nghttp3_conn_unschedule_stream(conn, stream); } @@ -2396,7 +3093,7 @@ int nghttp3_conn_add_write_offset(nghttp3_conn *conn, int64_t stream_id, stream->unscheduled_nwrite += n; - if (!nghttp3_client_stream_bidi(stream->node.id)) { + if (!nghttp3_stream_schedulable(stream)) { return 0; } @@ -2438,6 +3135,22 @@ int nghttp3_conn_update_ack_offset(nghttp3_conn *conn, int64_t stream_id, return nghttp3_stream_update_ack_offset(stream, offset); } +static nghttp3_ssize wt_session_read_data(nghttp3_conn *conn, int64_t stream_id, + nghttp3_vec *vec, size_t veccnt, + uint32_t *pflags, + void *conn_user_data, + void *stream_user_data) { + (void)conn; + (void)stream_id; + (void)vec; + (void)veccnt; + (void)pflags; + (void)conn_user_data; + (void)stream_user_data; + + return NGHTTP3_ERR_WOULDBLOCK; +} + static int conn_submit_headers_data(nghttp3_conn *conn, nghttp3_stream *stream, const nghttp3_nv *nva, size_t nvlen, const nghttp3_data_reader *dr) { @@ -2462,7 +3175,7 @@ static int conn_submit_headers_data(nghttp3_conn *conn, nghttp3_stream *stream, .nvlen = nvlen, }; - if (dr) { + if (dr && dr->read_data != wt_session_read_data) { rv = nghttp3_stream_frq_emplace(stream, &fr); if (rv != 0) { return rv; @@ -2528,6 +3241,10 @@ int nghttp3_conn_submit_request(nghttp3_conn *conn, int64_t stream_id, /* TODO Check GOAWAY last stream ID */ + if (nvlen == 0) { + return NGHTTP3_ERR_INVALID_ARGUMENT; + } + if (conn->flags & NGHTTP3_CONN_FLAG_GOAWAY_RECVED) { return NGHTTP3_ERR_CONN_CLOSING; } @@ -2580,6 +3297,10 @@ int nghttp3_conn_submit_response(nghttp3_conn *conn, int64_t stream_id, assert(conn->server); assert(conn->tx.qenc); + if (nvlen == 0) { + return NGHTTP3_ERR_INVALID_ARGUMENT; + } + stream = nghttp3_conn_find_stream(conn, stream_id); if (stream == NULL) { return NGHTTP3_ERR_STREAM_NOT_FOUND; @@ -2589,6 +3310,8 @@ int nghttp3_conn_submit_response(nghttp3_conn *conn, int64_t stream_id, stream->flags |= NGHTTP3_STREAM_FLAG_WRITE_END_STREAM; } + stream->flags |= NGHTTP3_STREAM_FLAG_RESP_SUBMITTED; + return conn_submit_headers_data(conn, stream, nva, nvlen, dr); } @@ -2619,6 +3342,10 @@ int nghttp3_conn_submit_shutdown_notice(nghttp3_conn *conn) { assert(conn->tx.ctrl); + if (conn->flags & NGHTTP3_CONN_FLAG_SHUTDOWN_COMMENCED) { + return 0; + } + rv = nghttp3_stream_frq_emplace(conn->tx.ctrl, &fr); if (rv != 0) { return rv; @@ -2644,6 +3371,10 @@ int nghttp3_conn_shutdown(nghttp3_conn *conn) { assert(conn->tx.ctrl); + if (conn->flags & NGHTTP3_CONN_FLAG_SHUTDOWN_COMMENCED) { + return 0; + } + rv = nghttp3_stream_frq_emplace(conn->tx.ctrl, &fr); if (rv != 0) { return rv; @@ -2666,14 +3397,27 @@ int nghttp3_conn_shutdown(nghttp3_conn *conn) { } int nghttp3_conn_reject_stream(nghttp3_conn *conn, nghttp3_stream *stream) { + return nghttp3_conn_abort_stream(conn, stream, NGHTTP3_H3_REQUEST_REJECTED); +} + +int nghttp3_conn_abort_stream(nghttp3_conn *conn, nghttp3_stream *stream, + uint64_t error_code) { int rv; + int remote_uni = conn_remote_stream_uni(conn, stream->node.id); + int bidi = !nghttp3_stream_uni(stream->node.id); - rv = conn_call_stop_sending(conn, stream, NGHTTP3_H3_REQUEST_REJECTED); - if (rv != 0) { - return rv; + if (remote_uni || bidi) { + rv = conn_call_stop_sending(conn, stream, error_code); + if (rv != 0) { + return rv; + } + } + + if (remote_uni) { + return 0; } - return conn_call_reset_stream(conn, stream, NGHTTP3_H3_REQUEST_REJECTED); + return conn_call_reset_stream(conn, stream, error_code); } void nghttp3_conn_block_stream(nghttp3_conn *conn, int64_t stream_id) { @@ -2686,7 +3430,7 @@ void nghttp3_conn_block_stream(nghttp3_conn *conn, int64_t stream_id) { stream->flags |= NGHTTP3_STREAM_FLAG_FC_BLOCKED; stream->unscheduled_nwrite = 0; - if (nghttp3_client_stream_bidi(stream->node.id)) { + if (nghttp3_stream_schedulable(stream)) { nghttp3_conn_unschedule_stream(conn, stream); } } @@ -2701,7 +3445,7 @@ void nghttp3_conn_shutdown_stream_write(nghttp3_conn *conn, int64_t stream_id) { stream->flags |= NGHTTP3_STREAM_FLAG_SHUT_WR; stream->unscheduled_nwrite = 0; - if (nghttp3_client_stream_bidi(stream->node.id)) { + if (nghttp3_stream_schedulable(stream)) { nghttp3_conn_unschedule_stream(conn, stream); } } @@ -2715,7 +3459,7 @@ int nghttp3_conn_unblock_stream(nghttp3_conn *conn, int64_t stream_id) { stream->flags &= (uint16_t)~NGHTTP3_STREAM_FLAG_FC_BLOCKED; - if (nghttp3_client_stream_bidi(stream->node.id) && + if (nghttp3_stream_schedulable(stream) && nghttp3_stream_require_schedule(stream)) { return nghttp3_conn_ensure_stream_scheduled(conn, stream); } @@ -2735,9 +3479,10 @@ int nghttp3_conn_is_stream_writable2(const nghttp3_conn *conn, return 0; } - return (stream->flags & (NGHTTP3_STREAM_FLAG_FC_BLOCKED | - NGHTTP3_STREAM_FLAG_READ_DATA_BLOCKED | - NGHTTP3_STREAM_FLAG_SHUT_WR)) == 0; + return (stream->flags & + (NGHTTP3_STREAM_FLAG_FC_BLOCKED | + NGHTTP3_STREAM_FLAG_READ_DATA_BLOCKED | NGHTTP3_STREAM_FLAG_SHUT_WR | + NGHTTP3_STREAM_FLAG_CLOSED)) == 0; } int nghttp3_conn_resume_stream(nghttp3_conn *conn, int64_t stream_id) { @@ -2749,7 +3494,7 @@ int nghttp3_conn_resume_stream(nghttp3_conn *conn, int64_t stream_id) { stream->flags &= (uint16_t)~NGHTTP3_STREAM_FLAG_READ_DATA_BLOCKED; - if (nghttp3_client_stream_bidi(stream->node.id) && + if (nghttp3_stream_schedulable(stream) && nghttp3_stream_require_schedule(stream)) { return nghttp3_conn_ensure_stream_scheduled(conn, stream); } @@ -2770,17 +3515,45 @@ int nghttp3_conn_close_stream2(nghttp3_conn *conn, uint32_t flags, int64_t stream_id, uint64_t rx_app_error_code, uint64_t tx_app_error_code) { nghttp3_stream *stream = nghttp3_conn_find_stream(conn, stream_id); + int rv; if (stream == NULL) { + if (conn->server && nghttp3_client_stream_bidi(stream_id)) { + /* The stream has been closed without reading any stream data + (e.g., it is reset before stream data is delivered). + Remember that this stream ID has been used so that + PRIORITY_UPDATE frame which arrives later does not create a + stream which is never closed. */ + rv = conn_bidi_idtr_open(conn, stream_id); + if (nghttp3_err_is_fatal(rv)) { + return rv; + } + } + return NGHTTP3_ERR_STREAM_NOT_FOUND; } - if (nghttp3_stream_uni(stream_id) && - stream->type != NGHTTP3_STREAM_TYPE_UNKNOWN) { + if (nghttp3_stream_critical(stream)) { return NGHTTP3_ERR_H3_CLOSED_CRITICAL_STREAM; } - nghttp3_conn_unschedule_stream(conn, stream); + /* If client has received all stream data, but the stream is blocked + by QPACK decoder, the buffered response can still be delivered + to an application. Defer the deletion of the stream until it is + unblocked. */ + if (!conn->server && stream->qpack_blocked_pe.index != NGHTTP3_PQ_BAD_INDEX && + (stream->flags & NGHTTP3_STREAM_FLAG_READ_EOF) && + !(stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD)) { + stream->flags |= NGHTTP3_STREAM_FLAG_CLOSED; + stream->close_flags = flags; + stream->close_rx_app_error_code = rx_app_error_code; + stream->close_tx_app_error_code = tx_app_error_code; + + /* The closed stream must be unscheduled. */ + nghttp3_conn_unschedule_stream(conn, stream); + + return 0; + } return conn_delete_stream(conn, stream, flags, rx_app_error_code, tx_app_error_code); @@ -2803,6 +3576,21 @@ int nghttp3_conn_shutdown_stream_read(nghttp3_conn *conn, int64_t stream_id) { } stream->flags |= NGHTTP3_STREAM_FLAG_SHUT_RD; + + if (stream->qpack_blocked_pe.index != NGHTTP3_PQ_BAD_INDEX) { + /* The stream stays in qpack_blocked_streams until it is + unblocked or closed so that the buffered stream data is + consumed, but a remote endpoint no longer counts it as + blocked stream once it receives Stream Cancellation. */ + ++conn->qpack_blocked_streams_cancelled; + } + + /* If stream is WebTransport data stream, do not send QPACK Stream + Cancellation. */ + if (nghttp3_stream_wt_data(stream) || + (stream->flags & NGHTTP3_STREAM_FLAG_WT_DATA)) { + return 0; + } } return nghttp3_qpack_decoder_cancel_stream(&conn->qdec, stream_id); @@ -2839,6 +3627,8 @@ void nghttp3_conn_set_max_client_streams_bidi(nghttp3_conn *conn, void nghttp3_conn_set_max_concurrent_streams(nghttp3_conn *conn, size_t max_concurrent_streams) { + conn->remote.bidi.max_concurrent_streams = max_concurrent_streams; + nghttp3_qpack_decoder_set_max_concurrent_streams(&conn->qdec, max_concurrent_streams); } @@ -3044,5 +3834,853 @@ int nghttp3_conn_is_stream_flushed(const nghttp3_conn *conn, fr = nghttp3_ringbuf_get(&stream->frq, 0); - return fr->hd.type == NGHTTP3_FRAME_DATA; + return fr->hd.type == NGHTTP3_FRAME_DATA || + (fr->hd.type == NGHTTP3_FRAME_EX_WT && + fr->wt.fr.hd.type == NGHTTP3_EXFR_WT_STREAM_DATA); +} + +int nghttp3_conn_submit_wt_request(nghttp3_conn *conn, int64_t stream_id, + const nghttp3_nv *nva, size_t nvlen, + void *stream_user_data) { + int rv; + nghttp3_stream *stream; + + if (!conn_wt_enabled(conn)) { + return NGHTTP3_ERR_INVALID_STATE; + } + + rv = nghttp3_conn_submit_request( + conn, stream_id, nva, nvlen, + &(nghttp3_data_reader){.read_data = wt_session_read_data}, + stream_user_data); + if (rv != 0) { + return rv; + } + + stream = nghttp3_conn_find_stream(conn, stream_id); + + assert(stream); + + return nghttp3_conn_open_wt_session(conn, stream); +} + +int nghttp3_conn_submit_wt_response(nghttp3_conn *conn, int64_t stream_id, + const nghttp3_nv *nva, size_t nvlen) { + int rv; + nghttp3_stream *stream; + + if (!(conn->flags & NGHTTP3_CONN_FLAG_SETTINGS_RECVED) || + !conn_wt_enabled(conn)) { + return NGHTTP3_ERR_INVALID_STATE; + } + + stream = nghttp3_conn_find_stream(conn, stream_id); + if (stream == NULL) { + return NGHTTP3_ERR_STREAM_NOT_FOUND; + } + + if (stream->flags & NGHTTP3_STREAM_FLAG_READ_EOF) { + return NGHTTP3_ERR_INVALID_STATE; + } + + rv = nghttp3_conn_submit_response( + conn, stream_id, nva, nvlen, + &(nghttp3_data_reader){.read_data = wt_session_read_data}); + if (rv != 0) { + return rv; + } + + if (!stream->wt.session) { + rv = nghttp3_conn_open_wt_session(conn, stream); + if (rv != 0) { + return rv; + } + } + + stream->wt.session->flags |= NGHTTP3_WT_SESSION_FLAG_RESP_SUBMITTED; + + return 0; +} + +int nghttp3_conn_server_confirm_wt_session(nghttp3_conn *conn, + int64_t session_id, + nghttp3_tstamp ts) { + nghttp3_stream *wt_ctrl_stream; + + wt_ctrl_stream = nghttp3_conn_find_stream(conn, session_id); + if (!wt_ctrl_stream) { + return NGHTTP3_ERR_STREAM_NOT_FOUND; + } + + assert(wt_ctrl_stream->wt.session); + assert(wt_ctrl_stream->wt.session->flags & + NGHTTP3_WT_SESSION_FLAG_RESP_SUBMITTED); + + wt_ctrl_stream->flags &= (uint16_t)~NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED; + + return nghttp3_conn_on_wt_session_confirmed(conn, wt_ctrl_stream, ts); +} + +int nghttp3_conn_open_wt_session(nghttp3_conn *conn, nghttp3_stream *stream) { + return nghttp3_wt_session_new(&stream->wt.session, stream->node.id, + conn->mem); +} + +int nghttp3_conn_open_wt_data_stream(nghttp3_conn *conn, int64_t session_id, + int64_t stream_id, + const nghttp3_data_reader *dr, + void *stream_user_data) { + nghttp3_stream *stream, *wt_ctrl_stream; + nghttp3_wt_session *wt_session; + nghttp3_frame *fr; + uint64_t type; + int rv; + int remote_bidi = 0; + + if (conn->server) { + assert(nghttp3_client_stream_bidi(stream_id) || + nghttp3_server_stream_bidi(stream_id) || + nghttp3_server_stream_uni(stream_id)); + } else { + assert(nghttp3_client_stream_bidi(stream_id) || + nghttp3_server_stream_bidi(stream_id) || + nghttp3_client_stream_uni(stream_id)); + } + + /* TODO Check session flow control */ + + assert(dr); + + if (conn->flags & NGHTTP3_CONN_FLAG_GOAWAY_RECVED) { + return NGHTTP3_ERR_CONN_CLOSING; + } + + wt_ctrl_stream = nghttp3_conn_find_stream(conn, session_id); + if (!wt_ctrl_stream || !wt_ctrl_stream->wt.session) { + return NGHTTP3_ERR_INVALID_ARGUMENT; + } + + wt_session = wt_ctrl_stream->wt.session; + + if (wt_session->flags & NGHTTP3_WT_SESSION_FLAG_CLOSED) { + return NGHTTP3_ERR_INVALID_STATE; + } + + stream = nghttp3_conn_find_stream(conn, stream_id); + if (stream) { + if (conn->server) { + assert(nghttp3_client_stream_bidi(stream_id)); + } else { + assert(nghttp3_server_stream_bidi(stream_id)); + } + + /* TODO verify that we do not start writing more than once. */ + + /* Normally, stream->wt.session is not NULL because we must + identify WT stream header first. The only exception is a + stream create by priority update on server side. But it must + be client initiated bidi stream, and we must wait for its WT + header. */ + if (!stream->wt.session) { + return NGHTTP3_ERR_INVALID_ARGUMENT; + } + + if (stream->wt.session->session_id != session_id) { + return NGHTTP3_ERR_INVALID_ARGUMENT; + } + + if (stream->flags & NGHTTP3_STREAM_FLAG_WRITE_END_STREAM) { + return NGHTTP3_ERR_INVALID_STATE; + } + + remote_bidi = 1; + + if (stream_user_data) { + stream->user_data = stream_user_data; + } + + if (conn->server) { + stream->flags |= NGHTTP3_STREAM_FLAG_SERVER_PRIORITY_SET; + } + + assert(!nghttp3_tnode_is_scheduled(&stream->node)); + } else { + if (conn->server) { + assert(nghttp3_server_stream_bidi(stream_id) || + nghttp3_server_stream_uni(stream_id)); + } else { + assert(nghttp3_client_stream_bidi(stream_id) || + nghttp3_client_stream_uni(stream_id)); + } + + rv = nghttp3_conn_create_stream(conn, &stream, stream_id); + if (rv != 0) { + return rv; + } + + nghttp3_wt_session_add_stream(wt_session, stream); + + if (conn->server) { + stream->rx.hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL; + } else { + stream->rx.hstate = NGHTTP3_HTTP_STATE_RESP_INITIAL; + } + + stream->user_data = stream_user_data; + + if (stream_id & 0x2) { + stream->flags |= NGHTTP3_STREAM_FLAG_SHUT_RD; + stream->type = NGHTTP3_STREAM_TYPE_WT_STREAM; + } + } + + stream->node.pri = (nghttp3_pri){ + .urgency = NGHTTP3_DEFAULT_URGENCY, + .inc = 1, + }; + + if (stream_id & 0x2) { + type = NGHTTP3_EXFR_WT_STREAM_UNI; + } else if (remote_bidi) { + type = NGHTTP3_EXFR_WT_STREAM_DATA; + } else { + type = NGHTTP3_EXFR_WT_STREAM_BIDI; + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_BEFORE_WT_DATA; + } + + rv = nghttp3_stream_frq_emplace(stream, &fr); + if (rv != 0) { + return rv; + } + + fr->wt = (nghttp3_frame_ex_wt){ + .type = NGHTTP3_FRAME_EX_WT, + .fr.wt_stream = + { + .type = type, + .session_id = session_id, + .dr = *dr, + }, + }; + + if (nghttp3_stream_require_schedule(stream)) { + return nghttp3_conn_ensure_stream_scheduled(conn, stream); + } + + return 0; +} + +int nghttp3_conn_close_wt_session(nghttp3_conn *conn, int64_t session_id, + uint32_t wt_error_code, const uint8_t *msg, + size_t msglen) { + nghttp3_stream *stream; + nghttp3_wt_session *wt_session; + nghttp3_frame *fr; + int rv; + + stream = nghttp3_conn_find_stream(conn, session_id); + if (stream == NULL) { + return NGHTTP3_ERR_STREAM_NOT_FOUND; + } + + if (!nghttp3_stream_wt_ctrl(stream) || msglen > 1024) { + return NGHTTP3_ERR_INVALID_ARGUMENT; + } + + if (stream->flags & NGHTTP3_STREAM_FLAG_WRITE_END_STREAM) { + return NGHTTP3_ERR_INVALID_STATE; + } + + stream->flags |= NGHTTP3_STREAM_FLAG_WRITE_END_STREAM; + + wt_session = stream->wt.session; + + assert(!wt_session->tx.error_msg.base); + + if (msglen) { + wt_session->tx.error_msg.base = nghttp3_mem_malloc(conn->mem, msglen); + if (!wt_session->tx.error_msg.base) { + return NGHTTP3_ERR_NOMEM; + } + + memcpy(wt_session->tx.error_msg.base, msg, msglen); + wt_session->tx.error_msg.len = msglen; + } + + rv = nghttp3_stream_frq_emplace(stream, &fr); + if (rv != 0) { + return rv; + } + + fr->cpsl = (nghttp3_frame_ex_cpsl){ + .type = NGHTTP3_FRAME_EX_CPSL, + .fr.wt_close_session = + { + .type = NGHTTP3_EXFR_CPSL_WT_CLOSE_SESSION, + .error_code = wt_error_code, + .error_msg = wt_session->tx.error_msg, + }, + }; + + if (nghttp3_stream_require_schedule(stream)) { + rv = nghttp3_conn_schedule_stream(conn, stream); + if (rv != 0) { + return rv; + } + } + + wt_session->flags |= NGHTTP3_WT_SESSION_FLAG_CLOSED; + + rv = nghttp3_conn_shutdown_all_wt_data_streams(conn, stream, + NGHTTP3_WT_SESSION_GONE); + if (rv != 0) { + return rv; + } + + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + + + return 0; + // turn it off for now as it set up firefox and chromium + // return conn_call_stop_sending(conn, stream, NGHTTP3_WT_SESSION_GONE); +} + +int nghttp3_conn_on_wt_stream(nghttp3_conn *conn, nghttp3_stream *stream, + int64_t session_id) { + nghttp3_stream *wt_ctrl_stream; + nghttp3_wt_session *wt_session; + int rv; + + if (!nghttp3_client_stream_bidi(session_id)) { + return NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED; + } + + if (stream->wt.session) { + assert(stream->wt.session->session_id != stream->node.id); + + if (stream->wt.session->session_id != session_id) { + return NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED; + } + + return 0; + } + + wt_ctrl_stream = nghttp3_conn_find_stream(conn, session_id); + if (wt_ctrl_stream) { + if (conn->server) { + if ((wt_ctrl_stream->flags & NGHTTP3_STREAM_FLAG_RESP_SUBMITTED) && + !wt_ctrl_stream->wt.session) { + /* Server has submitted the regular non-WebTransport response. + wt_ctrl_stream is not WebTransport session stream. */ + return NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED; + } + } else if (!wt_ctrl_stream->wt.session) { + /* On client side, if it has not submitted the request with + nghttp3_conn_submit_wt_request, it is not WebTransport + session stream. */ + return NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED; + } + } else { + if (!conn->server) { + /* On client's perspective, if session stream is not found, we are + sure that session is gone. */ + return NGHTTP3_ERR_WT_SESSION_GONE; + } + + if (nghttp3_ord_stream_id(session_id) > + conn->remote.bidi.max_client_streams) { + return NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED; + } + + if ((conn->flags & NGHTTP3_CONN_FLAG_GOAWAY_QUEUED) && + conn->tx.goaway_id <= session_id) { + return NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED; + } + + rv = conn_bidi_idtr_open(conn, session_id); + if (rv != 0) { + if (nghttp3_err_is_fatal(rv)) { + return rv; + } + + return NGHTTP3_ERR_WT_SESSION_GONE; + } + + conn->rx.max_stream_id_bidi = + nghttp3_max(conn->rx.max_stream_id_bidi, session_id); + rv = nghttp3_conn_create_stream(conn, &wt_ctrl_stream, session_id); + if (rv != 0) { + return rv; + } + + wt_ctrl_stream->rx.hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL; + } + + if (wt_ctrl_stream->wt.session) { + if (wt_ctrl_stream->wt.session->flags & NGHTTP3_WT_SESSION_FLAG_CLOSED) { + return NGHTTP3_ERR_WT_SESSION_GONE; + } + } else if (wt_ctrl_stream->flags & NGHTTP3_STREAM_FLAG_READ_EOF) { + return NGHTTP3_ERR_WT_SESSION_GONE; + } else { + rv = nghttp3_conn_open_wt_session(conn, wt_ctrl_stream); + if (rv != 0) { + return rv; + } + } + + wt_session = wt_ctrl_stream->wt.session; + + assert(wt_session); + + nghttp3_wt_session_add_stream(wt_session, stream); + + if (wt_ctrl_stream->wt.session->flags & NGHTTP3_WT_SESSION_FLAG_CONFIRMED) { + return conn_call_wt_data_stream_open(conn, stream); + } + + return 0; +} + +int nghttp3_conn_on_wt_session_confirmed(nghttp3_conn *conn, + nghttp3_stream *wt_ctrl_stream, + nghttp3_tstamp ts) { + nghttp3_stream *stream; + nghttp3_wt_session *wt_session = wt_ctrl_stream->wt.session; + int rv; + + if (wt_session->flags & NGHTTP3_WT_SESSION_FLAG_CLOSED) { + return NGHTTP3_ERR_INVALID_STATE; + } + + wt_session->flags |= NGHTTP3_WT_SESSION_FLAG_CONFIRMED; + + /* TODO Is stream gone during iteration? */ + for (stream = wt_session->head; stream; stream = stream->wt.next) { + stream->flags &= (uint16_t)~NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED; + + if (conn_remote_stream(conn, stream->node.id)) { + rv = conn_call_wt_data_stream_open(conn, stream); + if (rv != 0) { + return rv; + } + } + + rv = nghttp3_conn_process_blocked_wt_stream_data(conn, stream, ts); + if (rv != 0) { + return rv; + } + } + + return nghttp3_conn_process_blocked_wt_stream_data(conn, wt_ctrl_stream, ts); +} + +nghttp3_ssize nghttp3_conn_read_wt_stream_uni(nghttp3_conn *conn, + nghttp3_stream *stream, + const uint8_t *src, size_t srclen, + int fin, nghttp3_tstamp ts) { + const uint8_t *p = src, *end = src ? src + srclen : src; + int rv; + nghttp3_stream_read_state *rstate = &stream->rstate; + nghttp3_varint_read_state *rvint = &rstate->rvint; + nghttp3_ssize nread; + size_t nconsumed = 0; + nghttp3_stream *wt_ctrl_stream; + (void)ts; + + if ((stream->flags & NGHTTP3_STREAM_FLAG_SHUT_RD)) { + return (nghttp3_ssize)srclen; + } + + if (stream->flags & NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED) { + if (srclen == 0) { + return 0; + } + + rv = nghttp3_stream_buffer_data(stream, p, srclen); + if (rv != 0) { + return rv; + } + + return 0; + } + + if (srclen == 0) { + if (fin && rstate->state == NGHTTP3_WT_STREAM_STATE_SESSION_ID) { + return NGHTTP3_ERR_H3_GENERAL_PROTOCOL_ERROR; + } + + goto almost_done; + } + + switch (rstate->state) { + case NGHTTP3_WT_STREAM_STATE_SESSION_ID: + assert(end - p > 0); + nread = nghttp3_read_varint(rvint, p, end, fin); + if (nread < 0) { + return NGHTTP3_ERR_H3_FRAME_ERROR; + } + + p += nread; + nconsumed += (size_t)nread; + if (rvint->left) { + break; + } + + rstate->left = rvint->acc; + nghttp3_varint_read_state_reset(rvint); + + /* rstate->left is Session ID */ + rv = nghttp3_conn_on_wt_stream(conn, stream, (int64_t)rstate->left); + if (rv != 0) { + if (!nghttp3_err_is_wt(rv)) { + return rv; + } + + stream->rstate.state = NGHTTP3_WT_STREAM_STATE_IGN_REST; + + rv = nghttp3_conn_abort_stream(conn, stream, + nghttp3_err_infer_quic_app_error_code(rv)); + if (rv != 0) { + return rv; + } + + nconsumed += (size_t)(end - p); + + return (nghttp3_ssize)nconsumed; + } + + rstate->state = NGHTTP3_WT_STREAM_STATE_DATA; + + wt_ctrl_stream = + nghttp3_conn_find_stream(conn, stream->wt.session->session_id); + + if (!(wt_ctrl_stream->wt.session->flags & + NGHTTP3_WT_SESSION_FLAG_CONFIRMED)) { + stream->flags |= NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED; + + if (p != end) { + rv = nghttp3_stream_buffer_data(stream, p, (size_t)(end - p)); + if (rv != 0) { + return rv; + } + } + + return (nghttp3_ssize)nconsumed; + } + + if (p == end) { + break; + } + + /* Fall through */ + case NGHTTP3_WT_STREAM_STATE_DATA: + rv = conn_call_recv_wt_data(conn, stream, p, (size_t)(end - p)); + if (rv != 0) { + return rv; + } + + break; + case NGHTTP3_WT_STREAM_STATE_IGN_REST: + nconsumed += (size_t)(end - p); + + return (nghttp3_ssize)nconsumed; + } + +almost_done: + if (fin) { + rv = conn_call_end_stream(conn, stream); + if (rv != 0) { + return rv; + } + } + + return (nghttp3_ssize)nconsumed; +} + +int nghttp3_conn_process_blocked_wt_stream_data(nghttp3_conn *conn, + nghttp3_stream *stream, + nghttp3_tstamp ts) { + nghttp3_buf *buf; + nghttp3_ssize nconsumed; + size_t nproc; + int rv; + size_t len; + + /* The CONNECT reader handles FIN after parsing the rest of its + input. */ + if (nghttp3_stream_wt_data(stream) && + nghttp3_ringbuf_len(&stream->inq) == 0 && + (stream->flags & NGHTTP3_STREAM_FLAG_READ_EOF)) { + if (nghttp3_stream_uni(stream->node.id)) { + nconsumed = nghttp3_conn_read_wt_stream_uni(conn, stream, NULL, 0, + /* fin = */ 1, ts); + } else { + nconsumed = nghttp3_conn_read_bidi(conn, &nproc, stream, NULL, 0, + /* fin = */ 1, ts); + } + + if (nconsumed < 0) { + return (int)nconsumed; + } + + return conn_call_deferred_consume(conn, stream, (size_t)nconsumed); + } + + for (;;) { + len = nghttp3_ringbuf_len(&stream->inq); + if (len == 0) { + break; + } + + buf = nghttp3_ringbuf_get(&stream->inq, 0); + + if (nghttp3_stream_uni(stream->node.id)) { + nconsumed = nghttp3_conn_read_wt_stream_uni( + conn, stream, buf->pos, nghttp3_buf_len(buf), + len == 1 && (stream->flags & NGHTTP3_STREAM_FLAG_READ_EOF), ts); + } else { + nconsumed = nghttp3_conn_read_bidi( + conn, &nproc, stream, buf->pos, nghttp3_buf_len(buf), + len == 1 && (stream->flags & NGHTTP3_STREAM_FLAG_READ_EOF), ts); + } + + if (nconsumed < 0) { + return (int)nconsumed; + } + + rv = conn_call_deferred_consume(conn, stream, (size_t)nconsumed); + if (rv != 0) { + return rv; + } + + nghttp3_buf_free(buf, stream->mem); + nghttp3_ringbuf_pop_front(&stream->inq); + } + + return 0; +} + +int nghttp3_conn_shutdown_wt_session(nghttp3_conn *conn, + nghttp3_stream *wt_ctrl_stream, + uint64_t error_code) { + int rv; + + wt_ctrl_stream->wt.session->flags |= NGHTTP3_WT_SESSION_FLAG_CLOSED; + + rv = + nghttp3_conn_shutdown_all_wt_data_streams(conn, wt_ctrl_stream, error_code); + if (rv != 0) { + return rv; + } + + wt_ctrl_stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + + return nghttp3_conn_abort_stream(conn, wt_ctrl_stream, error_code); +} + +int nghttp3_conn_shutdown_all_wt_data_streams(nghttp3_conn *conn, + nghttp3_stream *wt_ctrl_stream, + uint64_t error_code) { + nghttp3_wt_session *wt_session = wt_ctrl_stream->wt.session; + nghttp3_stream *stream; + int rv; + + for (stream = wt_session->head; stream; stream = stream->wt.next) { + rv = nghttp3_conn_shutdown_wt_data_stream(conn, stream, error_code); + if (rv != 0) { + return rv; + } + } + + return 0; +} + +int nghttp3_conn_shutdown_wt_data_stream(nghttp3_conn *conn, + nghttp3_stream *stream, + uint64_t error_code) { + if (stream->node.id & 0x2) { + stream->rstate.state = NGHTTP3_WT_STREAM_STATE_IGN_REST; + } else { + stream->rstate.state = NGHTTP3_REQ_STREAM_STATE_IGN_REST; + } + + return nghttp3_conn_abort_stream(conn, stream, error_code); +} + +int64_t nghttp3_conn_get_stream_wt_session_id(const nghttp3_conn *conn, + int64_t stream_id) { + const nghttp3_stream *stream = nghttp3_conn_find_stream(conn, stream_id); + + if (!stream || !nghttp3_stream_wt_data(stream)) { + return -1; + } + + return stream->wt.session->session_id; +} + +int nghttp3_conn_read_wt_ctrl_stream(nghttp3_conn *conn, + const nghttp3_stream *stream, + const uint8_t *src, size_t srclen) { + const uint8_t *p, *end; + nghttp3_wt_session *wts = stream->wt.session; + nghttp3_wt_ctrl_read_state *rstate = &wts->rstate; + nghttp3_varint_read_state *rvint = &rstate->rvint; + nghttp3_ssize nread; + nghttp3_exfr_cpsl *cpsl = &rstate->cpsl; + size_t len; + size_t i; + int rv; + + if (srclen == 0) { + return 0; + } + + p = src; + end = src + srclen; + + for (; p != end;) { + switch (rstate->state) { + case NGHTTP3_WT_CTRL_STREAM_STATE_TYPE: + assert(end - p > 0); + nread = nghttp3_read_varint(rvint, p, end, /* fin = */ 0); + + assert(nread > 0); + + p += nread; + if (rvint->left) { + return 0; + } + + rstate->cpsl.hd.type = rvint->acc; + + nghttp3_varint_read_state_reset(rvint); + rstate->state = NGHTTP3_WT_CTRL_STREAM_STATE_LENGTH; + if (p == end) { + return 0; + } + /* Fall through */ + case NGHTTP3_WT_CTRL_STREAM_STATE_LENGTH: + assert(end - p > 0); + nread = nghttp3_read_varint(rvint, p, end, /* fin = */ 0); + assert(nread > 0); + + p += nread; + if (rvint->left) { + return 0; + } + + rstate->left = rvint->acc; + nghttp3_varint_read_state_reset(rvint); + + switch (rstate->cpsl.hd.type) { + case NGHTTP3_EXFR_CPSL_WT_CLOSE_SESSION: + if (rstate->left < sizeof(uint32_t) || + rstate->left > sizeof(uint32_t) + /* largest message size */ 1024) { + /* TODO Find better error code */ + return NGHTTP3_ERR_H3_MESSAGE_ERROR; + } + + rstate->field_left = sizeof(uint32_t); + rstate->state = + NGHTTP3_WT_CTRL_STREAM_STATE_WT_CLOSE_SESSION_ERROR_CODE; + + break; + default: + /* TODO Add rate limit after we implement all supported + capsules. */ + if (rstate->left == 0) { + nghttp3_wt_ctrl_read_state_reset(rstate); + break; + } + + rstate->state = NGHTTP3_WT_CTRL_STREAM_STATE_IGN; + } + + break; + case NGHTTP3_WT_CTRL_STREAM_STATE_WT_CLOSE_SESSION_ERROR_CODE: + len = nghttp3_min(rstate->field_left, (size_t)(end - p)); + + for (i = 0; i < len; ++i) { + cpsl->wt_close_session.error_code <<= 8; + cpsl->wt_close_session.error_code += *p++; + } + + rstate->left -= len; + rstate->field_left -= len; + if (rstate->field_left) { + break; + } + + wts->rx.error_code = cpsl->wt_close_session.error_code; + + if (rstate->left == 0) { + if (conn->callbacks.recv_wt_close_session) { + rv = conn->callbacks.recv_wt_close_session( + conn, wts->session_id, wts->rx.error_code, NULL, 0, conn->user_data, + stream->user_data); + if (rv != 0) { + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + } + + nghttp3_wt_ctrl_read_state_reset(rstate); + + return NGHTTP3_ERR_WT_SESSION_GONE; + } + + rstate->state = NGHTTP3_WT_CTRL_STREAM_STATE_WT_CLOSE_SESSION_ERROR_MSG; + + wts->rx.error_msg.base = + nghttp3_mem_malloc(conn->mem, (size_t)rstate->left); + if (!wts->rx.error_msg.base) { + return NGHTTP3_ERR_NOMEM; + } + + if (p == end) { + return 0; + } + + /* Fall through */ + case NGHTTP3_WT_CTRL_STREAM_STATE_WT_CLOSE_SESSION_ERROR_MSG: + len = (size_t)nghttp3_min(rstate->left, (uint64_t)(end - p)); + + memcpy(wts->rx.error_msg.base + wts->rx.error_msg.len, p, len); + wts->rx.error_msg.len += len; + + p += len; + rstate->left -= len; + + if (rstate->left) { + break; + } + + if (conn->callbacks.recv_wt_close_session) { + rv = conn->callbacks.recv_wt_close_session( + conn, wts->session_id, wts->rx.error_code, wts->rx.error_msg.base, + wts->rx.error_msg.len, conn->user_data, stream->user_data); + if (rv != 0) { + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + } + + nghttp3_wt_ctrl_read_state_reset(rstate); + + return NGHTTP3_ERR_WT_SESSION_GONE; + case NGHTTP3_WT_CTRL_STREAM_STATE_IGN: + len = (size_t)nghttp3_min(rstate->left, (uint64_t)(end - p)); + p += len; + rstate->left -= len; + + if (rstate->left) { + return 0; + } + + nghttp3_wt_ctrl_read_state_reset(rstate); + + break; + } + } + + return 0; } diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_conn.h b/deps/ngtcp2/nghttp3/lib/nghttp3_conn.h index 6841b1c343a3..28b96051236d 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_conn.h +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_conn.h @@ -85,6 +85,11 @@ struct nghttp3_conn { nghttp3_qpack_decoder qdec; nghttp3_qpack_encoder qenc; nghttp3_pq qpack_blocked_streams; + /* qpack_blocked_streams_cancelled is the number of streams in + qpack_blocked_streams for which QPACK Stream Cancellation has + been sent. A remote endpoint no longer counts them as blocked + streams. */ + size_t qpack_blocked_streams_cancelled; nghttp3_ratelim glitch_rlim; struct { nghttp3_pq spq; @@ -120,6 +125,9 @@ struct nghttp3_conn { streams that are currently open. This field is for server use only. */ size_t num_streams; + /* max_concurrent_streams is the maximum number of bidirectional + streams that the remote endpoint can create concurrently. */ + size_t max_concurrent_streams; } bidi; nghttp3_proto_settings settings; } remote; @@ -202,8 +210,8 @@ nghttp3_ssize nghttp3_conn_read_qpack_decoder(nghttp3_conn *conn, const uint8_t *src, size_t srclen); -int nghttp3_conn_on_data(nghttp3_conn *conn, nghttp3_stream *stream, - const uint8_t *data, size_t datalen); +nghttp3_ssize nghttp3_conn_on_data(nghttp3_conn *conn, nghttp3_stream *stream, + const uint8_t *data, size_t datalen); int nghttp3_conn_on_priority_update(nghttp3_conn *conn, const nghttp3_frame_priority_update *fr); @@ -211,11 +219,13 @@ int nghttp3_conn_on_priority_update(nghttp3_conn *conn, nghttp3_ssize nghttp3_conn_on_headers(nghttp3_conn *conn, nghttp3_stream *stream, const uint8_t *data, size_t datalen, - int fin); + int fin, nghttp3_tstamp ts); int nghttp3_conn_on_settings_entry_received(nghttp3_conn *conn, const nghttp3_frame_settings *fr); +int nghttp3_conn_on_settings_received(nghttp3_conn *conn); + int nghttp3_conn_qpack_blocked_streams_push(nghttp3_conn *conn, nghttp3_stream *stream); @@ -233,10 +243,47 @@ void nghttp3_conn_unschedule_stream(nghttp3_conn *conn, nghttp3_stream *stream); int nghttp3_conn_reject_stream(nghttp3_conn *conn, nghttp3_stream *stream); +int nghttp3_conn_abort_stream(nghttp3_conn *conn, nghttp3_stream *stream, + uint64_t error_code); + /* * nghttp3_conn_get_next_tx_stream returns next stream to send. It * returns NULL if there is no such stream. */ nghttp3_stream *nghttp3_conn_get_next_tx_stream(nghttp3_conn *conn); +int nghttp3_conn_open_wt_session(nghttp3_conn *conn, nghttp3_stream *stream); + +int nghttp3_conn_on_wt_stream(nghttp3_conn *conn, nghttp3_stream *stream, + int64_t session_id); + +nghttp3_ssize nghttp3_conn_read_wt_stream_uni(nghttp3_conn *conn, + nghttp3_stream *stream, + const uint8_t *src, size_t srclen, + int fin, nghttp3_tstamp ts); + +int nghttp3_conn_on_wt_session_confirmed(nghttp3_conn *conn, + nghttp3_stream *wt_ctrl_stream, + nghttp3_tstamp ts); + +int nghttp3_conn_process_blocked_wt_stream_data(nghttp3_conn *conn, + nghttp3_stream *stream, + nghttp3_tstamp ts); + +int nghttp3_conn_shutdown_wt_session(nghttp3_conn *conn, + nghttp3_stream *wt_ctrl_stream, + uint64_t error_code); + +int nghttp3_conn_shutdown_all_wt_data_streams(nghttp3_conn *conn, + nghttp3_stream *wt_ctrl_stream, + uint64_t error_code); + +int nghttp3_conn_shutdown_wt_data_stream(nghttp3_conn *conn, + nghttp3_stream *stream, + uint64_t error_code); + +int nghttp3_conn_read_wt_ctrl_stream(nghttp3_conn *conn, + const nghttp3_stream *stream, + const uint8_t *src, size_t srclen); + #endif /* !defined(NGHTTP3_CONN_H) */ diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_conv.c b/deps/ngtcp2/nghttp3/lib/nghttp3_conv.c index 031ac78d815f..a90e1d25b70e 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_conv.c +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_conv.c @@ -79,6 +79,12 @@ const uint8_t *nghttp3_get_varint(int64_t *dest, const uint8_t *p) { return p; } +const uint8_t *nghttp3_get_uint32be(uint32_t *dest, const uint8_t *p) { + memcpy(dest, p, sizeof(*dest)); + *dest = ntohl(*dest); + return p + sizeof(*dest); +} + uint8_t *nghttp3_put_uint64be(uint8_t *p, uint64_t n) { n = nghttp3_htonl64(n); return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n)); diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_conv.h b/deps/ngtcp2/nghttp3/lib/nghttp3_conv.h index bd1c518fa663..bd6f29a55db5 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_conv.h +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_conv.h @@ -92,6 +92,13 @@ # define ntohs(N) _byteswap_ushort(N) #endif /* defined(WIN32) */ +/* + * nghttp3_get_uint32be reads 4 bytes from |p| as 32 bits unsigned + * integer encoded as network byte order, and stores it in the buffer + * pointed by |dest| in host byte order. It returns |p| + 4. + */ +const uint8_t *nghttp3_get_uint32be(uint32_t *dest, const uint8_t *p); + /* * nghttp3_put_uint64be writes |n| in host byte order in |p| in * network byte order. It returns the one beyond of the last written diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_err.c b/deps/ngtcp2/nghttp3/lib/nghttp3_err.c index eff6ea6a63a2..335bebbd9460 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_err.c +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_err.c @@ -50,6 +50,8 @@ const char *nghttp3_strerror(int liberr) { return "ERR_CONN_CLOSING"; case NGHTTP3_ERR_STREAM_DATA_OVERFLOW: return "ERR_STREAM_DATA_OVERFLOW"; + case NGHTTP3_ERR_MAX_FIELD_SECTION_SIZE_EXCEEDED: + return "ERR_MAX_FIELD_SECTION_SIZE_EXCEEDED"; case NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED: return "ERR_QPACK_DECOMPRESSION_FAILED"; case NGHTTP3_ERR_QPACK_ENCODER_STREAM_ERROR: @@ -76,6 +78,10 @@ const char *nghttp3_strerror(int liberr) { return "ERR_H3_STREAM_CREATION_ERROR"; case NGHTTP3_ERR_H3_EXCESSIVE_LOAD: return "ERR_H3_EXCESSIVE_LOAD"; + case NGHTTP3_ERR_WT_SESSION_GONE: + return "ERR_WT_SESSION_GONE"; + case NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED: + return "ERR_WT_BUFFERED_STREAM_REJECTED"; case NGHTTP3_ERR_NOMEM: return "ERR_NOMEM"; case NGHTTP3_ERR_CALLBACK_FAILURE: @@ -122,10 +128,20 @@ uint64_t nghttp3_err_infer_quic_app_error_code(int liberr) { return NGHTTP3_H3_EXCESSIVE_LOAD; case NGHTTP3_ERR_MALFORMED_HTTP_HEADER: case NGHTTP3_ERR_MALFORMED_HTTP_MESSAGING: + case NGHTTP3_ERR_H3_MESSAGE_ERROR: return NGHTTP3_H3_MESSAGE_ERROR; + case NGHTTP3_ERR_WT_SESSION_GONE: + return NGHTTP3_WT_SESSION_GONE; + case NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED: + return NGHTTP3_WT_BUFFERED_STREAM_REJECTED; default: return NGHTTP3_H3_GENERAL_PROTOCOL_ERROR; } } int nghttp3_err_is_fatal(int liberr) { return liberr < NGHTTP3_ERR_FATAL; } + +int nghttp3_err_is_wt(int liberr) { + return liberr == NGHTTP3_ERR_WT_SESSION_GONE || + liberr == NGHTTP3_ERR_WT_BUFFERED_STREAM_REJECTED; +} diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_err.h b/deps/ngtcp2/nghttp3/lib/nghttp3_err.h index 6f8205cc17ce..8dbc234e0aec 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_err.h +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_err.h @@ -31,4 +31,10 @@ #include +/* + * nghttp3_err_wt returns nonzero if |liberr| is one of WebTransport + * errors. + */ +int nghttp3_err_is_wt(int liberr); + #endif /* !defined(NGHTTP3_ERR_H) */ diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_frame.c b/deps/ngtcp2/nghttp3/lib/nghttp3_frame.c index aea874fff4dd..57784f557655 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_frame.c +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_frame.c @@ -133,6 +133,44 @@ size_t nghttp3_frame_write_origin_len(uint64_t *ppayloadlen, payloadlen; } +uint8_t *nghttp3_frame_write_wt_stream(uint8_t *p, + const nghttp3_exfr_wt_stream *fr) { + p = nghttp3_put_uvarint(p, fr->type); + return nghttp3_put_uvarint(p, (uint64_t)fr->session_id); +} + +size_t nghttp3_frame_write_wt_stream_len(const nghttp3_exfr_wt_stream *fr) { + return nghttp3_put_uvarintlen(fr->type) + + nghttp3_put_uvarintlen((uint64_t)fr->session_id); +} + +uint8_t *nghttp3_frame_write_cpsl_wt_close_session( + uint8_t *p, const nghttp3_exfr_cpsl_wt_close_session *fr, + uint64_t payloadlen) { + p = nghttp3_frame_write_hd(p, NGHTTP3_FRAME_DATA, payloadlen); + p = nghttp3_frame_write_hd(p, fr->type, + sizeof(fr->error_code) + fr->error_msg.len); + p = nghttp3_put_uint32be(p, fr->error_code); + + if (fr->error_msg.len) { + p = nghttp3_cpymem(p, fr->error_msg.base, fr->error_msg.len); + } + + return p; +} + +size_t nghttp3_frame_write_cpsl_wt_close_session_len( + uint64_t *ppayloadlen, const nghttp3_exfr_cpsl_wt_close_session *fr) { + size_t cpsl_payloadlen = sizeof(fr->error_code) + fr->error_msg.len; + size_t payloadlen = nghttp3_put_uvarintlen(fr->type) + + nghttp3_put_uvarintlen(cpsl_payloadlen) + cpsl_payloadlen; + + *ppayloadlen = payloadlen; + + return nghttp3_put_uvarintlen(NGHTTP3_FRAME_DATA) + + nghttp3_put_uvarintlen(payloadlen) + payloadlen; +} + int nghttp3_nva_copy(nghttp3_nv **pnva, const nghttp3_nv *nva, size_t nvlen, const nghttp3_mem *mem) { size_t i; diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_frame.h b/deps/ngtcp2/nghttp3/lib/nghttp3_frame.h index fee30cb56d7d..44df99a81636 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_frame.h +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_frame.h @@ -46,6 +46,24 @@ #define NGHTTP3_FRAME_PRIORITY_UPDATE_PUSH_ID 0x0F0701U /* ORIGIN: https://datatracker.ietf.org/doc/html/rfc9412 */ #define NGHTTP3_FRAME_ORIGIN 0x0CU +/* WebTransport extended frame type */ +#define NGHTTP3_FRAME_EX_WT 0x4000000000000001ULL +/* WT_STREAM: + https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-14 */ +#define NGHTTP3_EXFR_WT_STREAM_BIDI 0x41U +#define NGHTTP3_EXFR_WT_STREAM_UNI 0x54U +#define NGHTTP3_EXFR_WT_STREAM_DATA 0x00U + +/* HTTP Capsule extended frame type */ +#define NGHTTP3_FRAME_EX_CPSL 0x4000000000000002ULL +#define NGHTTP3_EXFR_CPSL_WT_CLOSE_SESSION 0x2843U +#define NGHTTP3_EXFR_CPSL_WT_DRAIN_SESSION 0x78AEU +#define NGHTTP3_EXFR_CPSL_WT_MAX_STREAMS_BIDI 0x190B4D3FU +#define NGHTTP3_EXFR_CPSL_WT_MAX_STREAMS_UNI 0x190B4D40U +#define NGHTTP3_EXFR_CPSL_WT_STREAMS_BLOCKED_BIDI 0x190B4D43U +#define NGHTTP3_EXFR_CPSL_WT_STREAMS_BLOCKED_UNI 0x190B4D44U +#define NGHTTP3_EXFR_CPSL_WT_MAX_DATA 0x190B4D3DU +#define NGHTTP3_EXFR_CPSL_WT_DATA_BLOCKED 0x190B4D41U /* Frame types that are reserved for HTTP/2, and must not be used in HTTP/3. */ @@ -76,6 +94,16 @@ typedef struct nghttp3_frame_headers { #define NGHTTP3_SETTINGS_ID_QPACK_BLOCKED_STREAMS 0x07U #define NGHTTP3_SETTINGS_ID_ENABLE_CONNECT_PROTOCOL 0x08U #define NGHTTP3_SETTINGS_ID_H3_DATAGRAM 0x33U +/* https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-17 */ +#define NGHTTP3_SETTINGS_ID_WT_ENABLED 0X20B8933BU +/* https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-15 */ +#define NGHTTP3_SETTINGS_ID_WT_ENABLED_DRAFT15 0x2C7CF000U +/* https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-14 */ +#define NGHTTP3_SETTINGS_ID_WT_MAX_SESSIONS 0x14E9CD29U +/* https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-07 */ +#define NGHTTP3_SETTINGS_ID_WT_MAX_SESSIONS_DRAFT7 0xC671706AU +/* https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3-02 */ +#define NGHTTP3_SETTINGS_ID_ENABLE_WEBTRANSPORT_DRAFT2 0x2B603742U #define NGHTTP3_H2_SETTINGS_ID_ENABLE_PUSH 0x2U #define NGHTTP3_H2_SETTINGS_ID_MAX_CONCURRENT_STREAMS 0x3U @@ -132,6 +160,42 @@ typedef struct nghttp3_frame_origin { nghttp3_vec origin_list; } nghttp3_frame_origin; +typedef struct nghttp3_exfr_hd { + uint64_t type; +} nghttp3_exfr_hd; + +typedef struct nghttp3_exfr_wt_stream { + uint64_t type; + int64_t session_id; + nghttp3_data_reader dr; +} nghttp3_exfr_wt_stream; + +typedef union nghttp3_exfr_wt { + nghttp3_exfr_hd hd; + nghttp3_exfr_wt_stream wt_stream; +} nghttp3_exfr_wt; + +typedef struct nghttp3_frame_ex_wt { + uint64_t type; + nghttp3_exfr_wt fr; +} nghttp3_frame_ex_wt; + +typedef struct nghttp3_exfr_cpsl_wt_close_session { + uint64_t type; + nghttp3_vec error_msg; + uint32_t error_code; +} nghttp3_exfr_cpsl_wt_close_session; + +typedef union nghttp3_exfr_cpsl { + nghttp3_exfr_hd hd; + nghttp3_exfr_cpsl_wt_close_session wt_close_session; +} nghttp3_exfr_cpsl; + +typedef struct nghttp3_frame_ex_cpsl { + uint64_t type; + nghttp3_exfr_cpsl fr; +} nghttp3_frame_ex_cpsl; + typedef union nghttp3_frame { nghttp3_frame_hd hd; nghttp3_frame_data data; @@ -140,6 +204,8 @@ typedef union nghttp3_frame { nghttp3_frame_goaway goaway; nghttp3_frame_priority_update priority_update; nghttp3_frame_origin origin; + nghttp3_frame_ex_wt wt; + nghttp3_frame_ex_cpsl cpsl; } nghttp3_frame; /* @@ -233,6 +299,18 @@ uint8_t *nghttp3_frame_write_origin(uint8_t *dest, size_t nghttp3_frame_write_origin_len(uint64_t *ppayloadlen, const nghttp3_frame_origin *fr); +uint8_t *nghttp3_frame_write_wt_stream(uint8_t *dest, + const nghttp3_exfr_wt_stream *fr); + +size_t nghttp3_frame_write_wt_stream_len(const nghttp3_exfr_wt_stream *fr); + +uint8_t *nghttp3_frame_write_cpsl_wt_close_session( + uint8_t *dest, const nghttp3_exfr_cpsl_wt_close_session *fr, + uint64_t payloadlen); + +size_t nghttp3_frame_write_cpsl_wt_close_session_len( + uint64_t *ppayloadlen, const nghttp3_exfr_cpsl_wt_close_session *fr); + /* * nghttp3_nva_copy copies name/value pairs from |nva|, which contains * |nvlen| pairs, to |*nva_ptr|, which is dynamically allocated so diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_http.c b/deps/ngtcp2/nghttp3/lib/nghttp3_http.c index 4194a404b33f..e06ce6d27688 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_http.c +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_http.c @@ -39,6 +39,12 @@ #include "nghttp3_str.h" #include "sfparse/sfparse.h" +/* NGHTTP3_HTTP_MAX_PRIORITY_VALUELEN is the maximum length of + priority header field value that is parsed. A longer value is + treated as a value that cannot be parsed. This is the same limit + we impose against PRIORITY_UPDATE frame. */ +#define NGHTTP3_HTTP_MAX_PRIORITY_VALUELEN 8 + /* * memieq returns 1 if the data pointed by |a| of length |n| equals to * |b| of the same length in case-insensitive manner. The data @@ -239,7 +245,7 @@ static int check_scheme(const uint8_t *value, size_t len) { return 1; } -static const int8_t VALID_METHOD_CHARS[256] = { +static const int8_t VALID_TOKEN_CHARS[256] = { ['!'] = 1, ['#'] = 1, ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, ['*'] = 1, ['+'] = 1, ['-'] = 1, ['.'] = 1, ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, ['A'] = 1, @@ -253,13 +259,13 @@ static const int8_t VALID_METHOD_CHARS[256] = { ['v'] = 1, ['w'] = 1, ['x'] = 1, ['y'] = 1, ['z'] = 1, ['|'] = 1, ['~'] = 1, }; -static int check_method(const uint8_t *value, size_t len) { +static int check_token(const uint8_t *value, size_t len) { const uint8_t *last; if (len == 0) { return 0; } for (last = value + len; value != last; ++value) { - if (!VALID_METHOD_CHARS[*value]) { + if (!VALID_TOKEN_CHARS[*value]) { return 0; } } @@ -329,7 +335,7 @@ static int http_request_on_header(nghttp3_http_state *http, } break; case NGHTTP3_QPACK_TOKEN__METHOD: - if (!check_method(nv->value->base, nv->value->len) || + if (!check_token(nv->value->base, nv->value->len) || !check_pseudo_header(http, nv, NGHTTP3_HTTP_FLAG__METHOD)) { return NGHTTP3_ERR_MALFORMED_HTTP_HEADER; } @@ -379,11 +385,19 @@ static int http_request_on_header(nghttp3_http_state *http, } break; case NGHTTP3_QPACK_TOKEN__PROTOCOL: - if (!connect_protocol || - !nghttp3_check_header_value(nv->value->base, nv->value->len) || + /* https://datatracker.ietf.org/doc/html/rfc8441#section-4: + :protocol contains a single value from HTTP Upgrade Token + Registry, which is a token. */ + if (!connect_protocol || !check_token(nv->value->base, nv->value->len) || !check_pseudo_header(http, nv, NGHTTP3_HTTP_FLAG__PROTOCOL)) { return NGHTTP3_ERR_MALFORMED_HTTP_HEADER; } + + if (lstrieq("webtransport-h3", nv->value->base, nv->value->len) || + lstrieq("webtransport", nv->value->base, nv->value->len)) { + http->flags |= NGHTTP3_HTTP_FLAG_WEBTRANSPORT; + } + break; case NGHTTP3_QPACK_TOKEN_HOST: if (!check_authority(nv->value->base, nv->value->len)) { @@ -397,8 +411,11 @@ static int http_request_on_header(nghttp3_http_state *http, /* https://tools.ietf.org/html/rfc7230#section-4.1.2: A sender MUST NOT generate a trailer that contains a field necessary for message framing (e.g., Transfer-Encoding and Content-Length), - ... */ - if (trailers) { + ... + + Also do not emit content-length for CONNECT request. + */ + if (trailers || (http->flags & NGHTTP3_HTTP_FLAG_METH_CONNECT)) { return NGHTTP3_ERR_REMOVE_HTTP_HEADER; } if (http->content_length != -1) { @@ -436,8 +453,9 @@ static int http_request_on_header(nghttp3_http_state *http, pri = http->pri; - if (nghttp3_http_parse_priority(&pri, nv->value->base, nv->value->len) == - 0) { + if (nv->value->len <= NGHTTP3_HTTP_MAX_PRIORITY_VALUELEN && + nghttp3_http_parse_priority(&pri, nv->value->base, nv->value->len) == + 0) { http->pri = pri; http->flags |= NGHTTP3_HTTP_FLAG_PRIORITY; break; @@ -577,7 +595,6 @@ int nghttp3_http_on_request_headers(nghttp3_http_state *http) { (http->flags & NGHTTP3_HTTP_FLAG__AUTHORITY) == 0) { return NGHTTP3_ERR_MALFORMED_HTTP_HEADER; } - http->content_length = -1; } else { if ((http->flags & NGHTTP3_HTTP_FLAG_REQ_HEADERS) != NGHTTP3_HTTP_FLAG_REQ_HEADERS || @@ -585,13 +602,10 @@ int nghttp3_http_on_request_headers(nghttp3_http_state *http) { (NGHTTP3_HTTP_FLAG__AUTHORITY | NGHTTP3_HTTP_FLAG_HOST)) == 0) { return NGHTTP3_ERR_MALFORMED_HTTP_HEADER; } - if (http->flags & NGHTTP3_HTTP_FLAG__PROTOCOL) { - if ((http->flags & NGHTTP3_HTTP_FLAG_METH_CONNECT) == 0 || - (http->flags & NGHTTP3_HTTP_FLAG__AUTHORITY) == 0) { - return NGHTTP3_ERR_MALFORMED_HTTP_HEADER; - } - - http->content_length = -1; + if ((http->flags & NGHTTP3_HTTP_FLAG__PROTOCOL) && + ((http->flags & NGHTTP3_HTTP_FLAG_METH_CONNECT) == 0 || + (http->flags & NGHTTP3_HTTP_FLAG__AUTHORITY) == 0)) { + return NGHTTP3_ERR_MALFORMED_HTTP_HEADER; } if (!check_path_flags(http)) { return NGHTTP3_ERR_MALFORMED_HTTP_HEADER; @@ -619,8 +633,6 @@ int nghttp3_http_on_response_headers(nghttp3_http_state *http) { if (!expect_response_body(http)) { http->content_length = 0; - } else if (http->flags & NGHTTP3_HTTP_FLAG_METH_CONNECT) { - http->content_length = -1; } return 0; @@ -714,16 +726,19 @@ int nghttp3_check_header_name(const uint8_t *name, size_t len) { contains a character in [A-Z], otherwise 0. */ static int http_check_nonempty_header_name(const uint8_t *name, size_t len) { const uint8_t *last; - int rv; + int rv = 1; for (last = name + len; name != last; ++name) { - rv = VALID_HD_NAME_CHARS[*name]; - if (rv != 1) { - return rv; + switch (VALID_HD_NAME_CHARS[*name]) { + case -1: + return -1; + case 0: + rv = 0; + break; } } - return 1; + return rv; } static const int8_t VALID_HD_VALUE_CHARS[256] = { diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_http.h b/deps/ngtcp2/nghttp3/lib/nghttp3_http.h index 2bdf3110027c..ec32414c1d1d 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_http.h +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_http.h @@ -82,6 +82,8 @@ typedef struct nghttp3_http_state nghttp3_http_state; while parsing priority header field. */ #define NGHTTP3_HTTP_FLAG_BAD_PRIORITY 0x010000U +#define NGHTTP3_HTTP_FLAG_WEBTRANSPORT 0x020000U + /* * This function is called when HTTP header field |nv| received for * |http|. This function will validate |nv| against the current state diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_qpack.c b/deps/ngtcp2/nghttp3/lib/nghttp3_qpack.c index 2e3d063e1381..a0b5e98ae235 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_qpack.c +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_qpack.c @@ -783,6 +783,12 @@ static void qpack_map_remove(nghttp3_qpack_map *map, nghttp3_qpack_entry *ent) { } } +static void +qpack_context_set_max_concurrent_streams(nghttp3_qpack_context *ctx, + size_t max_concurrent_streams) { + ctx->max_concurrent_streams = max_concurrent_streams; +} + /* * qpack_context_can_reference returns nonzero if dynamic table entry * at |absidx| can be referenced. In other words, it is within @@ -847,6 +853,7 @@ static void encoder_qpack_map_find(nghttp3_qpack_encoder *encoder, static void qpack_context_init(nghttp3_qpack_context *ctx, size_t hard_max_dtable_capacity, size_t max_blocked_streams, + size_t max_concurrent_streams, const nghttp3_mem *mem) { nghttp3_ringbuf_init(&ctx->dtable, 0, sizeof(nghttp3_qpack_entry *), mem); @@ -856,6 +863,7 @@ static void qpack_context_init(nghttp3_qpack_context *ctx, ctx->hard_max_dtable_capacity = hard_max_dtable_capacity; ctx->max_dtable_capacity = 0; ctx->max_blocked_streams = max_blocked_streams; + ctx->max_concurrent_streams = max_concurrent_streams; ctx->next_absidx = 0; ctx->bad = 0; } @@ -899,7 +907,8 @@ nghttp3_ksl_search_def(max_cnt_greater, max_cnt_greater) void nghttp3_qpack_encoder_init(nghttp3_qpack_encoder *encoder, size_t hard_max_dtable_capacity, uint64_t seed, const nghttp3_mem *mem) { - qpack_context_init(&encoder->ctx, hard_max_dtable_capacity, 0, mem); + qpack_context_init(&encoder->ctx, hard_max_dtable_capacity, 0, + NGHTTP3_QPACK_MAX_QPACK_STREAMS, mem); nghttp3_map_init(&encoder->streams, seed, mem); @@ -915,6 +924,7 @@ void nghttp3_qpack_encoder_init(nghttp3_qpack_encoder *encoder, encoder->opcode = 0; encoder->min_dtable_update = SIZE_MAX; encoder->last_max_dtable_update = 0; + encoder->decoder_max_dtable_capacity = 0; encoder->uninterrupted_decoderlen = 0; encoder->indexing_strat = NGHTTP3_QPACK_INDEXING_STRAT_NONE; encoder->flags = NGHTTP3_QPACK_ENCODER_FLAG_NONE; @@ -940,10 +950,11 @@ void nghttp3_qpack_encoder_free(nghttp3_qpack_encoder *encoder) { void nghttp3_qpack_encoder_set_max_dtable_capacity( nghttp3_qpack_encoder *encoder, size_t max_dtable_capacity) { + encoder->decoder_max_dtable_capacity = max_dtable_capacity; max_dtable_capacity = nghttp3_min(max_dtable_capacity, encoder->ctx.hard_max_dtable_capacity); - if (encoder->ctx.max_dtable_capacity == max_dtable_capacity) { + if (encoder->last_max_dtable_update == max_dtable_capacity) { return; } @@ -966,6 +977,15 @@ void nghttp3_qpack_encoder_set_indexing_strat( encoder->indexing_strat = strat; } +void nghttp3_qpack_encoder_set_no_insert(nghttp3_qpack_encoder *encoder, + int no_insert) { + if (no_insert) { + encoder->flags |= NGHTTP3_QPACK_ENCODER_FLAG_NO_INSERT; + } else { + encoder->flags &= (uint8_t)~NGHTTP3_QPACK_ENCODER_FLAG_NO_INSERT; + } +} + uint64_t nghttp3_qpack_encoder_get_min_cnt(const nghttp3_qpack_encoder *encoder) { assert(!nghttp3_pq_empty(&encoder->min_cnts)); @@ -990,6 +1010,12 @@ void nghttp3_qpack_encoder_shrink_dtable(nghttp3_qpack_encoder *encoder) { min_cnt = nghttp3_qpack_encoder_get_min_cnt(encoder); } + /* An entry cannot be evicted until its insertion is acknowledged by + decoder. See RFC 9204, Section 2.1.1. */ + if (encoder->krcnt < encoder->ctx.next_absidx) { + min_cnt = nghttp3_min(min_cnt, encoder->krcnt + 1); + } + for (; encoder->ctx.dtable_size > encoder->ctx.max_dtable_capacity;) { len = nghttp3_ringbuf_len(dtable); ent = *(nghttp3_qpack_entry **)nghttp3_ringbuf_get(dtable, len - 1); @@ -1180,8 +1206,11 @@ int nghttp3_qpack_encoder_encode(nghttp3_qpack_encoder *encoder, } } - nghttp3_qpack_encoder_write_field_section_prefix(encoder, pbuf, max_cnt, - base); + rv = nghttp3_qpack_encoder_write_field_section_prefix(encoder, pbuf, max_cnt, + base); + if (rv != 0) { + goto fail; + } encoder->uninterrupted_decoderlen = 0; @@ -1383,6 +1412,10 @@ static int qpack_encoder_can_index(nghttp3_qpack_encoder *encoder, size_t need, nghttp3_qpack_entry *min_ent, *last_ent; nghttp3_ringbuf *dtable = &encoder->ctx.dtable; + if (encoder->flags & NGHTTP3_QPACK_ENCODER_FLAG_NO_INSERT) { + return 0; + } + if (encoder->ctx.max_dtable_capacity > encoder->ctx.dtable_size) { avail = encoder->ctx.max_dtable_capacity - encoder->ctx.dtable_size; if (need <= avail) { @@ -1395,6 +1428,12 @@ static int qpack_encoder_can_index(nghttp3_qpack_encoder *encoder, size_t need, min_cnt = nghttp3_min(min_cnt, gmin_cnt); } + /* An entry cannot be evicted until its insertion is acknowledged by + decoder. See RFC 9204, Section 2.1.1. */ + if (encoder->krcnt < encoder->ctx.next_absidx) { + min_cnt = nghttp3_min(min_cnt, encoder->krcnt + 1); + } + if (min_cnt == UINT64_MAX) { return encoder->ctx.max_dtable_capacity >= need; } @@ -1482,7 +1521,7 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, sres = nghttp3_qpack_lookup_stable(nv, token, indexing_mode); if (sres.index != -1 && sres.name_value_match) { return nghttp3_qpack_encoder_write_static_indexed(encoder, rbuf, - (size_t)sres.index); + (uint64_t)sres.index); } } @@ -1507,7 +1546,8 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, } } - if (nghttp3_map_size(&encoder->streams) < NGHTTP3_QPACK_MAX_QPACK_STREAMS) { + if (nghttp3_map_size(&encoder->streams) < + encoder->ctx.max_concurrent_streams) { dres = nghttp3_qpack_encoder_lookup_dtable( encoder, nv, token, hash, indexing_mode, encoder->krcnt, allow_blocking); just_index = @@ -1516,39 +1556,39 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, if (dres.index != -1 && dres.name_value_match) { if (allow_blocking && - qpack_context_check_draining(&encoder->ctx, (size_t)dres.index) && - qpack_encoder_can_index_duplicate(encoder, (size_t)dres.index, + qpack_context_check_draining(&encoder->ctx, (uint64_t)dres.index) && + qpack_encoder_can_index_duplicate(encoder, (uint64_t)dres.index, *pmin_cnt)) { rv = nghttp3_qpack_encoder_write_duplicate_insert(encoder, ebuf, - (size_t)dres.index); + (uint64_t)dres.index); if (rv != 0) { return rv; } - rv = - nghttp3_qpack_encoder_dtable_duplicate_add(encoder, (size_t)dres.index); + rv = nghttp3_qpack_encoder_dtable_duplicate_add(encoder, + (uint64_t)dres.index); if (rv != 0) { return rv; } new_ent = nghttp3_qpack_context_dtable_top(&encoder->ctx); - dres.index = (nghttp3_ssize)new_ent->absidx; + dres.index = (int64_t)new_ent->absidx; } *pmax_cnt = nghttp3_max(*pmax_cnt, (uint64_t)(dres.index + 1)); *pmin_cnt = nghttp3_min(*pmin_cnt, (uint64_t)(dres.index + 1)); return nghttp3_qpack_encoder_write_dynamic_indexed( - encoder, rbuf, (size_t)dres.index, base); + encoder, rbuf, (uint64_t)dres.index, base); } if (sres.index != -1) { if (just_index && qpack_encoder_can_index_nv(encoder, nv, *pmin_cnt)) { rv = nghttp3_qpack_encoder_write_static_insert(encoder, ebuf, - (size_t)sres.index, nv); + (uint64_t)sres.index, nv); if (rv != 0) { return rv; } - rv = nghttp3_qpack_encoder_dtable_static_add(encoder, (size_t)sres.index, - nv, hash); + rv = nghttp3_qpack_encoder_dtable_static_add( + encoder, (uint64_t)sres.index, nv, hash); if (rv != 0) { return rv; } @@ -1563,7 +1603,7 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, } return nghttp3_qpack_encoder_write_static_indexed_name( - encoder, rbuf, (size_t)sres.index, nv); + encoder, rbuf, (uint64_t)sres.index, nv, indexing_mode); } if (dres.index != -1) { @@ -1573,7 +1613,7 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, allow_blocking ? *pmin_cnt : nghttp3_min((uint64_t)dres.index + 1, *pmin_cnt))) { rv = nghttp3_qpack_encoder_write_dynamic_insert(encoder, ebuf, - (size_t)dres.index, nv); + (uint64_t)dres.index, nv); if (rv != 0) { return rv; } @@ -1582,8 +1622,8 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, *pmin_cnt = nghttp3_min(*pmin_cnt, (uint64_t)dres.index + 1); } - rv = nghttp3_qpack_encoder_dtable_dynamic_add(encoder, (size_t)dres.index, - nv, hash); + rv = nghttp3_qpack_encoder_dtable_dynamic_add( + encoder, (uint64_t)dres.index, nv, hash); if (rv != 0) { return rv; } @@ -1602,7 +1642,7 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, *pmin_cnt = nghttp3_min(*pmin_cnt, (uint64_t)(dres.index + 1)); return nghttp3_qpack_encoder_write_dynamic_indexed_name( - encoder, rbuf, (size_t)dres.index, base, nv); + encoder, rbuf, (uint64_t)dres.index, base, nv, indexing_mode); } if (just_index && qpack_encoder_can_index_nv(encoder, nv, *pmin_cnt)) { @@ -1624,14 +1664,14 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, } } - return nghttp3_qpack_encoder_write_literal(encoder, rbuf, nv); + return nghttp3_qpack_encoder_write_literal(encoder, rbuf, nv, indexing_mode); } nghttp3_qpack_lookup_result nghttp3_qpack_lookup_stable(const nghttp3_nv *nv, int32_t token, nghttp3_qpack_indexing_mode indexing_mode) { nghttp3_qpack_lookup_result res = { - .index = (nghttp3_ssize)token_stable[token].absidx, + .index = (int64_t)token_stable[token].absidx, .pb_index = -1, }; nghttp3_qpack_static_entry *ent; @@ -1651,7 +1691,7 @@ nghttp3_qpack_lookup_stable(const nghttp3_nv *nv, int32_t token, hdr = &stable[ent->absidx]; if (hdr->value.len == nv->valuelen && memeq(hdr->value.base, nv->value, nv->valuelen)) { - res.index = (nghttp3_ssize)ent->absidx; + res.index = (int64_t)ent->absidx; res.name_value_match = 1; return res; } @@ -1674,11 +1714,11 @@ nghttp3_qpack_lookup_result nghttp3_qpack_encoder_lookup_dtable( hash, krcnt, allow_blocking, indexing_mode == NGHTTP3_QPACK_INDEXING_MODE_NEVER); if (match) { - res.index = (nghttp3_ssize)match->absidx; + res.index = (int64_t)match->absidx; res.name_value_match = exact_match; } if (pb_match) { - res.pb_index = (nghttp3_ssize)pb_match->absidx; + res.pb_index = (int64_t)pb_match->absidx; } return res; @@ -1897,34 +1937,34 @@ qpack_encoder_write_indexed_name(const nghttp3_qpack_encoder *encoder, int nghttp3_qpack_encoder_write_static_indexed_name( const nghttp3_qpack_encoder *encoder, nghttp3_buf *rbuf, uint64_t absidx, - const nghttp3_nv *nv) { - uint8_t fb = - (uint8_t)(0x50U | - ((nv->flags & NGHTTP3_NV_FLAG_NEVER_INDEX) ? 0x20U : 0x00U)); + const nghttp3_nv *nv, nghttp3_qpack_indexing_mode indexing_mode) { + int never = indexing_mode == NGHTTP3_QPACK_INDEXING_MODE_NEVER; + uint8_t fb = (uint8_t)(0x50U | (never ? 0x20U : 0x00U)); DEBUGF("qpack::encode: Literal Field Line With Name Reference (static) " "absidx=%" PRIu64 " never=%d\n", - absidx, (nv->flags & NGHTTP3_NV_FLAG_NEVER_INDEX) != 0); + absidx, never); return qpack_encoder_write_indexed_name(encoder, rbuf, fb, absidx, 4, nv); } int nghttp3_qpack_encoder_write_dynamic_indexed_name( const nghttp3_qpack_encoder *encoder, nghttp3_buf *rbuf, uint64_t absidx, - uint64_t base, const nghttp3_nv *nv) { + uint64_t base, const nghttp3_nv *nv, + nghttp3_qpack_indexing_mode indexing_mode) { + int never = indexing_mode == NGHTTP3_QPACK_INDEXING_MODE_NEVER; uint8_t fb; DEBUGF("qpack::encode: Literal Field Line With Name Reference (dynamic) " "absidx=%" PRIu64 " base=%" PRIu64 " never=%d\n", - absidx, base, (nv->flags & NGHTTP3_NV_FLAG_NEVER_INDEX) != 0); + absidx, base, never); if (absidx < base) { - fb = (uint8_t)(0x40U | - ((nv->flags & NGHTTP3_NV_FLAG_NEVER_INDEX) ? 0x20U : 0x00U)); + fb = (uint8_t)(0x40U | (never ? 0x20U : 0x00U)); return qpack_encoder_write_indexed_name(encoder, rbuf, fb, base - absidx - 1, 4, nv); } - fb = (nv->flags & NGHTTP3_NV_FLAG_NEVER_INDEX) ? 0x08U : 0x0U; + fb = never ? 0x08U : 0x0U; return qpack_encoder_write_indexed_name(encoder, rbuf, fb, absidx - base, 3, nv); } @@ -2005,14 +2045,14 @@ static int qpack_encoder_write_literal(const nghttp3_qpack_encoder *encoder, return 0; } -int nghttp3_qpack_encoder_write_literal(const nghttp3_qpack_encoder *encoder, - nghttp3_buf *rbuf, - const nghttp3_nv *nv) { - uint8_t fb = - (uint8_t)(0x20U | - ((nv->flags & NGHTTP3_NV_FLAG_NEVER_INDEX) ? 0x10U : 0x0U)); +int nghttp3_qpack_encoder_write_literal( + const nghttp3_qpack_encoder *encoder, nghttp3_buf *rbuf, const nghttp3_nv *nv, + nghttp3_qpack_indexing_mode indexing_mode) { + int never = indexing_mode == NGHTTP3_QPACK_INDEXING_MODE_NEVER; + uint8_t fb = (uint8_t)(0x20U | (never ? 0x10U : 0x0U)); - DEBUGF("qpack::encode: Literal Field Line With Literal Name\n"); + DEBUGF("qpack::encode: Literal Field Line With Literal Name never=%d\n", + never); return qpack_encoder_write_literal(encoder, rbuf, fb, 3, nv); } @@ -2416,6 +2456,12 @@ nghttp3_qpack_encoder_get_num_blocked_streams(nghttp3_qpack_encoder *encoder) { return nghttp3_qpack_encoder_get_num_blocked_streams2(encoder); } +void nghttp3_qpack_encoder_set_max_concurrent_streams( + nghttp3_qpack_encoder *encoder, size_t max_concurrent_streams) { + qpack_context_set_max_concurrent_streams(&encoder->ctx, + max_concurrent_streams); +} + size_t nghttp3_qpack_encoder_get_num_blocked_streams2( const nghttp3_qpack_encoder *encoder) { return nghttp3_ksl_len(&encoder->blocked_streams); @@ -2425,7 +2471,7 @@ int nghttp3_qpack_encoder_write_field_section_prefix( const nghttp3_qpack_encoder *encoder, nghttp3_buf *pbuf, uint64_t ricnt, uint64_t base) { size_t max_ents = - encoder->ctx.hard_max_dtable_capacity / NGHTTP3_QPACK_ENTRY_OVERHEAD; + encoder->decoder_max_dtable_capacity / NGHTTP3_QPACK_ENTRY_OVERHEAD; uint64_t encricnt = ricnt == 0 ? 0 : (ricnt % (2 * max_ents)) + 1; int sign = base < ricnt; uint64_t delta_base = sign ? ricnt - base - 1 : base - ricnt; @@ -2704,12 +2750,11 @@ void nghttp3_qpack_decoder_init(nghttp3_qpack_decoder *decoder, size_t max_blocked_streams, const nghttp3_mem *mem) { qpack_context_init(&decoder->ctx, hard_max_dtable_capacity, - max_blocked_streams, mem); + max_blocked_streams, 0, mem); decoder->state = NGHTTP3_QPACK_ES_STATE_OPCODE; decoder->opcode = 0; decoder->written_icnt = 0; - decoder->max_concurrent_streams = 0; decoder->uninterrupted_encoderlen = 0; nghttp3_qpack_read_state_reset(&decoder->rstate); @@ -3303,8 +3348,8 @@ int nghttp3_qpack_decoder_dtable_literal_add(nghttp3_qpack_decoder *decoder) { void nghttp3_qpack_decoder_set_max_concurrent_streams( nghttp3_qpack_decoder *decoder, size_t max_concurrent_streams) { - decoder->max_concurrent_streams = - nghttp3_max(decoder->max_concurrent_streams, max_concurrent_streams); + qpack_context_set_max_concurrent_streams(&decoder->ctx, + max_concurrent_streams); } void nghttp3_qpack_stream_context_init(nghttp3_qpack_stream_context *sctx, @@ -3327,6 +3372,7 @@ void nghttp3_qpack_stream_context_free(nghttp3_qpack_stream_context *sctx) { } void nghttp3_qpack_stream_context_reset(nghttp3_qpack_stream_context *sctx) { + nghttp3_qpack_stream_context_free(sctx); nghttp3_qpack_stream_context_init(sctx, sctx->stream_id, sctx->mem); } @@ -3345,9 +3391,9 @@ nghttp3_qpack_decoder_read_request(nghttp3_qpack_decoder *decoder, nghttp3_qpack_stream_context *sctx, nghttp3_qpack_nv *nv, uint8_t *pflags, const uint8_t *src, size_t srclen, int fin) { - const uint8_t *p = src, *end = src ? src + srclen : src; + const uint8_t *p, *end; int rv; - int busy = 0; + int busy; nghttp3_ssize nread; int rfin; const nghttp3_mem *mem = decoder->ctx.mem; @@ -3357,8 +3403,20 @@ nghttp3_qpack_decoder_read_request(nghttp3_qpack_decoder *decoder, return NGHTTP3_ERR_QPACK_FATAL; } + if (srclen == 0) { + src = (const uint8_t *)""; + } + + p = src; + end = src + srclen; + *pflags = NGHTTP3_QPACK_DECODE_FLAG_NONE; + /* A field section might end with its prefix. In that case, we have + no input to read when decoding is resumed, but we still need to + leave NGHTTP3_QPACK_RS_STATE_BLOCKED to complete it. */ + busy = sctx->state == NGHTTP3_QPACK_RS_STATE_BLOCKED; + for (; p != end || busy;) { busy = 0; switch (sctx->state) { @@ -3794,7 +3852,7 @@ nghttp3_qpack_decoder_read_request(nghttp3_qpack_decoder *decoder, } static int qpack_decoder_dbuf_overflow(const nghttp3_qpack_decoder *decoder) { - size_t limit = nghttp3_max(decoder->max_concurrent_streams, 100); + size_t limit = nghttp3_max(decoder->ctx.max_concurrent_streams, 100); /* 10 = nghttp3_qpack_put_varint_len((1ULL << 62) - 1, 2)) */ return nghttp3_buf_len(&decoder->dbuf) > limit * 2 * 10; } diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_qpack.h b/deps/ngtcp2/nghttp3/lib/nghttp3_qpack.h index 934cae77c1bf..0adb3b365f5c 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_qpack.h +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_qpack.h @@ -56,6 +56,12 @@ that an encoder accepts without completely encoding a single field section. */ #define NGHTTP3_QPACK_MAX_DECODERLEN (4 * 1024) +/* NGHTTP3_QPACK_MAX_UNACKED_DECODERLEN is the maximum decoder stream + length that is kept in the outgoing queue of QPACK decoder stream + without being acknowledged by a remote endpoint. After this limit, + the pending decoder stream data stays in a decoder, which has its + own limit, until a remote endpoint acknowledges the queued data. */ +#define NGHTTP3_QPACK_MAX_UNACKED_DECODERLEN (16 * 1024) /* nghttp3_qpack_indexing_mode is a indexing strategy. */ typedef enum nghttp3_qpack_indexing_mode { @@ -171,6 +177,9 @@ typedef struct nghttp3_qpack_context { /* max_blocked_streams is the maximum number of stream which can be blocked. */ size_t max_blocked_streams; + /* max_concurrent_streams is the number of concurrent request + streams that are encoded/decoded concurrently. */ + size_t max_concurrent_streams; /* next_absidx is the next absolute index for nghttp3_qpack_entry. It is equivalent to insert count. */ uint64_t next_absidx; @@ -227,6 +236,10 @@ typedef enum nghttp3_qpack_decoder_stream_opcode { /* NGHTTP3_QPACK_ENCODER_FLAG_PENDING_SET_DTABLE_CAP indicates that Set Dynamic Table Capacity is required. */ #define NGHTTP3_QPACK_ENCODER_FLAG_PENDING_SET_DTABLE_CAP 0x01U +/* NGHTTP3_QPACK_ENCODER_FLAG_NO_INSERT indicates that no new entry is + inserted into the dynamic table for the moment, so that no more + insert instructions are produced for the encoder stream. */ +#define NGHTTP3_QPACK_ENCODER_FLAG_NO_INSERT 0x02U struct nghttp3_qpack_encoder { nghttp3_qpack_context ctx; @@ -258,6 +271,13 @@ struct nghttp3_qpack_encoder { /* last_max_dtable_update is the dynamic table size last requested. */ size_t last_max_dtable_update; + /* decoder_max_dtable_capacity is the maximum dynamic table capacity + that the decoder advertised (e.g., + SETTINGS_QPACK_MAX_TABLE_CAPACITY). Required Insert Count is + encoded with MaxEntries derived from this value. Unlike + ctx.hard_max_dtable_capacity, this is not the capacity that this + encoder is willing to use. */ + size_t decoder_max_dtable_capacity; /* uninterrupted_decoderlen is the number of bytes read from decoder stream without encoding a single field section. */ size_t uninterrupted_decoderlen; @@ -285,6 +305,17 @@ void nghttp3_qpack_encoder_init(nghttp3_qpack_encoder *encoder, */ void nghttp3_qpack_encoder_free(nghttp3_qpack_encoder *encoder); +/* + * nghttp3_qpack_encoder_set_no_insert tells |encoder| whether it is + * allowed to insert new entries into the dynamic table. If + * |no_insert| is nonzero, |encoder| stops inserting new entries, and + * thus stops producing insert instructions for the encoder stream, + * until this function is called with zero |no_insert|. The existing + * entries can still be referenced. + */ +void nghttp3_qpack_encoder_set_no_insert(nghttp3_qpack_encoder *encoder, + int no_insert); + /* * nghttp3_qpack_encoder_encode_nv encodes |nv|. It writes request * stream into |rbuf| and writes encoder stream into |ebuf|. |nv| is @@ -307,13 +338,13 @@ int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder, /* nghttp3_qpack_lookup_result stores a result of table lookup. */ typedef struct nghttp3_qpack_lookup_result { /* index is an index of matched entry. -1 if no match is made. */ - nghttp3_ssize index; + int64_t index; /* name_value_match is nonzero if both name and value are matched. */ int name_value_match; /* pb_index is the absolute index of matched post-based dynamic table entry. -1 if no such entry exists. */ - nghttp3_ssize pb_index; + int64_t pb_index; } nghttp3_qpack_lookup_result; /* @@ -385,7 +416,8 @@ int nghttp3_qpack_encoder_write_dynamic_indexed( * nghttp3_qpack_encoder_write_static_indexed writes Literal Header * Field With Name Reference to |rbuf|. |absidx| is an absolute index * into static table to reference a name. |nv| is a header field to - * encode. + * encode. If |indexing_mode| is NGHTTP3_QPACK_INDEXING_MODE_NEVER, N + * bit is set so that intermediaries never index this header field. * * This function returns 0 if it succeeds, or one of the following * negative error codes: @@ -395,13 +427,15 @@ int nghttp3_qpack_encoder_write_dynamic_indexed( */ int nghttp3_qpack_encoder_write_static_indexed_name( const nghttp3_qpack_encoder *encoder, nghttp3_buf *rbuf, uint64_t absidx, - const nghttp3_nv *nv); + const nghttp3_nv *nv, nghttp3_qpack_indexing_mode indexing_mode); /* * nghttp3_qpack_encoder_write_dynamic_indexed writes Literal Header * Field With Name Reference to |rbuf|. |absidx| is an absolute index * into dynamic table to reference a name. |base| is a base. |nv| is - * a header field to encode. + * a header field to encode. If |indexing_mode| is + * NGHTTP3_QPACK_INDEXING_MODE_NEVER, N bit is set so that + * intermediaries never index this header field. * * This function returns 0 if it succeeds, or one of the following * negative error codes: @@ -411,11 +445,14 @@ int nghttp3_qpack_encoder_write_static_indexed_name( */ int nghttp3_qpack_encoder_write_dynamic_indexed_name( const nghttp3_qpack_encoder *encoder, nghttp3_buf *rbuf, uint64_t absidx, - uint64_t base, const nghttp3_nv *nv); + uint64_t base, const nghttp3_nv *nv, + nghttp3_qpack_indexing_mode indexing_mode); /* * nghttp3_qpack_encoder_write_literal writes Literal Header Field - * With Literal Name to |rbuf|. |nv| is a header field to encode. + * With Literal Name to |rbuf|. |nv| is a header field to encode. If + * |indexing_mode| is NGHTTP3_QPACK_INDEXING_MODE_NEVER, N bit is set + * so that intermediaries never index this header field. * * This function returns 0 if it succeeds, or one of the following * negative error codes: @@ -423,9 +460,9 @@ int nghttp3_qpack_encoder_write_dynamic_indexed_name( * NGHTTP3_ERR_NOMEM * Out of memory. */ -int nghttp3_qpack_encoder_write_literal(const nghttp3_qpack_encoder *encoder, - nghttp3_buf *rbuf, - const nghttp3_nv *nv); +int nghttp3_qpack_encoder_write_literal( + const nghttp3_qpack_encoder *encoder, nghttp3_buf *rbuf, const nghttp3_nv *nv, + nghttp3_qpack_indexing_mode indexing_mode); /* * nghttp3_qpack_encoder_write_static_insert writes Insert With Name @@ -780,11 +817,6 @@ struct nghttp3_qpack_decoder { nghttp3_buf dbuf; /* written_icnt is Insert Count written to decoder stream so far. */ uint64_t written_icnt; - /* max_concurrent_streams is the number of concurrent streams that a - remote endpoint can open, including both bidirectional and - unidirectional streams which potentially receives QPACK encoded - HEADER frame. */ - size_t max_concurrent_streams; /* uninterrupted_encoderlen is the number of bytes read from encoder stream without completing a single field section. */ size_t uninterrupted_encoderlen; diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_stream.c b/deps/ngtcp2/nghttp3/lib/nghttp3_stream.c index 09298006f4f6..65cb46c832b4 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_stream.c +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_stream.c @@ -36,6 +36,7 @@ #include "nghttp3_http.h" #include "nghttp3_vec.h" #include "nghttp3_unreachable.h" +#include "nghttp3_wt.h" /* NGHTTP3_STREAM_MAX_COPY_THRES is the maximum size of buffer which makes a copy to outq. */ @@ -62,6 +63,7 @@ int nghttp3_stream_new(nghttp3_stream **pstream, int64_t stream_id, .stream_objalloc = stream_objalloc, .qpack_blocked_pe.index = NGHTTP3_PQ_BAD_INDEX, .mem = mem, + .type = NGHTTP3_STREAM_TYPE_UNKNOWN, .rx = { .http.status_code = -1, @@ -167,6 +169,10 @@ void nghttp3_stream_del(nghttp3_stream *stream) { delete_frq(&stream->frq, stream->mem); nghttp3_tnode_free(&stream->node); + if (nghttp3_stream_wt_ctrl(stream)) { + nghttp3_wt_session_del(stream->wt.session, stream->mem); + } + nghttp3_objalloc_stream_release(stream->stream_objalloc, stream); } @@ -294,6 +300,47 @@ int nghttp3_stream_fill_outq(nghttp3_stream *stream) { return rv; } + break; + case NGHTTP3_FRAME_EX_WT: + switch (fr->wt.fr.hd.type) { + case NGHTTP3_EXFR_WT_STREAM_BIDI: + case NGHTTP3_EXFR_WT_STREAM_UNI: + rv = nghttp3_stream_write_wt_stream(stream, &fr->wt.fr.wt_stream); + if (rv != 0) { + return rv; + } + + fr->wt.fr.wt_stream.type = NGHTTP3_EXFR_WT_STREAM_DATA; + + /* fall through */ + case NGHTTP3_EXFR_WT_STREAM_DATA: + rv = nghttp3_stream_write_wt_stream_data(stream, &data_eof, + &fr->wt.fr.wt_stream); + if (rv != 0) { + return rv; + } + + if ((stream->flags & NGHTTP3_STREAM_FLAG_READ_DATA_BLOCKED) || + !data_eof) { + return 0; + } + + break; + } + + break; + case NGHTTP3_FRAME_EX_CPSL: + switch (fr->cpsl.fr.hd.type) { + case NGHTTP3_EXFR_CPSL_WT_CLOSE_SESSION: + rv = nghttp3_stream_write_cpsl_wt_close_session( + stream, &fr->cpsl.fr.wt_close_session); + if (rv != 0) { + return rv; + } + + break; + } + break; default: /* TODO Not implemented */ @@ -372,6 +419,36 @@ int nghttp3_stream_write_settings(nghttp3_stream *stream, ++fr.niv; } + if (local_settings->wt_enabled) { + /* For client, only draft version sends SETTINGS_WT_ENABLED. */ + ents[fr.niv++] = (nghttp3_settings_entry){ + .id = NGHTTP3_SETTINGS_ID_WT_ENABLED, + .value = 1, + }; + + /* compat for pre draft-17 */ + ents[fr.niv++] = (nghttp3_settings_entry){ + .id = NGHTTP3_SETTINGS_ID_WT_ENABLED_DRAFT15, + .value = 1, + }; + + ents[fr.niv++] = (nghttp3_settings_entry){ + .id = NGHTTP3_SETTINGS_ID_WT_MAX_SESSIONS, + .value = 1, + }; + + ents[fr.niv++] = (nghttp3_settings_entry){ + .id = NGHTTP3_SETTINGS_ID_WT_MAX_SESSIONS_DRAFT7, + .value = 1, + }; + + /* compat for ancient draft */ + ents[fr.niv++] = (nghttp3_settings_entry){ + .id = NGHTTP3_SETTINGS_ID_ENABLE_WEBTRANSPORT_DRAFT2, + .value = 1, + }; + } + len = nghttp3_frame_write_settings_len(&payloadlen, &fr); rv = nghttp3_stream_ensure_chunk(stream, len); @@ -478,6 +555,150 @@ int nghttp3_stream_write_origin(nghttp3_stream *stream, return nghttp3_stream_outq_add(stream, &tbuf); } +int nghttp3_stream_write_wt_stream(nghttp3_stream *stream, + const nghttp3_exfr_wt_stream *fr) { + size_t len; + int rv; + nghttp3_buf *chunk; + nghttp3_typed_buf tbuf; + + len = nghttp3_frame_write_wt_stream_len(fr); + + rv = nghttp3_stream_ensure_chunk(stream, len); + if (rv != 0) { + return rv; + } + + chunk = nghttp3_stream_get_chunk(stream); + nghttp3_typed_buf_shared_init(&tbuf, chunk); + + chunk->last = nghttp3_frame_write_wt_stream(chunk->last, fr); + + tbuf.buf.last = chunk->last; + + return nghttp3_stream_outq_add(stream, &tbuf); +} + +int nghttp3_stream_write_wt_stream_data(nghttp3_stream *stream, int *peof, + const nghttp3_exfr_wt_stream *fr) { + int rv; + nghttp3_typed_buf tbuf; + nghttp3_buf buf; + nghttp3_read_data_callback read_data = fr->dr.read_data; + nghttp3_conn *conn = stream->conn; + uint64_t datalen; + uint32_t flags = 0; + nghttp3_vec vec[8]; + nghttp3_vec *v; + nghttp3_ssize sveccnt; + size_t i; + + assert(!(stream->flags & NGHTTP3_STREAM_FLAG_READ_DATA_BLOCKED)); + assert(read_data); + assert(conn); + + *peof = 0; + + sveccnt = read_data(conn, stream->node.id, vec, nghttp3_arraylen(vec), &flags, + conn->user_data, stream->user_data); + if (sveccnt < 0) { + if (sveccnt == NGHTTP3_ERR_WOULDBLOCK) { + stream->flags |= NGHTTP3_STREAM_FLAG_READ_DATA_BLOCKED; + return 0; + } + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + + rv = nghttp3_vec_len_uvarint(&datalen, vec, (size_t)sveccnt); + if (rv != 0) { + return NGHTTP3_ERR_STREAM_DATA_OVERFLOW; + } + + assert(datalen || flags & NGHTTP3_DATA_FLAG_EOF); + + if (flags & NGHTTP3_DATA_FLAG_EOF) { + *peof = 1; + + stream->flags |= NGHTTP3_STREAM_FLAG_WRITE_END_STREAM; + if (datalen == 0) { + if (nghttp3_stream_outq_write_done(stream)) { + /* If this is the last data and its is 0 length, we don't need + send data. We rely on the non-emptiness of outq to + schedule stream, so add empty tbuf to outq to just send + fin. */ + nghttp3_buf_init(&buf); + nghttp3_typed_buf_init(&tbuf, &buf, NGHTTP3_BUF_TYPE_PRIVATE); + return nghttp3_stream_outq_add(stream, &tbuf); + } + + /* We are going to send data, but nothing to send this time. */ + + return 0; + } + } + + assert(datalen); + + for (i = 0; i < (size_t)sveccnt; ++i) { + v = &vec[i]; + if (v->len == 0) { + continue; + } + nghttp3_buf_wrap_init(&buf, v->base, v->len); + buf.last = buf.end; + nghttp3_typed_buf_init(&tbuf, &buf, NGHTTP3_BUF_TYPE_ALIEN); + rv = nghttp3_stream_outq_add(stream, &tbuf); + if (rv != 0) { + return rv; + } + } + + return 0; +} + +int nghttp3_stream_write_cpsl_wt_close_session( + nghttp3_stream *stream, const nghttp3_exfr_cpsl_wt_close_session *fr) { + int rv; + nghttp3_buf *chunk; + nghttp3_buf buf; + nghttp3_typed_buf tbuf; + size_t cpsl_payloadlen = sizeof(fr->error_code) + fr->error_msg.len; + size_t fr_hdlen = nghttp3_frame_write_hd_len(fr->type, cpsl_payloadlen); + uint64_t payloadlen = fr_hdlen + cpsl_payloadlen; + + rv = nghttp3_stream_ensure_chunk( + stream, nghttp3_frame_write_hd_len(NGHTTP3_FRAME_DATA, payloadlen) + + fr_hdlen + sizeof(fr->error_code)); + if (rv != 0) { + return rv; + } + + chunk = nghttp3_stream_get_chunk(stream); + nghttp3_typed_buf_shared_init(&tbuf, chunk); + + chunk->last = + nghttp3_frame_write_hd(chunk->last, NGHTTP3_FRAME_DATA, payloadlen); + chunk->last = nghttp3_frame_write_hd(chunk->last, fr->type, cpsl_payloadlen); + chunk->last = nghttp3_put_uint32be(chunk->last, fr->error_code); + + tbuf.buf.last = chunk->last; + + rv = nghttp3_stream_outq_add(stream, &tbuf); + if (rv != 0) { + return rv; + } + + if (fr->error_msg.len == 0) { + return 0; + } + + nghttp3_buf_wrap_init(&buf, fr->error_msg.base, fr->error_msg.len); + buf.last = buf.end; + nghttp3_typed_buf_init(&tbuf, &buf, NGHTTP3_BUF_TYPE_ALIEN_NO_ACK); + + return nghttp3_stream_outq_add(stream, &tbuf); +} + int nghttp3_stream_write_headers(nghttp3_stream *stream, const nghttp3_frame_headers *fr) { nghttp3_conn *conn = stream->conn; @@ -506,6 +727,10 @@ int nghttp3_stream_write_header_block(nghttp3_stream *stream, nghttp3_buf_wrap_init(&pbuf, raw_pbuf, sizeof(raw_pbuf)); + nghttp3_qpack_encoder_set_no_insert( + qenc, qenc_stream && qenc_stream->tx.offset - qenc_stream->ack_offset >= + NGHTTP3_MAX_QENC_UNACKED_BYTES); + rv = nghttp3_qpack_encoder_encode(qenc, &pbuf, rbuf, ebuf, stream->node.id, nva, nvlen); if (rv != 0) { @@ -551,8 +776,14 @@ int nghttp3_stream_write_header_block(nghttp3_stream *stream, return rv; } nghttp3_buf_init(rbuf); - } else if (rbuflen) { - chunk->last = nghttp3_cpymem(chunk->last, rbuf->pos, rbuflen); + } else { + /* Field section might be empty. We still have to add the frame + header and field section prefix, which have been written to + chunk, to outq. */ + if (rbuflen) { + chunk->last = nghttp3_cpymem(chunk->last, rbuf->pos, rbuflen); + } + tbuf.buf.last = chunk->last; rv = nghttp3_stream_outq_add(stream, &tbuf); @@ -716,6 +947,11 @@ int nghttp3_stream_write_qpack_decoder_stream(nghttp3_stream *stream) { assert(qdec); + if (stream->tx.offset - stream->ack_offset >= + NGHTTP3_QPACK_MAX_UNACKED_DECODERLEN) { + return 0; + } + len = nghttp3_qpack_decoder_get_decoder_streamlen2(qdec); if (len == 0) { return 0; @@ -801,6 +1037,14 @@ int nghttp3_stream_ensure_chunk(nghttp3_stream *stream, size_t need) { for (; n < need; n *= 2) ; + if (nghttp3_ringbuf_full(chunks)) { + size_t nlen = nghttp3_max(NGHTTP3_MIN_RBLEN, len * 2); + rv = nghttp3_ringbuf_reserve(chunks, nlen); + if (rv != 0) { + return rv; + } + } + if (n == NGHTTP3_STREAM_MIN_CHUNK_SIZE) { p = (uint8_t *)nghttp3_objalloc_chunk_len_get(stream->out_chunk_objalloc, n); @@ -811,14 +1055,6 @@ int nghttp3_stream_ensure_chunk(nghttp3_stream *stream, size_t need) { return NGHTTP3_ERR_NOMEM; } - if (nghttp3_ringbuf_full(chunks)) { - size_t nlen = nghttp3_max(NGHTTP3_MIN_RBLEN, len * 2); - rv = nghttp3_ringbuf_reserve(chunks, nlen); - if (rv != 0) { - return rv; - } - } - chunk = nghttp3_ringbuf_push_back(chunks); nghttp3_buf_wrap_init(chunk, p, n); @@ -841,6 +1077,10 @@ int nghttp3_stream_is_blocked(const nghttp3_stream *stream) { } int nghttp3_stream_require_schedule(const nghttp3_stream *stream) { + if (stream->flags & NGHTTP3_STREAM_FLAG_CLOSED) { + return 0; + } + return (!nghttp3_stream_outq_write_done(stream) && !(stream->flags & NGHTTP3_STREAM_FLAG_FC_BLOCKED) && !(stream->flags & NGHTTP3_STREAM_FLAG_SHUT_WR)) || @@ -848,6 +1088,11 @@ int nghttp3_stream_require_schedule(const nghttp3_stream *stream) { !(stream->flags & NGHTTP3_STREAM_FLAG_READ_DATA_BLOCKED)); } +int nghttp3_stream_schedulable(const nghttp3_stream *stream) { + return !nghttp3_stream_uni(stream->node.id) || + stream->type == NGHTTP3_STREAM_TYPE_WT_STREAM; +} + size_t nghttp3_stream_writev(nghttp3_stream *stream, int *pfin, nghttp3_vec *vec, size_t veccnt) { nghttp3_ringbuf *outq = &stream->outq; @@ -1001,6 +1246,7 @@ int nghttp3_stream_buffer_data(nghttp3_stream *stream, const uint8_t *data, nghttp3_buf *buf; size_t nwrite; uint8_t *rawbuf; + size_t rawbuflen = NGHTTP3_STREAM_MIN_INQ_BUFLEN; size_t bufleft; int rv; @@ -1011,6 +1257,11 @@ int nghttp3_stream_buffer_data(nghttp3_stream *stream, const uint8_t *data, buf->last = nghttp3_cpymem(buf->last, data, nwrite); data += nwrite; datalen -= nwrite; + + /* Double the size of buffer so that the number of buffers stays + small even if data arrive in small pieces. */ + rawbuflen = + nghttp3_min(nghttp3_buf_cap(buf) * 2, NGHTTP3_STREAM_CAP_INQ_BUFLEN); } for (; datalen;) { @@ -1023,13 +1274,25 @@ int nghttp3_stream_buffer_data(nghttp3_stream *stream, const uint8_t *data, } } - rawbuf = nghttp3_mem_malloc(stream->mem, 16384); + if (datalen >= NGHTTP3_STREAM_CAP_INQ_BUFLEN) { + rawbuflen = datalen; + } else { + /* The buffered data is not reported as consumed, and the amount + of data is limited by QUIC flow control. Keep the size of + buffer proportional to the amount of data so that the memory + is limited as well. */ + for (; rawbuflen < datalen && rawbuflen < NGHTTP3_STREAM_CAP_INQ_BUFLEN; + rawbuflen *= 2) + ; + } + + rawbuf = nghttp3_mem_malloc(stream->mem, rawbuflen); if (rawbuf == NULL) { return NGHTTP3_ERR_NOMEM; } buf = nghttp3_ringbuf_push_back(inq); - nghttp3_buf_wrap_init(buf, rawbuf, 16384); + nghttp3_buf_wrap_init(buf, rawbuf, rawbuflen); bufleft = nghttp3_buf_left(buf); nwrite = nghttp3_min(datalen, bufleft); buf->last = nghttp3_cpymem(buf->last, data, nwrite); @@ -1235,8 +1498,25 @@ int nghttp3_stream_empty_headers_allowed(const nghttp3_stream *stream) { } } +int nghttp3_stream_critical(const nghttp3_stream *stream) { + return nghttp3_stream_uni(stream->node.id) && + (stream->type == NGHTTP3_STREAM_TYPE_CONTROL || + stream->type == NGHTTP3_STREAM_TYPE_QPACK_ENCODER || + stream->type == NGHTTP3_STREAM_TYPE_QPACK_DECODER); +} + int nghttp3_stream_uni(int64_t stream_id) { return (stream_id & 0x2) != 0; } +int nghttp3_stream_wt_ctrl(const nghttp3_stream *stream) { + return stream->wt.session && + stream->wt.session->session_id == stream->node.id; +} + +int nghttp3_stream_wt_data(const nghttp3_stream *stream) { + return stream->wt.session && + stream->wt.session->session_id != stream->node.id; +} + int nghttp3_client_stream_bidi(int64_t stream_id) { return (stream_id & 0x3) == 0; } @@ -1248,3 +1528,7 @@ int nghttp3_client_stream_uni(int64_t stream_id) { int nghttp3_server_stream_uni(int64_t stream_id) { return (stream_id & 0x3) == 0x3; } + +int nghttp3_server_stream_bidi(int64_t stream_id) { + return (stream_id & 0x3) == 0x1; +} diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_stream.h b/deps/ngtcp2/nghttp3/lib/nghttp3_stream.h index 07d71573a58f..36be016dfe1a 100644 --- a/deps/ngtcp2/nghttp3/lib/nghttp3_stream.h +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_stream.h @@ -41,11 +41,25 @@ #define NGHTTP3_STREAM_MIN_CHUNK_SIZE 256 +/* NGHTTP3_STREAM_MIN_INQ_BUFLEN is the minimum size of a buffer in + nghttp3_stream.inq. */ +#define NGHTTP3_STREAM_MIN_INQ_BUFLEN 256 +/* NGHTTP3_STREAM_CAP_INQ_BUFLEN is the maximum geometric growth in + buffer size in nghttp3_stream.inq. If there is more data to + buffer, it is allocated in its own size. */ +#define NGHTTP3_STREAM_CAP_INQ_BUFLEN 16384 + /* NGHTTP3_MIN_UNSENT_BYTES is the minimum unsent bytes which is large enough to fill outgoing single QUIC packet or TLS record in case of QMux (Cut 2 bytes for QMux record length). */ #define NGHTTP3_MIN_UNSENT_BYTES 16382 +/* NGHTTP3_MAX_QENC_UNACKED_BYTES is the number of bytes buffered in + QPACK encoder stream, and not yet acknowledged by a remote + endpoint, at which QPACK encoder stops inserting new entries into + the dynamic table. */ +#define NGHTTP3_MAX_QENC_UNACKED_BYTES 16384 + /* NGHTTP3_STREAM_MIN_WRITELEN is the minimum length of write to cause the stream to reschedule. */ #define NGHTTP3_STREAM_MIN_WRITELEN 800 @@ -57,6 +71,7 @@ typedef uint64_t nghttp3_stream_type; #define NGHTTP3_STREAM_TYPE_PUSH 0x01U #define NGHTTP3_STREAM_TYPE_QPACK_ENCODER 0x02U #define NGHTTP3_STREAM_TYPE_QPACK_DECODER 0x03U +#define NGHTTP3_STREAM_TYPE_WT_STREAM 0x54U #define NGHTTP3_STREAM_TYPE_UNKNOWN UINT64_MAX typedef enum nghttp3_ctrl_stream_state { @@ -79,10 +94,19 @@ typedef enum nghttp3_req_stream_state { NGHTTP3_REQ_STREAM_STATE_FRAME_LENGTH, NGHTTP3_REQ_STREAM_STATE_DATA, NGHTTP3_REQ_STREAM_STATE_HEADERS, + NGHTTP3_REQ_STREAM_STATE_BEFORE_WT_DATA, + NGHTTP3_REQ_STREAM_STATE_WT_DATA, NGHTTP3_REQ_STREAM_STATE_IGN_FRAME, NGHTTP3_REQ_STREAM_STATE_IGN_REST, } nghttp3_req_stream_state; +/* stream state for WebTransport unidirectional data stream */ +typedef enum nghttp3_wt_stream_state { + NGHTTP3_WT_STREAM_STATE_SESSION_ID, + NGHTTP3_WT_STREAM_STATE_DATA, + NGHTTP3_WT_STREAM_STATE_IGN_REST, +} nghttp3_wt_stream_state; + typedef struct nghttp3_varint_read_state { uint64_t acc; size_t left; @@ -96,6 +120,8 @@ typedef struct nghttp3_stream_read_state { int state; } nghttp3_stream_read_state; +typedef struct nghttp3_wt_session nghttp3_wt_session; + /* NGHTTP3_STREAM_FLAG_NONE indicates that no flag is set. */ #define NGHTTP3_STREAM_FLAG_NONE 0x0000U /* NGHTTP3_STREAM_FLAG_TYPE_IDENTIFIED is set when a unidirectional @@ -116,6 +142,10 @@ typedef struct nghttp3_stream_read_state { /* NGHTTP3_STREAM_FLAG_READ_EOF indicates that remote endpoint sent fin. */ #define NGHTTP3_STREAM_FLAG_READ_EOF 0x0020U +/* NGHTTP3_STREAM_FLAG_CLOSED indicates that QUIC stream was closed. + nghttp3_stream object can still be alive because it has received + all stream data, and is blocked by QPACK decoder. */ +#define NGHTTP3_STREAM_FLAG_CLOSED 0x0040U /* NGHTTP3_STREAM_FLAG_SHUT_WR indicates that any further write operation to a stream is prohibited. */ #define NGHTTP3_STREAM_FLAG_SHUT_WR 0x0100U @@ -129,6 +159,18 @@ typedef struct nghttp3_stream_read_state { /* NGHTTP3_STREAM_FLAG_PRIORITY_UPDATE_RECVED indicates that server received PRIORITY_UPDATE frame for this stream. */ #define NGHTTP3_STREAM_FLAG_PRIORITY_UPDATE_RECVED 0x0800U +/* NGHTTP3_STREAM_FLAG_MAYBE_WT_DATA indicates that the stream may be + WebTransport data stream. */ +#define NGHTTP3_STREAM_FLAG_MAYBE_WT_DATA 0x1000U +/* NGHTTP3_STREAM_FLAG_WT_DATA indicates that the stream is + WebTransport data stream. */ +#define NGHTTP3_STREAM_FLAG_WT_DATA 0x2000U +/* NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED indicates that the stream is + blocked because WebTransport session has not been established. */ +#define NGHTTP3_STREAM_FLAG_WT_SESSION_BLOCKED 0x4000U +/* NGHTTP3_STREAM_FLAG_RESP_SUBMITTED indicates that HTTP/3 response + has been submitted via nghttp3_conn_submit_response. */ +#define NGHTTP3_STREAM_FLAG_RESP_SUBMITTED 0x8000U typedef enum nghttp3_stream_http_state { NGHTTP3_HTTP_STATE_NONE, @@ -209,6 +251,9 @@ struct nghttp3_stream { stream is blocked by QPACK decoder. */ nghttp3_ringbuf inq; nghttp3_qpack_stream_context qpack_sctx; + /* field_blocklen is the sum of the decoded field name and value + length. */ + size_t field_blocklen; /* conn is a reference to underlying connection. It could be NULL if stream is not a request stream. */ nghttp3_conn *conn; @@ -236,6 +281,21 @@ struct nghttp3_stream { nghttp3_http_state http; } rx; + struct { + nghttp3_wt_session *session; + nghttp3_stream *prev; + nghttp3_stream *next; + } wt; + + /* close_rx_app_error_code, close_tx_app_error_code, and + close_flags are the arguments passed to + nghttp3_conn_close_stream2. They are kept until the deferred + deletion of this object takes place, and are only used when + NGHTTP3_STREAM_FLAG_CLOSED is set. */ + uint64_t close_rx_app_error_code; + uint64_t close_tx_app_error_code; + uint32_t close_flags; + uint16_t flags; }; @@ -311,6 +371,15 @@ int nghttp3_stream_write_priority_update( int nghttp3_stream_write_origin(nghttp3_stream *stream, const nghttp3_frame_origin *fr); +int nghttp3_stream_write_wt_stream(nghttp3_stream *stream, + const nghttp3_exfr_wt_stream *fr); + +int nghttp3_stream_write_wt_stream_data(nghttp3_stream *stream, int *peof, + const nghttp3_exfr_wt_stream *fr); + +int nghttp3_stream_write_cpsl_wt_close_session( + nghttp3_stream *stream, const nghttp3_exfr_cpsl_wt_close_session *frent); + int nghttp3_stream_ensure_chunk(nghttp3_stream *stream, size_t need); nghttp3_buf *nghttp3_stream_get_chunk(nghttp3_stream *stream); @@ -345,6 +414,8 @@ int nghttp3_stream_is_active(nghttp3_stream *stream); */ int nghttp3_stream_require_schedule(const nghttp3_stream *stream); +int nghttp3_stream_schedulable(const nghttp3_stream *stream); + int nghttp3_stream_buffer_data(nghttp3_stream *stream, const uint8_t *src, size_t srclen); @@ -359,6 +430,12 @@ int nghttp3_stream_transit_rx_http_state(nghttp3_stream *stream, int nghttp3_stream_empty_headers_allowed(const nghttp3_stream *stream); +int nghttp3_stream_wt_ctrl(const nghttp3_stream *stream); + +int nghttp3_stream_wt_data(const nghttp3_stream *stream); + +int nghttp3_stream_critical(const nghttp3_stream *stream); + /* * nghttp3_stream_uni returns nonzero if stream identified by * |stream_id| is unidirectional. @@ -383,4 +460,6 @@ int nghttp3_client_stream_uni(int64_t stream_id); */ int nghttp3_server_stream_uni(int64_t stream_id); +int nghttp3_server_stream_bidi(int64_t stream_id); + #endif /* !defined(NGHTTP3_STREAM_H) */ diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_wt.c b/deps/ngtcp2/nghttp3/lib/nghttp3_wt.c new file mode 100644 index 000000000000..91331206c5c6 --- /dev/null +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_wt.c @@ -0,0 +1,95 @@ +/* + * nghttp3 + * + * Copyright (c) 2025 nghttp3 contributors + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "nghttp3_wt.h" + +#include + +#include "nghttp3_mem.h" + +int nghttp3_wt_session_new(nghttp3_wt_session **pwts, int64_t session_id, + const nghttp3_mem *mem) { + *pwts = nghttp3_mem_malloc(mem, sizeof(**pwts)); + if (*pwts == NULL) { + return NGHTTP3_ERR_NOMEM; + } + + **pwts = (nghttp3_wt_session){ + .session_id = session_id, + }; + + return 0; +} + +void nghttp3_wt_session_del(nghttp3_wt_session *wts, const nghttp3_mem *mem) { + if (!wts) { + return; + } + + nghttp3_mem_free(mem, wts->rx.error_msg.base); + nghttp3_mem_free(mem, wts->tx.error_msg.base); + + nghttp3_mem_free(mem, wts); +} + +void nghttp3_wt_session_add_stream(nghttp3_wt_session *wts, + nghttp3_stream *stream) { + assert(!stream->wt.session); + assert(!stream->wt.prev); + assert(!stream->wt.next); + + stream->wt.session = wts; + stream->flags |= NGHTTP3_STREAM_FLAG_WT_DATA; + + if (wts->head) { + stream->wt.next = wts->head; + wts->head->wt.prev = stream; + } + + wts->head = stream; +} + +void nghttp3_wt_session_remove_stream(nghttp3_wt_session *wts, + nghttp3_stream *stream) { + assert(stream->wt.session); + + if (stream->wt.prev) { + stream->wt.prev->wt.next = stream->wt.next; + } + + if (stream->wt.next) { + stream->wt.next->wt.prev = stream->wt.prev; + } + + if (wts->head == stream) { + wts->head = stream->wt.next; + } + + stream->wt.session = NULL; + stream->wt.prev = stream->wt.next = NULL; +} + +void nghttp3_wt_ctrl_read_state_reset(nghttp3_wt_ctrl_read_state *rstate) { + *rstate = (nghttp3_wt_ctrl_read_state){0}; +} diff --git a/deps/ngtcp2/nghttp3/lib/nghttp3_wt.h b/deps/ngtcp2/nghttp3/lib/nghttp3_wt.h new file mode 100644 index 000000000000..af3eaa19022d --- /dev/null +++ b/deps/ngtcp2/nghttp3/lib/nghttp3_wt.h @@ -0,0 +1,89 @@ +/* + * nghttp3 + * + * Copyright (c) 2025 nghttp3 contributors + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef NGHTTP3_WT_H +#define NGHTTP3_WT_H + +#ifdef HAVE_CONFIG_H +# include +#endif /* defined(HAVE_CONFIG_H) */ + +#include + +#include "nghttp3_stream.h" + +/* NGHTTP3_WT_SESSION_FLAG_CONFIRMED indicates that WebTransport + session has been established. */ +#define NGHTTP3_WT_SESSION_FLAG_CONFIRMED 0x1 +/* NGHTTP3_WT_SESSION_FLAG_RESP_SUBMITTED indicates that HTTP/3 + response has been submitted via nghttp3_conn_submit_wt_response. */ +#define NGHTTP3_WT_SESSION_FLAG_RESP_SUBMITTED 0x2 +/* NGHTTP3_WT_SESSION_FLAG_CLOSED is set when WT_CLOSE_SESSION capsule + is queued, sent, or received. */ +#define NGHTTP3_WT_SESSION_FLAG_CLOSED 0x4 + +typedef enum nghttp3_wt_ctrl_stream_state { + NGHTTP3_WT_CTRL_STREAM_STATE_TYPE, + NGHTTP3_WT_CTRL_STREAM_STATE_LENGTH, + NGHTTP3_WT_CTRL_STREAM_STATE_WT_CLOSE_SESSION_ERROR_CODE, + NGHTTP3_WT_CTRL_STREAM_STATE_WT_CLOSE_SESSION_ERROR_MSG, + NGHTTP3_WT_CTRL_STREAM_STATE_IGN, +} nghttp3_wt_ctrl_stream_state; + +typedef struct nghttp3_wt_ctrl_read_state { + nghttp3_varint_read_state rvint; + nghttp3_exfr_cpsl cpsl; + uint64_t left; + size_t field_left; + int state; +} nghttp3_wt_ctrl_read_state; + +typedef struct nghttp3_wt_session { + nghttp3_wt_ctrl_read_state rstate; + struct { + nghttp3_vec error_msg; + } tx; + struct { + nghttp3_vec error_msg; + uint32_t error_code; + } rx; + int64_t session_id; + nghttp3_stream *head; + uint32_t flags; +} nghttp3_wt_session; + +void nghttp3_wt_session_add_stream(nghttp3_wt_session *wts, + nghttp3_stream *stream); + +void nghttp3_wt_session_remove_stream(nghttp3_wt_session *wts, + nghttp3_stream *stream); + +int nghttp3_wt_session_new(nghttp3_wt_session **pwts, int64_t stream_id, + const nghttp3_mem *mem); + +void nghttp3_wt_session_del(nghttp3_wt_session *wts, const nghttp3_mem *mem); + +void nghttp3_wt_ctrl_read_state_reset(nghttp3_wt_ctrl_read_state *rstate); + +#endif /* !defined(NGHTTP3_WT_H) */ diff --git a/deps/ngtcp2/ngtcp2.gyp b/deps/ngtcp2/ngtcp2.gyp index 7ae627924851..939b9b9eba44 100644 --- a/deps/ngtcp2/ngtcp2.gyp +++ b/deps/ngtcp2/ngtcp2.gyp @@ -90,6 +90,7 @@ 'nghttp3/lib/nghttp3_unreachable.c', 'nghttp3/lib/nghttp3_vec.c', 'nghttp3/lib/nghttp3_version.c', + 'nghttp3/lib/nghttp3_wt.c', ], 'ngtcp2_test_server_sources': [ 'ngtcp2/examples/tls_server_session_ossl.cc', diff --git a/lib/internal/quic/diagnostics.js b/lib/internal/quic/diagnostics.js index 10dac5693fba..afa2bf4fec7f 100644 --- a/lib/internal/quic/diagnostics.js +++ b/lib/internal/quic/diagnostics.js @@ -33,6 +33,8 @@ const onSessionGoawayChannel = dc.channel('quic.session.goaway'); const onSessionEarlyRejectedChannel = dc.channel('quic.session.early.rejected'); const onStreamClosedChannel = dc.channel('quic.stream.closed'); const onStreamHeadersChannel = dc.channel('quic.stream.headers'); +const onStreamSessionIdChannel = dc.channel('quic.stream.sessionid'); +const onStreamWTSessionCloseChannel = dc.channel('quic.stream.wtsessionclose'); const onStreamTrailersChannel = dc.channel('quic.stream.trailers'); const onStreamInfoChannel = dc.channel('quic.stream.info'); const onStreamResetChannel = dc.channel('quic.stream.reset'); @@ -68,6 +70,8 @@ module.exports = { onSessionEarlyRejectedChannel, onStreamClosedChannel, onStreamHeadersChannel, + onStreamSessionIdChannel, + onStreamWTSessionCloseChannel, onStreamTrailersChannel, onStreamInfoChannel, onStreamResetChannel, diff --git a/lib/internal/quic/quic.js b/lib/internal/quic/quic.js index 6a10f0f94ff5..e71083afb447 100644 --- a/lib/internal/quic/quic.js +++ b/lib/internal/quic/quic.js @@ -51,6 +51,9 @@ const { sendHeaders, setCallbacks, setHeadersInterest, + setWebtransportInterest, + makeWebtransportStream, + closeWebtransportSessionStream, // The constants to be exposed to end users for various options. CC_ALGO_RENO_STR: CC_ALGO_RENO, @@ -77,6 +80,7 @@ const { QUIC_STREAM_HEADERS_KIND_TRAILING: kHeadersKindTrailing, QUIC_STREAM_HEADERS_FLAGS_NONE: kHeadersFlagsNone, QUIC_STREAM_HEADERS_FLAGS_TERMINAL: kHeadersFlagsTerminal, + QUIC_STREAM_HEADERS_FLAGS_WEBTRANSPORT: kHeadersFlagsWebtransport, } = internalBinding('quic'); // Maps the numeric HeadersKind constants from C++ to user-facing strings. @@ -187,6 +191,8 @@ const { kHandshakeCompleted, kVerifyPeer, kHeaders, + kSessionId, + kWTSessionClose, kOwner, kRemoveSession, kKeylog, @@ -202,6 +208,8 @@ const { kPrivateConstructor, kReset, kSendHeaders, + kMakeWebtransportStream, + kCloseWebtransportSessionStream, kSessionApplication, kSessionTicket, kStopSending, @@ -265,6 +273,8 @@ const { onSessionEarlyRejectedChannel, onStreamClosedChannel, onStreamHeadersChannel, + onStreamSessionIdChannel, + onStreamWTSessionCloseChannel, onStreamTrailersChannel, onStreamInfoChannel, onStreamResetChannel, @@ -290,7 +300,9 @@ const endpointRegistry = new SafeSet(); * @property {string|ArrayBuffer|SharedArrayBuffer|ArrayBufferView|Blob| * FileHandle|AsyncIterable|Iterable|Promise|null} [body] The outbound * body source. See the public docs for `stream.setBody()` for details - * on supported types. When omitted, the stream is closed immediately. + * on supported types. When omitted, the stream is closed immediately, + * except if the stream is a webtransport stream. For a webtransport + * stream setting body is illegal. * @property {object} [headers] Initial request or response headers to * send. Only used when the negotiated application supports headers * (e.g. HTTP/3). @@ -298,7 +310,12 @@ const endpointRegistry = new SafeSet(); * @property {boolean} [incremental] Whether to interleave data with same-priority streams. * @property {number} [budget] The byte budget for write backpressure. * **Default:** `65536`. + * @property {QuicStream} [webtransportSession] The webtransport session control stream. + * @property {boolean} [webtransport] Indicates that the headers should send signal + * webtransport support. If no headers are provided, it has no effect. * @property {OnHeadersCallback} [onheaders] Callback for incoming initial headers + * @property {OnSessionIdCallback} [onsessionid] Callback for incoming sessionid + * @property {OnWTSessionCloseCallback} [onwtsessionclose] Callback for incoming close capsules * @property {OnTrailersCallback} [ontrailers] Callback for incoming trailing headers * @property {OnInfoCallback} [oninfo] Callback for informational (1xx) headers * @property {OnWantTrailersCallback} [onwanttrailers] Callback fired when the @@ -368,6 +385,7 @@ const endpointRegistry = new SafeSet(); * @property {bigint|number} [qpackBlockedStreams] The qpack blocked streams * @property {boolean} [enableConnectProtocol] Enable the connect protocol * @property {boolean} [enableDatagrams] Enable datagrams + * @property {boolean} [enableWebtransport] Enable webtransport */ /** @@ -464,6 +482,9 @@ const endpointRegistry = new SafeSet(); * @property {OnQlogCallback} [onqlog] qlog data callback. * @property {OnApplicationCallback} [onapplication] application options callback. * @property {OnHeadersCallback} [onheaders] Default per-stream initial-headers callback. + * @property {OnSessionIdCallback} [onsessionid] Default perstream initial callback for incoming sessionid. + * @property {OnWTSessionCloseCallback} [onwtsessionclose] Default perstream + * initial callback for incoming close capsules * @property {OnTrailersCallback} [ontrailers] Default per-stream trailing-headers callback. * @property {OnInfoCallback} [oninfo] Default per-stream informational-headers callback. * @property {OnWantTrailersCallback} [onwanttrailers] Default per-stream @@ -511,6 +532,10 @@ const endpointRegistry = new SafeSet(); * @typedef {object} SendHeadersOptions * @property {boolean} [terminal] When true, indicates that no body data will be * sent after these headers. + * @property {boolean} [webtransport] When true, indicates that this is a header + * for a webtransport connection. Note, on the response, if you deny a + * webtransport connection, set it to false. A webtransport header can not be + * terminal. So we throw an error, if webtransport and terminal are both true. */ /** @@ -707,6 +732,25 @@ const endpointRegistry = new SafeSet(); * @returns {void} */ +/** + * Called when session id (e.g. for Webtransport streams) is determined + * @callback OnSessionIdCallback + * @this {QuicStream} + * @param {bigint|undefined} sessionid Id of the session stream or undefined + * if no session stream is determined. + * @returns {void} + */ + +/** + * Called when session id (e.g. for Webtransport streams) is determined + * @callback OnWTSessionCloseCallback + * @this {QuicStream} + * @param {number} code error code from the webtransport session + * @param {string|undefined} reason error message from the webtransport session + * @returns {void} + */ + + /** * Called when trailing headers are received from the peer. * @callback OnTrailersCallback @@ -1008,6 +1052,18 @@ setCallbacks({ this[kOwner][kHeaders](headers, kind); }, + onStreamSessionId(sessionId) { + // Called when the stream C++ handle has determined the sessionId + debug(`stream ${this[kOwner].id} sessionid callback`, sessionId); + this[kOwner][kSessionId](sessionId); + }, + + onStreamWTSessionClose(errorcode, errormessage) { + // Called when the stream C++ handle has received a close capsule + debug(`stream ${this[kOwner].id} wtsessionclose callback`, errorcode, errormessage); + this[kOwner][kWTSessionClose](errorcode, errormessage); + }, + onStreamTrailers() { // Called when the stream C++ handle is ready to receive trailing headers. debug('stream want trailers callback', this[kOwner]); @@ -1326,6 +1382,15 @@ function updateHeaderInterest(handle, inner) { ); } +function updateWebtransportInterest(handle, inner) { + if (handle === undefined) return; + setWebtransportInterest( + handle, + inner.onsessionid !== undefined, + inner.onwtsessionclose !== undefined, + ); +} + /** * Applies session and stream callbacks from an options object to a session. * @param {QuicSession} session @@ -1356,6 +1421,8 @@ function applyCallbacks(session, cbs) { onwanttrailers: cbs.onwanttrailers, }; } + if (cbs.onsessionid) session.onsessionid = cbs.onsessionid; + if (cbs.onwtsessionclose) session.onwtsessionclose = cbs.onwtsessionclose; } /** @@ -1628,6 +1695,8 @@ class QuicStream { onreset: undefined, onstopsending: undefined, onheaders: undefined, + onsessionid: undefined, + onwtsessionclose: undefined, ontrailers: undefined, oninfo: undefined, onwanttrailers: undefined, @@ -1906,6 +1975,42 @@ class QuicStream { updateHeaderInterest(this.#handle, inner); } + /** @type {OnSessionIdCallback} */ + get onsessionid() { + assertIsQuicStream(this); + return this.#inner.onsessionid; + } + + set onsessionid(fn) { + assertIsQuicStream(this); + const inner = this.#inner; + if (fn === undefined) { + inner.onsessionid = undefined; + } else { + validateFunction(fn, 'onsessionid'); + inner.onsessionid = FunctionPrototypeBind(fn, this); + } + updateWebtransportInterest(this.#handle, inner); + } + + /** @type {OnWTSessionCloseCallback} */ + get onwtsessionclose() { + assertIsQuicStream(this); + return this.#inner.onwtsessionclose; + } + + set onwtsessionclose(fn) { + assertIsQuicStream(this); + const inner = this.#inner; + if (fn === undefined) { + inner.onwtsessionclose = undefined; + } else { + validateFunction(fn, 'onwtsessionclose'); + inner.onwtsessionclose = FunctionPrototypeBind(fn, this); + } + updateWebtransportInterest(this.#handle, inner); + } + /** @type {Function|undefined} */ get oninfo() { assertIsQuicStream(this); @@ -2059,6 +2164,37 @@ class QuicStream { return this.#inner.pendingClose.promise; } + /** + * Only for webtransport session streams + * Closes the webtransport session stream, and also closes + * connected datastream internally. + * @param {number|undefined} code optional error code + * @param {string|undefined} msg optional error message truncated to 1024 bytes + */ + closeWebtransportSessionStream(code, msg) { + assertIsQuicStream(this); + const inner = this.#inner; + if (inner.destroying || this.destroyed) return; + + if (msg !== undefined) { + validateString(msg, 'msg'); + } + if (code !== undefined) { + validateInteger(code); + } + inner.destroying = true; + const handle = this.#handle; + this[kCloseWebtransportSessionStream](code, msg); + const error = makeQuicError( + 'ERR_QUIC_APPLICATION_ERROR', + 'Webtransport error', + 'application', + code ?? 0, + msg ?? ''); + this[kFinishClose](error); + handle.destroy(); + } + /** * Immediately destroys the stream. Buffered incoming data remains readable. * If an error is given, the closed promise will be rejected with that @@ -2148,10 +2284,16 @@ class QuicStream { if (this.destroyed) return false; assertHeadersSupported(this.#inner.session); validateObject(headers, 'headers'); - const { terminal = false } = options; + const { terminal = false, webtransport = false } = options; + if (terminal && webtransport) { + throw new ERR_INVALID_ARG_VALUE( + 'webtransport and terminal can not be set simultaenously.', + { terminal, webtransport }); + } const headerString = buildNgHeaderString( headers, assertValidPseudoHeader, true /* strictSingleValueFields */); - const flags = terminal ? kHeadersFlagsTerminal : kHeadersFlagsNone; + const flags = terminal ? kHeadersFlagsTerminal : + (webtransport ? kHeadersFlagsWebtransport : kHeadersFlagsNone); return sendHeaders( this.#handle, kHeadersKindInitial, headerString, flags); } @@ -2407,6 +2549,38 @@ class QuicStream { return sendHeaders(this.#handle, kind, headerString, flags); } + /** + * Attaches a webtransport session to the stream and sends initial bytes + * indicating webtransport stream and the session id + * @param {QuicStream} session Webtransport session stream + * @returns {boolean} true if it succeeded. + */ + [kMakeWebtransportStream](session) { + if (this.pending) { + debug('pending stream enqueuing makeWebtransportStream for session', session.id); + } else { + debug(`stream ${this.id} makeWebtransportStream for session`, session.id); + } + return makeWebtransportStream(this.#handle, session.#handle); + } + + /** + * Closes a webtransport session stream and also closes associated data streams + * Passing optional error code and error message (limited to 1024 bytes). + * @param {number} code error code + * @param {string} msg error message limited to 1024 bytes + * @returns {boolean} true if it succeeded. + */ + [kCloseWebtransportSessionStream](code, msg) { + if (this.pending) { + debug('pending stream enqueuing closeWebtransportSessionStream with code', code, ' and msg:', msg); + } else { + debug(`stream ${this.id} closeWebtransportSessionStream with code`, code, + ' and msg:', msg); + } + return closeWebtransportSessionStream(this.#handle, code, msg); + } + [kFinishClose](error) { const inner = this.#inner; inner.pendingClose ??= PromiseWithResolvers(); @@ -2416,6 +2590,10 @@ class QuicStream { // Prefer an error staged by destroy() (the original object the caller // passed) over the error delivered by the native close callback. error = inner.destroyError ?? error; + if (error?.errorCode === 0x170D7B68n && inner.wtSessionCloseError) { + error = inner.wtSessionCloseError; + inner.wtSessionCloseError = undefined; + } if (error !== undefined) { inner.pendingClose.reject(error); inner.pendingStream.reject(error); @@ -2446,6 +2624,7 @@ class QuicStream { inner.session[kRemoveStream](this); inner.writer?.fail(error); inner.session = undefined; + inner.wtSessionCloseError = undefined; inner.pendingClose.reject = undefined; inner.pendingClose.resolve = undefined; inner.destroyError = undefined; @@ -2453,6 +2632,8 @@ class QuicStream { inner.onreset = undefined; inner.onstopsending = undefined; inner.onheaders = undefined; + inner.onsessionid = undefined; + inner.onwtsessionclose = undefined; inner.onerror = undefined; inner.ontrailers = undefined; inner.oninfo = undefined; @@ -2563,6 +2744,45 @@ class QuicStream { } } + [kSessionId](sessionId) { + if (this.destroyed) return; + const inner = this.#inner; + if (onStreamSessionIdChannel.hasSubscribers) { + onStreamSessionIdChannel.publish({ + __proto__: null, + stream: this, + session: inner.session, + sessionId, + }); + } + if (typeof inner.onsessionid === 'function') { + safeCallbackInvoke(inner.onsessionid, this, sessionId); + } + } + + [kWTSessionClose](errorcode, errormessage) { + if (this.destroyed) return; + const inner = this.#inner; + if (onStreamWTSessionCloseChannel.hasSubscribers) { + onStreamWTSessionCloseChannel.publish({ + __proto__: null, + stream: this, + session: inner.session, + errorcode, + errormessage, + }); + } + if (typeof inner.onwtsessionclose === 'function') { + safeCallbackInvoke(inner.onwtsessionclose, this, errorcode, errormessage); + } + inner.wtSessionCloseError = makeQuicError( + 'ERR_QUIC_APPLICATION_ERROR', + 'Webtransport error', + 'application', + errorcode ?? 0, + errormessage ?? ''); + } + [kTrailers]() { if (this.destroyed) return; const inner = this.#inner; @@ -3228,9 +3448,13 @@ class QuicSession { priority = 'default', incremental = false, budget = kDefaultBudget, + webtransport = false, + webtransportSession = undefined, headers, waitUntilAvailable = true, onheaders, + onsessionid, + onwtsessionclose, ontrailers, oninfo, onwanttrailers, @@ -3238,9 +3462,33 @@ class QuicSession { validateOneOf(priority, 'options.priority', ['default', 'low', 'high']); validateBoolean(incremental, 'options.incremental'); + validateBoolean(webtransport, 'options.webtransport'); validateBoolean(waitUntilAvailable, 'options.waitUntilAvailable'); + const validatedBody = validateBody(body); + if (typeof body !== 'undefined' && webtransport) { + throw new ERR_INVALID_ARG_TYPE('options.body', 'A body can not be provided in webtransport mode'); + } + + if (webtransportSession) { + if (webtransport && webtransportSession) { + throw new ERR_INVALID_ARG_TYPE('options.webtransport', + 'webtransport can not be set for ' + + 'creating a webtransport stream associated with a webtransport session'); + } + + if (headers && webtransportSession) { + throw new ERR_INVALID_ARG_TYPE('options.headers', + 'headers can not be set for ' + + 'creating a webtransport stream associated with a webtransport session'); + } + if (!isQuicStream(webtransportSession)) { + throw new ERR_INVALID_ARG_TYPE('options.webtransportSession', + 'webtransportSession was to be of type QuicStream'); + } + } + const handle = this.#handle.openStream(direction, waitUntilAvailable, validatedBody); if (handle === undefined) { throw new ERR_QUIC_OPEN_STREAM_FAILED(); @@ -3270,12 +3518,24 @@ class QuicSession { // Set stream callbacks before sending headers to avoid missing events. if (onheaders) stream.onheaders = onheaders; + if (onsessionid) stream.onsessionid = onsessionid; + if (onwtsessionclose) stream.onwtsessionclose = onwtsessionclose; if (ontrailers) stream.ontrailers = ontrailers; if (oninfo) stream.oninfo = oninfo; if (onwanttrailers) stream.onwanttrailers = onwanttrailers; if (headers !== undefined) { - stream.sendHeaders(headers, { terminal: validatedBody === undefined }); + stream.sendHeaders(headers, + { terminal: validatedBody === undefined && !webtransport, + webtransport }); + } else if (webtransport) { + throw new ERR_INVALID_ARG_VALUE('options.webtransport', + 'Specifying webtransport without a header has no effect'); + } + if (webtransportSession) { + if (!stream[kMakeWebtransportStream](webtransportSession)) { + throw new ERR_QUIC_OPEN_STREAM_FAILED(); + } } if (onSessionOpenStreamChannel.hasSubscribers) { @@ -4035,6 +4295,8 @@ class QuicSession { const scbs = this[kStreamCallbacks]; if (scbs) { if (scbs.onheaders) stream.onheaders = scbs.onheaders; + if (scbs.onsessionid) stream.onsessionid = scbs.onsessionid; + if (scbs.onwtsessionclose) stream.onwtsessionclose = scbs.onwtsessionclose; if (scbs.ontrailers) stream.ontrailers = scbs.ontrailers; if (scbs.oninfo) stream.oninfo = scbs.oninfo; if (scbs.onwanttrailers) stream.onwanttrailers = scbs.onwanttrailers; @@ -4420,6 +4682,8 @@ class QuicEndpoint { onapplication, // Stream-level callbacks applied to each incoming stream. onheaders, + onsessionid, + onwtsessionclose, ontrailers, oninfo, onwanttrailers, @@ -4449,6 +4713,8 @@ class QuicEndpoint { onqlog, onapplication, onheaders, + onsessionid, + onwtsessionclose, ontrailers, oninfo, onwanttrailers, @@ -5229,6 +5495,8 @@ function processSessionOptions(options, config = kEmptyObject) { // Application level options changed, e.g. HTTP/3 settings related // Stream-level callbacks. onheaders, + onsessionid, + onwtsessionclose, ontrailers, oninfo, onwanttrailers, @@ -5354,6 +5622,8 @@ function processSessionOptions(options, config = kEmptyObject) { onqlog, onapplication, onheaders, + onsessionid, + onwtsessionclose, ontrailers, oninfo, onwanttrailers, diff --git a/lib/internal/quic/symbols.js b/lib/internal/quic/symbols.js index 1ed44ebe2b13..e660a2ba0164 100644 --- a/lib/internal/quic/symbols.js +++ b/lib/internal/quic/symbols.js @@ -30,6 +30,7 @@ const { const kAttachFileHandle = Symbol('kAttachFileHandle'); const kAvailable = Symbol('kAvailable'); const kBlocked = Symbol('kBlocked'); +const kCloseWebtransportSessionStream = Symbol('kCloseWebtransportSessionStream'); const kConnect = Symbol('kConnect'); const kDrain = Symbol('kDrain'); const kDatagram = Symbol('kDatagram'); @@ -44,6 +45,7 @@ const kHeaders = Symbol('kHeaders'); const kKeylog = Symbol('kKeylog'); const kListen = Symbol('kListen'); const kQlog = Symbol('kQlog'); +const kMakeWebtransportStream = Symbol('kMakeWebtransportStream'); const kNewSession = Symbol('kNewSession'); const kNewStream = Symbol('kNewStream'); const kNewToken = Symbol('kNewToken'); @@ -57,10 +59,12 @@ const kRemoveStream = Symbol('kRemoveStream'); const kReset = Symbol('kReset'); const kSendHeaders = Symbol('kSendHeaders'); const kSessionApplication = Symbol('kSessionApplication'); +const kSessionId = Symbol('kSessionId'); const kSessionTicket = Symbol('kSessionTicket'); const kStopSending = Symbol('kStopSending'); const kTrailers = Symbol('kTrailers'); const kVersionNegotiation = Symbol('kVersionNegotiation'); +const kWTSessionClose = Symbol('kWTSessionClose'); module.exports = { kAttachFileHandle, @@ -81,6 +85,7 @@ module.exports = { kKeylog, kKeyObjectHandle, kListen, + kMakeWebtransportStream, kNewSession, kNewStream, kNewToken, @@ -94,11 +99,14 @@ module.exports = { kRemoveStream, kReset, kSendHeaders, + kCloseWebtransportSessionStream, kSessionApplication, + kSessionId, kSessionTicket, kStopSending, kTrailers, kVersionNegotiation, + kWTSessionClose, }; /* c8 ignore stop */ diff --git a/src/quic/application.cc b/src/quic/application.cc index 11753aeb90f8..a560146e9664 100644 --- a/src/quic/application.cc +++ b/src/quic/application.cc @@ -56,6 +56,7 @@ Session::Application_Options::operator const nghttp3_settings() const { .glitch_ratelim_burst = 1000, .glitch_ratelim_rate = 33, .qpack_indexing_strat = NGHTTP3_QPACK_INDEXING_STRAT_EAGER, + .wt_enabled = enable_webtransport }; } @@ -77,6 +78,8 @@ std::string Session::Application_Options::ToString() const { (enable_connect_protocol ? std::string("yes") : std::string("no")); res += prefix + "enable datagrams: " + (enable_datagrams ? std::string("yes") : std::string("no")); + res += prefix + "webtransport enabled: " + + (enable_webtransport ? std::string("yes") : std::string("no")); res += indent.Close(); return res; } @@ -106,7 +109,7 @@ Maybe Session::Application_Options::From( !SET(max_field_section_size) || !SET(qpack_max_dtable_capacity) || !SET(qpack_encoder_max_dtable_capacity) || !SET(qpack_blocked_streams) || !SET(enable_connect_protocol) || - !SET(enable_datagrams)) { + !SET(enable_datagrams) || !SET(enable_webtransport)) { // The call to SetOption should have scheduled an exception to be thrown. return Nothing(); } @@ -137,6 +140,7 @@ MaybeLocal Session::Application_Options::ToObject( "qpackBlockedStreams", "enableConnectProtocol", "enableDatagrams", + "enableWebtransport" }; if (tmpl.IsEmpty()) { tmpl = DictionaryTemplate::New(env->isolate(), names); @@ -152,6 +156,7 @@ MaybeLocal Session::Application_Options::ToObject( BigInt::NewFromUnsigned(env->isolate(), qpack_blocked_streams), Boolean::New(env->isolate(), enable_connect_protocol), Boolean::New(env->isolate(), enable_datagrams), + Boolean::New(env->isolate(), enable_webtransport), }; static_assert(std::size(values) == std::size(names)); @@ -411,7 +416,7 @@ class DefaultApplication final : public Session::Application { void BlockStream(stream_id id) override { if (auto stream = session().FindStream(id)) [[likely]] { // Remove the stream from the send queue. It will be re-scheduled - // via ExtendMaxStreamData when the peer grants more flow control. + // via ExtendMax(Stream)Data when the peer grants more flow control. // Without this, SendPendingData would repeatedly pop and retry // the same blocked stream in an infinite loop. stream->Unschedule(); @@ -427,6 +432,15 @@ class DefaultApplication final : public Session::Application { stream->Schedule(&stream_queue_); } + void ExtendMaxData(uint64_t max_data) override { + // The peer granted more flow control for session. Re-schedule + // all streams so SendPendingData will resume writing. + for (auto& [id, stream] : session().streams()) { + stream->Schedule(&stream_queue_); + } + } + + bool StreamCommit(Session::StreamData* stream_data, size_t datalen) override { DCHECK_NOT_NULL(stream_data); CHECK(stream_data->stream); diff --git a/src/quic/application.h b/src/quic/application.h index e199660b66e3..903bef5f4097 100644 --- a/src/quic/application.h +++ b/src/quic/application.h @@ -20,6 +20,7 @@ enum class HeadersKind : uint8_t { enum class HeadersFlags : uint8_t { NONE, TERMINAL, + WEBTRANSPORT, }; // An Application implements the ALPN-protocol specific semantics on behalf @@ -147,6 +148,14 @@ class Session::Application : public MemoryRetainer { // By default do nothing. } + // Called when the Session determines that the flow control window for the + // session has been expanded. Not all Application types will require + // this notification so the default is to do nothing. + virtual void ExtendMaxData(uint64_t max_data) { + Debug(session_, "Application extending max data"); + // By default do nothing. + } + // Different Applications may wish to set some application data in the // session ticket (e.g. http/3 would set server settings in the application // data). The first byte written MUST be the Application::Type enum value. @@ -197,6 +206,32 @@ class Session::Application : public MemoryRetainer { virtual void SetHeadersInterest(Stream& stream, bool wants_headers, bool wants_trailers) {} + // Updates JavaScript callback interest concerning new webtransport sessions + // Applications without Webtransport support ignore this + virtual void SetWebtransportInterest(Stream& stream, + bool wants_sessionid, + bool wants_wtsessionclose) {} + + // connects the webtransport session stream to stream object, + // it also sends some initial bytes to the wire to signal + // the other side, that this is a webtransport stream + // it is a noop, if we can not send on this stream incoming + // unidirectional stream + virtual bool MakeWebtransportStream(Stream& stream, + int64_t sessionid) { + return false; + } + + // closes the webtransort session stream, + // and also closes connect webtransport data streams + virtual bool CloseWebtransportSessionStream( + const Stream& stream, + uint32_t wt_error_code, + const uint8_t* msg, + size_t msglen + ) { + return false; + } // Returns true if the application protocol supports sending and // receiving headers on streams (e.g. HTTP/3). Applications that diff --git a/src/quic/bindingdata.cc b/src/quic/bindingdata.cc index e49da50f3f03..0b93257319f2 100644 --- a/src/quic/bindingdata.cc +++ b/src/quic/bindingdata.cc @@ -281,6 +281,18 @@ void BindingData::InitPerContext(Realm* realm, Local target) { SetMethod(realm->context(), target, "setCallbacks", SetCallbacks); SetMethod(realm->context(), target, "sendHeaders", SendHeaders); SetMethod(realm->context(), target, "setHeadersInterest", SetHeadersInterest); + SetMethod(realm->context(), + target, + "setWebtransportInterest", + SetWebtransportInterest); + SetMethod(realm->context(), + target, + "closeWebtransportSessionStream", + CloseWebtransportSessionStream); + SetMethod(realm->context(), + target, + "makeWebtransportStream", + MakeWebtransportStream); constexpr int QUIC_STREAM_HEADERS_KIND_HINTS = static_cast(HeadersKind::HINTS); @@ -292,12 +304,15 @@ void BindingData::InitPerContext(Realm* realm, Local target) { static_cast(HeadersFlags::NONE); constexpr int QUIC_STREAM_HEADERS_FLAGS_TERMINAL = static_cast(HeadersFlags::TERMINAL); + constexpr int QUIC_STREAM_HEADERS_FLAGS_WEBTRANSPORT = + static_cast(HeadersFlags::WEBTRANSPORT); NODE_DEFINE_CONSTANT(target, QUIC_STREAM_HEADERS_KIND_HINTS); NODE_DEFINE_CONSTANT(target, QUIC_STREAM_HEADERS_KIND_INITIAL); NODE_DEFINE_CONSTANT(target, QUIC_STREAM_HEADERS_KIND_TRAILING); NODE_DEFINE_CONSTANT(target, QUIC_STREAM_HEADERS_FLAGS_NONE); NODE_DEFINE_CONSTANT(target, QUIC_STREAM_HEADERS_FLAGS_TERMINAL); + NODE_DEFINE_CONSTANT(target, QUIC_STREAM_HEADERS_FLAGS_WEBTRANSPORT); Realm::GetCurrent(realm->context())->AddBindingData(target); } @@ -308,6 +323,8 @@ void BindingData::RegisterExternalReferences( registry->Register(SetCallbacks); registry->Register(SendHeaders); registry->Register(SetHeadersInterest); + registry->Register(SetWebtransportInterest); + registry->Register(MakeWebtransportStream); } JS_METHOD_IMPL(BindingData::SendHeaders) { @@ -334,6 +351,67 @@ JS_METHOD_IMPL(BindingData::SetHeadersInterest) { *stream, args[1]->IsTrue(), args[2]->IsTrue()); } +JS_METHOD_IMPL(BindingData::SetWebtransportInterest) { + Stream* stream; + ASSIGN_OR_RETURN_UNWRAP(&stream, args[0]); + CHECK(args[1]->IsBoolean()); + CHECK(args[2]->IsBoolean()); + stream->session().application().SetWebtransportInterest( + *stream, args[1]->IsTrue(), args[2]->IsTrue()); +} + +// Connects a stream to a webtransport session stream, +// also sends the initial bytes of a stream to signel the wt stream +// also connects the readers +JS_METHOD_IMPL(BindingData::MakeWebtransportStream) { + Stream* stream; + CHECK_GT(args.Length(), 1); + ASSIGN_OR_RETURN_UNWRAP(&stream, args[0]); + CHECK(args[1]->IsObject()); + Stream* session; + ASSIGN_OR_RETURN_UNWRAP(&session, args[1].As()); + args.GetReturnValue() + .Set(stream->session() + .application().MakeWebtransportStream( + *stream, + session->id())); +} + +// Closes a webtransport session stream, +// also closes connected data streams +JS_METHOD_IMPL(BindingData::CloseWebtransportSessionStream) { + Stream* stream; + CHECK_GT(args.Length(), 1); + ASSIGN_OR_RETURN_UNWRAP(&stream, args[0]); + uint32_t wt_error_code = 0; + if (args.Length() > 1) { + CHECK(args[1]->IsUint32()); + wt_error_code = FromV8Value(args[1]); + } + uint8_t * msg = nullptr; + size_t msglen = 0; + if (args.Length() > 2) { + CHECK(args[2]->IsString()); + Local msgstr = args[2].As(); + const size_t length = msgstr->Utf8LengthV2(args.GetIsolate()); + msg = new uint8_t[length]; + msgstr->WriteUtf8V2( + args.GetIsolate(), reinterpret_cast(msg), + length, + String::WriteFlags::kNone); + msglen = std::min(length, 1024); + } + args.GetReturnValue().Set(stream->session().application() + .CloseWebtransportSessionStream( + *stream, + wt_error_code, + msg, + msglen)); + if (msg) { + delete[] msg; + } +} + BindingData::BindingData(Realm* realm, Local object) : BaseObject(realm, object), alloc_state_(new QuicAllocState{this}), diff --git a/src/quic/bindingdata.h b/src/quic/bindingdata.h index 7d424b9bf6f6..93978b84964c 100644 --- a/src/quic/bindingdata.h +++ b/src/quic/bindingdata.h @@ -62,6 +62,8 @@ struct QuicAllocState; V(stream_created, StreamCreated) \ V(stream_drain, StreamDrain) \ V(stream_headers, StreamHeaders) \ + V(stream_sessionid, StreamSessionId) \ + V(stream_wtsessionclose, StreamWTSessionClose) \ V(stream_reset, StreamReset) \ V(stream_stop_sending, StreamStopSending) \ V(stream_trailers, StreamTrailers) @@ -93,6 +95,7 @@ struct QuicAllocState; V(enable_connect_protocol, "enableConnectProtocol") \ V(enable_early_data, "enableEarlyData") \ V(enable_datagrams, "enableDatagrams") \ + V(enable_webtransport, "enableWebtransport") \ V(enable_tls_trace, "tlsTrace") \ V(endpoint, "Endpoint") \ V(endpoint_udp, "Endpoint::UDP") \ @@ -296,6 +299,9 @@ class BindingData final JS_METHOD(SetCallbacks); JS_METHOD(SendHeaders); JS_METHOD(SetHeadersInterest); + JS_METHOD(MakeWebtransportStream); + JS_METHOD(SetWebtransportInterest); + JS_METHOD(CloseWebtransportSessionStream); // Lazily-created per-Realm SessionManager. Centralizes CID -> Session // routing so that any endpoint can route packets to any session. diff --git a/src/quic/data.h b/src/quic/data.h index 004ba7734ed3..3dc18bcae554 100644 --- a/src/quic/data.h +++ b/src/quic/data.h @@ -187,7 +187,11 @@ class Store final : public MemoryRetainer { V(H3_VERSION_FALLBACK) \ V(QPACK_DECOMPRESSION_FAILED) \ V(QPACK_ENCODER_STREAM_ERROR) \ - V(QPACK_DECODER_STREAM_ERROR) + V(QPACK_DECODER_STREAM_ERROR) \ + V(WT_BUFFERED_STREAM_REJECTED) \ + V(WT_SESSION_GONE) \ + V(WT_ALPN_ERROR) \ + V(WT_REQUIREMENTS_NOT_MET) class QuicError final : public MemoryRetainer { public: diff --git a/src/quic/http3.cc b/src/quic/http3.cc index d77accd10cdc..4a7a597b7d33 100644 --- a/src/quic/http3.cc +++ b/src/quic/http3.cc @@ -21,18 +21,20 @@ namespace node { using v8::Array; +using v8::BigInt; using v8::Global; using v8::Integer; using v8::Local; using v8::LocalVector; +using v8::String; using v8::Value; namespace quic { namespace { constexpr uint8_t kSessionTicketAppDataVersion = 1; -// Layout: [type(1)][version(1)][crc(4)][payload(34)] = 40 bytes -constexpr size_t kSessionTicketAppDataSize = 40; +// Layout: [type(1)][version(1)][crc(4)][payload(35)] = 41 bytes +constexpr size_t kSessionTicketAppDataSize = 41; constexpr size_t kSessionTicketAppDataHeaderSize = 6; // type + version + crc constexpr size_t kSessionTicketAppDataPayloadSize = kSessionTicketAppDataSize - kSessionTicketAppDataHeaderSize; @@ -161,6 +163,13 @@ struct Http3StreamState final : public StreamApplicationState { size_t headers_length = 0; bool wants_headers = false; bool wants_trailers = false; + bool wants_session_id = false; + bool wants_wtsessionclose = false; + int64_t pending_webtransport_session = -1; + // Until is is clear, that this has a session stream, it is kMaxStreamId + // after this it is -1, if it is not a webtransport stream + // and >= 0 it is a webtransport stream. + stream_id session_id = kMaxStreamId; }; // Implements the low-level HTTP/3 Application semantics. @@ -398,6 +407,16 @@ class Http3ApplicationImpl final : public Session::Application { nghttp3_conn_unblock_stream(*this, stream->id()); } + void ExtendMaxData(uint64_t max_data) override { + Debug(&session(), + "HTTP/3 application extending max data to %" PRIu64, + max_data); + for (auto& [id, stream] : session().streams()) { + stream->UpdateWriteDesiredSize(); // the stream might be blocked on js side + // is unblock stream also required? + } + } + void CollectSessionTicketAppData( SessionTicket::AppData* app_data) const override { uint8_t buf[kSessionTicketAppDataSize]; @@ -410,7 +429,9 @@ class Http3ApplicationImpl final : public Session::Application { WriteBE64(payload + 16, options_.qpack_encoder_max_dtable_capacity); WriteBE64(payload + 24, options_.qpack_blocked_streams); payload[32] = options_.enable_connect_protocol ? 1 : 0; + // May be bitfield should be used! payload[33] = options_.enable_datagrams ? 1 : 0; + payload[34] = options_.enable_webtransport ? 1 : 0; uLong crc = crc32(0L, Z_NULL, 0); crc = crc32(crc, payload, kSessionTicketAppDataPayloadSize); @@ -463,32 +484,36 @@ class Http3ApplicationImpl final : public Session::Application { uint64_t stored_qpack_blocked_streams = ReadBE64(payload + 24); bool stored_enable_connect_protocol = payload[32] != 0; bool stored_enable_datagrams = payload[33] != 0; + bool stored_enable_webtransport = payload[34] != 0; Debug(&session(), "Ticket app data: stored mfss=%" PRIu64 " qmdc=%" PRIu64 - " qemdc=%" PRIu64 " qbs=%" PRIu64 " ecp=%d ed=%d", + " qemdc=%" PRIu64 " qbs=%" PRIu64 " ecp=%d ed=%d ew=%d", stored_max_field_section_size, stored_qpack_max_dtable_capacity, stored_qpack_encoder_max_dtable_capacity, stored_qpack_blocked_streams, stored_enable_connect_protocol, - stored_enable_datagrams); + stored_enable_datagrams, + stored_enable_webtransport); Debug(&session(), "Current opts: mfss=%" PRIu64 " qmdc=%" PRIu64 " qemdc=%" PRIu64 - " qbs=%" PRIu64 " ecp=%d ed=%d", + " qbs=%" PRIu64 " ecp=%d ed=%d ew %d", options_.max_field_section_size, options_.qpack_max_dtable_capacity, options_.qpack_encoder_max_dtable_capacity, options_.qpack_blocked_streams, options_.enable_connect_protocol, - options_.enable_datagrams); + options_.enable_datagrams, + options_.enable_webtransport); if (options_.max_field_section_size < stored_max_field_section_size || options_.qpack_max_dtable_capacity < stored_qpack_max_dtable_capacity || options_.qpack_encoder_max_dtable_capacity < stored_qpack_encoder_max_dtable_capacity || options_.qpack_blocked_streams < stored_qpack_blocked_streams || (stored_enable_connect_protocol && !options_.enable_connect_protocol) || - (stored_enable_datagrams && !options_.enable_datagrams)) { + (stored_enable_datagrams && !options_.enable_datagrams) || + (stored_enable_webtransport && !options_.enable_webtransport)) { Debug(&session(), "Ticket app data REJECTED"); return SessionTicket::AppData::Status::TICKET_IGNORE_RENEW; } @@ -545,19 +570,29 @@ class Http3ApplicationImpl final : public Session::Application { bool StreamOpened(Stream& stream) override { auto* state = GetStreamState(stream); - if (state == nullptr || state->pending_headers.empty()) return true; - - decltype(state->pending_headers) pending; - state->pending_headers.swap(pending); - Session::SendPendingDataScope send_scope(&session()); - for (auto& headers : pending) { - if (!SubmitHeaders(stream, - headers->kind, - headers->headers.Get(env()->isolate()), - headers->flags)) { - return false; + if (state == nullptr) return true; + + if (!state->pending_headers.empty()) { + decltype(state->pending_headers) pending; + state->pending_headers.swap(pending); + Session::SendPendingDataScope send_scope(&session()); + for (auto& headers : pending) { + if (!SubmitHeaders(stream, + headers->kind, + headers->headers.Get(env()->isolate()), + headers->flags)) { + return false; + } } } + + if (state->pending_webtransport_session < 0) return true; + + if (!MakeWebtransportStream(stream, + state->pending_webtransport_session)) { + return false; + } + state->pending_webtransport_session = 0; return true; } @@ -599,6 +634,61 @@ class Http3ApplicationImpl final : public Session::Application { state.wants_trailers = wants_trailers; } + void SetWebtransportInterest(Stream& stream, + bool wants_sessionid, + bool wants_wtsessionclose) override { + auto& state = GetOrCreateStreamState(stream); + state.wants_session_id = wants_sessionid; + state.wants_wtsessionclose = wants_wtsessionclose; + } + + bool MakeWebtransportStream(Stream& stream, int64_t sessionid) override { + if (stream.is_pending()) { + Debug(&session(), + "Enqueing Webtransport Session stream for pending stream"); + auto& state = GetOrCreateStreamState(stream); + state.pending_webtransport_session = sessionid; + return true; + } + Session::SendPendingDataScope send_scope(&session()); + static constexpr nghttp3_data_reader reader = {on_read_data_callback}; + const nghttp3_data_reader* reader_ptr = &reader; // can use the same reader + + Debug(&session(), + "Make stream %" PRIu64 " webtransport stream of session %" PRIu64, + stream.id(), + sessionid); + // we only need to do this, if we can send data + if (stream.is_remote_unidirectional()) + return true; // so bail out for remote unidirectional streams + return nghttp3_conn_open_wt_data_stream(*this, + sessionid, + stream.id(), + reader_ptr, + const_cast(&stream)) + == 0; + } + + // closes the webtransort session stream, + // and also closes connect webtransport data streams + // msg is optional + // msg length is maximum 1024 + bool CloseWebtransportSessionStream( + const Stream& stream, + uint32_t wt_error_code, + const uint8_t* msg, + size_t msglen) override { + Session::SendPendingDataScope send_scope(&session()); + Debug(&session(), + "Close webtransport session stream %" PRIu64, + stream.id()); + return nghttp3_conn_close_wt_session(*this, + stream.id(), + wt_error_code, + msg, + msglen) == 0; + } + void SetStreamPriority(const Stream& stream, StreamPriority priority, StreamPriorityFlags flags) override { @@ -756,30 +846,58 @@ class Http3ApplicationImpl final : public Session::Application { case HeadersKind::INITIAL: { static constexpr nghttp3_data_reader reader = {on_read_data_callback}; const nghttp3_data_reader* reader_ptr = nullptr; - if (flags != HeadersFlags::TERMINAL) reader_ptr = &reader; + if (flags != HeadersFlags::TERMINAL + && flags != HeadersFlags::WEBTRANSPORT) { + reader_ptr = &reader; + } if (session().is_server()) { + if (flags != HeadersFlags::WEBTRANSPORT) { + Debug(&session(), + "Submitting %" PRIu64 " response headers for stream %" PRIu64, + nva.length(), + stream.id()); + return nghttp3_conn_submit_response(*this, + stream.id(), + nva.data(), + nva.length(), + reader_ptr) == 0; + } Debug(&session(), - "Submitting %" PRIu64 " response headers for stream %" PRIu64, + "Submitting %" PRIu64 " wt resp. headers for stream %" PRIu64, nva.length(), stream.id()); - return nghttp3_conn_submit_response(*this, + if (nghttp3_conn_submit_wt_response(*this, stream.id(), nva.data(), - nva.length(), - reader_ptr) == 0; + nva.length()) != 0) + return false; + return nghttp3_conn_server_confirm_wt_session(*this, + stream.id(), + 0) == 0; } - - Debug(&session(), - "Submitting %" PRIu64 " request headers for stream %" PRIu64, - nva.length(), - stream.id()); - return nghttp3_conn_submit_request(*this, - stream.id(), - nva.data(), - nva.length(), - reader_ptr, - &stream) == 0; + if (flags != HeadersFlags::WEBTRANSPORT) { + Debug(&session(), + "Submitting %" PRIu64 " request headers for stream %" PRIu64, + nva.length(), + stream.id()); + return nghttp3_conn_submit_request(*this, + stream.id(), + nva.data(), + nva.length(), + reader_ptr, + &stream) == 0; + } + Debug(&session(), + "Submitting %" PRIu64 " wt req. headers for stream %" PRIu64, + nva.length(), + stream.id()); + return nghttp3_conn_submit_wt_request(*this, + stream.id(), + nva.data(), + nva.length(), + const_cast(&stream)) + == 0; } case HeadersKind::TRAILING: { Debug(&session(), @@ -824,6 +942,21 @@ class Http3ApplicationImpl final : public Session::Application { return true; } + void NotifyWTSession(Stream& stream, stream_id session_id) { + auto& state = GetOrCreateStreamState(stream); + if (state.session_id != session_id) { + state.session_id = session_id; + EmitSessionid(stream, session_id); + } + } + + void NotifyWTSessionClose(Stream& stream, + uint32_t wt_error_code, + const uint8_t* msg, + size_t msglen) { + EmitWTSessionClose(stream, wt_error_code, msg, msglen); + } + void EmitHeaders(Stream& stream) { auto& state = GetOrCreateStreamState(stream); stream.RecordReceivedActivity(); @@ -858,6 +991,36 @@ class Http3ApplicationImpl final : public Session::Application { binding.stream_headers_callback(), arraysize(argv), argv); } + void EmitSessionid(Stream& stream, stream_id session_id) { + auto* state = GetStreamState(stream); + if (!env()->can_call_into_js() || !state->wants_session_id) { + return; + } + CallbackScope cb_scope(&stream); + auto& binding = BindingData::Get(env()); + Local sid = BigInt::New(env()->isolate(), session_id); + stream.MakeCallback( + binding.stream_sessionid_callback(), 1, &sid); + } + + void EmitWTSessionClose(Stream& stream, + uint32_t wt_error_code, + const uint8_t* msg, + size_t msglen) { + if (!env()->can_call_into_js()) return; + CallbackScope cb_scope(&stream); + auto& binding = BindingData::Get(env()); + Local argv[] = { + Integer::NewFromUnsigned(env()->isolate(), + wt_error_code), + String::NewFromUtf8(env()->isolate(), + reinterpret_cast(msg), + v8::NewStringType::kNormal, msglen).ToLocalChecked() + }; + stream.MakeCallback(binding.stream_wtsessionclose_callback(), + arraysize(argv), argv); +} + void EmitWantTrailers(Stream& stream) { auto* state = GetStreamState(stream); if (!env()->can_call_into_js() || state == nullptr || @@ -1105,6 +1268,7 @@ class Http3ApplicationImpl final : public Session::Application { void OnReceiveSettings(const nghttp3_proto_settings* settings) { options_.enable_connect_protocol = settings->enable_connect_protocol; options_.enable_datagrams = settings->h3_datagram; + options_.enable_webtransport = settings->wt_enabled; options_.max_field_section_size = settings->max_field_section_size; options_.qpack_blocked_streams = settings->qpack_blocked_streams; options_.qpack_max_dtable_capacity = settings->qpack_max_dtable_capacity; @@ -1344,6 +1508,83 @@ class Http3ApplicationImpl final : public Session::Application { return NGHTTP3_ERR_CALLBACK_FAILURE; } + static int on_receive_wt_data(nghttp3_conn* conn, + int64_t session_id, + int64_t stream_id, + const uint8_t* data, + size_t datalen, + void* conn_user_data, + void* stream_user_data) { + NGHTTP3_CALLBACK_SCOPE(app); + + // A cached Stream* is cleared before the Stream is removed from the + // session, non-null here means the stream is good to go. + if (auto* cached = static_cast(stream_user_data)) [[likely]] { + BaseObjectPtr stream(cached); + stream->ReceiveData(data, datalen, Stream::ReceiveDataFlags{}); + return NGTCP2_SUCCESS; + } + + auto& session = app.session(); + + // DATA frames for a wt stream the application already destroyed can + // still arrive. Drop the payload rather than resurrecting the stream or + // tearing down the connection, but return its credit: unlike framing + // bytes, DATA payload is not included in the count nghttp3 reports to + // ReceiveStreamData, so we own it. The is_destroyed() check must come + // first, see DefaultApplication::ReceiveStreamData. + if (!session.is_destroyed() && !session.FindStream(stream_id) && + ngtcp2_conn_is_local_stream(session, stream_id)) { + Debug(&session, + "HTTP/3 discarding %zu bytes" + " for destroyed wt local stream %" PRIi64, + datalen, + stream_id); + app.ReturnConnectionCredit(datalen); + return NGTCP2_SUCCESS; + } + + if (auto stream = app.FindOrCreateStream(stream_id)) [[likely]] { + stream->ReceiveData(data, datalen, Stream::ReceiveDataFlags{}); + return NGTCP2_SUCCESS; + } + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + + static int on_wt_data_stream_open(nghttp3_conn* conn, + int64_t session_id, + int64_t stream_id, + void* conn_user_data, + void* stream_user_data) { + NGHTTP3_CALLBACK_SCOPE(app); + + if (auto stream = app.FindOrCreateStream(stream_id)) [[likely]] { + if (!app.MakeWebtransportStream(*stream.get(), session_id)) { + stream->Destroy(); // close stream forcefully, + // may be use an assert instead? + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + app.NotifyWTSession(*stream.get(), session_id); + return NGTCP2_SUCCESS; + } + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + + static int on_recv_wt_close_session(nghttp3_conn* conn, + int64_t session_id, + uint32_t wt_error_code, + const uint8_t* msg, + size_t msglen, + void* conn_user_data, + void* stream_user_data) { + NGHTTP3_CALLBACK_SCOPE(app); + if (auto stream = app.FindOrCreateStream(session_id)) [[likely]] { + app.NotifyWTSessionClose(*stream.get(), wt_error_code, msg, msglen); + return NGTCP2_SUCCESS; + } + return NGHTTP3_ERR_CALLBACK_FAILURE; + } + static int on_deferred_consume(nghttp3_conn* conn, stream_id id, size_t consumed, @@ -1546,7 +1787,10 @@ class Http3ApplicationImpl final : public Session::Application { on_receive_settings, // We don't have to listen for stream_close - nghttp3 only closes when // ReceiveStreamClose requests it, when we've already handled this. - nullptr}; + nullptr, + on_receive_wt_data, + on_wt_data_stream_open, + on_recv_wt_close_session}; }; std::unique_ptr CreateHttp3Application( diff --git a/src/quic/session.cc b/src/quic/session.cc index 66899f43ad68..f53af85f0dd1 100644 --- a/src/quic/session.cc +++ b/src/quic/session.cc @@ -1381,6 +1381,14 @@ struct Session::Impl final : public MemoryRetainer { return NGTCP2_SUCCESS; } + static int on_extend_max_data(ngtcp2_conn* conn, + uint64_t max_data, + void* user_data) { + NGTCP2_CALLBACK_SCOPE(session) + session->application().ExtendMaxData(max_data); + return NGTCP2_SUCCESS; + } + static int on_get_new_cid(ngtcp2_conn* conn, ngtcp2_cid* cid, ngtcp2_stateless_reset_token* token, @@ -1727,11 +1735,11 @@ struct Session::Impl final : public MemoryRetainer { ngtcp2_crypto_get_path_challenge_data2_cb, on_receive_stream_stop_sending, #ifdef NGTCP2_CALLBACKS_V5 - nullptr, // stream_close2 -#endif + nullptr, #ifdef NGTCP2_CALLBACKS_V6 - nullptr, // extend_max_data + on_extend_max_data, #endif +#endif // NGTCP2_CALLBACKS_V5 }; static constexpr ngtcp2_callbacks SERVER = { @@ -1782,11 +1790,11 @@ struct Session::Impl final : public MemoryRetainer { ngtcp2_crypto_get_path_challenge_data2_cb, on_receive_stream_stop_sending, #ifdef NGTCP2_CALLBACKS_V5 - nullptr, // stream_close2 -#endif + nullptr, #ifdef NGTCP2_CALLBACKS_V6 - nullptr, // extend_max_data + on_extend_max_data, #endif +#endif // NGTCP2_CALLBACKS_V5 }; }; diff --git a/src/quic/session.h b/src/quic/session.h index 941351c7b7b3..21605e7b2111 100644 --- a/src/quic/session.h +++ b/src/quic/session.h @@ -84,6 +84,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source { bool enable_connect_protocol = true; bool enable_datagrams = true; + // for a client always enabling wt, may be good + bool enable_webtransport = false; operator const nghttp3_settings() const; diff --git a/src/quic/streams.cc b/src/quic/streams.cc index f29bab507a40..6656fc3940c7 100644 --- a/src/quic/streams.cc +++ b/src/quic/streams.cc @@ -821,7 +821,6 @@ class Stream::Outbound final : public MemoryRetainer { PullUncommitted(std::move(next)); return bob::Status::STATUS_CONTINUE; } - std::move(next)(bob::Status::STATUS_BLOCK, nullptr, 0, [](int) {}); return bob::Status::STATUS_BLOCK; } @@ -1901,7 +1900,9 @@ void Stream::EmitStopSending(const QuicError& error) { void Stream::Schedule(Queue* queue) { // If this stream is not already in the queue to send data, add it. Debug(this, "Scheduled"); - if (outbound_ && stream_queue_.IsEmpty()) queue->PushBack(this); + if (outbound_ && stream_queue_.IsEmpty()) { + queue->PushBack(this); + } } void Stream::Unschedule() { diff --git a/src/quic/streams.h b/src/quic/streams.h index 0264feccb482..9ec94ba3b6cb 100644 --- a/src/quic/streams.h +++ b/src/quic/streams.h @@ -295,6 +295,9 @@ class Stream final : public AsyncWrap, // True if a Blob::Reader has been created for the inbound data. bool has_reader() const; + bool is_local_unidirectional() const; + bool is_remote_unidirectional() const; + // Returns the Blob::Reader for the inbound data, or nullptr. Blob::Reader* reader() const; @@ -435,8 +438,6 @@ class Stream final : public AsyncWrap, void WriteStreamData(const v8::FunctionCallbackInfo& args); void EndWriting(); - bool is_local_unidirectional() const; - bool is_remote_unidirectional() const; void ReleaseArenaSlots(); // JavaScript callouts @@ -468,6 +469,7 @@ class Stream final : public AsyncWrap, // When a pending stream is finally opened, the NotifyStreamOpened method // will be called and the id will be assigned. void NotifyStreamOpened(stream_id id); + ArenaSlotBase stats_slot_; ArenaSlotBase state_slot_; BaseObjectWeakPtr session_; diff --git a/test/parallel/test-quic-h3-maxdata-external-buffer-failure.mjs b/test/parallel/test-quic-h3-maxdata-external-buffer-failure.mjs new file mode 100644 index 000000000000..e93a2cf173a8 --- /dev/null +++ b/test/parallel/test-quic-h3-maxdata-external-buffer-failure.mjs @@ -0,0 +1,76 @@ +// Flags: --experimental-quic --experimental-stream-iter --no-warnings + +// Test: Quic maxdata updates on http/3 +// Client sends a body that precisely fills the session window size, +// and verifies that it is data transfer is not stalled. + +import { hasQuic, skip } from '../common/index.mjs'; +import { readFile } from 'node:fs/promises'; +import { setTimeout as sleep } from 'node:timers/promises'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} +const { listen, connect } = await import('node:quic'); +const { createPrivateKey } = await import('node:crypto'); +const { drainableProtocol } = await import('stream/iter'); + +const keys = 'test/fixtures/keys'; +const key = createPrivateKey(await readFile(`${keys}/agent1-key.pem`)); +const cert = await readFile(`${keys}/agent1-cert.pem`); + +const WINDOW = 4096; +// Fills the window exactly: +// considers all framing including some initial session capsules +const BODY = WINDOW - 38; + +let letServerRead; +const serverMayRead = new Promise((resolve) => { letServerRead = resolve; }); + +const endpoint = await listen((session) => { + session.onstream = async (stream) => { + await serverMayRead; + // eslint-disable-next-line no-unused-vars + for await (const _ of stream) { /* reading extends the window */ } + }; +}, { + sni: { '*': { keys: [key], certs: [cert] } }, + transportParams: { + initialMaxStreamDataBidiRemote: 1024 * 1024, // Make sure maxstreamdata does not block + initialMaxData: WINDOW, + }, + onheaders() { this.sendHeaders({ ':status': '200' }); }, +}); + +const session = await connect(endpoint.address, { + servername: 'localhost', + verifyPeer: 'manual', +}); +await session.opened; + +// Budget well above the window, so the window is what stops the writer. +const stream = await session.createBidirectionalStream({ budget: 1024 * 1024 }); +stream.sendHeaders({ + ':method': 'POST', + ':path': '/', + ':scheme': 'https', + ':authority': 'localhost', +}, { terminal: false }); + +const writer = stream.writer; +writer.writeSync(new Uint8Array(BODY)); + +// Long enough for every byte to be acked. The peer acks as data arrives, +// whether or not its application has read any of it, so by now the window is +// exhausted, the send buffer is empty, and no further ACK can arrive. +await sleep(500); + +const watchdog = setTimeout(() => { + console.error('STALLED: no drain after MAX_STREAM_DATA'); + process.exit(1); +}, 5000); +letServerRead(); // Extend the window, with no ack attached +await writer[drainableProtocol](); + +clearTimeout(watchdog); +process.exit(0); diff --git a/test/parallel/test-quic-h3-settings.mjs b/test/parallel/test-quic-h3-settings.mjs index 733d3865e872..01b3e9edf9fe 100644 --- a/test/parallel/test-quic-h3-settings.mjs +++ b/test/parallel/test-quic-h3-settings.mjs @@ -5,6 +5,7 @@ // maxHeaderLength enforcement - reject headers exceeding byte length // enableConnectProtocol setting (accepted without error) // enableDatagrams setting (accepted without error) +// enableWebtransport setting (accepted without error) import { hasQuic, skip, mustCall } from '../common/index.mjs'; import assert from 'node:assert'; diff --git a/test/parallel/test-quic-h3-wt-bidi-from-client.mjs b/test/parallel/test-quic-h3-wt-bidi-from-client.mjs new file mode 100644 index 000000000000..c42c2698f9c4 --- /dev/null +++ b/test/parallel/test-quic-h3-wt-bidi-from-client.mjs @@ -0,0 +1,224 @@ +// Flags: --experimental-quic --experimental-stream-iter --no-warnings + +// Test: HTTP/3 webtransport client initiated bidi stream +// Client creates a bidi stream and send data that is echoed back to the client + +import { hasQuic, skip, mustCall, mustNotCall, mustCallAtLeast } from '../common/index.mjs'; +import assert from 'node:assert'; +import * as fixtures from '../common/fixtures.mjs'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} + +const { listen, connect } = await import('node:quic'); +const { createPrivateKey } = await import('node:crypto'); +const { drainableProtocol: dp } = await import('stream/iter'); + +const key = createPrivateKey(fixtures.readKey('agent1-key.pem')); +const cert = fixtures.readKey('agent1-cert.pem'); + +const chunkSizes = [60000, 12, 1000000, 50000, 1600, 20000, 1000000, 30000, 0, 100]; +const numChunks = chunkSizes.length; +const byteLength = chunkSizes.reduce((accumulator, currentValue) => accumulator + currentValue, 0); + + +// Build a deterministic payload so we can verify integrity. +function buildChunk(index) { + const chunk = new Uint8Array(chunkSizes[index]); + // Fill with a pattern derived from the chunk index. + const val = index & 0xff; + for (let i = 0; i < chunkSizes[index]; i++) { + chunk[i] = (val + i) & 0xff; + } + return chunk; +} + +function checksum(data) { + let sum = 0; + for (let i = 0; i < data.byteLength; i++) { + sum = (sum + data[i]) | 0; + } + return sum; +} + +// Compute expected checksum. +let expectedChecksum = 0; +for (let i = 0; i < numChunks; i++) { + const chunk = buildChunk(i); + expectedChecksum = (expectedChecksum + checksum(chunk)) | 0; +} + +const serverSessionOpened = Promise.withResolvers(); + + +const serverEndpoint = await listen(mustCall(async (ss) => { + ss.onapplication = mustCall((aopts) => { + assert.strictEqual(!aopts.enableDatagrams, false); + }); + ss.onhandshake = mustCall(function() { + assert.strictEqual(BigInt(this?.remoteTransportParams?.maxDatagramFrameSize) > 0, true); + }); + ss.onstream = mustCallAtLeast((stream) => { + stream.onsessionid = mustCallAtLeast(async (sessionid) => { + // Deinstall handlers + stream.onheaders = undefined; + stream.onsessionid = undefined; + const sessionid2 = await serverSessionOpened.promise; + assert.strictEqual(sessionid, sessionid2); + // Now we can echo all data. + const writer = stream.writer; + for await (const chunks of stream) { + for (const chunk of chunks) { + while (!writer.writeSync(chunk)) { + // Flow controlled — wait for drain before retrying. + const drainable = writer[dp](); + if (drainable) await drainable; + } + } + } + writer.end(); + await stream.closed; + }, stream.id !== 0n ? 1 : 0); // The second stream is a datastream + }, 2); +}), { + sni: { '*': { keys: [key], certs: [cert] } }, + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { + maxDatagramFrameSize: 1000, + initialMaxStreamsBidi: 100, // Default value according to spec + initialMaxStreamsUni: 100, // Especially important as limit default is 0 + }, + onheaders: mustCall(function(headers) { + try { + assert.strictEqual(headers[':scheme'], 'https'); + assert.strictEqual(headers[':method'], 'CONNECT'); + assert.strictEqual(headers[':protocol'], 'webtransport'); // depends on the draft + assert.strictEqual(headers[':path'], '/testwtpath'); + // We could also check for wt-available protocols + this.sendHeaders( + { ':status': '200' }, + { terminal: false, webtransport: true } + ); + serverSessionOpened.resolve(this.id); + } catch (error) { + serverSessionOpened.reject(error); + } + // Should only be installed on wt streams + this.onwtsessionclose = mustCall((code, reason) => { + assert.strictEqual(code, 200); + assert.strictEqual(reason, 'all perfect'); + this.session.close(); + }); + }), +}); + +const clientSession = await connect(serverEndpoint.address, { + servername: 'localhost', + verifyPeer: 'manual', + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { + maxDatagramFrameSize: 1000, + initialMaxStreamsBidi: 100, // Default value according to spec + initialMaxStreamsUni: 100, // Especially important as limit default is 0 + }, +}); + +const webtransportSupport = Promise.withResolvers(); + +clientSession.onapplication = mustCall((aopts) => { + try { + // Test for webtransport support + assert.strictEqual(aopts.enableConnectProtocol, true); + assert.strictEqual(aopts.enableDatagrams, true); + assert.strictEqual(aopts.enableWebtransport, true); + // Ok we have wt support + webtransportSupport.resolve(); + } catch (error) { + webtransportSupport.reject(error); + } +}); + +clientSession.onstream = mustNotCall((stream) => { + stream.onheaders = mustNotCall((stream) => { + // Well this should not happen on client side + console.log('Called onheaders on the client side!'); + }); +}); + +await clientSession.opened; +await webtransportSupport.promise; +// Now we open a webtransport session, which is actually +// a special bidirectional stream +const wtSessionStream = await clientSession.createBidirectionalStream({ + body: '', +}); +wtSessionStream.sendHeaders({ + ':method': 'CONNECT', + ':scheme': 'https', + // This one depends on draft, draft14 says "webtransport", draft15 says "webtransport-h3" + ':protocol': 'webtransport', + ':path': '/testwtpath', + ':authority': 'testserver:' + serverEndpoint.address.port +}, { + webtransport: true // Tell nghttp3 to treat the stream as a WT session stream +}); + +// Well let's get a bidi stream and send something +const clientBidiStream = await clientSession.createBidirectionalStream({ + incremental: true, + webtransportSession: wtSessionStream // Associate it with the sessionStream +}); +// Next step send some data + + +const readFromStream = mustCallAtLeast(async (stream) => { + const readChunks = []; + for await (const chunks of stream) { + readChunks.push(...chunks); + } + const receivedBytes = readChunks.reduce((accu, curVal) => accu + curVal.byteLength, 0); + + assert.strictEqual(receivedBytes, byteLength); + let receivedChecksum = 0; + for (const chunk of readChunks) { + receivedChecksum = (receivedChecksum + checksum(chunk)) | 0; + } + assert.strictEqual(receivedChecksum, expectedChecksum); +}, 1); + + +const writeToStream = mustCallAtLeast(async (stream) => { + const w = stream.writer; + for (let i = 0; i < numChunks; i++) { + const chunk = buildChunk(i); + while (!w.writeSync(chunk)) { + // Flow controlled — wait for drain before retrying. + const drainable = w[dp](); + if (drainable) await drainable; + } + } + w.endSync(); +}, 1); + +await serverSessionOpened.promise; + +await Promise.all([readFromStream(clientBidiStream), writeToStream(clientBidiStream)]); +await wtSessionStream.closeWebtransportSessionStream(200, 'all perfect'); + +try { + await wtSessionStream.closed; +} catch (error) { + assert.strictEqual(error.errorCode, 200n); + assert.strictEqual(error.reason, 'all perfect'); +} +await clientSession.close(); +await serverEndpoint.close(); diff --git a/test/parallel/test-quic-h3-wt-bidi-from-server.mjs b/test/parallel/test-quic-h3-wt-bidi-from-server.mjs new file mode 100644 index 000000000000..24b44a45dbaf --- /dev/null +++ b/test/parallel/test-quic-h3-wt-bidi-from-server.mjs @@ -0,0 +1,227 @@ +// Flags: --experimental-quic --experimental-stream-iter --no-warnings + +// Test: HTTP/3 webtransport client initiated bidi stream +// Server creates a bidi stream and client send data that is echoed back to the client + +import { hasQuic, skip, mustCall, mustNotCall, mustCallAtLeast } from '../common/index.mjs'; +import assert from 'node:assert'; +import * as fixtures from '../common/fixtures.mjs'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} + +const { listen, connect } = await import('node:quic'); +const { createPrivateKey } = await import('node:crypto'); +const { drainableProtocol: dp } = await import('stream/iter'); + +const key = createPrivateKey(fixtures.readKey('agent1-key.pem')); +const cert = fixtures.readKey('agent1-cert.pem'); + +const chunkSizes = [60000, 12, 1000000, 50000, 1600, 20000, 1000000, 30000, 0, 100]; +const numChunks = chunkSizes.length; +const byteLength = chunkSizes.reduce((accumulator, currentValue) => accumulator + currentValue, 0); + + +// Build a deterministic payload so we can verify integrity. +function buildChunk(index) { + const chunk = new Uint8Array(chunkSizes[index]); + // Fill with a pattern derived from the chunk index. + const val = index & 0xff; + for (let i = 0; i < chunkSizes[index]; i++) { + chunk[i] = (val + i) & 0xff; + } + return chunk; +} + +function checksum(data) { + let sum = 0; + for (let i = 0; i < data.byteLength; i++) { + sum = (sum + data[i]) | 0; + } + return sum; +} + +// Compute expected checksum. +let expectedChecksum = 0; +for (let i = 0; i < numChunks; i++) { + const chunk = buildChunk(i); + expectedChecksum = (expectedChecksum + checksum(chunk)) | 0; +} + +const serverSessionOpened = Promise.withResolvers(); + + +const serverEndpoint = await listen(mustCall(async (ss) => { + ss.onapplication = mustCall((aopts) => { + assert.strictEqual(!aopts.enableDatagrams, false); + }); + ss.onhandshake = mustCall(function() { + assert.strictEqual(BigInt(this?.remoteTransportParams?.maxDatagramFrameSize) > 0, true); + }); + ss.onstream = mustCall((stream) => { + stream.onsessionid = mustNotCall(async (sessionid) => { + console.log('No client initiated stream expected!'); + }); + }); +}), { + sni: { '*': { keys: [key], certs: [cert] } }, + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { + maxDatagramFrameSize: 1000, + initialMaxStreamsBidi: 100, // Default value according to spec + initialMaxStreamsUni: 100, // Especially important as limit default is 0 + }, + onheaders: mustCall(async function(headers) { + try { + assert.strictEqual(headers[':scheme'], 'https'); + assert.strictEqual(headers[':method'], 'CONNECT'); + assert.strictEqual(headers[':protocol'], 'webtransport'); // depends on the draft + assert.strictEqual(headers[':path'], '/testwtpath'); + // We could also check for wt-available protocols + this.sendHeaders( + { ':status': '200' }, + { terminal: false, webtransport: true } + ); + serverSessionOpened.resolve(this.id); + } catch (error) { + serverSessionOpened.reject(error); + } + // Should only be installed on wt streams + this.onwtsessionclose = mustCall((code, reason) => { + assert.strictEqual(code, 200); + assert.strictEqual(reason, 'all perfect'); + this.session.close(); + }); + + // Well let's get a bidi stream and echo all + const serverBidiStream = await this.session.createBidirectionalStream({ + incremental: true, + webtransportSession: this // Associate it with the sessionStream + }); + // Now we can echo all data. + const writer = serverBidiStream.writer; + for await (const chunks of serverBidiStream) { + for (const chunk of chunks) { + while (!writer.writeSync(chunk)) { + // Flow controlled — wait for drain before retrying. + const drainable = writer[dp](); + if (drainable) await drainable; + } + } + } + writer.end(); + }), +}); + +const clientSession = await connect(serverEndpoint.address, { + servername: 'localhost', + verifyPeer: 'manual', + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { + maxDatagramFrameSize: 1000, + initialMaxStreamsBidi: 100, // Default value according to spec + initialMaxStreamsUni: 100, // Especially important as limit default is 0 + }, +}); + +const webtransportSupport = Promise.withResolvers(); + +clientSession.onapplication = mustCall((aopts) => { + try { + // Test for webtransport support + assert.strictEqual(aopts.enableConnectProtocol, true); + assert.strictEqual(aopts.enableDatagrams, true); + assert.strictEqual(aopts.enableWebtransport, true); + // Ok we have wt support + webtransportSupport.resolve(); + } catch (error) { + webtransportSupport.reject(error); + } +}); + +clientSession.onstream = mustNotCall((stream) => { + stream.onheaders = mustNotCall((stream) => { + // Well this should not happen on client side + console.log('Called onheaders on the client side!'); + }); +}); + +await clientSession.opened; +await webtransportSupport.promise; + +clientSession.onstream = mustCall((stream) => { + stream.onsessionid = mustCall(async (sessionid) => { + assert.strictEqual(sessionid, wtSessionStream.id); + await Promise.all([readFromStream(stream), writeToStream(stream)]); + await wtSessionStream.closeWebtransportSessionStream(200, 'all perfect'); + }); +}); + +// Now we open a webtransport session, which is actually +// a special bidirectional stream +const wtSessionStream = await clientSession.createBidirectionalStream({ + body: '', +}); +wtSessionStream.sendHeaders({ + ':method': 'CONNECT', + ':scheme': 'https', + // This one depends on draft, draft14 says "webtransport", draft15 says "webtransport-h3" + ':protocol': 'webtransport', + ':path': '/testwtpath', + ':authority': 'testserver:' + serverEndpoint.address.port +}, { + webtransport: true // Tell nghttp3 to treat the stream as a WT session stream +}); + + +// Next step send some data + + +const readFromStream = mustCallAtLeast(async (stream) => { + const readChunks = []; + for await (const chunks of stream) { + readChunks.push(...chunks); + } + const receivedBytes = readChunks.reduce((accu, curVal) => accu + curVal.byteLength, 0); + + assert.strictEqual(receivedBytes, byteLength); + let receivedChecksum = 0; + for (const chunk of readChunks) { + receivedChecksum = (receivedChecksum + checksum(chunk)) | 0; + } + assert.strictEqual(receivedChecksum, expectedChecksum); +}, 1); + + +const writeToStream = mustCallAtLeast(async (stream) => { + const w = stream.writer; + for (let i = 0; i < numChunks; i++) { + const chunk = buildChunk(i); + while (!w.writeSync(chunk)) { + // Flow controlled — wait for drain before retrying. + const drainable = w[dp](); + if (drainable) await drainable; + } + } + w.endSync(); +}, 1); + +await serverSessionOpened.promise; + +try { + await wtSessionStream.closed; +} catch (error) { + assert.strictEqual(error.errorCode, 200n); + assert.strictEqual(error.reason, 'all perfect'); +} +await clientSession.close(); +await serverEndpoint.close(); diff --git a/test/parallel/test-quic-h3-wt-session.mjs b/test/parallel/test-quic-h3-wt-session.mjs new file mode 100644 index 000000000000..fb61e321adac --- /dev/null +++ b/test/parallel/test-quic-h3-wt-session.mjs @@ -0,0 +1,127 @@ +// Flags: --experimental-quic --experimental-stream-iter --no-warnings + +// Test: HTTP/3 webtransport session establishment +// Verifies that a session established and properly closed + +import { hasQuic, skip, mustCall, mustNotCall } from '../common/index.mjs'; +import assert from 'node:assert'; +import * as fixtures from '../common/fixtures.mjs'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} + +const { listen, connect } = await import('node:quic'); +const { createPrivateKey } = await import('node:crypto'); +const { setTimeout: sleep } = await import('timers/promises'); + +const key = createPrivateKey(fixtures.readKey('agent1-key.pem')); +const cert = fixtures.readKey('agent1-cert.pem'); + +const serverSessionOpened = Promise.withResolvers(); + + +const serverEndpoint = await listen(mustCall(async (ss) => { + ss.onstream = mustCall(async (stream) => { + console.log('Does nothing but is required'); + }); + ss.onapplication = mustCall((aopts) => { + assert.strictEqual(!aopts.enableDatagrams, false); + }); + ss.onhandshake = mustCall(function() { + assert.strictEqual(BigInt(this?.remoteTransportParams?.maxDatagramFrameSize) > 0, true); + }); +}), { + sni: { '*': { keys: [key], certs: [cert] } }, + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { maxDatagramFrameSize: 100 }, + onheaders: mustCall(function(headers) { + try { + assert.strictEqual(headers[':scheme'], 'https'); + assert.strictEqual(headers[':method'], 'CONNECT'); + assert.strictEqual(headers[':protocol'], 'webtransport'); // depends on the draft + assert.strictEqual(headers[':path'], '/testwtpath'); + // We could also check for wt-available protocols + this.sendHeaders( + { ':status': '200' }, + { terminal: false, webtransport: true } + ); + serverSessionOpened.resolve(); + } catch (error) { + serverSessionOpened.reject(error); + } + // Should only be installed on wt streams + this.onwtsessionclose = mustCall((code, reason) => { + assert.strictEqual(code, 200); + assert.strictEqual(reason, 'all perfect'); + this.session.close(); + }); + }), +}); + +const clientSession = await connect(serverEndpoint.address, { + servername: 'localhost', + verifyPeer: 'manual', + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { maxDatagramFrameSize: 1000 }, +}); + +const webtransportSupport = Promise.withResolvers(); + +clientSession.onapplication = mustCall((aopts) => { + try { + // Test for webtransport support + assert.strictEqual(aopts.enableConnectProtocol, true); + assert.strictEqual(aopts.enableDatagrams, true); + assert.strictEqual(aopts.enableWebtransport, true); + // Ok we have wt support + webtransportSupport.resolve(); + } catch (error) { + webtransportSupport.reject(error); + } +}); + +clientSession.onstream = mustNotCall((stream) => { + stream.onheaders = mustNotCall((stream) => { + // Well this should not happen on client side + console.log('Called forbidden onheaders!'); + }); +}); + +await clientSession.opened; +await webtransportSupport.promise; +// Now we open a webtransport session, which is actually +// a special bidirectional stream +const wtSessionStream = await clientSession.createBidirectionalStream({ + body: '', +}); +wtSessionStream.sendHeaders({ + ':method': 'CONNECT', + ':scheme': 'https', + // This one depends on draft, draft14 says "webtransport", draft15 says "webtransport-h3" + ':protocol': 'webtransport', + ':path': '/testwtpath', + ':authority': 'testserver:' + serverEndpoint.address.port +}, { + webtransport: true // Tell nghttp3 to treat the stream as a WT session stream +}); +await serverSessionOpened.promise; +await sleep(500); +await wtSessionStream.closeWebtransportSessionStream(200, 'all perfect'); +try { + await wtSessionStream.closed; +} catch (error) { + assert.strictEqual(error.errorCode, 200n); + assert.strictEqual(error.reason, 'all perfect'); +} + +await clientSession.close(); +await serverEndpoint.close(); diff --git a/test/parallel/test-quic-h3-wt-unidi-from-client.mjs b/test/parallel/test-quic-h3-wt-unidi-from-client.mjs new file mode 100644 index 000000000000..184c4aeb0dc0 --- /dev/null +++ b/test/parallel/test-quic-h3-wt-unidi-from-client.mjs @@ -0,0 +1,207 @@ +// Flags: --experimental-quic --experimental-stream-iter --no-warnings + +// Test: HTTP/3 webtransport client initiated uni stream +// Client creates an uni stream and send data to the server + +import { hasQuic, skip, mustCall, mustNotCall, mustCallAtLeast } from '../common/index.mjs'; +import assert from 'node:assert'; +import * as fixtures from '../common/fixtures.mjs'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} + +const { listen, connect } = await import('node:quic'); +const { createPrivateKey } = await import('node:crypto'); +const { drainableProtocol: dp } = await import('stream/iter'); + +const key = createPrivateKey(fixtures.readKey('agent1-key.pem')); +const cert = fixtures.readKey('agent1-cert.pem'); + +const chunkSizes = [60000, 12, 1000000, 50000, 1600, 20000, 1000000, 30000, 0, 100]; +const numChunks = chunkSizes.length; +const byteLength = chunkSizes.reduce((accumulator, currentValue) => accumulator + currentValue, 0); + + +// Build a deterministic payload so we can verify integrity. +function buildChunk(index) { + const chunk = new Uint8Array(chunkSizes[index]); + // Fill with a pattern derived from the chunk index. + const val = index & 0xff; + for (let i = 0; i < chunkSizes[index]; i++) { + chunk[i] = (val + i) & 0xff; + } + return chunk; +} + +function checksum(data) { + let sum = 0; + for (let i = 0; i < data.byteLength; i++) { + sum = (sum + data[i]) | 0; + } + return sum; +} + +// Compute expected checksum. +let expectedChecksum = 0; +for (let i = 0; i < numChunks; i++) { + const chunk = buildChunk(i); + expectedChecksum = (expectedChecksum + checksum(chunk)) | 0; +} + +const serverSessionOpened = Promise.withResolvers(); + +const readChunks = []; +let serverSessionStream; +const serverEndpoint = await listen(mustCall(async (ss) => { + ss.onapplication = mustCall((aopts) => { + assert.strictEqual(!aopts.enableDatagrams, false); + }); + ss.onhandshake = mustCall(function() { + assert.strictEqual(BigInt(this?.remoteTransportParams?.maxDatagramFrameSize) > 0, true); + }); + ss.onstream = mustCallAtLeast((stream) => { + stream.onsessionid = mustCallAtLeast(async (sessionid) => { + // Deinstall handlers + stream.onheaders = undefined; + stream.onsessionid = undefined; + const sessionid2 = await serverSessionOpened.promise; + assert.strictEqual(sessionid, sessionid2); + // Now we get the data + for await (const chunks of stream) { + readChunks.push(...chunks); + } + const receivedBytes = readChunks.reduce((accu, curVal) => accu + curVal.byteLength, 0); + + assert.strictEqual(receivedBytes, byteLength); + let receivedChecksum = 0; + for (const chunk of readChunks) { + receivedChecksum = (receivedChecksum + checksum(chunk)) | 0; + } + assert.strictEqual(receivedChecksum, expectedChecksum); + serverSessionStream.closed.catch(mustCall((error) => { + assert.strictEqual(error.errorCode, 200n); + assert.strictEqual(error.reason, 'all perfect'); + })); + await serverSessionStream.closeWebtransportSessionStream(200, 'all perfect'); + }, stream.id !== 0n ? 1 : 0); // The second stream is a datastream + }, 2); +}), { + sni: { '*': { keys: [key], certs: [cert] } }, + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { + maxDatagramFrameSize: 100, + initialMaxStreamsBidi: 100, // Default value according to spec + initialMaxStreamsUni: 100, // Especially important as limit default is 0 + }, + onheaders: mustCall(function(headers) { + try { + assert.strictEqual(headers[':scheme'], 'https'); + assert.strictEqual(headers[':method'], 'CONNECT'); + assert.strictEqual(headers[':protocol'], 'webtransport'); // depends on the draft + assert.strictEqual(headers[':path'], '/testwtpath'); + // We could also check for wt-available protocols + this.sendHeaders( + { ':status': '200' }, + { terminal: false, webtransport: true } + ); + serverSessionOpened.resolve(this.id); + serverSessionStream = this; + } catch (error) { + serverSessionOpened.reject(error); + } + }), +}); + +const clientSession = await connect(serverEndpoint.address, { + servername: 'localhost', + verifyPeer: 'manual', + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { + maxDatagramFrameSize: 1000, + initialMaxStreamsBidi: 100, // Default value according to spec + initialMaxStreamsUni: 100, // Especially important as limit default is 0 + }, +}); + +const webtransportSupport = Promise.withResolvers(); + +clientSession.onapplication = mustCall((aopts) => { + try { + // Test for webtransport support + assert.strictEqual(aopts.enableConnectProtocol, true); + assert.strictEqual(aopts.enableDatagrams, true); + assert.strictEqual(aopts.enableWebtransport, true); + // Ok we have wt support + webtransportSupport.resolve(); + } catch (error) { + webtransportSupport.reject(error); + } +}); + +clientSession.onstream = mustNotCall((stream) => { + stream.onheaders = mustNotCall((stream) => { + // Well this should not happen on client side + console.log('Called onheaders on the client side!'); + }); +}); + +await clientSession.opened; +await webtransportSupport.promise; +// Now we open a webtransport session, which is actually +// a special bidirectional stream +const wtSessionStream = await clientSession.createBidirectionalStream({ + body: '', +}); +wtSessionStream.onwtsessionclose = mustCall((code, reason) => { + assert.strictEqual(code, 200); + assert.strictEqual(reason, 'all perfect'); +}); +wtSessionStream.sendHeaders({ + ':method': 'CONNECT', + ':scheme': 'https', + // This one depends on draft, draft14 says "webtransport", draft15 says "webtransport-h3" + ':protocol': 'webtransport', + ':path': '/testwtpath', + ':authority': 'testserver:' + serverEndpoint.address.port +}, { + webtransport: true // Tell nghttp3 to treat the stream as a WT session stream +}); + +// Well let's get a unidi stream and send something +const clientUnidiStream = await clientSession.createUnidirectionalStream({ + incremental: true, + webtransportSession: wtSessionStream // Associate it with the sessionStream +}); + +// Next step send some data +await serverSessionOpened.promise; + +const w = clientUnidiStream.writer; +for (let i = 0; i < numChunks; i++) { + const chunk = buildChunk(i); + while (!w.writeSync(chunk)) { + // Flow controlled — wait for drain before retrying. + const drainable = w[dp](); + if (drainable) await drainable; + } +} +w.endSync(); + +try { + await wtSessionStream.closed; +} catch (error) { + assert.strictEqual(error.errorCode, 200n); + assert.strictEqual(error.reason, 'all perfect'); +} + +await clientSession.close(); +await serverEndpoint.close(); diff --git a/test/parallel/test-quic-h3-wt-unidi-from-server.mjs b/test/parallel/test-quic-h3-wt-unidi-from-server.mjs new file mode 100644 index 000000000000..650f02905a74 --- /dev/null +++ b/test/parallel/test-quic-h3-wt-unidi-from-server.mjs @@ -0,0 +1,212 @@ +// Flags: --experimental-quic --experimental-stream-iter --no-warnings + +// Test: HTTP/3 webtransport client initiated uni stream +// Server creates an uni stream and send data to the client + +import { hasQuic, skip, mustCall, mustNotCall, mustCallAtLeast } from '../common/index.mjs'; +import assert from 'node:assert'; +import * as fixtures from '../common/fixtures.mjs'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} + +const { listen, connect } = await import('node:quic'); +const { createPrivateKey } = await import('node:crypto'); +const { drainableProtocol: dp } = await import('stream/iter'); + +const key = createPrivateKey(fixtures.readKey('agent1-key.pem')); +const cert = fixtures.readKey('agent1-cert.pem'); + +const chunkSizes = [60000, 12, 1000000, 50000, 1600, 20000, 1000000, 30000, 0, 100]; +const numChunks = chunkSizes.length; +const byteLength = chunkSizes.reduce((accumulator, currentValue) => accumulator + currentValue, 0); + + +// Build a deterministic payload so we can verify integrity. +function buildChunk(index) { + const chunk = new Uint8Array(chunkSizes[index]); + // Fill with a pattern derived from the chunk index. + const val = index & 0xff; + for (let i = 0; i < chunkSizes[index]; i++) { + chunk[i] = (val + i) & 0xff; + } + return chunk; +} + +function checksum(data) { + let sum = 0; + for (let i = 0; i < data.byteLength; i++) { + sum = (sum + data[i]) | 0; + } + return sum; +} + +// Compute expected checksum. +let expectedChecksum = 0; +for (let i = 0; i < numChunks; i++) { + const chunk = buildChunk(i); + expectedChecksum = (expectedChecksum + checksum(chunk)) | 0; +} + +const serverSessionOpened = Promise.withResolvers(); + +const readChunks = []; +let serverSessionStream; +const serverEndpoint = await listen(mustCall(async (ss) => { + ss.onapplication = mustCall((aopts) => { + assert.strictEqual(!aopts.enableDatagrams, false); + }); + ss.onhandshake = mustCall(function() { + assert.strictEqual(BigInt(this?.remoteTransportParams?.maxDatagramFrameSize) > 0, true); + }); + ss.onstream = mustCallAtLeast((stream) => { + console.log('Does nothing but is required'); + }, 1); +}), { + sni: { '*': { keys: [key], certs: [cert] } }, + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { + maxDatagramFrameSize: 100, + initialMaxStreamsBidi: 100, // Default value according to spec + initialMaxStreamsUni: 100, // Especially important as limit default is 0 + }, + onheaders: mustCall(async function(headers) { + try { + assert.strictEqual(headers[':scheme'], 'https'); + assert.strictEqual(headers[':method'], 'CONNECT'); + assert.strictEqual(headers[':protocol'], 'webtransport'); // depends on the draft + assert.strictEqual(headers[':path'], '/testwtpath'); + // We could also check for wt-available protocols + this.sendHeaders( + { ':status': '200' }, + { terminal: false, webtransport: true } + ); + serverSessionOpened.resolve(this.id); + serverSessionStream = this; + + serverSessionStream.onwtsessionclose = mustCall((code, reason) => { + assert.strictEqual(code, 200); + assert.strictEqual(reason, 'all perfect'); + }); + } catch (error) { + serverSessionOpened.reject(error); + } + // Well let's get a unidi stream and send something + const serverUnidiStream = await this.session.createUnidirectionalStream({ + incremental: true, + webtransportSession: serverSessionStream // Associate it with the sessionStream + }); + + // Next step send some data + await serverSessionOpened.promise; + + const w = serverUnidiStream.writer; + for (let i = 0; i < numChunks; i++) { + const chunk = buildChunk(i); + while (!w.writeSync(chunk)) { + // Flow controlled — wait for drain before retrying. + const drainable = w[dp](); + if (drainable) await drainable; + } + } + w.endSync(); + }), +}); + +const clientSession = await connect(serverEndpoint.address, { + servername: 'localhost', + verifyPeer: 'manual', + application: { + enableConnectProtocol: true, + enableDatagrams: true, + enableWebtransport: true + }, + transportParams: { + maxDatagramFrameSize: 1000, + initialMaxStreamsBidi: 100, // Default value according to spec + initialMaxStreamsUni: 100, // Especially important as limit default is 0 + }, +}); + +const webtransportSupport = Promise.withResolvers(); + +clientSession.onapplication = mustCall((aopts) => { + try { + // Test for webtransport support + assert.strictEqual(aopts.enableConnectProtocol, true); + assert.strictEqual(aopts.enableDatagrams, true); + assert.strictEqual(aopts.enableWebtransport, true); + // Ok we have wt support + webtransportSupport.resolve(); + } catch (error) { + webtransportSupport.reject(error); + } +}); + +let wtSessionStream; + +clientSession.onstream = mustCall((stream) => { + stream.onheaders = mustNotCall((stream) => { + // Well this should not happen on client side + console.log('Called onheaders on the client side!'); + }); + stream.onsessionid = mustCallAtLeast(async (sessionid) => { + // Deinstall handlers + stream.onheaders = undefined; + stream.onsessionid = undefined; + + const sessionid2 = await serverSessionOpened.promise; + assert.strictEqual(sessionid, sessionid2); + // Now we get the data + for await (const chunks of stream) { + readChunks.push(...chunks); + } + const receivedBytes = readChunks.reduce((accu, curVal) => accu + curVal.byteLength, 0); + + assert.strictEqual(receivedBytes, byteLength); + let receivedChecksum = 0; + for (const chunk of readChunks) { + receivedChecksum = (receivedChecksum + checksum(chunk)) | 0; + } + assert.strictEqual(receivedChecksum, expectedChecksum); + wtSessionStream.closed.catch(mustCall((error) => { + assert.strictEqual(error.errorCode, 200n); + assert.strictEqual(error.reason, 'all perfect'); + })); + await wtSessionStream.closeWebtransportSessionStream(200, 'all perfect'); + }, stream.id !== 0n ? 1 : 0); // The second stream is a datastream +}); + +await clientSession.opened; +await webtransportSupport.promise; +// Now we open a webtransport session, which is actually +// a special bidirectional stream +wtSessionStream = await clientSession.createBidirectionalStream({ + body: '', +}); +wtSessionStream.sendHeaders({ + ':method': 'CONNECT', + ':scheme': 'https', + // This one depends on draft, draft14 says "webtransport", draft15 says "webtransport-h3" + ':protocol': 'webtransport', + ':path': '/testwtpath', + ':authority': 'testserver:' + serverEndpoint.address.port +}, { + webtransport: true // Tell nghttp3 to treat the stream as a WT session stream +}); + + +try { + await wtSessionStream.closed; +} catch (error) { + assert.strictEqual(error.errorCode, 200n); + assert.strictEqual(error.reason, 'all perfect'); +} + +await clientSession.close(); +await serverEndpoint.close(); diff --git a/test/parallel/test-quic-internal-setcallbacks.mjs b/test/parallel/test-quic-internal-setcallbacks.mjs index 910f1fa97a74..2cd3688378ef 100644 --- a/test/parallel/test-quic-internal-setcallbacks.mjs +++ b/test/parallel/test-quic-internal-setcallbacks.mjs @@ -32,9 +32,11 @@ const callbacks = { onStreamClose() {}, onStreamDrain() {}, onStreamReset() {}, + onStreamSessionId() {}, onStreamStopSending() {}, onStreamHeaders() {}, onStreamTrailers() {}, + onStreamWTSessionClose() {}, }; // Fail if any callback is missing for (const fn of Object.keys(callbacks)) { diff --git a/test/parallel/test-quic-session-application-options.mjs b/test/parallel/test-quic-session-application-options.mjs index a97b654e98b3..b25db7bff062 100644 --- a/test/parallel/test-quic-session-application-options.mjs +++ b/test/parallel/test-quic-session-application-options.mjs @@ -24,6 +24,7 @@ const customAppOptions = { qpackBlockedStreams: 50n, enableConnectProtocol: false, enableDatagrams: false, + enableWebtransport: false, }; const serverDone = Promise.withResolvers(); @@ -52,6 +53,7 @@ const serverEndpoint = await listen(mustCall((serverSession) => { assert.strictEqual(opts.enableConnectProtocol, customAppOptions.enableConnectProtocol); assert.strictEqual(opts.enableDatagrams, customAppOptions.enableDatagrams); + assert.strictEqual(opts.enableWebtransport, customAppOptions.enableWebtransport); stream.writer.endSync(); await stream.closed;