Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http2.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
40#ifndef __NILOREA_HTTP2_HEADER
41#define __NILOREA_HTTP2_HEADER
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47#include <stddef.h>
48#include <stdint.h>
49
51#define N_H2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
53#define N_H2_FRAME_HEADER_LEN 9
55#define N_H2_MAX_FRAME_PAYLOAD 16384
56
59#define N_H2_DATA 0x0
60#define N_H2_HEADERS 0x1
61#define N_H2_PRIORITY 0x2
62#define N_H2_RST_STREAM 0x3
63#define N_H2_SETTINGS 0x4
64#define N_H2_PUSH_PROMISE 0x5
65#define N_H2_PING 0x6
66#define N_H2_GOAWAY 0x7
67#define N_H2_WINDOW_UPDATE 0x8
68#define N_H2_CONTINUATION 0x9
73#define N_H2_FLAG_END_STREAM 0x1
74#define N_H2_FLAG_ACK 0x1
75#define N_H2_FLAG_END_HEADERS 0x4
76#define N_H2_FLAG_PADDED 0x8
77#define N_H2_FLAG_PRIORITY 0x20
82#define N_H2_SETTINGS_HEADER_TABLE_SIZE 0x1
83#define N_H2_SETTINGS_ENABLE_PUSH 0x2
84#define N_H2_SETTINGS_MAX_CONCURRENT_STREAMS 0x3
85#define N_H2_SETTINGS_INITIAL_WINDOW_SIZE 0x4
86#define N_H2_SETTINGS_MAX_FRAME_SIZE 0x5
87#define N_H2_SETTINGS_MAX_HEADER_LIST_SIZE 0x6
91typedef struct N_H2_FRAME {
92 uint32_t length;
93 int type;
94 int flags;
95 uint32_t stream_id;
96 const unsigned char* payload;
98
100typedef struct N_H2_SETTING {
101 uint16_t id;
102 uint32_t value;
104
108int n_http2_frame_parse(const unsigned char* buf, size_t len, N_H2_FRAME* frame, size_t* consumed);
109
112size_t n_http2_frame_build_header(unsigned char* out, size_t out_cap, uint32_t length, int type, int flags, uint32_t stream_id);
113
117int n_http2_settings_parse(const unsigned char* payload, size_t len, N_H2_SETTING* out, size_t max, size_t* count);
118
120#define N_H2_HPACK_DEFAULT_TABLE_SIZE 4096
121
123typedef struct N_H2_HEADER {
124 char* name;
125 char* value;
127
129typedef struct N_H2_HPACK N_H2_HPACK;
130
133N_H2_HPACK* n_http2_hpack_new(size_t max_table_size);
134
137
140void n_http2_hpack_set_max_size(N_H2_HPACK* ctx, size_t max_table_size);
141
158int n_http2_hpack_decode(N_H2_HPACK* ctx, const unsigned char* block, size_t len, N_H2_HEADER* out, size_t max_out, size_t* count);
159
170size_t n_http2_hpack_encode(const N_H2_HEADER* headers, size_t n, unsigned char* out, size_t out_cap);
171
174void n_http2_hpack_headers_free(N_H2_HEADER* headers, size_t n);
175
176#ifdef __cplusplus
177}
178#endif
179
180#endif /* __NILOREA_HTTP2_HEADER */
HPACK codec context: the dynamic table (index 0 = most recently added)
Definition n_http2.c:182
int n_http2_hpack_decode(N_H2_HPACK *ctx, const unsigned char *block, size_t len, N_H2_HEADER *out, size_t max_out, size_t *count)
decode an HPACK header block into out[] (owned name/value strings).
Definition n_http2.c:472
char * value
header field value (owned after decode)
Definition n_http2.h:125
size_t n_http2_hpack_encode(const N_H2_HEADER *headers, size_t n, unsigned char *out, size_t out_cap)
encode a header list into a block using literal representations without indexing and without Huffman ...
Definition n_http2.c:554
N_H2_HPACK * n_http2_hpack_new(size_t max_table_size)
create an HPACK codec context with the given dynamic table size limit (0 uses N_H2_HPACK_DEFAULT_TABL...
Definition n_http2.c:353
uint32_t value
parameter value
Definition n_http2.h:102
char * name
header field name (owned after decode)
Definition n_http2.h:124
uint16_t id
parameter identifier (N_H2_SETTINGS_*)
Definition n_http2.h:101
void n_http2_hpack_headers_free(N_H2_HEADER *headers, size_t n)
free the owned name/value strings of a decoded header array (does not free the array itself)
Definition n_http2.c:460
int n_http2_settings_parse(const unsigned char *payload, size_t len, N_H2_SETTING *out, size_t max, size_t *count)
parse a SETTINGS frame payload (6-byte id/value entries) into out.
Definition n_http2.c:70
uint32_t length
payload length (24-bit)
Definition n_http2.h:92
const unsigned char * payload
pointer into the input buffer (may be NULL when length is 0)
Definition n_http2.h:96
int type
frame type (N_H2_*)
Definition n_http2.h:93
int flags
frame flags
Definition n_http2.h:94
uint32_t stream_id
stream identifier (31-bit; the reserved bit is cleared)
Definition n_http2.h:95
int n_http2_frame_parse(const unsigned char *buf, size_t len, N_H2_FRAME *frame, size_t *consumed)
parse one HTTP/2 frame from a byte buffer (stateless).
Definition n_http2.c:32
size_t n_http2_frame_build_header(unsigned char *out, size_t out_cap, uint32_t length, int type, int flags, uint32_t stream_id)
write a 9-byte HTTP/2 frame header into out.
Definition n_http2.c:52
void n_http2_hpack_free(N_H2_HPACK **ctx)
free an HPACK codec context and NULL the caller's pointer
Definition n_http2.c:375
void n_http2_hpack_set_max_size(N_H2_HPACK *ctx, size_t max_table_size)
change the dynamic table size limit (a SETTINGS_HEADER_TABLE_SIZE change), evicting entries as needed...
Definition n_http2.c:390
a parsed HTTP/2 frame; payload aliases the input buffer
Definition n_http2.h:91
one HPACK header field; after a decode both strings are owned (heap)
Definition n_http2.h:123
one SETTINGS parameter (identifier and value)
Definition n_http2.h:100