ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
http2.c
Go to the documentation of this file.
1#include "http2.h"
2
3#include <ascii-chat/common.h>
4#include <nghttp2/nghttp2.h>
5#include <string.h>
6
7#define HTTP2_MAX_TEXT 8192u
8#define HTTP2_MAX_HEADERS 64u
9#define HTTP2_MAX_CONCURRENT_STREAMS 128u
10
24
25typedef struct {
26 char *data;
27 size_t length;
28 size_t offset;
30
40
41static request_state_t *get_request(nghttp2_session *session, int32_t stream_id) {
42 return nghttp2_session_get_stream_user_data(session, stream_id);
43}
44
45static char *copy_text(const uint8_t *data, size_t length) {
46 if (length > HTTP2_MAX_TEXT)
47 return NULL;
48 char *copy = SAFE_MALLOC(length + 1, char *);
49 if (!copy)
50 return NULL;
51 memcpy(copy, data, length);
52 copy[length] = 0;
53 return copy;
54}
55
56static int on_begin_headers(nghttp2_session *session, const nghttp2_frame *frame, void *user_data) {
57 (void)user_data;
58 if (frame->hd.type != NGHTTP2_HEADERS)
59 return 0;
60 request_state_t *request = SAFE_CALLOC(1, sizeof(*request), request_state_t *);
61 if (!request)
62 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
63 request->stream_id = frame->hd.stream_id;
64 nghttp2_session_set_stream_user_data(session, frame->hd.stream_id, request);
65 return 0;
66}
67
68static int on_header(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name, size_t name_length, const uint8_t *value, size_t value_length, uint8_t flags, void *user_data) {
69 (void)flags;
70 (void)user_data;
71 request_state_t *request = get_request(session, frame->hd.stream_id);
72 if (!request)
73 return NGHTTP2_ERR_CALLBACK_FAILURE;
74 if (name_length == sizeof(":method") - 1 && memcmp(name, ":method", sizeof(":method") - 1) == 0) {
75 SAFE_FREE(request->method);
76 request->method = copy_text(value, value_length);
77 } else if (name_length == sizeof(":path") - 1 && memcmp(name, ":path", sizeof(":path") - 1) == 0) {
78 SAFE_FREE(request->path);
79 request->path = copy_text(value, value_length);
80 } else if (request->header_count < HTTP2_MAX_HEADERS) {
81 char *header_name = copy_text(name, name_length);
82 char *header_value = copy_text(value, value_length);
83 if (!header_name || !header_value) {
84 SAFE_FREE(header_name);
85 SAFE_FREE(header_value);
86 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
87 }
88 request->header_names[request->header_count] = header_name;
89 request->header_values[request->header_count] = header_value;
90 request->headers[request->header_count] = (http2_header_t){header_name, header_value};
91 request->header_count++;
92 }
93 return (request->method || request->path) ? 0 : NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
94}
95
96static int on_data(nghttp2_session *session, uint8_t flags, int32_t stream_id, const uint8_t *data, size_t length, void *user_data) {
97 (void)flags;
98 (void)user_data;
99 request_state_t *request = get_request(session, stream_id);
100 http2_connection_t *connection = user_data;
101 if (!request || request->body_length > connection->max_body_length || length > connection->max_body_length - request->body_length)
102 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
103 size_t required = request->body_length + length;
104 if (request->body_capacity < required) {
105 size_t capacity = request->body_capacity ? request->body_capacity : 1024;
106 while (capacity < required) {
107 if (capacity > SIZE_MAX / 2) {
108 capacity = required;
109 break;
110 }
111 capacity *= 2;
112 }
113 char *body = SAFE_REALLOC(request->body, capacity, char *);
114 if (!body)
115 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
116 request->body = body;
117 request->body_capacity = capacity;
118 }
119 memcpy(request->body + request->body_length, data, length);
120 request->body_length += length;
121 return 0;
122}
123
124static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame, void *user_data) {
125 http2_connection_t *connection = user_data;
126 if (!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) || (frame->hd.type != NGHTTP2_HEADERS && frame->hd.type != NGHTTP2_DATA))
127 return 0;
128 request_state_t *request = get_request(session, frame->hd.stream_id);
129 if (!request || request->dispatched || !request->method || !request->path)
130 return NGHTTP2_ERR_PROTO;
131 request->dispatched = true;
132 http2_request_view_t view = {.method = request->method,
133 .path = request->path,
134 .body = request->body,
135 .body_length = request->body_length,
136 .stream_id = request->stream_id,
137 .headers = request->headers,
138 .header_count = request->header_count};
139 char *query = strchr(request->path, '?');
140 if (query) {
141 *query = 0;
142 view.query = query + 1;
143 }
144 return connection->on_request(connection, &view, connection->user_data) == ASCIICHAT_OK ? 0 : NGHTTP2_ERR_CALLBACK_FAILURE;
145}
146
147static int on_stream_close(nghttp2_session *session, int32_t stream_id, uint32_t error_code, void *user_data) {
148 (void)error_code;
149 (void)user_data;
150 request_state_t *request = get_request(session, stream_id);
151 if (!request)
152 return 0;
153 SAFE_FREE(request->method);
154 SAFE_FREE(request->path);
155 SAFE_FREE(request->body);
156 for (size_t i = 0; i < request->header_count; ++i) {
157 SAFE_FREE(request->header_names[i]);
158 SAFE_FREE(request->header_values[i]);
159 }
160 SAFE_FREE(request);
161 return 0;
162}
163
164static ssize_t read_response(nghttp2_session *session, int32_t stream_id, uint8_t *buffer, size_t length, uint32_t *flags, nghttp2_data_source *source, void *user_data) {
165 (void)session;
166 (void)stream_id;
167 (void)user_data;
168 response_state_t *response = source->ptr;
169 size_t remaining = response->length - response->offset;
170 size_t copied = remaining < length ? remaining : length;
171 if (copied)
172 memcpy(buffer, response->data + response->offset, copied);
173 response->offset += copied;
174 if (response->offset == response->length) {
175 *flags |= NGHTTP2_DATA_FLAG_EOF;
176 SAFE_FREE(response->data);
177 SAFE_FREE(response);
178 }
179 return (ssize_t)copied;
180}
181
182asciichat_error_t http2_connection_create(http2_connection_t **out, http2_request_fn on_request, void *user_data, size_t max_request_body_length, size_t max_response_body_length) {
183 if (!out || !on_request || max_request_body_length == 0 || max_response_body_length == 0)
184 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing HTTP/2 request callback");
185 http2_connection_t *connection = SAFE_CALLOC(1, sizeof(*connection), http2_connection_t *);
186 nghttp2_session_callbacks *callbacks = NULL;
187 nghttp2_option *options = NULL;
188 if (nghttp2_session_callbacks_new(&callbacks) != 0 || nghttp2_option_new(&options) != 0) {
189 nghttp2_session_callbacks_del(callbacks);
190 nghttp2_option_del(options);
191 SAFE_FREE(connection);
192 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Cannot initialize HTTP/2 callbacks");
193 }
194 nghttp2_session_callbacks_set_on_begin_headers_callback(callbacks, on_begin_headers);
195 nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header);
196 nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, on_data);
197 nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, on_frame_recv);
198 nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, on_stream_close);
199 if (nghttp2_session_server_new2(&connection->session, callbacks, connection, options) != 0) {
200 nghttp2_session_callbacks_del(callbacks);
201 nghttp2_option_del(options);
202 SAFE_FREE(connection);
203 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Cannot initialize HTTP/2 session");
204 }
205 nghttp2_session_callbacks_del(callbacks);
206 nghttp2_option_del(options);
207 nghttp2_settings_entry settings = {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, HTTP2_MAX_CONCURRENT_STREAMS};
208 if (nghttp2_submit_settings(connection->session, NGHTTP2_FLAG_NONE, &settings, 1) != 0) {
209 nghttp2_session_del(connection->session);
210 SAFE_FREE(connection);
211 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Cannot configure HTTP/2 stream limit");
212 }
213 connection->on_request = on_request;
214 connection->user_data = user_data;
215 connection->max_body_length = max_request_body_length;
216 connection->max_response_body_length = max_response_body_length;
217 *out = connection;
218 return ASCIICHAT_OK;
219}
220
222 if (!connection || !*connection)
223 return;
224 nghttp2_session_del((*connection)->session);
225 SAFE_FREE(*connection);
226 *connection = NULL;
227}
228
229asciichat_error_t http2_connection_receive(http2_connection_t *connection, const void *data, size_t length) {
230 if (!connection || (!data && length))
231 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid HTTP/2 input");
232 if (nghttp2_session_mem_recv(connection->session, data, length) < 0)
233 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid HTTP/2 frame sequence");
234 return ASCIICHAT_OK;
235}
236
238 if (!connection || !data || !length)
239 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid HTTP/2 output request");
240 ssize_t output_length = nghttp2_session_mem_send(connection->session, &connection->output);
241 if (output_length < 0)
242 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Cannot serialize HTTP/2 output");
243 connection->output_length = (size_t)output_length;
244 *data = connection->output;
245 *length = connection->output_length;
246 return ASCIICHAT_OK;
247}
248
249asciichat_error_t http2_connection_submit_response(http2_connection_t *connection, int32_t stream_id, unsigned status, const http_header_t *headers, size_t header_count, const void *body,
250 size_t body_length) {
251 if (!connection || status < 100 || status > 599 || body_length > connection->max_response_body_length || (!body && body_length))
252 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid HTTP/2 response");
253 if (header_count > HTTP2_MAX_HEADERS || (header_count && !headers))
254 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid HTTP/2 response headers");
255 char *status_text = SAFE_MALLOC(4, char *);
256 if (!status_text)
257 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate HTTP/2 response status");
258 safe_snprintf(status_text, 4, "%u", status);
259 response_state_t *response = SAFE_CALLOC(1, sizeof(*response), response_state_t *);
260 if (!response) {
261 SAFE_FREE(status_text);
262 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate HTTP/2 response state");
263 }
264 response->length = body_length;
265 if (body_length) {
266 response->data = SAFE_MALLOC(body_length, char *);
267 if (!response->data) {
268 SAFE_FREE(response);
269 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate HTTP/2 response body");
270 }
271 memcpy(response->data, body, body_length);
272 }
273 nghttp2_nv *fields = SAFE_CALLOC(header_count + 1, sizeof(*fields), nghttp2_nv *);
274 if (!fields) {
275 SAFE_FREE(status_text);
276 SAFE_FREE(response->data);
277 SAFE_FREE(response);
278 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate HTTP/2 response headers");
279 }
280 fields[0] = (nghttp2_nv){(uint8_t *)":status", (uint8_t *)status_text, 7, strlen(status_text), NGHTTP2_NV_FLAG_NONE};
281 for (size_t i = 0; i < header_count; ++i) {
282 if (!headers[i].name || !headers[i].value) {
283 SAFE_FREE(fields);
284 SAFE_FREE(status_text);
285 SAFE_FREE(response->data);
286 SAFE_FREE(response);
287 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid HTTP/2 response header");
288 }
289 fields[i + 1] = (nghttp2_nv){(uint8_t *)headers[i].name, (uint8_t *)headers[i].value, strlen(headers[i].name), strlen(headers[i].value), NGHTTP2_NV_FLAG_NONE};
290 }
291 nghttp2_data_provider provider = {.source = {.ptr = response}, .read_callback = read_response};
292 int result = nghttp2_submit_response(connection->session, stream_id, fields, header_count + 1, body_length ? &provider : NULL);
293 SAFE_FREE(fields);
294 SAFE_FREE(status_text);
295 if (!body_length)
296 SAFE_FREE(response);
297 if (result != 0) {
298 if (body_length) {
299 SAFE_FREE(response->data);
300 SAFE_FREE(response);
301 }
302 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Cannot submit HTTP/2 response");
303 }
304 return ASCIICHAT_OK;
305}
#define SET_ERRNO(code,...)
audio_visualization_source_t source
Definition fft.c:25
#define SAFE_REALLOC(ptr, size, cast)
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_CALLOC(count, size, cast)
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
void http2_connection_destroy(http2_connection_t **connection)
Definition http2.c:221
asciichat_error_t http2_connection_receive(http2_connection_t *connection, const void *data, size_t length)
Definition http2.c:229
asciichat_error_t http2_connection_create(http2_connection_t **out, http2_request_fn on_request, void *user_data, size_t max_request_body_length, size_t max_response_body_length)
Definition http2.c:182
#define HTTP2_MAX_CONCURRENT_STREAMS
Definition http2.c:9
asciichat_error_t http2_connection_submit_response(http2_connection_t *connection, int32_t stream_id, unsigned status, const http_header_t *headers, size_t header_count, const void *body, size_t body_length)
Definition http2.c:249
#define HTTP2_MAX_HEADERS
Definition http2.c:8
asciichat_error_t http2_connection_next_output(http2_connection_t *connection, const uint8_t **data, size_t *length)
Definition http2.c:237
#define HTTP2_MAX_TEXT
Definition http2.c:7
struct http2_header http2_header_t
asciichat_error_t(* http2_request_fn)(http2_connection_t *connection, const http2_request_view_t *request, void *user_data)
Definition http2.h:23
struct http2_connection http2_connection_t
Definition http2.h:8
⚙️ Common definitions, error codes, macros, and types shared throughout the application
static bool size_t gotel_query_t * out
Definition queries.h:10
🔤 String Manipulation and Shell Escaping Utilities
size_t max_response_body_length
Definition http2.c:36
nghttp2_session * session
Definition http2.c:32
size_t output_length
Definition http2.c:38
size_t max_body_length
Definition http2.c:35
http2_request_fn on_request
Definition http2.c:33
void * user_data
Definition http2.c:34
const uint8_t * output
Definition http2.c:37
const char * query
Definition http2.h:12
size_t body_length
Definition http2.c:16
http2_header_t headers[64u]
Definition http2.c:18
char * path
Definition http2.c:14
char * header_values[64u]
Definition http2.c:20
bool dispatched
Definition http2.c:22
size_t header_count
Definition http2.c:21
size_t body_capacity
Definition http2.c:17
char * body
Definition http2.c:15
char * header_names[64u]
Definition http2.c:19
char * method
Definition http2.c:13
int32_t stream_id
Definition http2.c:12
char * data
Definition http2.c:26
size_t length
Definition http2.c:27
size_t offset
Definition http2.c:28