libmicrohttpd2

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

h2_frame_init.h (6908B)


      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_frame_init.h
     41  * @brief  Declarations of HTTP/2 frame decoding and encoding functions
     42  * @author Karlson2k (Evgeny Grin)
     43  */
     44 
     45 #ifndef MHD_H2_FRAME_INIT_H
     46 #define MHD_H2_FRAME_INIT_H 1
     47 
     48 #include "mhd_sys_options.h"
     49 
     50 #include "sys_bool_type.h"
     51 #include "sys_base_types.h"
     52 
     53 #include <string.h>
     54 
     55 #include "h2_frame_types.h"
     56 #include "h2_frame_length.h"
     57 
     58 #if defined(_MSC_FULL_VER)
     59 #pragma warning(push)
     60 /* Disable C4505 "unreferenced local function has been removed" */
     61 #pragma warning(disable:4505)
     62 #endif /* _MSC_FULL_VER */
     63 
     64 mhd_static_inline struct mhd_H2FrameDataInfo*
     65 mhd_h2_frame_init_data (union mhd_H2FrameUnion *restrict frame_info,
     66                         uint_least32_t stream_id,
     67                         bool end_stream)
     68 {
     69   struct mhd_H2FrameDataInfo *const data =  &(frame_info->data);
     70   data->type = mhd_H2_FRAME_DATA_ID;
     71   data->length = 0u; /* To be set later */
     72   data->stream_id = stream_id;
     73 
     74   data->padded = false;
     75   data->end_stream = end_stream;
     76 
     77   data->pad_length = 0u;
     78 
     79   return data;
     80 }
     81 
     82 
     83 mhd_static_inline struct mhd_H2FrameHeadersInfo*
     84 mhd_h2_frame_init_headers (union mhd_H2FrameUnion *restrict frame_info,
     85                            uint_least32_t stream_id,
     86                            bool end_headers,
     87                            bool end_stream)
     88 {
     89   struct mhd_H2FrameHeadersInfo *const headers = &(frame_info->headers);
     90   headers->type = mhd_H2_FRAME_HEADERS_ID;
     91   headers->length = 0u; /* To be set later */
     92   headers->stream_id = stream_id;
     93 
     94   headers->priority = false;
     95   headers->padded = false;
     96   headers->end_headers = end_headers;
     97   headers->end_stream = end_stream;
     98 
     99   headers->pad_length = 0u;
    100   headers->exclusive = false;
    101   headers->stream_dependency = 0u;
    102   headers->weight = 0u;
    103 
    104   return headers;
    105 }
    106 
    107 
    108 mhd_static_inline struct mhd_H2FrameRstStreamInfo*
    109 mhd_h2_frame_init_rst_stream (union mhd_H2FrameUnion *restrict frame_info,
    110                               uint_least32_t stream_id,
    111                               enum mhd_H2ErrorCode error_code)
    112 {
    113   struct mhd_H2FrameRstStreamInfo *const rst_stream = &(frame_info->rst_stream);
    114   rst_stream->type = mhd_H2_FRAME_RST_STREAM_ID;
    115   rst_stream->length = mhd_H2_FR_FIXED_LEN_RST_STREAM; /* Fixed size */
    116   rst_stream->stream_id = stream_id;
    117 
    118   rst_stream->error_code = error_code;
    119 
    120   return rst_stream;
    121 }
    122 
    123 
    124 mhd_static_inline struct mhd_H2FrameSettingsInfo*
    125 mhd_h2_frame_init_settings (union mhd_H2FrameUnion *restrict frame_info,
    126                             bool ack)
    127 {
    128   struct mhd_H2FrameSettingsInfo *const settings = &(frame_info->settings);
    129   settings->type = mhd_H2_FRAME_SETTINGS_ID;
    130   settings->length = 0u; /* Could be increased later */
    131   settings->stream_id = 0u;
    132 
    133   settings->ack = ack;
    134 
    135   return settings;
    136 }
    137 
    138 
    139 mhd_static_inline struct mhd_H2FramePingInfo*
    140 mhd_h2_frame_init_ping (union mhd_H2FrameUnion *restrict frame_info,
    141                         bool ack,
    142                         const uint8_t opaque_data[MHD_FN_PAR_FIX_ARR_SIZE_ (8)])
    143 {
    144   struct mhd_H2FramePingInfo *const ping = &(frame_info->ping);
    145   ping->type = mhd_H2_FRAME_PING_ID;
    146   ping->length = mhd_H2_FR_FIXED_LEN_PING; /* Fixed size */
    147   ping->stream_id = 0u;
    148 
    149   ping->ack = ack;
    150 
    151   memcpy (ping->opaque_data,
    152           opaque_data,
    153           8u);
    154 
    155   return ping;
    156 }
    157 
    158 
    159 mhd_static_inline struct mhd_H2FrameGoawayInfo*
    160 mhd_h2_frame_init_goaway (union mhd_H2FrameUnion *restrict frame_info,
    161                           uint_least32_t last_stream_id,
    162                           enum mhd_H2ErrorCode error_code)
    163 {
    164   struct mhd_H2FrameGoawayInfo *const goaway = &(frame_info->goaway);
    165   goaway->type = mhd_H2_FRAME_GOAWAY_ID;
    166   goaway->length = 8u; /* Could be increased later */
    167   goaway->stream_id = 0u;
    168 
    169   goaway->last_stream_id = last_stream_id;
    170   goaway->error_code = error_code;
    171 
    172   return goaway;
    173 }
    174 
    175 
    176 mhd_static_inline struct mhd_H2FrameWindowUpdateInfo*
    177 mhd_h2_frame_init_window_update (union mhd_H2FrameUnion *restrict frame_info,
    178                                  uint_least32_t stream_id,
    179                                  uint_least32_t window_size_increment)
    180 {
    181   struct mhd_H2FrameWindowUpdateInfo *const window_update =
    182     &(frame_info->window_update);
    183   window_update->type = mhd_H2_FRAME_WINDOW_UPDATE_ID;
    184   window_update->length = mhd_H2_FR_FIXED_LEN_WINDOW_UPDATE; /* Fixed size */
    185   window_update->stream_id = stream_id;
    186 
    187   window_update->window_size_increment = window_size_increment;
    188 
    189   return window_update;
    190 }
    191 
    192 
    193 mhd_static_inline struct mhd_H2FrameContinuationInfo*
    194 mhd_h2_frame_init_continuation (union mhd_H2FrameUnion *restrict frame_info,
    195                                 uint_least32_t stream_id,
    196                                 bool end_headers)
    197 {
    198   struct mhd_H2FrameContinuationInfo *const continuation =
    199     &(frame_info->continuation);
    200   continuation->type = mhd_H2_FRAME_CONTINUATION_ID;
    201   continuation->length = 0u; /* To be set later */
    202   continuation->stream_id = stream_id;
    203 
    204   continuation->end_headers = end_headers;
    205 
    206   return continuation;
    207 }
    208 
    209 
    210 #if defined(_MSC_FULL_VER)
    211 /* Restore warnings */
    212 #pragma warning(pop)
    213 #endif /* _MSC_FULL_VER */
    214 
    215 #endif /* ! MHD_H2_FRAME_INIT_H */