libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

h2_settings.h (6075B)


      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_settings.h
     41  * @brief  HTTP2 SETTINGS identifiers, coding and decoding
     42  * @author Karlson2k (Evgeny Grin)
     43  *
     44  * SETTINGS parameters are defined in RFC 9113. Some additional identifiers
     45  * are taken from other RFCs (see comments in the code).
     46  */
     47 
     48 #ifndef MHD_H2_SETTINGS_H
     49 #define MHD_H2_SETTINGS_H 1
     50 
     51 #include "mhd_sys_options.h"
     52 
     53 #include "sys_base_types.h"
     54 
     55 #include "mhd_assert.h"
     56 
     57 #include "mhd_bithelpers.h"
     58 
     59 #if defined(_MSC_FULL_VER)
     60 #pragma warning(push)
     61 /* Disable C4505 "unreferenced local function has been removed" */
     62 #pragma warning(disable:4505)
     63 #endif /* _MSC_FULL_VER */
     64 
     65 /**
     66  * HTTP/2 SETTINGS identifiers
     67  *
     68  * Extracted from RFC 9113, Section 6.5.2; RFC 8441, Section 9.1;
     69  * RFC 9218, Section 16.
     70  */
     71 enum mhd_H2SettingsID
     72 mhd_ENUM_BASE_T (uint_least16_t)
     73 {
     74   mhd_H2_STNGS_HEADER_TABLE_SIZE = 0x01u
     75   ,
     76   mhd_H2_STNGS_ENABLE_PUSH = 0x02u
     77   ,
     78   mhd_H2_STNGS_CONCURRENT_STREAMS = 0x03u
     79   ,
     80   mhd_H2_STNGS_INITIAL_WINDOW_SIZE = 0x04u
     81   ,
     82   mhd_H2_STNGS_MAX_FRAME_SIZE = 0x05u
     83   ,
     84   mhd_H2_STNGS_MAX_HEADER_LIST_SIZE = 0x06u
     85   ,
     86   mhd_H2_STNGS_ENABLE_CONNECT_PROTOCOL = 0x08u
     87   ,
     88   mhd_H2_STNGS_NO_RFC7540_PRIORITIES = 0x09u
     89 #ifndef mhd_USE_ENUM_BASE_T
     90   ,
     91   /**
     92    * Not a real identifier, no not use
     93    */
     94   mhd_H2_STNGS_SENTINEL = 0xFFFFu
     95 #endif /* ! mhd_USE_ENUM_BASE_T */
     96 };
     97 
     98 
     99 /**
    100  * HTTP/2 setting
    101  */
    102 struct mhd_H2Setting
    103 {
    104   /**
    105    * Setting's identifier
    106    */
    107   enum mhd_H2SettingsID identifier;
    108   /**
    109    * Setting's value
    110    */
    111   uint_least32_t value;
    112 };
    113 
    114 /**
    115  * The size of single HTTP/2 setting
    116  */
    117 #define mhd_H2_SETTING_SIZE     (6u)
    118 
    119 /**
    120  * Decode a SETTINGS parameter from "on-wire" 6-byte form.
    121  *
    122  * @param encoded the 6-byte array with the encoded parameter
    123  * @param[out] p_identifier receives the 16-bit setting identifier
    124  * @param[out] p_value receives the 32-bit setting value
    125  */
    126 static inline MHD_FN_PAR_NONNULL_ALL_
    127 MHD_FN_PAR_IN_ (1)
    128 MHD_FN_PAR_OUT_ (2) MHD_FN_PAR_OUT_ (3) void
    129 mhd_h2_setting_decode3 (const uint8_t encoded[MHD_FN_PAR_FIX_ARR_SIZE_ (6)],
    130                         uint_least16_t *restrict p_identifier,
    131                         uint_least32_t *restrict p_value)
    132 {
    133   *p_identifier = mhd_GET_16BIT_BE_UNALIGN (encoded);
    134   *p_value = mhd_GET_32BIT_BE_UNALIGN (encoded + 2u);
    135 }
    136 
    137 
    138 /**
    139  * Decode a SETTINGS parameter into a @ref mhd_H2Setting structure.
    140  *
    141  * @param encoded the 6-byte array with the encoded parameter
    142  * @param[out] setting receives the decoded identifier and value
    143  */
    144 static inline MHD_FN_PAR_NONNULL_ALL_
    145 MHD_FN_PAR_IN_ (1)
    146 MHD_FN_PAR_OUT_ (2) void
    147 mhd_h2_setting_decode (const uint8_t encoded[MHD_FN_PAR_FIX_ARR_SIZE_ (6)],
    148                        struct mhd_H2Setting *restrict setting)
    149 {
    150   uint_least16_t identifier;
    151   mhd_h2_setting_decode3 (encoded,
    152                           &identifier,
    153                           &(setting->value));
    154   setting->identifier = (enum mhd_H2SettingsID) identifier;
    155 }
    156 
    157 
    158 /**
    159  * Encode a SETTINGS parameter to on-wire 6-byte form.
    160  *
    161  * @param identifier the 16-bit setting identifier
    162  * @param value the 32-bit setting value
    163  * @param[out] encoded the destination 6-byte array
    164  */
    165 static inline MHD_FN_PAR_NONNULL_ALL_
    166 MHD_FN_PAR_OUT_ (3) void
    167 mhd_h2_setting_encode3 (uint_least16_t identifier,
    168                         uint_least32_t value,
    169                         uint8_t encoded[MHD_FN_PAR_FIX_ARR_SIZE_ (6)])
    170 {
    171   mhd_assert (identifier == (identifier & 0xFFFFu));
    172   mhd_assert (value == (value & 0xFFFFFFFFu));
    173   mhd_PUT_16BIT_BE_UNALIGN (encoded,
    174                             identifier);
    175   mhd_PUT_32BIT_BE_UNALIGN (encoded + 2u,
    176                             value);
    177 }
    178 
    179 
    180 /**
    181  * Encode a SETTINGS parameter from @ref mhd_H2Setting.
    182  *
    183  * @param setting the setting to encode
    184  * @param[out] encoded the destination 6-byte array
    185  */
    186 static inline MHD_FN_PAR_NONNULL_ALL_
    187 MHD_FN_PAR_IN_ (1)
    188 MHD_FN_PAR_OUT_ (2) void
    189 mhd_h2_setting_encode (const struct mhd_H2Setting *restrict setting,
    190                        uint8_t encoded[MHD_FN_PAR_FIX_ARR_SIZE_ (6)])
    191 {
    192   mhd_h2_setting_encode3 ((uint_least16_t) setting->identifier,
    193                           setting->value,
    194                           encoded);
    195 }
    196 
    197 
    198 #if defined(_MSC_FULL_VER)
    199 /* Restore warnings */
    200 #pragma warning(pop)
    201 #endif /* _MSC_FULL_VER */
    202 
    203 #endif /* ! MHD_H2_SETTINGS_H */