Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http2_server.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
51
52#ifndef __NILOREA_HTTP2_SERVER_HEADER
53#define __NILOREA_HTTP2_SERVER_HEADER
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59#include "n_http2.h"
60
61#include <stddef.h>
62#include <stdint.h>
63#include <sys/types.h>
64
66#define N_H2_SRV_MAX_HEADERS 128
68#define N_H2_SRV_MAX_BODY (16 * 1024 * 1024)
70#define N_H2_SRV_MAX_STREAMS 128
72#define N_H2_SRV_READ_CHUNK 16384
73
76#define N_H2_ERR_NO_ERROR 0x0
77#define N_H2_ERR_PROTOCOL_ERROR 0x1
78#define N_H2_ERR_INTERNAL_ERROR 0x2
79#define N_H2_ERR_FLOW_CONTROL_ERROR 0x3
80#define N_H2_ERR_SETTINGS_TIMEOUT 0x4
81#define N_H2_ERR_STREAM_CLOSED 0x5
82#define N_H2_ERR_FRAME_SIZE_ERROR 0x6
83#define N_H2_ERR_REFUSED_STREAM 0x7
84#define N_H2_ERR_CANCEL 0x8
85#define N_H2_ERR_COMPRESSION_ERROR 0x9
86#define N_H2_ERR_CONNECT_ERROR 0xa
87#define N_H2_ERR_ENHANCE_YOUR_CALM 0xb
88#define N_H2_ERR_INADEQUATE_SECURITY 0xc
89#define N_H2_ERR_HTTP_1_1_REQUIRED 0xd
91
94typedef ssize_t (*n_h2_srv_read_fn)(void* io, void* buf, size_t len);
95
97typedef ssize_t (*n_h2_srv_write_fn)(void* io, const void* buf, size_t len);
98
100typedef struct N_H2_SERVER N_H2_SERVER;
101
104typedef struct N_H2_REQUEST {
105 uint32_t stream_id;
107 size_t nheaders;
108 const char* method;
109 const char* scheme;
110 const char* authority;
111 const char* path;
112 const unsigned char* body;
113 size_t body_len;
115
119typedef int (*n_h2_srv_request_cb)(void* user_data, N_H2_SERVER* srv, const N_H2_REQUEST* req);
120
128
131
141
153int n_http2_server_run(N_H2_SERVER* srv, n_h2_srv_request_cb cb, void* user_data);
154
170int n_http2_server_respond(N_H2_SERVER* srv, uint32_t stream_id, const N_H2_HEADER* headers, size_t nheaders, const unsigned char* body, size_t body_len);
171
177int n_http2_server_rst_stream(N_H2_SERVER* srv, uint32_t stream_id, uint32_t error_code);
178
183int n_http2_server_goaway(N_H2_SERVER* srv, uint32_t error_code);
184
186const char* n_http2_server_error(const N_H2_SERVER* srv);
187
190
191#ifdef __cplusplus
192}
193#endif
194
195#endif /* __NILOREA_HTTP2_SERVER_HEADER */
HTTP/2 (RFC 7540) wire framing plus HPACK header compression (RFC 7541).
one HPACK header field; after a decode both strings are owned (heap)
Definition n_http2.h:123
server connection state
const char * authority
the :authority pseudo-header, or NULL when absent
size_t nheaders
number of header fields
uint32_t stream_id
the stream the request arrived on
ssize_t(* n_h2_srv_read_fn)(void *io, void *buf, size_t len)
read up to len bytes into buf: returns the bytes read (>0), 0 on a clean end of stream,...
int n_http2_server_run(N_H2_SERVER *srv, n_h2_srv_request_cb cb, void *user_data)
serve the connection until it ends
int n_http2_server_handshake(N_H2_SERVER *srv)
read and validate the client connection preface, then send ours
int n_http2_server_respond(N_H2_SERVER *srv, uint32_t stream_id, const N_H2_HEADER *headers, size_t nheaders, const unsigned char *body, size_t body_len)
write a complete response on a stream (HEADERS then DATA, END_STREAM)
ssize_t(* n_h2_srv_write_fn)(void *io, const void *buf, size_t len)
write exactly len bytes from buf: returns len on success, -1 on error
size_t n_http2_server_request_count(const N_H2_SERVER *srv)
the number of requests delivered to the callback so far
int n_http2_server_goaway(N_H2_SERVER *srv, uint32_t error_code)
send GOAWAY, naming the last stream we processed
const char * method
the :method pseudo-header, or NULL when absent
N_H2_HEADER * headers
the decoded header fields, pseudo-headers included
size_t body_len
the body length in bytes
N_H2_SERVER * n_http2_server_new(void *io, n_h2_srv_read_fn rd, n_h2_srv_write_fn wr)
create a server connection over the given I/O callbacks
void n_http2_server_free(N_H2_SERVER **srv)
free a server connection and NULL the caller's pointer
const char * scheme
the :scheme pseudo-header, or NULL when absent
int(* n_h2_srv_request_cb)(void *user_data, N_H2_SERVER *srv, const N_H2_REQUEST *req)
called with each complete request; answer it with n_http2_server_respond() (or n_http2_server_rst_str...
const char * path
the :path pseudo-header, or NULL when absent
const char * n_http2_server_error(const N_H2_SERVER *srv)
the reason the connection failed, or NULL when none was recorded
const unsigned char * body
the request body, or NULL when there is none
int n_http2_server_rst_stream(N_H2_SERVER *srv, uint32_t stream_id, uint32_t error_code)
abort one stream with RST_STREAM
one complete request received on a stream; every pointer is owned by the server and valid only for th...