From e808f728bb7f4434a7eac2a0afd2f9257e06c007 Mon Sep 17 00:00:00 2001 From: Pierre Brochard Date: Thu, 26 Nov 2026 20:30:22 +0100 Subject: [PATCH 1/4] fix(mod_http3): resolve worker concurrency races and QPACK encoder desync --- modules/http3/h3_config.c | 27 + modules/http3/h3_config.c.orig | 837 +++++++++++++++++++++++++ modules/http3/h3_io.c | 54 +- modules/http3/h3_request.c | 15 +- modules/http3/h3_session.c | 22 +- modules/http3/h3_stream.c | 66 +- modules/http3/h3_threads.c | 18 +- modules/http3/h3_threads.c.orig | 209 ++++++ modules/http3/include/h3_config.h | 2 + modules/http3/include/h3_config.h.orig | 140 +++++ modules/http3/include/h3_io.h | 4 +- modules/http3/include/h3_session.h | 8 +- 12 files changed, 1337 insertions(+), 65 deletions(-) create mode 100644 modules/http3/h3_config.c.orig create mode 100644 modules/http3/h3_threads.c.orig create mode 100644 modules/http3/include/h3_config.h.orig diff --git a/modules/http3/h3_config.c b/modules/http3/h3_config.c index 2cd85a82730..3a7d85bbbd8 100644 --- a/modules/http3/h3_config.c +++ b/modules/http3/h3_config.c @@ -80,6 +80,8 @@ void* h3_merge_server_config(apr_pool_t* p, void* base_conf, void* new_conf) merged->h3_max_stream_errors = new->h3_max_stream_errors ? new->h3_max_stream_errors : base->h3_max_stream_errors; merged->h3_qpack_capacity_set = new->h3_qpack_capacity_set ? new->h3_qpack_capacity_set : base->h3_qpack_capacity_set; merged->h3_qpack_table_capacity = new->h3_qpack_capacity_set ? new->h3_qpack_table_capacity : base->h3_qpack_table_capacity; + merged->h3_qpack_encoder_capacity_set = new->h3_qpack_encoder_capacity_set ? new->h3_qpack_encoder_capacity_set : base->h3_qpack_encoder_capacity_set; + merged->h3_qpack_encoder_table_capacity = new->h3_qpack_encoder_capacity_set ? new->h3_qpack_encoder_table_capacity : base->h3_qpack_encoder_table_capacity; merged->h3_qpack_blocked_set = new->h3_qpack_blocked_set ? new->h3_qpack_blocked_set : base->h3_qpack_blocked_set; merged->h3_qpack_blocked_streams = new->h3_qpack_blocked_set ? new->h3_qpack_blocked_streams : base->h3_qpack_blocked_streams; merged->h3_min_workers = new->h3_min_workers ? new->h3_min_workers : base->h3_min_workers; @@ -318,6 +320,26 @@ static const char* set_h3_qpack_table_capacity(cmd_parms* cmd, void* dummy H3_UN return NULL; } +static const char* set_h3_qpack_encoder_table_capacity(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3QpackTableCapacity: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv != APR_SUCCESS || val < 0) + { + return apr_psprintf(cmd->pool, "H3QpackEncoderTableCapacity: '%s' is not a number", arg); + } + + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_qpack_encoder_table_capacity = (apr_uint32_t)val; + conf->h3_qpack_encoder_capacity_set = 1; + return NULL; +} + static const char* set_h3_qpack_blocked_streams(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) { if (!arg || !*arg) @@ -719,6 +741,10 @@ int h3_post_config(apr_pool_t* p, apr_pool_t* plog H3_UNUSED, apr_pool_t* ptemp, { vc->h3_qpack_table_capacity = H3_QPACK_TABLE_CAPACITY_DEFAULT; } + if (!vc->h3_qpack_encoder_capacity_set) + { + vc->h3_qpack_encoder_table_capacity = 0; + } if (!vc->h3_qpack_blocked_set) { vc->h3_qpack_blocked_streams = H3_QPACK_BLOCKED_STREAMS_DEFAULT; @@ -829,6 +855,7 @@ const command_rec h3_cmds[] = { AP_INIT_TAKE1("H3StreamTimeout", set_h3_stream_timeout, NULL, RSRC_CONF, "Seconds a response may make no progress before the stream is aborted (default: the server Timeout)"), AP_INIT_TAKE1("H3MaxStreamErrors", set_h3_max_stream_errors, NULL, RSRC_CONF, "Stream errors one connection may cause before it is closed with H3_EXCESSIVE_LOAD (default: 8)"), AP_INIT_TAKE1("H3QpackTableCapacity", set_h3_qpack_table_capacity, NULL, RSRC_CONF, "Bytes of QPACK dynamic table a client may use for request headers; 0 disables it (default: 4096)"), + AP_INIT_TAKE1("H3QpackEncoderTableCapacity", set_h3_qpack_encoder_table_capacity, NULL, RSRC_CONF, "Maximum QPACK dynamic table capacity used by the server encoder (default: 0)"), AP_INIT_TAKE1("H3QpackBlockedStreams", set_h3_qpack_blocked_streams, NULL, RSRC_CONF, "Requests that may wait on a QPACK table insert that has not arrived; each one is unbounded memory (default: 0)"), AP_INIT_TAKE1("H3MinWorkers", set_h3_min_workers, NULL, RSRC_CONF, "Request worker threads started per child process (default: 16)"), AP_INIT_TAKE1("H3MaxWorkers", set_h3_max_workers, NULL, RSRC_CONF, "Maximum request worker threads per child process (default: 64)"), diff --git a/modules/http3/h3_config.c.orig b/modules/http3/h3_config.c.orig new file mode 100644 index 00000000000..2cd85a82730 --- /dev/null +++ b/modules/http3/h3_config.c.orig @@ -0,0 +1,837 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "h3.h" +#include "h3_check.h" +#include "h3_config.h" +#include "h3_os.h" +#include "h3_request.h" +#include "h3_server.h" +#include "mod_http3.h" +#include "quic/detail/h3q_tls.h" + +apr_port_t get_server_port(const server_rec* s) +{ + for (const server_addr_rec* sar = s->addrs; sar != NULL; sar = sar->next) + { + if (sar->host_port != 0) + { + return sar->host_port; + } + } + return 0; +} + +void* h3_create_server_config(apr_pool_t* p, server_rec* s H3_UNUSED) +{ + return apr_pcalloc(p, sizeof(h3_server_conf)); +} + +void* h3_merge_server_config(apr_pool_t* p, void* base_conf, void* new_conf) +{ + h3_server_conf* merged = apr_pcalloc(p, sizeof(h3_server_conf)); + h3_server_conf* base = (h3_server_conf*)base_conf; + h3_server_conf* new = (h3_server_conf*)new_conf; + + merged->h3_port = new->h3_port ? new->h3_port : base->h3_port; + merged->h3_max_concurrent_streams = new->h3_max_concurrent_streams ? new->h3_max_concurrent_streams : base->h3_max_concurrent_streams; + merged->h3_max_connections = new->h3_max_connections ? new->h3_max_connections : base->h3_max_connections; + merged->h3_stream_buffer_size = new->h3_stream_buffer_size ? new->h3_stream_buffer_size : base->h3_stream_buffer_size; + merged->h3_max_request_body_size = new->h3_max_request_body_size ? new->h3_max_request_body_size : base->h3_max_request_body_size; + merged->h3_max_response_body_size = new->h3_max_response_body_size ? new->h3_max_response_body_size : base->h3_max_response_body_size; + merged->h3_alt_svc = new->h3_alt_svc != H3_FLAG_UNSET ? new->h3_alt_svc : base->h3_alt_svc; + merged->h3_address_validation = new->h3_address_validation != H3_FLAG_UNSET ? new->h3_address_validation : base->h3_address_validation; + merged->h3_alt_svc_max_age = new->h3_alt_svc_max_age ? new->h3_alt_svc_max_age : base->h3_alt_svc_max_age; + merged->h3_handshake_timeout = new->h3_handshake_timeout ? new->h3_handshake_timeout : base->h3_handshake_timeout; + merged->h3_idle_timeout = new->h3_idle_timeout ? new->h3_idle_timeout : base->h3_idle_timeout; + merged->h3_socket_buffer_size = new->h3_socket_buffer_size ? new->h3_socket_buffer_size : base->h3_socket_buffer_size; + merged->h3_max_window = new->h3_max_window ? new->h3_max_window : base->h3_max_window; + merged->h3_session_tickets = new->h3_session_tickets != H3_FLAG_UNSET ? new->h3_session_tickets : base->h3_session_tickets; + merged->h3_early_data = new->h3_early_data != H3_FLAG_UNSET ? new->h3_early_data : base->h3_early_data; + merged->h3_stream_timeout = new->h3_stream_timeout ? new->h3_stream_timeout : base->h3_stream_timeout; + merged->h3_max_stream_errors = new->h3_max_stream_errors ? new->h3_max_stream_errors : base->h3_max_stream_errors; + merged->h3_qpack_capacity_set = new->h3_qpack_capacity_set ? new->h3_qpack_capacity_set : base->h3_qpack_capacity_set; + merged->h3_qpack_table_capacity = new->h3_qpack_capacity_set ? new->h3_qpack_table_capacity : base->h3_qpack_table_capacity; + merged->h3_qpack_blocked_set = new->h3_qpack_blocked_set ? new->h3_qpack_blocked_set : base->h3_qpack_blocked_set; + merged->h3_qpack_blocked_streams = new->h3_qpack_blocked_set ? new->h3_qpack_blocked_streams : base->h3_qpack_blocked_streams; + merged->h3_min_workers = new->h3_min_workers ? new->h3_min_workers : base->h3_min_workers; + merged->h3_max_workers = new->h3_max_workers ? new->h3_max_workers : base->h3_max_workers; + merged->h3_max_worker_idle_seconds = new->h3_max_worker_idle_seconds ? new->h3_max_worker_idle_seconds : base->h3_max_worker_idle_seconds; + + return merged; +} + +/// Value of an MPM query, or -1 when the active MPM does not answer it. +static int mpm_query(int code) +{ + int value = 0; + return ap_mpm_query(code, &value) == APR_SUCCESS ? value : -1; +} + +static const char* set_h3_port(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3Port: empty port number"; + } + apr_int64_t port = 0; + apr_status_t rv = apr_cstr_atoi64(&port, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3Port: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3Port: '%s' is out of representable range", arg); + } + if (port < 1 || port > 65535) + { + return apr_psprintf(cmd->pool, "H3Port: '%s' is out of allowed range (1-65535)", arg); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_port = (apr_port_t)port; + return NULL; +} + +static const char* set_h3_max_concurrent_streams(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3MaxConcurrentStreams: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3MaxConcurrentStreams: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3MaxConcurrentStreams: '%s' is out of representable range", arg); + } + if (val == 0 || val > UINT16_MAX) + { + return apr_psprintf(cmd->pool, "H3MaxConcurrentStreams: '%s' is out of allowed range (1-%u)", arg, (unsigned)UINT16_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_max_concurrent_streams = (apr_uint32_t)val; + return NULL; +} + +static const char* set_h3_max_connections(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3MaxConnections: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3MaxConnections: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3MaxConnections: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_MAX_CONNECTIONS_MAX) + { + return apr_psprintf(cmd->pool, "H3MaxConnections: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_MAX_CONNECTIONS_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_max_connections = (apr_uint32_t)val; + return NULL; +} + +static const char* set_h3_stream_buffer_size(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3StreamBufferSize: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3StreamBufferSize: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3StreamBufferSize: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_STREAM_BUFFER_SIZE_MAX) + { + return apr_psprintf(cmd->pool, "H3StreamBufferSize: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_STREAM_BUFFER_SIZE_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_stream_buffer_size = (apr_size_t)val; + return NULL; +} + +static const char* set_h3_max_window(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + apr_uint64_t val = 0; + if (apr_cstr_atoui64(&val, arg) != APR_SUCCESS || val < H3_MAX_WINDOW_MIN || val > H3_MAX_WINDOW_MAX) + { + return apr_psprintf(cmd->pool, "H3MaxWindow: '%s' must be a byte count from %lu to %lu", arg, (unsigned long)H3_MAX_WINDOW_MIN, (unsigned long)H3_MAX_WINDOW_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_max_window = (apr_size_t)val; + return NULL; +} + +static const char* set_h3_socket_buffer_size(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3SocketBufferSize: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3SocketBufferSize: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3SocketBufferSize: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_SOCKET_BUFFER_SIZE_MAX) + { + return apr_psprintf(cmd->pool, "H3SocketBufferSize: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_SOCKET_BUFFER_SIZE_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_socket_buffer_size = (apr_size_t)val; + return NULL; +} + +static const char* set_h3_stream_timeout(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3StreamTimeout: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3StreamTimeout: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3StreamTimeout: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_STREAM_TIMEOUT_MAX) + { + return apr_psprintf(cmd->pool, "H3StreamTimeout: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_STREAM_TIMEOUT_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_stream_timeout = (apr_uint32_t)val; + return NULL; +} + +static const char* set_h3_max_stream_errors(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3MaxStreamErrors: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3MaxStreamErrors: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3MaxStreamErrors: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_MAX_STREAM_ERRORS_MAX) + { + return apr_psprintf(cmd->pool, "H3MaxStreamErrors: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_MAX_STREAM_ERRORS_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_max_stream_errors = (apr_uint32_t)val; + return NULL; +} + +static const char* set_h3_qpack_table_capacity(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3QpackTableCapacity: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3QpackTableCapacity: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3QpackTableCapacity: '%s' is out of representable range", arg); + } + if (val > H3_QPACK_TABLE_CAPACITY_MAX) + { + return apr_psprintf(cmd->pool, "H3QpackTableCapacity: '%s' is out of allowed range (0-%lu)", arg, (unsigned long)H3_QPACK_TABLE_CAPACITY_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_qpack_table_capacity = (apr_uint32_t)val; + conf->h3_qpack_capacity_set = 1; + return NULL; +} + +static const char* set_h3_qpack_blocked_streams(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3QpackBlockedStreams: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3QpackBlockedStreams: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3QpackBlockedStreams: '%s' is out of representable range", arg); + } + if (val > H3_QPACK_BLOCKED_STREAMS_MAX) + { + return apr_psprintf(cmd->pool, "H3QpackBlockedStreams: '%s' is out of allowed range (0-%lu)", arg, (unsigned long)H3_QPACK_BLOCKED_STREAMS_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_qpack_blocked_streams = (apr_uint32_t)val; + conf->h3_qpack_blocked_set = 1; + return NULL; +} + +static const char* set_h3_min_workers(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3MinWorkers: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3MinWorkers: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3MinWorkers: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_WORKERS_MAX) + { + return apr_psprintf(cmd->pool, "H3MinWorkers: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_WORKERS_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_min_workers = (apr_uint32_t)val; + return NULL; +} + +static const char* set_h3_max_workers(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3MaxWorkers: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3MaxWorkers: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3MaxWorkers: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_WORKERS_MAX) + { + return apr_psprintf(cmd->pool, "H3MaxWorkers: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_WORKERS_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_max_workers = (apr_uint32_t)val; + return NULL; +} + +static const char* set_h3_max_worker_idle_seconds(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3MaxWorkerIdleSeconds: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3MaxWorkerIdleSeconds: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3MaxWorkerIdleSeconds: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_MAX_WORKER_IDLE_SECONDS_MAX) + { + return apr_psprintf(cmd->pool, "H3MaxWorkerIdleSeconds: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_MAX_WORKER_IDLE_SECONDS_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_max_worker_idle_seconds = (apr_uint32_t)val; + return NULL; +} + +static const char* set_h3_max_request_body_size(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3MaxRequestBodySize: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3MaxRequestBodySize: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3MaxRequestBodySize: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_MAX_REQUEST_BODY_SIZE_MAX) + { + return apr_psprintf(cmd->pool, "H3MaxRequestBodySize: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_MAX_REQUEST_BODY_SIZE_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_max_request_body_size = (apr_size_t)val; + return NULL; +} + +static const char* set_h3_max_response_body_size(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3MaxResponseBodySize: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3MaxResponseBodySize: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3MaxResponseBodySize: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_MAX_RESPONSE_BODY_SIZE_MAX) + { + return apr_psprintf(cmd->pool, "H3MaxResponseBodySize: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_MAX_RESPONSE_BODY_SIZE_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_max_response_body_size = (apr_size_t)val; + return NULL; +} + +static const char* set_h3_handshake_timeout(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3HandshakeTimeout: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3HandshakeTimeout: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3HandshakeTimeout: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_HANDSHAKE_TIMEOUT_MAX) + { + return apr_psprintf(cmd->pool, "H3HandshakeTimeout: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_HANDSHAKE_TIMEOUT_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_handshake_timeout = (apr_uint32_t)val; + return NULL; +} + +static const char* set_h3_idle_timeout(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3IdleTimeout: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3IdleTimeout: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3IdleTimeout: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_IDLE_TIMEOUT_MAX) + { + return apr_psprintf(cmd->pool, "H3IdleTimeout: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_IDLE_TIMEOUT_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_idle_timeout = (apr_uint32_t)val; + return NULL; +} + +static const char* set_h3_session_tickets(cmd_parms* cmd, void* dummy H3_UNUSED, int flag) +{ + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_session_tickets = flag ? H3_FLAG_ON : H3_FLAG_OFF; + return NULL; +} + +static const char* set_h3_early_data(cmd_parms* cmd, void* dummy H3_UNUSED, int flag) +{ +#ifdef H3Q_BACKEND_OSSL + if (flag) + { + return "H3EarlyData is not supported when mod_http3 is built with --with-http3-quic=openssl"; + } +#endif + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_early_data = flag ? H3_FLAG_ON : H3_FLAG_OFF; + return NULL; +} + +static const char* set_h3_alt_svc(cmd_parms* cmd, void* dummy H3_UNUSED, int flag) +{ + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_alt_svc = flag ? H3_FLAG_ON : H3_FLAG_OFF; + return NULL; +} + +static const char* set_h3_address_validation(cmd_parms* cmd, void* dummy H3_UNUSED, int flag) +{ + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_address_validation = flag ? H3_FLAG_ON : H3_FLAG_OFF; + return NULL; +} + +static const char* set_h3_alt_svc_max_age(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) +{ + if (!arg || !*arg) + { + return "H3AltSvcMaxAge: empty value"; + } + apr_uint64_t val = 0; + apr_status_t rv = apr_cstr_atoui64(&val, arg); + if (rv == APR_EINVAL) + { + return apr_psprintf(cmd->pool, "H3AltSvcMaxAge: '%s' is not a number", arg); + } + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3AltSvcMaxAge: '%s' is out of representable range", arg); + } + if (val == 0 || val > H3_ALT_SVC_MAX_AGE_MAX) + { + return apr_psprintf(cmd->pool, "H3AltSvcMaxAge: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_ALT_SVC_MAX_AGE_MAX); + } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_alt_svc_max_age = (apr_uint32_t)val; + return NULL; +} + +static apr_status_t ssl_ctx_cleanup(void* data) +{ + SSL_CTX_free(data); + return APR_SUCCESS; +} + +int h3_ssl_add_cert_files(server_rec* s, apr_pool_t* p, apr_array_header_t* cert_files, apr_array_header_t* key_files) +{ + CHECK(s && p && cert_files && key_files, return DECLINED;); + h3_server_conf* conf = ap_get_module_config(s->module_config, &http3_module); + if (!conf || ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG || !ap_is_allowed_protocol(NULL, NULL, s, "h3") || cert_files->nelts == 0) + { + return DECLINED; + } + /* Still privileged here, so a root-only key loads the way it does for mod_ssl. */ + char err[H3Q_ERRLEN] = {0}; + conf->ssl_ctx = h3q_tls_ctx_create((const char* const*)cert_files->elts, (size_t)cert_files->nelts, (const char* const*)key_files->elts, (size_t)key_files->nelts, conf->h3_session_tickets != H3_FLAG_OFF, err, sizeof(err)); + if (!conf->ssl_ctx) + { + ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_http3: %s", err); + return HTTP_INTERNAL_SERVER_ERROR; + } + apr_pool_cleanup_register(p, conf->ssl_ctx, ssl_ctx_cleanup, apr_pool_cleanup_null); + ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "mod_http3: serving HTTP/3 with mod_ssl certificate %s", APR_ARRAY_IDX(cert_files, 0, const char*)); + return DECLINED; +} + +/** One HTTP/3 host name and the TLS context that carries its certificate. */ +typedef struct +{ + const char* name; + SSL_CTX* ctx; +} h3_sni_host; + +/// cert_cb: serve each host its own certificate over one listener, selected by SNI. +/// SSL_set_SSL_CTX does not switch the certificate of a QUIC connection; applying +/// the matched host's certificate, key and chain to the connection does. +static int h3_sni_select_cert(SSL* ssl, void* arg) +{ + const apr_array_header_t* hosts = arg; + const char* sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); + for (int i = 0; sni && i < hosts->nelts; i++) + { + const h3_sni_host* h = &APR_ARRAY_IDX(hosts, i, h3_sni_host); + /* httpd's ServerAlias matcher: wildcards for wild_names, exact otherwise. */ + if (ap_strcasecmp_match(sni, h->name) == 0) + { + STACK_OF(X509)* chain = NULL; + SSL_CTX_get0_chain_certs(h->ctx, &chain); + return SSL_use_certificate(ssl, SSL_CTX_get0_certificate(h->ctx)) == 1 && SSL_use_PrivateKey(ssl, SSL_CTX_get0_privatekey(h->ctx)) == 1 && (!chain || SSL_set1_chain(ssl, chain) == 1); + } + } + return 1; /* no match: the listener's own certificate */ +} + +int h3_post_config(apr_pool_t* p, apr_pool_t* plog H3_UNUSED, apr_pool_t* ptemp, server_rec* s) +{ + CHECK(p); + CHECK(ptemp); + CHECK(s); + h3_server_conf* conf = NULL; + server_rec* owner = NULL; + + if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG) + { + return OK; + } + + /* Names of every HTTP/3 host, so the listener can pick a certificate by SNI. */ + apr_array_header_t* sni = apr_array_make(p, 4, sizeof(h3_sni_host)); + + for (server_rec* vs = s; vs; vs = vs->next) + { + h3_server_conf* vc = ap_get_module_config(vs->module_config, &http3_module); + if (vc->ssl_ctx) + { + if (vs->server_hostname) + { + h3_sni_host* e = apr_array_push(sni); + e->name = vs->server_hostname; + e->ctx = vc->ssl_ctx; + } + /* ServerAlias: httpd keeps exact names in names and wildcards in wild_names. */ + for (int i = 0; vs->names && i < vs->names->nelts; i++) + { + h3_sni_host* e = apr_array_push(sni); + e->name = APR_ARRAY_IDX(vs->names, i, const char*); + e->ctx = vc->ssl_ctx; + } + for (int i = 0; vs->wild_names && i < vs->wild_names->nelts; i++) + { + h3_sni_host* e = apr_array_push(sni); + e->name = APR_ARRAY_IDX(vs->wild_names, i, const char*); + e->ctx = vc->ssl_ctx; + } + vc->host_port = get_server_port(vs); + if (vc->h3_port == 0) + { + vc->h3_port = vc->host_port; + } + if (vc->h3_max_concurrent_streams == 0) + { + vc->h3_max_concurrent_streams = H3_MAX_CONCURRENT_STREAMS_DEFAULT; + } + if (vc->h3_max_connections == 0) + { + vc->h3_max_connections = H3_MAX_CONNECTIONS_DEFAULT; + } + if (vc->h3_stream_buffer_size == 0) + { + vc->h3_stream_buffer_size = H3_STREAM_BUFFER_SIZE_DEFAULT; + } + if (vc->h3_socket_buffer_size == 0) + { + vc->h3_socket_buffer_size = H3_SOCKET_BUFFER_SIZE_DEFAULT; + } + if (vc->h3_max_window == 0) + { + vc->h3_max_window = H3_MAX_WINDOW_DEFAULT; + } + if (vc->h3_max_stream_errors == 0) + { + vc->h3_max_stream_errors = H3_MAX_STREAM_ERRORS_DEFAULT; + } + if (!vc->h3_qpack_capacity_set) + { + vc->h3_qpack_table_capacity = H3_QPACK_TABLE_CAPACITY_DEFAULT; + } + if (!vc->h3_qpack_blocked_set) + { + vc->h3_qpack_blocked_streams = H3_QPACK_BLOCKED_STREAMS_DEFAULT; + } + if (vc->h3_min_workers == 0) + { + vc->h3_min_workers = H3_MIN_WORKERS_DEFAULT; + } + if (vc->h3_max_workers == 0) + { + vc->h3_max_workers = H3_MAX_WORKERS_DEFAULT; + } + if (vc->h3_max_workers < vc->h3_min_workers) + { + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, vs, "mod_http3: H3MaxWorkers (%u) is below H3MinWorkers (%u); raising it to match", (unsigned)vc->h3_max_workers, (unsigned)vc->h3_min_workers); + vc->h3_max_workers = vc->h3_min_workers; + } + if (vc->h3_max_worker_idle_seconds == 0) + { + vc->h3_max_worker_idle_seconds = H3_MAX_WORKER_IDLE_SECONDS_DEFAULT; + } + if (vc->h3_max_request_body_size == 0) + { + vc->h3_max_request_body_size = H3_MAX_REQUEST_BODY_SIZE_DEFAULT; + } + if (vc->h3_max_response_body_size == 0) + { + vc->h3_max_response_body_size = H3_MAX_RESPONSE_BODY_SIZE_DEFAULT; + } + if (vc->h3_alt_svc == H3_FLAG_UNSET) + { + vc->h3_alt_svc = H3_FLAG_ON; + } + if (vc->h3_address_validation == H3_FLAG_UNSET) + { + vc->h3_address_validation = H3_FLAG_ON; + } + if (vc->h3_session_tickets == H3_FLAG_UNSET) + { + vc->h3_session_tickets = H3_FLAG_ON; + } + if (vc->h3_early_data == H3_FLAG_UNSET) + { + vc->h3_early_data = H3_FLAG_OFF; + } + if (vc->h3_alt_svc_max_age == 0) + { + vc->h3_alt_svc_max_age = H3_ALT_SVC_MAX_AGE_DEFAULT; + } + if (vc->h3_handshake_timeout == 0) + { + vc->h3_handshake_timeout = H3_HANDSHAKE_TIMEOUT_DEFAULT; + } + if (vc->h3_idle_timeout == 0) + { + vc->h3_idle_timeout = H3_IDLE_TIMEOUT_DEFAULT; + } + /* httpd prepends virtual hosts, so the last match is the first one defined: + * it owns the listener, as it is mod_ssl's default; the rest still advertise it. */ + conf = vc; + owner = vs; + } + } + + if (!conf) + { + ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_http3: no host serves HTTP/3: add h3 to Protocols on a host with SSLEngine on"); + return HTTP_INTERNAL_SERVER_ERROR; + } + ap_log_error(APLOG_MARK, APLOG_INFO, 0, owner, "h3_post_config: pid=%d h3_port=%d mpm=%s threaded=%d forked=%d max_threads=%d", h3_getpid(), (int)conf->h3_port, ap_show_mpm(), mpm_query(AP_MPMQ_IS_THREADED), mpm_query(AP_MPMQ_IS_FORKED), mpm_query(AP_MPMQ_MAX_THREADS)); + + /* The listener serves conf's certificate by default and swaps by SNI. */ + SSL_CTX_set_cert_cb(conf->ssl_ctx, h3_sni_select_cert, sni); + + h3_request_init(); + h3_server_post_config(s); + return OK; +} + +void* h3_create_dir_config(apr_pool_t* p, char* dir H3_UNUSED) +{ + CHECK(p); + return apr_pcalloc(p, 1); +} + +void* h3_merge_dir_config(apr_pool_t* p H3_UNUSED, void* base, void* add H3_UNUSED) +{ + CHECK(base); + return base; +} + +const command_rec h3_cmds[] = { + AP_INIT_TAKE1("H3Port", set_h3_port, NULL, RSRC_CONF, "UDP port to listen on for QUIC/HTTP-3 (default: same as main server)"), + AP_INIT_TAKE1("H3MaxConcurrentStreams", set_h3_max_concurrent_streams, NULL, RSRC_CONF, "Maximum number of concurrent HTTP/3 streams per connection (default: 100)"), + AP_INIT_TAKE1("H3MaxConnections", set_h3_max_connections, NULL, RSRC_CONF, "Maximum concurrent QUIC/HTTP/3 connections per child process (default: 256)"), + AP_INIT_TAKE1("H3StreamBufferSize", set_h3_stream_buffer_size, NULL, RSRC_CONF, "Per-stream request and streaming-response buffer size in bytes (default: 65536)"), + AP_INIT_TAKE1("H3MaxRequestBodySize", set_h3_max_request_body_size, NULL, RSRC_CONF, "Maximum HTTP/3 request body size in bytes, fully buffered in memory (default: 10485760)"), + AP_INIT_FLAG("H3AltSvc", set_h3_alt_svc, NULL, RSRC_CONF, "Whether to advertise HTTP/3 support via an Alt-Svc response header, required for browser discovery (default: on)"), + AP_INIT_TAKE1("H3AltSvcMaxAge", set_h3_alt_svc_max_age, NULL, RSRC_CONF, "Seconds a client may cache the Alt-Svc HTTP/3 advertisement for (default: 86400)"), + AP_INIT_TAKE1("H3HandshakeTimeout", set_h3_handshake_timeout, NULL, RSRC_CONF, "Timeout in seconds for QUIC handshakes to complete (default: 10)"), + AP_INIT_TAKE1("H3IdleTimeout", set_h3_idle_timeout, NULL, RSRC_CONF, "Idle timeout in seconds for QUIC connections (default: 300)"), + AP_INIT_TAKE1("H3MaxResponseBodySize", set_h3_max_response_body_size, NULL, RSRC_CONF, "Maximum HTTP/3 response body size in bytes; an explicit limit enables bounded whole-response buffering (default: unlimited streaming)"), + AP_INIT_FLAG("H3AddressValidation", set_h3_address_validation, NULL, RSRC_CONF, "Whether to validate client addresses with a QUIC Retry packet before accepting a connection (default: on)"), + AP_INIT_TAKE1("H3SocketBufferSize", set_h3_socket_buffer_size, NULL, RSRC_CONF, "Bytes requested for the QUIC socket send and receive buffers; the OS may grant less (default: 2097152)"), + AP_INIT_TAKE1("H3MaxWindow", set_h3_max_window, NULL, RSRC_CONF, "Largest flow-control window, per connection and per stream, that autotuning may grow a client upload window to (default: 6291456)"), + AP_INIT_FLAG("H3SessionTickets", set_h3_session_tickets, NULL, RSRC_CONF, "Whether to issue TLS session tickets so returning clients can resume instead of running a full handshake (default: on)"), + AP_INIT_FLAG("H3EarlyData", set_h3_early_data, NULL, RSRC_CONF, "Whether to accept 0-RTT request data on resumed connections; unsafe methods wait for the handshake (default: off)"), + AP_INIT_TAKE1("H3StreamTimeout", set_h3_stream_timeout, NULL, RSRC_CONF, "Seconds a response may make no progress before the stream is aborted (default: the server Timeout)"), + AP_INIT_TAKE1("H3MaxStreamErrors", set_h3_max_stream_errors, NULL, RSRC_CONF, "Stream errors one connection may cause before it is closed with H3_EXCESSIVE_LOAD (default: 8)"), + AP_INIT_TAKE1("H3QpackTableCapacity", set_h3_qpack_table_capacity, NULL, RSRC_CONF, "Bytes of QPACK dynamic table a client may use for request headers; 0 disables it (default: 4096)"), + AP_INIT_TAKE1("H3QpackBlockedStreams", set_h3_qpack_blocked_streams, NULL, RSRC_CONF, "Requests that may wait on a QPACK table insert that has not arrived; each one is unbounded memory (default: 0)"), + AP_INIT_TAKE1("H3MinWorkers", set_h3_min_workers, NULL, RSRC_CONF, "Request worker threads started per child process (default: 16)"), + AP_INIT_TAKE1("H3MaxWorkers", set_h3_max_workers, NULL, RSRC_CONF, "Maximum request worker threads per child process (default: 64)"), + AP_INIT_TAKE1("H3MaxWorkerIdleSeconds", set_h3_max_worker_idle_seconds, NULL, RSRC_CONF, "Seconds an idle request worker is kept before it exits (default: 600)"), + AP_INIT_TAKE1(NULL, NULL, NULL, RSRC_CONF, NULL), +}; diff --git a/modules/http3/h3_io.c b/modules/http3/h3_io.c index 5f2ca30b116..087e46e03ad 100644 --- a/modules/http3/h3_io.c +++ b/modules/http3/h3_io.c @@ -69,7 +69,7 @@ static void teardown(h3_io_t* io) CHECK(io); if (io->event_thread) { - io->thread_running = 0; + apr_atomic_set32(&io->thread_running,0); h3_wakeup_signal_always(&io->wakeup); apr_status_t status; apr_thread_join(&status, io->event_thread); @@ -109,7 +109,7 @@ static void teardown(h3_io_t* io) static apr_status_t listen_stop_cleanup(void* data) { h3_io_t* io = data; - if (io && io->draining) + if (io && apr_atomic_read32(&io->draining)) { h3_io_listen_stop(io); } @@ -172,7 +172,7 @@ apr_status_t h3_io_listen_start(apr_pool_t* pchild, server_rec* s, h3_server_con ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, "active MPM '%s' does not report externally accepted connections; graceful child shutdown may end active HTTP/3 connections early (an MPM with ap_mpm_note_extra_connection_added/_removed avoids this)", ap_show_mpm()); } - io->thread_running = 1; + apr_atomic_set32(&io->thread_running,1); child_h3_io = io; /* The event thread allocates from pchild too, so allocate everything * from it here before the thread starts. */ @@ -180,7 +180,7 @@ apr_status_t h3_io_listen_start(apr_pool_t* pchild, server_rec* s, h3_server_con apr_pool_pre_cleanup_register(pchild, io, listen_stop_cleanup); if (apr_threadattr_create(&attr, pchild) != APR_SUCCESS || apr_thread_create(&io->event_thread, attr, h3_event_thread, io, pchild) != APR_SUCCESS) { - io->thread_running = 0; + apr_atomic_set32(&io->thread_running,0); teardown(io); child_h3_io = NULL; return APR_EGENERAL; @@ -192,9 +192,9 @@ apr_status_t h3_io_listen_start(apr_pool_t* pchild, server_rec* s, h3_server_con void h3_io_listen_drain(h3_io_t* io) { - if (io && !io->draining) + if (io && !apr_atomic_read32(&io->draining)) { - io->draining = 1; + apr_atomic_set32(&io->draining,1); ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "graceful stop: draining %d connection(s), the MPM waits for them to finish", io->active_sessions ? io->active_sessions->nelts : 0); h3_wakeup_signal_always(&io->wakeup); } @@ -413,14 +413,15 @@ static int h3_method_is_safe(const char* method) return m == M_GET || m == M_OPTIONS || m == M_TRACE; } -static void abandon_stalled_responses(h3_session* session, apr_interval_time_t stall_timeout) +static void abandon_stalled_responses(h3_session* session, apr_pool_t* loop_pool, apr_interval_time_t stall_timeout) { - if (stall_timeout <= 0) + if (!session || !loop_pool || stall_timeout <= 0) { return; } apr_time_t now = apr_time_now(); - for (apr_hash_index_t* hi = apr_hash_first(NULL, session->streams); hi; hi = apr_hash_next(hi)) + apr_array_header_t* stalled = apr_array_make(loop_pool, 4, sizeof(h3_stream*)); + for (apr_hash_index_t* hi = apr_hash_first(loop_pool, session->streams); hi; hi = apr_hash_next(hi)) { h3_stream* h3s = apr_hash_this_val(hi); if (!h3s || h3s->response_cancelled || h3s->response_complete) @@ -435,6 +436,17 @@ static void abandon_stalled_responses(h3_session* session, apr_interval_time_t s { continue; } + *(h3_stream**)apr_array_push(stalled) = h3s; + } + + for (int i = 0; i < stalled->nelts; i++) + { + h3_stream* h3s = ((h3_stream**)stalled->elts)[i]; + if (!h3s || h3s->response_cancelled || h3s->response_complete) + { + continue; + } + ap_log_error(APLOG_MARK, APLOG_INFO, 0, session->s, "HTTP/3 stream %" APR_INT64_T_FMT " made no progress for %" APR_TIME_T_FMT " seconds; abandoning the response", (apr_int64_t)h3s->stream_id, apr_time_sec(stall_timeout)); h3_stream_response_cancel_locked(h3s); } @@ -447,12 +459,12 @@ int service_session_pass(h3_io_t* io, h3_session* session) server_rec* s = session->s; h3q_conn* conn = session->qconn; - if (session->aborted) + if (apr_atomic_read32(&session->aborted)) { return 0; } - if (io->draining || !io->thread_running) + if (apr_atomic_read32(&io->draining) || !apr_atomic_read32(&io->thread_running)) { int drained; apr_thread_mutex_lock(session->lock); @@ -460,7 +472,7 @@ int service_session_pass(h3_io_t* io, h3_session* session) { nghttp3_conn_submit_shutdown_notice(session->ngh3); } - if (!io->thread_running && !session->final_goaway_sent) + if (!apr_atomic_read32(&io->thread_running) && !session->final_goaway_sent) { nghttp3_conn_shutdown(session->ngh3); session->final_goaway_sent = 1; @@ -473,7 +485,7 @@ int service_session_pass(h3_io_t* io, h3_session* session) session->goaway_sent = 1; ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "sent HTTP/3 GOAWAY; letting in-flight streams finish"); } - if ((!io->thread_running || io->draining) && !session->goaway_deadline) + if ((!apr_atomic_read32(&io->thread_running) || apr_atomic_read32(&io->draining)) && !session->goaway_deadline) { session->goaway_deadline = apr_time_now() + apr_time_from_sec(H3_GOAWAY_GRACE_SECS); ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "stopping: allowing up to %d more second(s) for in-flight streams", H3_GOAWAY_GRACE_SECS); @@ -488,7 +500,7 @@ int service_session_pass(h3_io_t* io, h3_session* session) } flush_nghttp3(session); apr_thread_mutex_unlock(session->lock); - session->aborted = 1; + apr_atomic_set32(&session->aborted,1); return 0; } } @@ -498,13 +510,13 @@ int service_session_pass(h3_io_t* io, h3_session* session) char detail[H3Q_ERRLEN] = {0}; h3q_conn_close_reason(conn, detail, sizeof(detail)); ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "QUIC connection terminated (idle timeout, peer close, or transport error) close=[%s]", detail); - session->aborted = 1; + apr_atomic_set32(&session->aborted,1); return 0; } /* ngtcp2 gave the peer more streams: nghttp3 checks PRIORITY_UPDATE against its own copy of the limit. */ uint64_t max_bidi = h3q_conn_max_remote_streams_bidi(conn); - if (max_bidi > session->max_client_streams_bidi && session->ngh3 && !session->ngh3_dead) + if (max_bidi > session->max_client_streams_bidi && session->ngh3 && !apr_atomic_read32(&session->ngh3_dead)) { apr_thread_mutex_lock(session->lock); nghttp3_conn_set_max_client_streams_bidi(session->ngh3, max_bidi); @@ -545,9 +557,9 @@ int service_session_pass(h3_io_t* io, h3_session* session) flush_nghttp3(session); apr_thread_mutex_unlock(session->lock); - if (session->ngh3_dead) + if (apr_atomic_read32(&session->ngh3_dead)) { - session->aborted = 1; + apr_atomic_set32(&session->aborted,1); apr_pool_destroy(scratch); return 0; } @@ -567,7 +579,7 @@ int service_session_pass(h3_io_t* io, h3_session* session) h3_server_conf* sconf = ap_get_module_config(s->module_config, &http3_module); apr_thread_mutex_lock(session->lock); flush_nghttp3(session); - abandon_stalled_responses(session, (sconf && sconf->h3_stream_timeout) ? apr_time_from_sec(sconf->h3_stream_timeout) : s->timeout); + abandon_stalled_responses(session, scratch, (sconf && sconf->h3_stream_timeout) ? apr_time_from_sec(sconf->h3_stream_timeout) : s->timeout); apr_thread_mutex_unlock(session->lock); apr_pool_destroy(scratch); @@ -579,13 +591,13 @@ int service_session_pass(h3_io_t* io, h3_session* session) { session->last_activity = apr_time_now(); } - else if (io->thread_running && apr_atomic_read32(&session->active_tasks) == 0) + else if (apr_atomic_read32(&io->thread_running) && apr_atomic_read32(&session->active_tasks) == 0) { h3_server_conf* conf = ap_get_module_config(s->module_config, &http3_module); if (conf && apr_time_now() - session->last_activity >= apr_time_from_sec(conf->h3_idle_timeout)) { ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "closing HTTP/3 connection idle for %u second(s)", (unsigned)conf->h3_idle_timeout); - session->aborted = 1; + apr_atomic_set32(&session->aborted,1); } } return data_read; diff --git a/modules/http3/h3_request.c b/modules/http3/h3_request.c index babb3b51c75..99a25a187aa 100644 --- a/modules/http3/h3_request.c +++ b/modules/http3/h3_request.c @@ -202,13 +202,15 @@ static apr_status_t submit_response_nva(h3_stream* h3s, nghttp3_nv* nva, size_t apr_thread_mutex_unlock(session->lock); return APR_SUCCESS; } - if (h3s->response_cancelled || session->aborted || session->ngh3_dead || !session->ngh3) + if (h3s->response_cancelled || apr_atomic_read32(&session->aborted) || apr_atomic_read32(&session->ngh3_dead) || !session->ngh3) { apr_thread_mutex_unlock(session->lock); return APR_ECONNABORTED; } nghttp3_data_reader dr = {.read_data = h3_session_read_data}; - int rv = nghttp3_conn_submit_response(session->ngh3, h3s->stream_id, nva, nvlen, &dr); + int64_t sid = h3s->stream_id; + + int rv = nghttp3_conn_submit_response(session->ngh3, sid, nva, nvlen, &dr); if (rv == 0) { h3s->response_submitted = 1; @@ -220,7 +222,7 @@ static apr_status_t submit_response_nva(h3_stream* h3s, nghttp3_nv* nva, size_t apr_thread_mutex_unlock(session->lock); if (rv != 0) { - ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "nghttp3_conn_submit_response failed for stream %" APR_INT64_T_FMT ": %d", (apr_int64_t)h3s->stream_id, rv); + ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "nghttp3_conn_submit_response failed for stream %" APR_INT64_T_FMT ": %d", (apr_int64_t)sid, rv); return APR_EGENERAL; } h3_session_wake(session); @@ -238,7 +240,7 @@ apr_status_t h3_response_start(request_rec* r, h3_conn_ctx_t* h3ctx) h3_response_finalize(r, h3ctx); #endif nghttp3_nv nva[64] = {0}; - size_t nvlen = build_response_nva(nva, OSSL_NELEM(nva), r, h3ctx, h3s->pool); + size_t nvlen = build_response_nva(nva, OSSL_NELEM(nva), r, h3ctx, r->pool); return submit_response_nva(h3s, nva, nvlen); } @@ -379,7 +381,7 @@ static void* APR_THREAD_FUNC stream_worker(apr_thread_t* thd, void* data) /* The partial body and its Content-Length no longer agree; send a clean error. */ static const char oversized_msg[] = "Response exceeded H3MaxResponseBodySize\n"; status = HTTP_INTERNAL_SERVER_ERROR; - char* status_str = apr_psprintf(h3s->pool, "%d", status); + char* status_str = apr_psprintf(r->pool, "%d", status); nghttp3_nv nva[2] = {0}; NV_SET(nva, 0, ":status", status_str); NV_SET(nva, 1, "content-type", "text/plain"); @@ -406,9 +408,8 @@ static void* APR_THREAD_FUNC stream_worker(apr_thread_t* thd, void* data) { response_rv = h3_stream_response_append(h3s, buffered_body, buffered_body_len); } - h3_stream_response_complete(h3s); - size_t body_len = h3s->response_len; + const size_t body_len = h3_stream_response_complete(h3s); int64_t sid = h3s->stream_id; if (response_rv != APR_SUCCESS && response_rv != APR_ECONNABORTED) { diff --git a/modules/http3/h3_session.c b/modules/http3/h3_session.c index 999f6407d7d..a8814288e7e 100644 --- a/modules/http3/h3_session.c +++ b/modules/http3/h3_session.c @@ -90,7 +90,8 @@ apr_status_t h3_session_create(h3_session** psession, server_rec* s, h3q_conn* q CHECK(conf, return APR_EGENERAL;); settings.qpack_max_dtable_capacity = conf->h3_qpack_table_capacity; settings.qpack_blocked_streams = conf->h3_qpack_blocked_streams; - + settings.qpack_encoder_max_dtable_capacity = conf->h3_qpack_encoder_table_capacity; + if (s->limit_req_fields > 0 && s->limit_req_fieldsize > 0) { settings.max_field_section_size = (uint64_t)s->limit_req_fields * ((uint64_t)s->limit_req_fieldsize + 32); @@ -174,7 +175,7 @@ apr_status_t h3_session_create_control_streams(h3_session* session) } track_stream(session, cs[0].id, cs[0].st); - track_stream(session, cs[1].id, cs[1].st); + session->qpack_encoder_stream = track_stream(session, cs[1].id, cs[1].st); track_stream(session, cs[2].id, cs[2].st); session->control_streams_created = 1; return APR_SUCCESS; @@ -192,7 +193,7 @@ void h3_session_destroy(h3_session* session) nghttp3_conn_del(session->ngh3); session->ngh3 = NULL; } - for (apr_hash_index_t* hi = apr_hash_first(NULL, session->streams); hi; hi = apr_hash_next(hi)) + for (apr_hash_index_t* hi = apr_hash_first(session->pool, session->streams); hi; hi = apr_hash_next(hi)) { h3_stream* stream = apr_hash_this_val(hi); if (stream) @@ -240,7 +241,7 @@ apr_status_t h3_stream_response_append(h3_stream* stream, const uint8_t* data, s while (offset < len) { apr_thread_mutex_lock(session->lock); - while (stream->response_buffered >= stream->response_buffer_limit && !stream->response_cancelled && !session->aborted && !session->ngh3_dead) + while (stream->response_buffered >= stream->response_buffer_limit && !stream->response_cancelled && !apr_atomic_read32(&session->aborted) && !apr_atomic_read32(&session->ngh3_dead)) { apr_status_t rv = apr_thread_cond_timedwait(stream->response_cond, session->lock, apr_time_from_msec(100)); if (rv != APR_SUCCESS && !APR_STATUS_IS_TIMEUP(rv)) @@ -249,7 +250,7 @@ apr_status_t h3_stream_response_append(h3_stream* stream, const uint8_t* data, s return rv; } } - if (stream->response_cancelled || session->aborted || session->ngh3_dead || !session->ngh3) + if (stream->response_cancelled || apr_atomic_read32(&session->aborted) || apr_atomic_read32(&session->ngh3_dead) || !session->ngh3) { apr_thread_mutex_unlock(session->lock); return APR_ECONNABORTED; @@ -311,22 +312,27 @@ apr_status_t h3_stream_response_append(h3_stream* stream, const uint8_t* data, s return APR_SUCCESS; } -void h3_stream_response_complete(h3_stream* stream) +size_t h3_stream_response_complete(h3_stream* stream) { if (!stream) { - return; + return 0; } h3_session* session = stream->session; + size_t resplen = 0; apr_thread_mutex_lock(session->lock); stream->response_complete = 1; - if (stream->response_submitted && session->ngh3 && !session->ngh3_dead) + if (stream->response_submitted && session->ngh3 && !apr_atomic_read32(&session->ngh3_dead)) { (void)nghttp3_conn_resume_stream(session->ngh3, stream->stream_id); } + + resplen = stream->response_len; apr_thread_cond_broadcast(stream->response_cond); apr_thread_mutex_unlock(session->lock); h3_session_wake(session); + + return resplen; } void h3_stream_response_ack_locked(h3_stream* stream, uint64_t datalen) diff --git a/modules/http3/h3_stream.c b/modules/http3/h3_stream.c index 131511e35c9..6ac6d7954f4 100644 --- a/modules/http3/h3_stream.c +++ b/modules/http3/h3_stream.c @@ -52,11 +52,20 @@ static void mark_ngh3_dead(h3_session* session, const char* op, int64_t stream_i /* Re-enable write-blocked streams once their QUIC send buffer has room. */ static void unblock_writable_streams(h3_session* session) { - if (session->blocked_streams <= 0) + if (!session || session->blocked_streams <= 0 || !session->ngh3) { return; } - for (apr_hash_index_t* hi = apr_hash_first(NULL, session->streams); hi; hi = apr_hash_next(hi)) + + apr_pool_t* tmp_pool = NULL; + if (apr_pool_create(&tmp_pool, session->pool) != APR_SUCCESS) + { + return; + } + + apr_array_header_t* unblocked = apr_array_make(tmp_pool, 4, sizeof(h3_stream*)); + + for (apr_hash_index_t* hi = apr_hash_first(tmp_pool, session->streams); hi; hi = apr_hash_next(hi)) { h3_stream* h3s = apr_hash_this_val(hi); if (!h3s || !h3s->write_blocked) @@ -67,17 +76,35 @@ static void unblock_writable_streams(h3_session* session) { continue; /* still full */ } + *(h3_stream**)apr_array_push(unblocked) = h3s; + } + + for (int i = 0; i < unblocked->nelts; i++) + { + h3_stream* h3s = ((h3_stream**)unblocked->elts)[i]; + if (!h3s || !h3s->write_blocked || !session->ngh3) + { + continue; + } h3s->write_blocked = 0; - session->blocked_streams--; + session->blocked_streams--; nghttp3_conn_unblock_stream(session->ngh3, h3s->stream_id); } + + apr_pool_destroy(tmp_pool); } void flush_nghttp3(h3_session* session) { CHECK(session); - CHECK(!session->ngh3_dead, return;); + CHECK(!apr_atomic_read32(&session->ngh3_dead), return;); unblock_writable_streams(session); + + if (session->qpack_encoder_stream && session->qpack_encoder_stream->write_blocked) + { + goto drain_pending_free; + } + for (;;) { nghttp3_vec vec[16] = {0}; @@ -137,7 +164,7 @@ void flush_nghttp3(h3_session* session) session->blocked_streams++; } nghttp3_conn_block_stream(session->ngh3, sid); - if (res.conn_blocked) + if (res.conn_blocked || h3s == session->qpack_encoder_stream) { /* A stream written later in this pass could overtake this one, * e.g. HEADERS overtaking the QPACK inserts they reference. */ @@ -146,6 +173,8 @@ void flush_nghttp3(h3_session* session) continue; } } + + drain_pending_free: if (session->pending_free->nelts > 0) { while (session->pending_free->nelts > 0) @@ -191,10 +220,17 @@ h3_stream* track_stream(h3_session* session, int64_t sid, h3q_stream* qstream) static void mark_ngh3_dead(h3_session* session, const char* op, int64_t stream_id, nghttp3_ssize liberr) { - session->ngh3_dead = 1; - session->abort_quic_error_code = nghttp3_err_infer_quic_app_error_code((int)liberr); - session->abort_reason = nghttp3_strerror((int)liberr); - ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "%s failed for stream %" APR_INT64_T_FMT " (%s, err=%" APR_INT64_T_FMT "); closing with QUIC error 0x%" APR_UINT64_T_HEX_FMT, op, (apr_int64_t)stream_id, session->abort_reason, (apr_int64_t)liberr, (apr_uint64_t)session->abort_quic_error_code); + uint64_t code = nghttp3_err_infer_quic_app_error_code((int)liberr); + const char* reason = nghttp3_strerror((int)liberr); + + if (apr_atomic_cas32(&session->ngh3_dead, 1, 0) == 0) + { + session->abort_quic_error_code = code; + session->abort_reason = reason; + apr_atomic_set32(&session->aborted, 1); + } + + ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "%s failed for stream %" APR_INT64_T_FMT " (%s, err=%" APR_INT64_T_FMT "); closing with QUIC error 0x%" APR_UINT64_T_HEX_FMT, op, (apr_int64_t)stream_id, reason, (apr_int64_t)liberr, (apr_uint64_t)code); } /* nghttp3 reports an RFC 9114 4.x header violation as one of these non-fatal read errors. */ @@ -237,7 +273,7 @@ static void reject_malformed_stream(h3_session* session, h3_stream* h3s, nghttp3 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, session->s, "closing HTTP/3 connection after %u client-caused stream errors (H3MaxStreamErrors %u)", (unsigned)session->stream_errors, (unsigned)limit); session->abort_quic_error_code = NGHTTP3_H3_EXCESSIVE_LOAD; session->abort_reason = "too many malformed requests"; - session->ngh3_dead = 1; + apr_atomic_set32(&session->ngh3_dead,1); } } @@ -401,12 +437,12 @@ apr_array_header_t* drain_ready_streams(h3_session* session, apr_pool_t* loop_po unsigned int total_streams = apr_hash_count(session->streams); unsigned int iterations = 0; apr_array_header_t* snapshot = apr_array_make(loop_pool, 8, sizeof(h3_stream*)); - for (apr_hash_index_t* hi = apr_hash_first(NULL, session->streams); hi; hi = apr_hash_next(hi)) + for (apr_hash_index_t* hi = apr_hash_first(loop_pool, session->streams); hi; hi = apr_hash_next(hi)) { if (++iterations > total_streams + 100) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "hash iteration did not terminate after %u entries (hash reports %u) - hash corruption, aborting connection", iterations, total_streams); - session->aborted = 1; + apr_atomic_set32(&session->aborted,1); break; } h3_stream* h3s = apr_hash_this_val(hi); @@ -421,7 +457,7 @@ apr_array_header_t* drain_ready_streams(h3_session* session, apr_pool_t* loop_po size_t start = snapshot->nelts > 0 ? session->stream_drain_cursor % (size_t)snapshot->nelts : 0; for (int offset = 0; offset < snapshot->nelts; offset++) { - if (session->ngh3_dead) + if (apr_atomic_read32(&session->ngh3_dead)) { break; } @@ -436,7 +472,7 @@ apr_array_header_t* drain_ready_streams(h3_session* session, apr_pool_t* loop_po { continue; } - if (drain_one_stream(session, h3s, data_read, &reads_remaining, &bytes_remaining) && !session->ngh3_dead) + if (drain_one_stream(session, h3s, data_read, &reads_remaining, &bytes_remaining) && !apr_atomic_read32(&session->ngh3_dead)) { h3_stream** slot = (h3_stream**)apr_array_push(completed); *slot = h3s; @@ -497,7 +533,7 @@ void h3_stream_acked(void* user, int64_t stream_id, size_t len) { h3_session* session = user; apr_thread_mutex_lock(session->lock); - if (!session->ngh3_dead) + if (!apr_atomic_read32(&session->ngh3_dead)) { (void)nghttp3_conn_add_ack_offset(session->ngh3, stream_id, len); } diff --git a/modules/http3/h3_threads.c b/modules/http3/h3_threads.c index ec6c4576987..06297f126e4 100644 --- a/modules/http3/h3_threads.c +++ b/modules/http3/h3_threads.c @@ -73,7 +73,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "event thread started"); int work_pending = 0; int listener_failed = 0; - while (io->thread_running || io->active_sessions->nelts > 0) + while (apr_atomic_read32(&io->thread_running) || io->active_sessions->nelts > 0) { if (!work_pending) { @@ -103,7 +103,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) } while (pumped > 0 && ++pump_rounds < H3_PUMP_BUDGET); } - if (io->thread_running) + if (apr_atomic_read32(&io->thread_running)) { for (;;) { @@ -113,7 +113,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) break; } /* The engine has already answered these clients: refuse them, or they wait out their idle timeout. */ - if (io->draining) + if (apr_atomic_read32(&io->draining)) { h3q_conn_refuse(io->qengine, conn); /* no new connections during a graceful stop */ continue; @@ -132,7 +132,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) } } } - if (io->thread_running) + if (apr_atomic_read32(&io->thread_running)) { /* Also while draining: a pending handshake counts as an MPM connection, so it must finish or time out. */ progress_pending_handshakes(io); @@ -156,7 +156,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) * stream limit change in the engine, no worker output. The sweep still * serves it once a second, for its idle and stream timeouts. Every session * keeps its control streams, so a stream count cannot tell idle sessions. */ - if (!io->draining && io->thread_running && !session->aborted + if (!apr_atomic_read32(&io->draining) && apr_atomic_read32(&io->thread_running) && !apr_atomic_read32(&session->aborted) && apr_atomic_read32(&session->active_tasks) == 0 && !h3q_conn_take_activity(session->qconn) && !apr_atomic_xchg32(&session->wake, 0) @@ -167,16 +167,16 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) } session->next_sweep = now + apr_time_from_sec(1); - if (!session->aborted && service_session_pass(io, session)) + if (!apr_atomic_read32(&session->aborted) && service_session_pass(io, session)) { work_pending = 1; apr_atomic_set32(&session->wake, 1); /* the pass ran out of budget: serve it again next round */ } - if (session->aborted) + if (apr_atomic_read32(&session->aborted)) { - int is_rapid = (!io->thread_running || io->draining); - int shutdown_done = h3q_conn_shutdown(session->qconn, is_rapid, session->abort_quic_error_code, session->ngh3_dead ? session->abort_reason : NULL); + int is_rapid = (!apr_atomic_read32(&io->thread_running) || apr_atomic_read32(&io->draining)); + int shutdown_done = h3q_conn_shutdown(session->qconn, is_rapid, session->abort_quic_error_code, apr_atomic_read32(&session->ngh3_dead) ? session->abort_reason : NULL); if (shutdown_done && apr_atomic_read32(&session->active_tasks) == 0) { diff --git a/modules/http3/h3_threads.c.orig b/modules/http3/h3_threads.c.orig new file mode 100644 index 00000000000..ec6c4576987 --- /dev/null +++ b/modules/http3/h3_threads.c.orig @@ -0,0 +1,209 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +#include +#include +#include + +#include "h3.h" +#include "h3_io.h" +#include "h3_session.h" +#include "h3_threads.h" +#include "mod_http3.h" +#include "quic/h3q.h" +#include "quic/h3q_conn.h" + +#define H3_PUMP_BUDGET 32 + +apr_status_t h3_thread_safe_pool_create(apr_pool_t* parent, const char* tag, apr_pool_t** out) +{ + apr_allocator_t* allocator = NULL; + apr_thread_mutex_t* mutex = NULL; + apr_status_t rv = apr_allocator_create(&allocator); + if (rv != APR_SUCCESS) + { + return rv; + } + rv = apr_pool_create_ex(out, parent, NULL, allocator); + if (rv != APR_SUCCESS) + { + apr_allocator_destroy(allocator); + return rv; + } + apr_allocator_owner_set(allocator, *out); + apr_pool_tag(*out, tag); + rv = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, *out); + if (rv != APR_SUCCESS) + { + apr_pool_destroy(*out); + *out = NULL; + return rv; + } + apr_allocator_mutex_set(allocator, mutex); + return APR_SUCCESS; +} + +void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) +{ + (void)thread; + h3_io_t* io = data; + if (!io) + { + ap_log_perror(APLOG_MARK, APLOG_ERR, 0, NULL, "h3_event_thread: NULL io"); + return NULL; + } + ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "event thread started"); + int work_pending = 0; + int listener_failed = 0; + while (io->thread_running || io->active_sessions->nelts > 0) + { + if (!work_pending) + { + wait_for_event(io); + } + work_pending = 0; + { + int pump_rounds = 0; + int pumped; + do { + pumped = h3q_engine_pump(io->qengine); + if (pumped < 0) + { + if (!listener_failed) + { + listener_failed = 1; + ap_log_error(APLOG_MARK, APLOG_ERR, 0, io->server, "QUIC listener event processing failed; no further datagrams will be handled"); + } + apr_sleep(apr_time_from_msec(H3_LISTENER_FAILED_BACKOFF_MS)); + break; + } + listener_failed = 0; + if (pumped > 0) + { + work_pending = 1; + } + } while (pumped > 0 && ++pump_rounds < H3_PUMP_BUDGET); + } + + if (io->thread_running) + { + for (;;) + { + h3q_conn* conn = h3q_engine_accept_conn(io->qengine); + if (!conn) + { + break; + } + /* The engine has already answered these clients: refuse them, or they wait out their idle timeout. */ + if (io->draining) + { + h3q_conn_refuse(io->qengine, conn); /* no new connections during a graceful stop */ + continue; + } + ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "accepted new QUIC connection"); + if (h3_io_at_connection_limit(io)) + { + h3_server_conf* conf = ap_get_module_config(io->server->module_config, &http3_module); + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, io->server, "dropping QUIC connection: at H3MaxConnections limit (%u)", conf->h3_max_connections); + h3q_conn_refuse(io->qengine, conn); + continue; + } + if (!prepare_accepted_connection(io, conn)) + { + h3q_conn_free(conn); + } + } + } + if (io->thread_running) + { + /* Also while draining: a pending handshake counts as an MPM connection, so it must finish or time out. */ + progress_pending_handshakes(io); + } + + apr_time_t now = apr_time_now(); + for (int i = 0; i < io->active_sessions->nelts;) + { + if (i > 0 && (i % 8) == 0) + { + int p = h3q_engine_pump(io->qengine); + if (p > 0) + { + work_pending = 1; + } + } + + h3_session* session = ((h3_session**)io->active_sessions->elts)[i]; + + /* Skip a session with nothing new: no busy worker, no packet, timer or + * stream limit change in the engine, no worker output. The sweep still + * serves it once a second, for its idle and stream timeouts. Every session + * keeps its control streams, so a stream count cannot tell idle sessions. */ + if (!io->draining && io->thread_running && !session->aborted + && apr_atomic_read32(&session->active_tasks) == 0 + && !h3q_conn_take_activity(session->qconn) + && !apr_atomic_xchg32(&session->wake, 0) + && now < session->next_sweep) + { + i++; + continue; + } + session->next_sweep = now + apr_time_from_sec(1); + + if (!session->aborted && service_session_pass(io, session)) + { + work_pending = 1; + apr_atomic_set32(&session->wake, 1); /* the pass ran out of budget: serve it again next round */ + } + + if (session->aborted) + { + int is_rapid = (!io->thread_running || io->draining); + int shutdown_done = h3q_conn_shutdown(session->qconn, is_rapid, session->abort_quic_error_code, session->ngh3_dead ? session->abort_reason : NULL); + + if (shutdown_done && apr_atomic_read32(&session->active_tasks) == 0) + { + ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "connection servicing done"); + h3_session_destroy(session); + if (io->note_conn_removed) + { + io->note_conn_removed(); + } + if (i < io->active_sessions->nelts - 1) + { + ((h3_session**)io->active_sessions->elts)[i] = ((h3_session**)io->active_sessions->elts)[io->active_sessions->nelts - 1]; + } + io->active_sessions->nelts--; + apr_atomic_dec32(&io->active_session_count); + continue; /* Do not increment i: the last element was swapped into this slot. */ + } + } + i++; + } + } + while (io->pending_handshakes->nelts > 0) + { + h3_pending_handshake* pending = (h3_pending_handshake*)io->pending_handshakes->elts; + h3q_conn_shutdown(pending[0].conn, 1, 0, NULL); + remove_pending_handshake(io, 0, 1); + } + ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "event thread exiting"); + return NULL; +} diff --git a/modules/http3/include/h3_config.h b/modules/http3/include/h3_config.h index ddbeea0e009..481cc7ef89a 100644 --- a/modules/http3/include/h3_config.h +++ b/modules/http3/include/h3_config.h @@ -57,8 +57,10 @@ struct h3_server_conf apr_uint32_t h3_stream_timeout; apr_uint32_t h3_max_stream_errors; int h3_qpack_capacity_set; + int h3_qpack_encoder_capacity_set; int h3_qpack_blocked_set; apr_uint32_t h3_qpack_table_capacity; + apr_uint32_t h3_qpack_encoder_table_capacity; apr_uint32_t h3_qpack_blocked_streams; apr_uint32_t h3_min_workers; apr_uint32_t h3_max_workers; diff --git a/modules/http3/include/h3_config.h.orig b/modules/http3/include/h3_config.h.orig new file mode 100644 index 00000000000..ddbeea0e009 --- /dev/null +++ b/modules/http3/include/h3_config.h.orig @@ -0,0 +1,140 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef H3_CONFIG_H +#define H3_CONFIG_H + +#include + +#include + +extern const command_rec h3_cmds[]; + +typedef struct h3_server_conf h3_server_conf; + +typedef enum +{ + H3_FLAG_UNSET = 0, + H3_FLAG_ON, + H3_FLAG_OFF +} h3_tri_flag; + +struct h3_server_conf +{ + apr_port_t host_port; + + /** QUIC TLS context, built from mod_ssl's certificate when the host lists h3 in Protocols. */ + struct ssl_ctx_st* ssl_ctx; + + apr_port_t h3_port; + apr_uint32_t h3_max_concurrent_streams; + apr_uint32_t h3_max_connections; + apr_size_t h3_stream_buffer_size; + apr_size_t h3_max_request_body_size; + apr_size_t h3_max_response_body_size; + h3_tri_flag h3_alt_svc; + h3_tri_flag h3_address_validation; + apr_uint32_t h3_alt_svc_max_age; + apr_uint32_t h3_handshake_timeout; + apr_uint32_t h3_idle_timeout; + apr_size_t h3_socket_buffer_size; + apr_size_t h3_max_window; + h3_tri_flag h3_session_tickets; + h3_tri_flag h3_early_data; + apr_uint32_t h3_stream_timeout; + apr_uint32_t h3_max_stream_errors; + int h3_qpack_capacity_set; + int h3_qpack_blocked_set; + apr_uint32_t h3_qpack_table_capacity; + apr_uint32_t h3_qpack_blocked_streams; + apr_uint32_t h3_min_workers; + apr_uint32_t h3_max_workers; + apr_uint32_t h3_max_worker_idle_seconds; +}; + +/** + * Look up the TCP Listen port of a vhost by scanning its `Port` and + * `Listen` directive state. + * @param s The server_rec. + * @return The TCP port, or 0 if undetermined. + */ +apr_port_t get_server_port(const server_rec* s); + +/** + * ap_create_server_config callback: allocate a fresh zero-initialised + * h3_server_conf from @p p. All fields start as NULL/0. + * @param p Pool used for the allocation. + * @param s The vhost the config belongs to (unused). + * @return The new h3_server_conf. + */ +void* h3_create_server_config(apr_pool_t* p, server_rec* s); + +/** + * ap_merge_server_config callback: produce a child vhost config that + * inherits each unset field from the parent. + * @param p Pool for the merged config. + * @param base_conf Parent h3_server_conf. + * @param new_conf Child h3_server_conf. + * @return The merged h3_server_conf. + */ +void* h3_merge_server_config(apr_pool_t* p, void* base_conf, void* new_conf); + +/** + * ap_create_dir_config callback. mod_http3 has no per-directory state; + * returns a 1-byte allocation so httpd takes ownership of a non-NULL + * pointer. + * @param p Pool for the allocation. + * @param dir The directory path (unused). + * @return A 1-byte zero-initialised block. + */ +void* h3_create_dir_config(apr_pool_t* p, char* dir); + +/** + * ap_merge_dir_config callback. Identity merge: per-directory config is + * a no-op for mod_http3, so the base is returned unchanged. + * @param p Pool for any allocation (unused). + * @param base Base per-dir config. + * @param add Per-dir config being merged in (unused). + * @return The base per-dir config. + */ +void* h3_merge_dir_config(apr_pool_t* p, void* base, void* add); + +/** + * ap_ssl_add_cert_files hook: mod_ssl runs it for every SSLEngine vhost with + * the certificate and key files it is about to load (SSLCertificateFile plus + * anything mod_md added). When the vhost lists h3 in Protocols, builds its + * QUIC TLS context from them, here, before the server drops privileges. + * @param s The vhost being configured. + * @param p Config pool; owns the context. + * @param cert_files Certificate chain files, const char* elements. + * @param key_files Private key files, const char* elements. + * @return DECLINED, or HTTP_INTERNAL_SERVER_ERROR if the files do not load. + */ +int h3_ssl_add_cert_files(server_rec* s, apr_pool_t* p, apr_array_header_t* cert_files, apr_array_header_t* key_files); + +/** + * ap_post_config hook: fill in defaults on every vhost that serves HTTP/3; + * the first one owns the listener, and an SNI callback swaps in each other + * host's certificate by name. No-op in AP_SQ_MS_CREATE_PRE_CONFIG. + * @param p Config pool; owns the SNI host table. + * @param plog Log pool (unused). + * @param ptemp Temp pool (unused). + * @param s The first server_rec in the configuration. + * @return OK, or HTTP_INTERNAL_SERVER_ERROR if no vhost serves HTTP/3. + */ +int h3_post_config(apr_pool_t* p, apr_pool_t* plog, apr_pool_t* ptemp, server_rec* s); + +#endif /* H3_CONFIG_H */ diff --git a/modules/http3/include/h3_io.h b/modules/http3/include/h3_io.h index 917dbed7bbb..3ea6966e9ac 100644 --- a/modules/http3/include/h3_io.h +++ b/modules/http3/include/h3_io.h @@ -44,11 +44,11 @@ typedef struct h3_io_t volatile apr_uint32_t total_streams; volatile apr_uint64_t total_bytes_read; volatile apr_uint64_t total_bytes_written; - volatile int thread_running; + volatile apr_uint32_t thread_running; /* Set by a graceful stop: keep servicing the sessions we already have, * but accept no new ones. The event thread keeps running so in-flight * streams can finish; the MPM bounds how long the child waits for them. */ - volatile int draining; + volatile apr_uint32_t draining; h3_wakeup wakeup; APR_OPTIONAL_FN_TYPE(ap_mpm_note_extra_connection_added) * note_conn_added; diff --git a/modules/http3/include/h3_session.h b/modules/http3/include/h3_session.h index 0d5ac91ee81..366c973e428 100644 --- a/modules/http3/include/h3_session.h +++ b/modules/http3/include/h3_session.h @@ -58,11 +58,11 @@ struct h3_session apr_thread_mutex_t* lock; apr_hash_t* streams; - int aborted; + volatile int aborted; /* Number of streams write-blocked in nghttp3 (see flush_nghttp3). */ int blocked_streams; - int ngh3_dead; + volatile int ngh3_dead; uint64_t abort_quic_error_code; const char* abort_reason; @@ -90,6 +90,8 @@ struct h3_session int64_t sid; h3_stream* h3s; } pending; + + h3_stream* qpack_encoder_stream; }; struct h3_stream @@ -201,7 +203,7 @@ nghttp3_ssize h3_session_read_data(nghttp3_conn* conn, int64_t stream_id, nghttp apr_status_t h3_stream_response_append(h3_stream* stream, const uint8_t* data, size_t len); /** Mark the response producer complete and resume a blocked nghttp3 reader. */ -void h3_stream_response_complete(h3_stream* stream); +size_t h3_stream_response_complete(h3_stream* stream); /** * Account application response bytes acknowledged by nghttp3. The session From 5fc6533222744b078062a75db69b003b20c7ef5c Mon Sep 17 00:00:00 2001 From: Pierre Brochard Date: Thu, 26 Nov 2026 23:11:26 +0100 Subject: [PATCH 2/4] Corrections --- modules/http3/h3_config.c.orig | 837 ------------------------- modules/http3/h3_threads.c.orig | 209 ------ modules/http3/include/h3_config.h.orig | 140 ----- modules/http3/include/h3_session.h | 4 +- 4 files changed, 2 insertions(+), 1188 deletions(-) delete mode 100644 modules/http3/h3_config.c.orig delete mode 100644 modules/http3/h3_threads.c.orig delete mode 100644 modules/http3/include/h3_config.h.orig diff --git a/modules/http3/h3_config.c.orig b/modules/http3/h3_config.c.orig deleted file mode 100644 index 2cd85a82730..00000000000 --- a/modules/http3/h3_config.c.orig +++ /dev/null @@ -1,837 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include "h3.h" -#include "h3_check.h" -#include "h3_config.h" -#include "h3_os.h" -#include "h3_request.h" -#include "h3_server.h" -#include "mod_http3.h" -#include "quic/detail/h3q_tls.h" - -apr_port_t get_server_port(const server_rec* s) -{ - for (const server_addr_rec* sar = s->addrs; sar != NULL; sar = sar->next) - { - if (sar->host_port != 0) - { - return sar->host_port; - } - } - return 0; -} - -void* h3_create_server_config(apr_pool_t* p, server_rec* s H3_UNUSED) -{ - return apr_pcalloc(p, sizeof(h3_server_conf)); -} - -void* h3_merge_server_config(apr_pool_t* p, void* base_conf, void* new_conf) -{ - h3_server_conf* merged = apr_pcalloc(p, sizeof(h3_server_conf)); - h3_server_conf* base = (h3_server_conf*)base_conf; - h3_server_conf* new = (h3_server_conf*)new_conf; - - merged->h3_port = new->h3_port ? new->h3_port : base->h3_port; - merged->h3_max_concurrent_streams = new->h3_max_concurrent_streams ? new->h3_max_concurrent_streams : base->h3_max_concurrent_streams; - merged->h3_max_connections = new->h3_max_connections ? new->h3_max_connections : base->h3_max_connections; - merged->h3_stream_buffer_size = new->h3_stream_buffer_size ? new->h3_stream_buffer_size : base->h3_stream_buffer_size; - merged->h3_max_request_body_size = new->h3_max_request_body_size ? new->h3_max_request_body_size : base->h3_max_request_body_size; - merged->h3_max_response_body_size = new->h3_max_response_body_size ? new->h3_max_response_body_size : base->h3_max_response_body_size; - merged->h3_alt_svc = new->h3_alt_svc != H3_FLAG_UNSET ? new->h3_alt_svc : base->h3_alt_svc; - merged->h3_address_validation = new->h3_address_validation != H3_FLAG_UNSET ? new->h3_address_validation : base->h3_address_validation; - merged->h3_alt_svc_max_age = new->h3_alt_svc_max_age ? new->h3_alt_svc_max_age : base->h3_alt_svc_max_age; - merged->h3_handshake_timeout = new->h3_handshake_timeout ? new->h3_handshake_timeout : base->h3_handshake_timeout; - merged->h3_idle_timeout = new->h3_idle_timeout ? new->h3_idle_timeout : base->h3_idle_timeout; - merged->h3_socket_buffer_size = new->h3_socket_buffer_size ? new->h3_socket_buffer_size : base->h3_socket_buffer_size; - merged->h3_max_window = new->h3_max_window ? new->h3_max_window : base->h3_max_window; - merged->h3_session_tickets = new->h3_session_tickets != H3_FLAG_UNSET ? new->h3_session_tickets : base->h3_session_tickets; - merged->h3_early_data = new->h3_early_data != H3_FLAG_UNSET ? new->h3_early_data : base->h3_early_data; - merged->h3_stream_timeout = new->h3_stream_timeout ? new->h3_stream_timeout : base->h3_stream_timeout; - merged->h3_max_stream_errors = new->h3_max_stream_errors ? new->h3_max_stream_errors : base->h3_max_stream_errors; - merged->h3_qpack_capacity_set = new->h3_qpack_capacity_set ? new->h3_qpack_capacity_set : base->h3_qpack_capacity_set; - merged->h3_qpack_table_capacity = new->h3_qpack_capacity_set ? new->h3_qpack_table_capacity : base->h3_qpack_table_capacity; - merged->h3_qpack_blocked_set = new->h3_qpack_blocked_set ? new->h3_qpack_blocked_set : base->h3_qpack_blocked_set; - merged->h3_qpack_blocked_streams = new->h3_qpack_blocked_set ? new->h3_qpack_blocked_streams : base->h3_qpack_blocked_streams; - merged->h3_min_workers = new->h3_min_workers ? new->h3_min_workers : base->h3_min_workers; - merged->h3_max_workers = new->h3_max_workers ? new->h3_max_workers : base->h3_max_workers; - merged->h3_max_worker_idle_seconds = new->h3_max_worker_idle_seconds ? new->h3_max_worker_idle_seconds : base->h3_max_worker_idle_seconds; - - return merged; -} - -/// Value of an MPM query, or -1 when the active MPM does not answer it. -static int mpm_query(int code) -{ - int value = 0; - return ap_mpm_query(code, &value) == APR_SUCCESS ? value : -1; -} - -static const char* set_h3_port(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3Port: empty port number"; - } - apr_int64_t port = 0; - apr_status_t rv = apr_cstr_atoi64(&port, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3Port: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3Port: '%s' is out of representable range", arg); - } - if (port < 1 || port > 65535) - { - return apr_psprintf(cmd->pool, "H3Port: '%s' is out of allowed range (1-65535)", arg); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_port = (apr_port_t)port; - return NULL; -} - -static const char* set_h3_max_concurrent_streams(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3MaxConcurrentStreams: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3MaxConcurrentStreams: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3MaxConcurrentStreams: '%s' is out of representable range", arg); - } - if (val == 0 || val > UINT16_MAX) - { - return apr_psprintf(cmd->pool, "H3MaxConcurrentStreams: '%s' is out of allowed range (1-%u)", arg, (unsigned)UINT16_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_max_concurrent_streams = (apr_uint32_t)val; - return NULL; -} - -static const char* set_h3_max_connections(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3MaxConnections: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3MaxConnections: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3MaxConnections: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_MAX_CONNECTIONS_MAX) - { - return apr_psprintf(cmd->pool, "H3MaxConnections: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_MAX_CONNECTIONS_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_max_connections = (apr_uint32_t)val; - return NULL; -} - -static const char* set_h3_stream_buffer_size(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3StreamBufferSize: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3StreamBufferSize: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3StreamBufferSize: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_STREAM_BUFFER_SIZE_MAX) - { - return apr_psprintf(cmd->pool, "H3StreamBufferSize: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_STREAM_BUFFER_SIZE_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_stream_buffer_size = (apr_size_t)val; - return NULL; -} - -static const char* set_h3_max_window(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - apr_uint64_t val = 0; - if (apr_cstr_atoui64(&val, arg) != APR_SUCCESS || val < H3_MAX_WINDOW_MIN || val > H3_MAX_WINDOW_MAX) - { - return apr_psprintf(cmd->pool, "H3MaxWindow: '%s' must be a byte count from %lu to %lu", arg, (unsigned long)H3_MAX_WINDOW_MIN, (unsigned long)H3_MAX_WINDOW_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_max_window = (apr_size_t)val; - return NULL; -} - -static const char* set_h3_socket_buffer_size(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3SocketBufferSize: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3SocketBufferSize: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3SocketBufferSize: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_SOCKET_BUFFER_SIZE_MAX) - { - return apr_psprintf(cmd->pool, "H3SocketBufferSize: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_SOCKET_BUFFER_SIZE_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_socket_buffer_size = (apr_size_t)val; - return NULL; -} - -static const char* set_h3_stream_timeout(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3StreamTimeout: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3StreamTimeout: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3StreamTimeout: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_STREAM_TIMEOUT_MAX) - { - return apr_psprintf(cmd->pool, "H3StreamTimeout: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_STREAM_TIMEOUT_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_stream_timeout = (apr_uint32_t)val; - return NULL; -} - -static const char* set_h3_max_stream_errors(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3MaxStreamErrors: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3MaxStreamErrors: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3MaxStreamErrors: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_MAX_STREAM_ERRORS_MAX) - { - return apr_psprintf(cmd->pool, "H3MaxStreamErrors: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_MAX_STREAM_ERRORS_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_max_stream_errors = (apr_uint32_t)val; - return NULL; -} - -static const char* set_h3_qpack_table_capacity(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3QpackTableCapacity: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3QpackTableCapacity: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3QpackTableCapacity: '%s' is out of representable range", arg); - } - if (val > H3_QPACK_TABLE_CAPACITY_MAX) - { - return apr_psprintf(cmd->pool, "H3QpackTableCapacity: '%s' is out of allowed range (0-%lu)", arg, (unsigned long)H3_QPACK_TABLE_CAPACITY_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_qpack_table_capacity = (apr_uint32_t)val; - conf->h3_qpack_capacity_set = 1; - return NULL; -} - -static const char* set_h3_qpack_blocked_streams(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3QpackBlockedStreams: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3QpackBlockedStreams: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3QpackBlockedStreams: '%s' is out of representable range", arg); - } - if (val > H3_QPACK_BLOCKED_STREAMS_MAX) - { - return apr_psprintf(cmd->pool, "H3QpackBlockedStreams: '%s' is out of allowed range (0-%lu)", arg, (unsigned long)H3_QPACK_BLOCKED_STREAMS_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_qpack_blocked_streams = (apr_uint32_t)val; - conf->h3_qpack_blocked_set = 1; - return NULL; -} - -static const char* set_h3_min_workers(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3MinWorkers: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3MinWorkers: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3MinWorkers: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_WORKERS_MAX) - { - return apr_psprintf(cmd->pool, "H3MinWorkers: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_WORKERS_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_min_workers = (apr_uint32_t)val; - return NULL; -} - -static const char* set_h3_max_workers(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3MaxWorkers: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3MaxWorkers: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3MaxWorkers: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_WORKERS_MAX) - { - return apr_psprintf(cmd->pool, "H3MaxWorkers: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_WORKERS_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_max_workers = (apr_uint32_t)val; - return NULL; -} - -static const char* set_h3_max_worker_idle_seconds(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3MaxWorkerIdleSeconds: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3MaxWorkerIdleSeconds: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3MaxWorkerIdleSeconds: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_MAX_WORKER_IDLE_SECONDS_MAX) - { - return apr_psprintf(cmd->pool, "H3MaxWorkerIdleSeconds: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_MAX_WORKER_IDLE_SECONDS_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_max_worker_idle_seconds = (apr_uint32_t)val; - return NULL; -} - -static const char* set_h3_max_request_body_size(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3MaxRequestBodySize: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3MaxRequestBodySize: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3MaxRequestBodySize: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_MAX_REQUEST_BODY_SIZE_MAX) - { - return apr_psprintf(cmd->pool, "H3MaxRequestBodySize: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_MAX_REQUEST_BODY_SIZE_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_max_request_body_size = (apr_size_t)val; - return NULL; -} - -static const char* set_h3_max_response_body_size(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3MaxResponseBodySize: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3MaxResponseBodySize: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3MaxResponseBodySize: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_MAX_RESPONSE_BODY_SIZE_MAX) - { - return apr_psprintf(cmd->pool, "H3MaxResponseBodySize: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_MAX_RESPONSE_BODY_SIZE_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_max_response_body_size = (apr_size_t)val; - return NULL; -} - -static const char* set_h3_handshake_timeout(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3HandshakeTimeout: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3HandshakeTimeout: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3HandshakeTimeout: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_HANDSHAKE_TIMEOUT_MAX) - { - return apr_psprintf(cmd->pool, "H3HandshakeTimeout: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_HANDSHAKE_TIMEOUT_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_handshake_timeout = (apr_uint32_t)val; - return NULL; -} - -static const char* set_h3_idle_timeout(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3IdleTimeout: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3IdleTimeout: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3IdleTimeout: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_IDLE_TIMEOUT_MAX) - { - return apr_psprintf(cmd->pool, "H3IdleTimeout: '%s' is out of allowed range (1-%u)", arg, (unsigned)H3_IDLE_TIMEOUT_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_idle_timeout = (apr_uint32_t)val; - return NULL; -} - -static const char* set_h3_session_tickets(cmd_parms* cmd, void* dummy H3_UNUSED, int flag) -{ - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_session_tickets = flag ? H3_FLAG_ON : H3_FLAG_OFF; - return NULL; -} - -static const char* set_h3_early_data(cmd_parms* cmd, void* dummy H3_UNUSED, int flag) -{ -#ifdef H3Q_BACKEND_OSSL - if (flag) - { - return "H3EarlyData is not supported when mod_http3 is built with --with-http3-quic=openssl"; - } -#endif - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_early_data = flag ? H3_FLAG_ON : H3_FLAG_OFF; - return NULL; -} - -static const char* set_h3_alt_svc(cmd_parms* cmd, void* dummy H3_UNUSED, int flag) -{ - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_alt_svc = flag ? H3_FLAG_ON : H3_FLAG_OFF; - return NULL; -} - -static const char* set_h3_address_validation(cmd_parms* cmd, void* dummy H3_UNUSED, int flag) -{ - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_address_validation = flag ? H3_FLAG_ON : H3_FLAG_OFF; - return NULL; -} - -static const char* set_h3_alt_svc_max_age(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) -{ - if (!arg || !*arg) - { - return "H3AltSvcMaxAge: empty value"; - } - apr_uint64_t val = 0; - apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv == APR_EINVAL) - { - return apr_psprintf(cmd->pool, "H3AltSvcMaxAge: '%s' is not a number", arg); - } - if (rv == APR_ERANGE) - { - return apr_psprintf(cmd->pool, "H3AltSvcMaxAge: '%s' is out of representable range", arg); - } - if (val == 0 || val > H3_ALT_SVC_MAX_AGE_MAX) - { - return apr_psprintf(cmd->pool, "H3AltSvcMaxAge: '%s' is out of allowed range (1-%lu)", arg, (unsigned long)H3_ALT_SVC_MAX_AGE_MAX); - } - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_alt_svc_max_age = (apr_uint32_t)val; - return NULL; -} - -static apr_status_t ssl_ctx_cleanup(void* data) -{ - SSL_CTX_free(data); - return APR_SUCCESS; -} - -int h3_ssl_add_cert_files(server_rec* s, apr_pool_t* p, apr_array_header_t* cert_files, apr_array_header_t* key_files) -{ - CHECK(s && p && cert_files && key_files, return DECLINED;); - h3_server_conf* conf = ap_get_module_config(s->module_config, &http3_module); - if (!conf || ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG || !ap_is_allowed_protocol(NULL, NULL, s, "h3") || cert_files->nelts == 0) - { - return DECLINED; - } - /* Still privileged here, so a root-only key loads the way it does for mod_ssl. */ - char err[H3Q_ERRLEN] = {0}; - conf->ssl_ctx = h3q_tls_ctx_create((const char* const*)cert_files->elts, (size_t)cert_files->nelts, (const char* const*)key_files->elts, (size_t)key_files->nelts, conf->h3_session_tickets != H3_FLAG_OFF, err, sizeof(err)); - if (!conf->ssl_ctx) - { - ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_http3: %s", err); - return HTTP_INTERNAL_SERVER_ERROR; - } - apr_pool_cleanup_register(p, conf->ssl_ctx, ssl_ctx_cleanup, apr_pool_cleanup_null); - ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "mod_http3: serving HTTP/3 with mod_ssl certificate %s", APR_ARRAY_IDX(cert_files, 0, const char*)); - return DECLINED; -} - -/** One HTTP/3 host name and the TLS context that carries its certificate. */ -typedef struct -{ - const char* name; - SSL_CTX* ctx; -} h3_sni_host; - -/// cert_cb: serve each host its own certificate over one listener, selected by SNI. -/// SSL_set_SSL_CTX does not switch the certificate of a QUIC connection; applying -/// the matched host's certificate, key and chain to the connection does. -static int h3_sni_select_cert(SSL* ssl, void* arg) -{ - const apr_array_header_t* hosts = arg; - const char* sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); - for (int i = 0; sni && i < hosts->nelts; i++) - { - const h3_sni_host* h = &APR_ARRAY_IDX(hosts, i, h3_sni_host); - /* httpd's ServerAlias matcher: wildcards for wild_names, exact otherwise. */ - if (ap_strcasecmp_match(sni, h->name) == 0) - { - STACK_OF(X509)* chain = NULL; - SSL_CTX_get0_chain_certs(h->ctx, &chain); - return SSL_use_certificate(ssl, SSL_CTX_get0_certificate(h->ctx)) == 1 && SSL_use_PrivateKey(ssl, SSL_CTX_get0_privatekey(h->ctx)) == 1 && (!chain || SSL_set1_chain(ssl, chain) == 1); - } - } - return 1; /* no match: the listener's own certificate */ -} - -int h3_post_config(apr_pool_t* p, apr_pool_t* plog H3_UNUSED, apr_pool_t* ptemp, server_rec* s) -{ - CHECK(p); - CHECK(ptemp); - CHECK(s); - h3_server_conf* conf = NULL; - server_rec* owner = NULL; - - if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG) - { - return OK; - } - - /* Names of every HTTP/3 host, so the listener can pick a certificate by SNI. */ - apr_array_header_t* sni = apr_array_make(p, 4, sizeof(h3_sni_host)); - - for (server_rec* vs = s; vs; vs = vs->next) - { - h3_server_conf* vc = ap_get_module_config(vs->module_config, &http3_module); - if (vc->ssl_ctx) - { - if (vs->server_hostname) - { - h3_sni_host* e = apr_array_push(sni); - e->name = vs->server_hostname; - e->ctx = vc->ssl_ctx; - } - /* ServerAlias: httpd keeps exact names in names and wildcards in wild_names. */ - for (int i = 0; vs->names && i < vs->names->nelts; i++) - { - h3_sni_host* e = apr_array_push(sni); - e->name = APR_ARRAY_IDX(vs->names, i, const char*); - e->ctx = vc->ssl_ctx; - } - for (int i = 0; vs->wild_names && i < vs->wild_names->nelts; i++) - { - h3_sni_host* e = apr_array_push(sni); - e->name = APR_ARRAY_IDX(vs->wild_names, i, const char*); - e->ctx = vc->ssl_ctx; - } - vc->host_port = get_server_port(vs); - if (vc->h3_port == 0) - { - vc->h3_port = vc->host_port; - } - if (vc->h3_max_concurrent_streams == 0) - { - vc->h3_max_concurrent_streams = H3_MAX_CONCURRENT_STREAMS_DEFAULT; - } - if (vc->h3_max_connections == 0) - { - vc->h3_max_connections = H3_MAX_CONNECTIONS_DEFAULT; - } - if (vc->h3_stream_buffer_size == 0) - { - vc->h3_stream_buffer_size = H3_STREAM_BUFFER_SIZE_DEFAULT; - } - if (vc->h3_socket_buffer_size == 0) - { - vc->h3_socket_buffer_size = H3_SOCKET_BUFFER_SIZE_DEFAULT; - } - if (vc->h3_max_window == 0) - { - vc->h3_max_window = H3_MAX_WINDOW_DEFAULT; - } - if (vc->h3_max_stream_errors == 0) - { - vc->h3_max_stream_errors = H3_MAX_STREAM_ERRORS_DEFAULT; - } - if (!vc->h3_qpack_capacity_set) - { - vc->h3_qpack_table_capacity = H3_QPACK_TABLE_CAPACITY_DEFAULT; - } - if (!vc->h3_qpack_blocked_set) - { - vc->h3_qpack_blocked_streams = H3_QPACK_BLOCKED_STREAMS_DEFAULT; - } - if (vc->h3_min_workers == 0) - { - vc->h3_min_workers = H3_MIN_WORKERS_DEFAULT; - } - if (vc->h3_max_workers == 0) - { - vc->h3_max_workers = H3_MAX_WORKERS_DEFAULT; - } - if (vc->h3_max_workers < vc->h3_min_workers) - { - ap_log_error(APLOG_MARK, APLOG_WARNING, 0, vs, "mod_http3: H3MaxWorkers (%u) is below H3MinWorkers (%u); raising it to match", (unsigned)vc->h3_max_workers, (unsigned)vc->h3_min_workers); - vc->h3_max_workers = vc->h3_min_workers; - } - if (vc->h3_max_worker_idle_seconds == 0) - { - vc->h3_max_worker_idle_seconds = H3_MAX_WORKER_IDLE_SECONDS_DEFAULT; - } - if (vc->h3_max_request_body_size == 0) - { - vc->h3_max_request_body_size = H3_MAX_REQUEST_BODY_SIZE_DEFAULT; - } - if (vc->h3_max_response_body_size == 0) - { - vc->h3_max_response_body_size = H3_MAX_RESPONSE_BODY_SIZE_DEFAULT; - } - if (vc->h3_alt_svc == H3_FLAG_UNSET) - { - vc->h3_alt_svc = H3_FLAG_ON; - } - if (vc->h3_address_validation == H3_FLAG_UNSET) - { - vc->h3_address_validation = H3_FLAG_ON; - } - if (vc->h3_session_tickets == H3_FLAG_UNSET) - { - vc->h3_session_tickets = H3_FLAG_ON; - } - if (vc->h3_early_data == H3_FLAG_UNSET) - { - vc->h3_early_data = H3_FLAG_OFF; - } - if (vc->h3_alt_svc_max_age == 0) - { - vc->h3_alt_svc_max_age = H3_ALT_SVC_MAX_AGE_DEFAULT; - } - if (vc->h3_handshake_timeout == 0) - { - vc->h3_handshake_timeout = H3_HANDSHAKE_TIMEOUT_DEFAULT; - } - if (vc->h3_idle_timeout == 0) - { - vc->h3_idle_timeout = H3_IDLE_TIMEOUT_DEFAULT; - } - /* httpd prepends virtual hosts, so the last match is the first one defined: - * it owns the listener, as it is mod_ssl's default; the rest still advertise it. */ - conf = vc; - owner = vs; - } - } - - if (!conf) - { - ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_http3: no host serves HTTP/3: add h3 to Protocols on a host with SSLEngine on"); - return HTTP_INTERNAL_SERVER_ERROR; - } - ap_log_error(APLOG_MARK, APLOG_INFO, 0, owner, "h3_post_config: pid=%d h3_port=%d mpm=%s threaded=%d forked=%d max_threads=%d", h3_getpid(), (int)conf->h3_port, ap_show_mpm(), mpm_query(AP_MPMQ_IS_THREADED), mpm_query(AP_MPMQ_IS_FORKED), mpm_query(AP_MPMQ_MAX_THREADS)); - - /* The listener serves conf's certificate by default and swaps by SNI. */ - SSL_CTX_set_cert_cb(conf->ssl_ctx, h3_sni_select_cert, sni); - - h3_request_init(); - h3_server_post_config(s); - return OK; -} - -void* h3_create_dir_config(apr_pool_t* p, char* dir H3_UNUSED) -{ - CHECK(p); - return apr_pcalloc(p, 1); -} - -void* h3_merge_dir_config(apr_pool_t* p H3_UNUSED, void* base, void* add H3_UNUSED) -{ - CHECK(base); - return base; -} - -const command_rec h3_cmds[] = { - AP_INIT_TAKE1("H3Port", set_h3_port, NULL, RSRC_CONF, "UDP port to listen on for QUIC/HTTP-3 (default: same as main server)"), - AP_INIT_TAKE1("H3MaxConcurrentStreams", set_h3_max_concurrent_streams, NULL, RSRC_CONF, "Maximum number of concurrent HTTP/3 streams per connection (default: 100)"), - AP_INIT_TAKE1("H3MaxConnections", set_h3_max_connections, NULL, RSRC_CONF, "Maximum concurrent QUIC/HTTP/3 connections per child process (default: 256)"), - AP_INIT_TAKE1("H3StreamBufferSize", set_h3_stream_buffer_size, NULL, RSRC_CONF, "Per-stream request and streaming-response buffer size in bytes (default: 65536)"), - AP_INIT_TAKE1("H3MaxRequestBodySize", set_h3_max_request_body_size, NULL, RSRC_CONF, "Maximum HTTP/3 request body size in bytes, fully buffered in memory (default: 10485760)"), - AP_INIT_FLAG("H3AltSvc", set_h3_alt_svc, NULL, RSRC_CONF, "Whether to advertise HTTP/3 support via an Alt-Svc response header, required for browser discovery (default: on)"), - AP_INIT_TAKE1("H3AltSvcMaxAge", set_h3_alt_svc_max_age, NULL, RSRC_CONF, "Seconds a client may cache the Alt-Svc HTTP/3 advertisement for (default: 86400)"), - AP_INIT_TAKE1("H3HandshakeTimeout", set_h3_handshake_timeout, NULL, RSRC_CONF, "Timeout in seconds for QUIC handshakes to complete (default: 10)"), - AP_INIT_TAKE1("H3IdleTimeout", set_h3_idle_timeout, NULL, RSRC_CONF, "Idle timeout in seconds for QUIC connections (default: 300)"), - AP_INIT_TAKE1("H3MaxResponseBodySize", set_h3_max_response_body_size, NULL, RSRC_CONF, "Maximum HTTP/3 response body size in bytes; an explicit limit enables bounded whole-response buffering (default: unlimited streaming)"), - AP_INIT_FLAG("H3AddressValidation", set_h3_address_validation, NULL, RSRC_CONF, "Whether to validate client addresses with a QUIC Retry packet before accepting a connection (default: on)"), - AP_INIT_TAKE1("H3SocketBufferSize", set_h3_socket_buffer_size, NULL, RSRC_CONF, "Bytes requested for the QUIC socket send and receive buffers; the OS may grant less (default: 2097152)"), - AP_INIT_TAKE1("H3MaxWindow", set_h3_max_window, NULL, RSRC_CONF, "Largest flow-control window, per connection and per stream, that autotuning may grow a client upload window to (default: 6291456)"), - AP_INIT_FLAG("H3SessionTickets", set_h3_session_tickets, NULL, RSRC_CONF, "Whether to issue TLS session tickets so returning clients can resume instead of running a full handshake (default: on)"), - AP_INIT_FLAG("H3EarlyData", set_h3_early_data, NULL, RSRC_CONF, "Whether to accept 0-RTT request data on resumed connections; unsafe methods wait for the handshake (default: off)"), - AP_INIT_TAKE1("H3StreamTimeout", set_h3_stream_timeout, NULL, RSRC_CONF, "Seconds a response may make no progress before the stream is aborted (default: the server Timeout)"), - AP_INIT_TAKE1("H3MaxStreamErrors", set_h3_max_stream_errors, NULL, RSRC_CONF, "Stream errors one connection may cause before it is closed with H3_EXCESSIVE_LOAD (default: 8)"), - AP_INIT_TAKE1("H3QpackTableCapacity", set_h3_qpack_table_capacity, NULL, RSRC_CONF, "Bytes of QPACK dynamic table a client may use for request headers; 0 disables it (default: 4096)"), - AP_INIT_TAKE1("H3QpackBlockedStreams", set_h3_qpack_blocked_streams, NULL, RSRC_CONF, "Requests that may wait on a QPACK table insert that has not arrived; each one is unbounded memory (default: 0)"), - AP_INIT_TAKE1("H3MinWorkers", set_h3_min_workers, NULL, RSRC_CONF, "Request worker threads started per child process (default: 16)"), - AP_INIT_TAKE1("H3MaxWorkers", set_h3_max_workers, NULL, RSRC_CONF, "Maximum request worker threads per child process (default: 64)"), - AP_INIT_TAKE1("H3MaxWorkerIdleSeconds", set_h3_max_worker_idle_seconds, NULL, RSRC_CONF, "Seconds an idle request worker is kept before it exits (default: 600)"), - AP_INIT_TAKE1(NULL, NULL, NULL, RSRC_CONF, NULL), -}; diff --git a/modules/http3/h3_threads.c.orig b/modules/http3/h3_threads.c.orig deleted file mode 100644 index ec6c4576987..00000000000 --- a/modules/http3/h3_threads.c.orig +++ /dev/null @@ -1,209 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include - -#include -#include -#include - -#include "h3.h" -#include "h3_io.h" -#include "h3_session.h" -#include "h3_threads.h" -#include "mod_http3.h" -#include "quic/h3q.h" -#include "quic/h3q_conn.h" - -#define H3_PUMP_BUDGET 32 - -apr_status_t h3_thread_safe_pool_create(apr_pool_t* parent, const char* tag, apr_pool_t** out) -{ - apr_allocator_t* allocator = NULL; - apr_thread_mutex_t* mutex = NULL; - apr_status_t rv = apr_allocator_create(&allocator); - if (rv != APR_SUCCESS) - { - return rv; - } - rv = apr_pool_create_ex(out, parent, NULL, allocator); - if (rv != APR_SUCCESS) - { - apr_allocator_destroy(allocator); - return rv; - } - apr_allocator_owner_set(allocator, *out); - apr_pool_tag(*out, tag); - rv = apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_DEFAULT, *out); - if (rv != APR_SUCCESS) - { - apr_pool_destroy(*out); - *out = NULL; - return rv; - } - apr_allocator_mutex_set(allocator, mutex); - return APR_SUCCESS; -} - -void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) -{ - (void)thread; - h3_io_t* io = data; - if (!io) - { - ap_log_perror(APLOG_MARK, APLOG_ERR, 0, NULL, "h3_event_thread: NULL io"); - return NULL; - } - ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "event thread started"); - int work_pending = 0; - int listener_failed = 0; - while (io->thread_running || io->active_sessions->nelts > 0) - { - if (!work_pending) - { - wait_for_event(io); - } - work_pending = 0; - { - int pump_rounds = 0; - int pumped; - do { - pumped = h3q_engine_pump(io->qengine); - if (pumped < 0) - { - if (!listener_failed) - { - listener_failed = 1; - ap_log_error(APLOG_MARK, APLOG_ERR, 0, io->server, "QUIC listener event processing failed; no further datagrams will be handled"); - } - apr_sleep(apr_time_from_msec(H3_LISTENER_FAILED_BACKOFF_MS)); - break; - } - listener_failed = 0; - if (pumped > 0) - { - work_pending = 1; - } - } while (pumped > 0 && ++pump_rounds < H3_PUMP_BUDGET); - } - - if (io->thread_running) - { - for (;;) - { - h3q_conn* conn = h3q_engine_accept_conn(io->qengine); - if (!conn) - { - break; - } - /* The engine has already answered these clients: refuse them, or they wait out their idle timeout. */ - if (io->draining) - { - h3q_conn_refuse(io->qengine, conn); /* no new connections during a graceful stop */ - continue; - } - ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "accepted new QUIC connection"); - if (h3_io_at_connection_limit(io)) - { - h3_server_conf* conf = ap_get_module_config(io->server->module_config, &http3_module); - ap_log_error(APLOG_MARK, APLOG_WARNING, 0, io->server, "dropping QUIC connection: at H3MaxConnections limit (%u)", conf->h3_max_connections); - h3q_conn_refuse(io->qengine, conn); - continue; - } - if (!prepare_accepted_connection(io, conn)) - { - h3q_conn_free(conn); - } - } - } - if (io->thread_running) - { - /* Also while draining: a pending handshake counts as an MPM connection, so it must finish or time out. */ - progress_pending_handshakes(io); - } - - apr_time_t now = apr_time_now(); - for (int i = 0; i < io->active_sessions->nelts;) - { - if (i > 0 && (i % 8) == 0) - { - int p = h3q_engine_pump(io->qengine); - if (p > 0) - { - work_pending = 1; - } - } - - h3_session* session = ((h3_session**)io->active_sessions->elts)[i]; - - /* Skip a session with nothing new: no busy worker, no packet, timer or - * stream limit change in the engine, no worker output. The sweep still - * serves it once a second, for its idle and stream timeouts. Every session - * keeps its control streams, so a stream count cannot tell idle sessions. */ - if (!io->draining && io->thread_running && !session->aborted - && apr_atomic_read32(&session->active_tasks) == 0 - && !h3q_conn_take_activity(session->qconn) - && !apr_atomic_xchg32(&session->wake, 0) - && now < session->next_sweep) - { - i++; - continue; - } - session->next_sweep = now + apr_time_from_sec(1); - - if (!session->aborted && service_session_pass(io, session)) - { - work_pending = 1; - apr_atomic_set32(&session->wake, 1); /* the pass ran out of budget: serve it again next round */ - } - - if (session->aborted) - { - int is_rapid = (!io->thread_running || io->draining); - int shutdown_done = h3q_conn_shutdown(session->qconn, is_rapid, session->abort_quic_error_code, session->ngh3_dead ? session->abort_reason : NULL); - - if (shutdown_done && apr_atomic_read32(&session->active_tasks) == 0) - { - ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "connection servicing done"); - h3_session_destroy(session); - if (io->note_conn_removed) - { - io->note_conn_removed(); - } - if (i < io->active_sessions->nelts - 1) - { - ((h3_session**)io->active_sessions->elts)[i] = ((h3_session**)io->active_sessions->elts)[io->active_sessions->nelts - 1]; - } - io->active_sessions->nelts--; - apr_atomic_dec32(&io->active_session_count); - continue; /* Do not increment i: the last element was swapped into this slot. */ - } - } - i++; - } - } - while (io->pending_handshakes->nelts > 0) - { - h3_pending_handshake* pending = (h3_pending_handshake*)io->pending_handshakes->elts; - h3q_conn_shutdown(pending[0].conn, 1, 0, NULL); - remove_pending_handshake(io, 0, 1); - } - ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "event thread exiting"); - return NULL; -} diff --git a/modules/http3/include/h3_config.h.orig b/modules/http3/include/h3_config.h.orig deleted file mode 100644 index ddbeea0e009..00000000000 --- a/modules/http3/include/h3_config.h.orig +++ /dev/null @@ -1,140 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef H3_CONFIG_H -#define H3_CONFIG_H - -#include - -#include - -extern const command_rec h3_cmds[]; - -typedef struct h3_server_conf h3_server_conf; - -typedef enum -{ - H3_FLAG_UNSET = 0, - H3_FLAG_ON, - H3_FLAG_OFF -} h3_tri_flag; - -struct h3_server_conf -{ - apr_port_t host_port; - - /** QUIC TLS context, built from mod_ssl's certificate when the host lists h3 in Protocols. */ - struct ssl_ctx_st* ssl_ctx; - - apr_port_t h3_port; - apr_uint32_t h3_max_concurrent_streams; - apr_uint32_t h3_max_connections; - apr_size_t h3_stream_buffer_size; - apr_size_t h3_max_request_body_size; - apr_size_t h3_max_response_body_size; - h3_tri_flag h3_alt_svc; - h3_tri_flag h3_address_validation; - apr_uint32_t h3_alt_svc_max_age; - apr_uint32_t h3_handshake_timeout; - apr_uint32_t h3_idle_timeout; - apr_size_t h3_socket_buffer_size; - apr_size_t h3_max_window; - h3_tri_flag h3_session_tickets; - h3_tri_flag h3_early_data; - apr_uint32_t h3_stream_timeout; - apr_uint32_t h3_max_stream_errors; - int h3_qpack_capacity_set; - int h3_qpack_blocked_set; - apr_uint32_t h3_qpack_table_capacity; - apr_uint32_t h3_qpack_blocked_streams; - apr_uint32_t h3_min_workers; - apr_uint32_t h3_max_workers; - apr_uint32_t h3_max_worker_idle_seconds; -}; - -/** - * Look up the TCP Listen port of a vhost by scanning its `Port` and - * `Listen` directive state. - * @param s The server_rec. - * @return The TCP port, or 0 if undetermined. - */ -apr_port_t get_server_port(const server_rec* s); - -/** - * ap_create_server_config callback: allocate a fresh zero-initialised - * h3_server_conf from @p p. All fields start as NULL/0. - * @param p Pool used for the allocation. - * @param s The vhost the config belongs to (unused). - * @return The new h3_server_conf. - */ -void* h3_create_server_config(apr_pool_t* p, server_rec* s); - -/** - * ap_merge_server_config callback: produce a child vhost config that - * inherits each unset field from the parent. - * @param p Pool for the merged config. - * @param base_conf Parent h3_server_conf. - * @param new_conf Child h3_server_conf. - * @return The merged h3_server_conf. - */ -void* h3_merge_server_config(apr_pool_t* p, void* base_conf, void* new_conf); - -/** - * ap_create_dir_config callback. mod_http3 has no per-directory state; - * returns a 1-byte allocation so httpd takes ownership of a non-NULL - * pointer. - * @param p Pool for the allocation. - * @param dir The directory path (unused). - * @return A 1-byte zero-initialised block. - */ -void* h3_create_dir_config(apr_pool_t* p, char* dir); - -/** - * ap_merge_dir_config callback. Identity merge: per-directory config is - * a no-op for mod_http3, so the base is returned unchanged. - * @param p Pool for any allocation (unused). - * @param base Base per-dir config. - * @param add Per-dir config being merged in (unused). - * @return The base per-dir config. - */ -void* h3_merge_dir_config(apr_pool_t* p, void* base, void* add); - -/** - * ap_ssl_add_cert_files hook: mod_ssl runs it for every SSLEngine vhost with - * the certificate and key files it is about to load (SSLCertificateFile plus - * anything mod_md added). When the vhost lists h3 in Protocols, builds its - * QUIC TLS context from them, here, before the server drops privileges. - * @param s The vhost being configured. - * @param p Config pool; owns the context. - * @param cert_files Certificate chain files, const char* elements. - * @param key_files Private key files, const char* elements. - * @return DECLINED, or HTTP_INTERNAL_SERVER_ERROR if the files do not load. - */ -int h3_ssl_add_cert_files(server_rec* s, apr_pool_t* p, apr_array_header_t* cert_files, apr_array_header_t* key_files); - -/** - * ap_post_config hook: fill in defaults on every vhost that serves HTTP/3; - * the first one owns the listener, and an SNI callback swaps in each other - * host's certificate by name. No-op in AP_SQ_MS_CREATE_PRE_CONFIG. - * @param p Config pool; owns the SNI host table. - * @param plog Log pool (unused). - * @param ptemp Temp pool (unused). - * @param s The first server_rec in the configuration. - * @return OK, or HTTP_INTERNAL_SERVER_ERROR if no vhost serves HTTP/3. - */ -int h3_post_config(apr_pool_t* p, apr_pool_t* plog, apr_pool_t* ptemp, server_rec* s); - -#endif /* H3_CONFIG_H */ diff --git a/modules/http3/include/h3_session.h b/modules/http3/include/h3_session.h index 366c973e428..4b13788a9c9 100644 --- a/modules/http3/include/h3_session.h +++ b/modules/http3/include/h3_session.h @@ -58,11 +58,11 @@ struct h3_session apr_thread_mutex_t* lock; apr_hash_t* streams; - volatile int aborted; + volatile apr_uint32_t aborted; /* Number of streams write-blocked in nghttp3 (see flush_nghttp3). */ int blocked_streams; - volatile int ngh3_dead; + volatile apr_uint32_t ngh3_dead; uint64_t abort_quic_error_code; const char* abort_reason; From 8429cf27781bf39d799052c1fb6204ccbd9a0008 Mon Sep 17 00:00:00 2001 From: Pierre Brochard Date: Fri, 27 Nov 2026 19:00:35 +0100 Subject: [PATCH 3/4] add -1 for H3_QPACK_TABLE_ENCODER_CAPACITY_MAX to preserve nghttp3 and client-negotiated defaults --- modules/http3/h3_config.c | 64 +++++++++++++++++++--- modules/http3/h3_session.c | 3 +- modules/http3/include/h3_config.h | 11 +++- test/modules/http3/test_205_qpack_order.py | 42 ++++++++------ 4 files changed, 93 insertions(+), 27 deletions(-) diff --git a/modules/http3/h3_config.c b/modules/http3/h3_config.c index 3a7d85bbbd8..c6f9fc83867 100644 --- a/modules/http3/h3_config.c +++ b/modules/http3/h3_config.c @@ -60,7 +60,8 @@ void* h3_merge_server_config(apr_pool_t* p, void* base_conf, void* new_conf) h3_server_conf* merged = apr_pcalloc(p, sizeof(h3_server_conf)); h3_server_conf* base = (h3_server_conf*)base_conf; h3_server_conf* new = (h3_server_conf*)new_conf; - + + merged->h3_enabled = new->h3_enabled != H3_FLAG_UNSET ? new->h3_enabled : base->h3_enabled; merged->h3_port = new->h3_port ? new->h3_port : base->h3_port; merged->h3_max_concurrent_streams = new->h3_max_concurrent_streams ? new->h3_max_concurrent_streams : base->h3_max_concurrent_streams; merged->h3_max_connections = new->h3_max_connections ? new->h3_max_connections : base->h3_max_connections; @@ -98,6 +99,17 @@ static int mpm_query(int code) return ap_mpm_query(code, &value) == APR_SUCCESS ? value : -1; } +static const char* set_h3_engine(cmd_parms* cmd, void* dummy H3_UNUSED, int flag) +{ + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); + if (err != NULL) + return err; + h3_server_conf *conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + conf->h3_enabled = flag ? H3_FLAG_ON : H3_FLAG_OFF; + return NULL; +} + static const char* set_h3_port(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg) { if (!arg || !*arg) @@ -324,18 +336,34 @@ static const char* set_h3_qpack_encoder_table_capacity(cmd_parms* cmd, void* dum { if (!arg || !*arg) { - return "H3QpackTableCapacity: empty value"; + return "H3QpackEncoderTableCapacity: empty value"; } + h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); + CHECK(conf); + + if (strcmp(arg, "-1") == 0 || strcasecmp(arg, "auto") == 0) + { + conf->h3_qpack_encoder_table_capacity = -1; + conf->h3_qpack_encoder_capacity_set = 1; + return NULL; + } + apr_uint64_t val = 0; apr_status_t rv = apr_cstr_atoui64(&val, arg); - if (rv != APR_SUCCESS || val < 0) + if (rv == APR_EINVAL) { return apr_psprintf(cmd->pool, "H3QpackEncoderTableCapacity: '%s' is not a number", arg); } - - h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module); - CHECK(conf); - conf->h3_qpack_encoder_table_capacity = (apr_uint32_t)val; + if (rv == APR_ERANGE) + { + return apr_psprintf(cmd->pool, "H3QpackEncoderTableCapacity: '%s' is out of representable range", arg); + } + if (val > H3_QPACK_TABLE_CAPACITY_MAX) + { + return apr_psprintf(cmd->pool, "H3QpackEncoderTableCapacity: '%s' is out of allowed range (0-%lu)", arg, (unsigned long)H3_QPACK_TABLE_CAPACITY_MAX); + } + + conf->h3_qpack_encoder_table_capacity = (apr_int32_t)val; conf->h3_qpack_encoder_capacity_set = 1; return NULL; } @@ -668,8 +696,22 @@ static int h3_sni_select_cert(SSL* ssl, void* arg) return 1; /* no match: the listener's own certificate */ } +int h3_is_enabled(const server_rec *s) +{ + server_rec *base = s ? s : ap_server_conf; + h3_server_conf *conf = ap_get_module_config(base->module_config, &http3_module); + + return conf ? conf->h3_enabled != H3_FLAG_OFF : 1; +} + int h3_post_config(apr_pool_t* p, apr_pool_t* plog H3_UNUSED, apr_pool_t* ptemp, server_rec* s) { + if (!h3_is_enabled(s)) { + ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, + "mod_http3: inhibited globally via H3Engine Off"); + return OK; + } + CHECK(p); CHECK(ptemp); CHECK(s); @@ -709,6 +751,10 @@ int h3_post_config(apr_pool_t* p, apr_pool_t* plog H3_UNUSED, apr_pool_t* ptemp, e->ctx = vc->ssl_ctx; } vc->host_port = get_server_port(vs); + if (vc->h3_enabled == H3_FLAG_UNSET) + { + vc->h3_enabled = H3_FLAG_ON; + } if (vc->h3_port == 0) { vc->h3_port = vc->host_port; @@ -837,6 +883,8 @@ void* h3_merge_dir_config(apr_pool_t* p H3_UNUSED, void* base, void* add H3_UNUS } const command_rec h3_cmds[] = { + AP_INIT_FLAG("H3Engine", set_h3_engine, NULL, RSRC_CONF, + "Enable or inhibit mod_http3 globally (on|off)"), AP_INIT_TAKE1("H3Port", set_h3_port, NULL, RSRC_CONF, "UDP port to listen on for QUIC/HTTP-3 (default: same as main server)"), AP_INIT_TAKE1("H3MaxConcurrentStreams", set_h3_max_concurrent_streams, NULL, RSRC_CONF, "Maximum number of concurrent HTTP/3 streams per connection (default: 100)"), AP_INIT_TAKE1("H3MaxConnections", set_h3_max_connections, NULL, RSRC_CONF, "Maximum concurrent QUIC/HTTP/3 connections per child process (default: 256)"), @@ -855,7 +903,7 @@ const command_rec h3_cmds[] = { AP_INIT_TAKE1("H3StreamTimeout", set_h3_stream_timeout, NULL, RSRC_CONF, "Seconds a response may make no progress before the stream is aborted (default: the server Timeout)"), AP_INIT_TAKE1("H3MaxStreamErrors", set_h3_max_stream_errors, NULL, RSRC_CONF, "Stream errors one connection may cause before it is closed with H3_EXCESSIVE_LOAD (default: 8)"), AP_INIT_TAKE1("H3QpackTableCapacity", set_h3_qpack_table_capacity, NULL, RSRC_CONF, "Bytes of QPACK dynamic table a client may use for request headers; 0 disables it (default: 4096)"), - AP_INIT_TAKE1("H3QpackEncoderTableCapacity", set_h3_qpack_encoder_table_capacity, NULL, RSRC_CONF, "Maximum QPACK dynamic table capacity used by the server encoder (default: 0)"), + AP_INIT_TAKE1("H3QpackEncoderTableCapacity", set_h3_qpack_encoder_table_capacity, NULL, RSRC_CONF, "Maximum QPACK dynamic table capacity used by the server encoder, -1 don't set a value : keep nghttp3 default settings (default: 0)"), AP_INIT_TAKE1("H3QpackBlockedStreams", set_h3_qpack_blocked_streams, NULL, RSRC_CONF, "Requests that may wait on a QPACK table insert that has not arrived; each one is unbounded memory (default: 0)"), AP_INIT_TAKE1("H3MinWorkers", set_h3_min_workers, NULL, RSRC_CONF, "Request worker threads started per child process (default: 16)"), AP_INIT_TAKE1("H3MaxWorkers", set_h3_max_workers, NULL, RSRC_CONF, "Maximum request worker threads per child process (default: 64)"), diff --git a/modules/http3/h3_session.c b/modules/http3/h3_session.c index a8814288e7e..7a6cedaaaa3 100644 --- a/modules/http3/h3_session.c +++ b/modules/http3/h3_session.c @@ -90,7 +90,8 @@ apr_status_t h3_session_create(h3_session** psession, server_rec* s, h3q_conn* q CHECK(conf, return APR_EGENERAL;); settings.qpack_max_dtable_capacity = conf->h3_qpack_table_capacity; settings.qpack_blocked_streams = conf->h3_qpack_blocked_streams; - settings.qpack_encoder_max_dtable_capacity = conf->h3_qpack_encoder_table_capacity; + if(conf->h3_qpack_encoder_table_capacity > -1) + settings.qpack_encoder_max_dtable_capacity = (size_t) conf->h3_qpack_encoder_table_capacity; if (s->limit_req_fields > 0 && s->limit_req_fieldsize > 0) { diff --git a/modules/http3/include/h3_config.h b/modules/http3/include/h3_config.h index 481cc7ef89a..20cf806d617 100644 --- a/modules/http3/include/h3_config.h +++ b/modules/http3/include/h3_config.h @@ -39,6 +39,7 @@ struct h3_server_conf /** QUIC TLS context, built from mod_ssl's certificate when the host lists h3 in Protocols. */ struct ssl_ctx_st* ssl_ctx; + h3_tri_flag h3_enabled; apr_port_t h3_port; apr_uint32_t h3_max_concurrent_streams; apr_uint32_t h3_max_connections; @@ -60,7 +61,7 @@ struct h3_server_conf int h3_qpack_encoder_capacity_set; int h3_qpack_blocked_set; apr_uint32_t h3_qpack_table_capacity; - apr_uint32_t h3_qpack_encoder_table_capacity; + apr_int32_t h3_qpack_encoder_table_capacity; apr_uint32_t h3_qpack_blocked_streams; apr_uint32_t h3_min_workers; apr_uint32_t h3_max_workers; @@ -139,4 +140,12 @@ int h3_ssl_add_cert_files(server_rec* s, apr_pool_t* p, apr_array_header_t* cert */ int h3_post_config(apr_pool_t* p, apr_pool_t* plog, apr_pool_t* ptemp, server_rec* s); +/** + * Test if http3 is enabled. + * @param s The server_rec. + * @return 1 if HTTP3 is enabled, else 0. + */ + +int h3_is_enabled(const server_rec *s); + #endif /* H3_CONFIG_H */ diff --git a/test/modules/http3/test_205_qpack_order.py b/test/modules/http3/test_205_qpack_order.py index 5a73987da27..12d57473396 100644 --- a/test/modules/http3/test_205_qpack_order.py +++ b/test/modules/http3/test_205_qpack_order.py @@ -67,19 +67,21 @@ def start_get(self, authority: str, path: str) -> int: class TestQpackOrder: - @pytest.fixture(autouse=True, scope="class") - @classmethod - def _class_scope(cls, env): - # a new value per response: each header block needs a fresh insert - H3Conf(env).add_h3_vhost(directives=[ - 'Header always set Cache-Control "expr=max-age=%{QUERY_STRING}"', - ]).install() - assert env.apache_restart() == 0 - - def test_h3_205_01_inserts_sent_before_header_blocks(self, env): + @pytest.mark.parametrize("cap", [0, -1]) + def test_h3_205_01_inserts_sent_before_header_blocks(self, env, cap): env.require(env.has_python_package("aioquic"), "python package 'aioquic' is not installed", dep="aioquic") + directives = [ + 'Header always set Cache-Control "expr=max-age=%{QUERY_STRING}"', + ] + if cap != 0: + # Explicit -1 enables nghttp3/client dynamic table; 0 tests the default + directives.insert(0, f"H3QpackEncoderTableCapacity {cap}") + + H3Conf(env).add_h3_vhost(directives=directives).install() + assert env.apache_restart() == 0 + authority = env.h3_domain seq = itertools.count() @@ -108,12 +110,18 @@ async def run(): hdrs = client.headers.get(sid, {}) assert hdrs.get(b":status") == b"200", f"stream {sid}: {hdrs}" assert hdrs.get(b"cache-control") == value, f"stream {sid}: {hdrs}" - # Without the dynamic table (aioquic >= 1.8 turns it off) nothing can - # be blocked, and the check below would pass whatever the server does. - assert client.http.dynamic >= len(expected) // 2, \ - f"only {client.http.dynamic} of {len(expected)} header blocks " \ - f"used the QPACK dynamic table" - # A header block sent ahead of the encoder stream data it depends on - # stalls the stream, and a client that closes it then loses the response. + + if cap == 0: + # Default static-only mode: server must never emit dynamic inserts + assert client.http.dynamic == 0, \ + f"expected 0 dynamic header blocks when cap=0, got {client.http.dynamic}" + else: + # Without the dynamic table (aioquic >= 1.8 turns it off) nothing can + # be blocked, and the check below would pass whatever the server does. + assert client.http.dynamic >= len(expected) // 2, \ + f"only {client.http.dynamic} of {len(expected)} header blocks " \ + f"used the QPACK dynamic table" + # A header block sent ahead of the encoder stream data it depends on + # stalls the stream, and a client that closes it then loses the response. assert client.http.blocked == 0, \ f"{client.http.blocked} of {len(expected)} header blocks were blocked" From f3f306dd180df7c03e1657edaa960b5d697047d5 Mon Sep 17 00:00:00 2001 From: Pierre Brochard Date: Fri, 27 Nov 2026 19:35:20 +0100 Subject: [PATCH 4/4] ajout de H3_QPACK_TABLE_ENCODER_CAPACITY_MAX a test_011_directives.py --- test/modules/http3/test_011_directives.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/modules/http3/test_011_directives.py b/test/modules/http3/test_011_directives.py index 825f8f34903..a8df3a79037 100644 --- a/test/modules/http3/test_011_directives.py +++ b/test/modules/http3/test_011_directives.py @@ -19,6 +19,7 @@ ("H3StreamTimeout", 1, 86400), ("H3MaxStreamErrors", 1, 10000), ("H3QpackTableCapacity", 0, 1048576), + ("H3QpackEncoderTableCapacity", -1, 1048576), ("H3QpackBlockedStreams", 0, 1000), ("H3MinWorkers", 1, 4096), ("H3MaxWorkers", 1, 4096),