h2_proc_conn.c (6229B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2025 Evgeny Grin (Karlson2k) 5 6 GNU libmicrohttpd is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 GNU libmicrohttpd is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 Alternatively, you can redistribute GNU libmicrohttpd and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of 19 the License, or (at your option) any later version, together 20 with the eCos exception, as follows: 21 22 As a special exception, if other files instantiate templates or 23 use macros or inline functions from this file, or you compile this 24 file and link it with other works to produce a work based on this 25 file, this file does not by itself cause the resulting work to be 26 covered by the GNU General Public License. However the source code 27 for this file must still be made available in accordance with 28 section (3) of the GNU General Public License v2. 29 30 This exception does not invalidate any other reasons why a work 31 based on this file might be covered by the GNU General Public 32 License. 33 34 You should have received copies of the GNU Lesser General Public 35 License and the GNU General Public License along with this library; 36 if not, see <https://www.gnu.org/licenses/>. 37 */ 38 39 /** 40 * @file src/mhd2/h2/h2_proc_conn.c 41 * @brief Implementation of HTTP/2 connection processing functions 42 * @author Karlson2k (Evgeny Grin) 43 */ 44 45 #include "mhd_sys_options.h" 46 47 #include "sys_bool_type.h" 48 #include "sys_base_types.h" 49 50 #include "mhd_assert.h" 51 #include "mhd_unreachable.h" 52 53 #include "mhd_connection.h" 54 55 #include "h2_frame_types.h" 56 57 #include "h2_frame_init.h" 58 #include "h2_frame_codec.h" 59 60 #include "h2_proc_settings.h" 61 #include "h2_conn_streams.h" 62 #include "h2_proc_out.h" 63 64 #include "h2_proc_conn.h" 65 66 67 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ bool 68 mhd_h2_conn_process_first_fr (struct MHD_Connection *restrict c) 69 { 70 union mhd_H2FrameUnion first_fr; 71 struct mhd_Buffer payload; 72 enum mhd_H2FrameDecodeResult dec_res; 73 bool ret; 74 75 mhd_assert (mhd_C_IS_HTTP2 (c)); 76 mhd_assert (! c->h2.state.init.got_setns); 77 78 dec_res = mhd_h2_frame_decode (c->read_buffer_offset - c->h2.buff.r_cur_frame, 79 (uint8_t *) c->read_buffer 80 + c->h2.buff.r_cur_frame, 81 mhd_H2_STNG_DEF_MAX_FRAME_SIZE, /* Settings are not yet ACKed */ 82 &first_fr, 83 &payload); 84 if (mhd_H2_FRAME_DEC_ERR_IS_HARD (dec_res)) 85 { 86 mhd_h2_conn_finish (c, 87 mhd_H2_F_DEC_CONN_ERR_F_SIZE == dec_res ? 88 mhd_H2_ERR_FRAME_SIZE_ERROR : mhd_H2_ERR_PROTOCOL_ERROR, 89 true); 90 return false; 91 } 92 93 if (mhd_H2_F_DEC_OK != dec_res) 94 return false; /* Not yet complete */ 95 96 if ((mhd_H2_FRAME_IDS_SETTINGS_ID != first_fr.selector.type) || 97 (first_fr.settings.ack)) 98 { 99 mhd_h2_conn_finish (c, 100 mhd_H2_ERR_PROTOCOL_ERROR, 101 true); 102 return false; 103 } 104 105 ret = mhd_h2_proc_first_settings (c, 106 &payload); 107 108 c->h2.buff.r_cur_frame += mhd_h2_frame_get_total_size (&first_fr); 109 110 return ret; 111 } 112 113 114 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ bool 115 mhd_h2_conn_process_in_goaway (struct MHD_Connection *restrict c, 116 uint_least32_t last_stream_id, 117 enum mhd_H2ErrorCode err) 118 { 119 c->h2.state.recvd_goaway.occurred = true; 120 c->h2.state.recvd_goaway.code = err; 121 c->h2.peer.stream_id_limit = last_stream_id; 122 123 // TODO: close all streams with higher IDs 124 125 return true; 126 } 127 128 129 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ bool 130 mhd_h2_conn_update_stream_init_window (struct MHD_Connection *restrict c, 131 uint_least32_t init_wind_size) 132 { 133 mhd_assert (0x7FFFFFFFu >= init_wind_size); 134 (void) c; // TODO: change window in all streams 135 136 c->h2.peer.stream_init_win_sz = init_wind_size; 137 return true; 138 } 139 140 141 static MHD_FN_PAR_NONNULL_ALL_ bool 142 conn_win_update (struct MHD_Connection *restrict c) 143 { 144 mhd_assert (0 <= c->h2.state.recv_window); 145 /* Dumb algorithm: if receive windows is less than three quarters of the full 146 * window size, then bump to the full size. */ 147 if ((c->h2.rcv_cfg.conn_full_win_sz - c->h2.rcv_cfg.conn_full_win_sz / 4) >= 148 (uint_least32_t) c->h2.state.recv_window) 149 { 150 const uint_least32_t incr = 151 (uint_least32_t) 152 (c->h2.rcv_cfg.conn_full_win_sz 153 - (uint_least32_t) c->h2.state.recv_window); 154 mhd_assert (0x7FFFFFFFu >= incr); 155 if (! mhd_h2_q_window_update (c, 156 0u, 157 incr)) 158 return false; 159 c->h2.state.recv_window = (int_least32_t) c->h2.rcv_cfg.conn_full_win_sz; 160 } 161 return true; 162 } 163 164 165 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ bool 166 mhd_h2_conn_process_changes (struct MHD_Connection *restrict c) 167 { 168 if (! conn_win_update (c)) 169 return false; 170 171 if (! mhd_h2_conn_maintain_streams_all (c)) 172 return false; 173 174 return true; 175 } 176 177 178 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ bool 179 mhd_h2_conn_finish (struct MHD_Connection *restrict c, 180 enum mhd_H2ErrorCode err, 181 bool forced) 182 { 183 if (! mhd_h2_q_goaway (c, 184 err)) 185 { 186 if (! forced) 187 return false; 188 189 c->h2.state.sent_goaway.occurred = true; 190 c->h2.state.sent_goaway.code = (uint_least32_t) err; 191 192 c->h_layer.state = mhd_HTTP_LAYER_BROKEN; 193 return true; 194 } 195 196 c->h2.state.sent_goaway.occurred = true; 197 c->h2.state.sent_goaway.code = (uint_least32_t) err; 198 199 c->h_layer.state = mhd_HTTP_LAYER_CLOSING; 200 return true; 201 }