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 File Reference

Go to the source code of this file.

Data Structures

struct  request_state_t
struct  response_state_t
struct  http2_connection

Macros

#define HTTP2_MAX_TEXT   8192u
#define HTTP2_MAX_HEADERS   64u
#define HTTP2_MAX_CONCURRENT_STREAMS   128u

Functions

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)
void http2_connection_destroy (http2_connection_t **connection)
asciichat_error_t http2_connection_receive (http2_connection_t *connection, const void *data, size_t length)
asciichat_error_t http2_connection_next_output (http2_connection_t *connection, const uint8_t **data, size_t *length)
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)

Macro Definition Documentation

◆ HTTP2_MAX_CONCURRENT_STREAMS

#define HTTP2_MAX_CONCURRENT_STREAMS   128u

Definition at line 9 of file http2.c.

Referenced by http2_connection_create().

◆ HTTP2_MAX_HEADERS

#define HTTP2_MAX_HEADERS   64u

Definition at line 8 of file http2.c.

Referenced by http2_connection_submit_response().

◆ HTTP2_MAX_TEXT

#define HTTP2_MAX_TEXT   8192u

Definition at line 7 of file http2.c.

Function Documentation

◆ http2_connection_create()

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 at line 182 of file http2.c.

182 {
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}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define HTTP2_MAX_CONCURRENT_STREAMS
Definition http2.c:9
struct http2_connection http2_connection_t
Definition http2.h:8
static bool size_t gotel_query_t * out
Definition queries.h:10
size_t max_response_body_length
Definition http2.c:36
nghttp2_session * session
Definition http2.c:32
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

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_NETWORK_PROTOCOL, HTTP2_MAX_CONCURRENT_STREAMS, http2_connection::max_body_length, http2_connection::max_response_body_length, http2_connection::on_request, out, SAFE_CALLOC, SAFE_FREE, http2_connection::session, SET_ERRNO, and http2_connection::user_data.

◆ http2_connection_destroy()

void http2_connection_destroy ( http2_connection_t ** connection)

Definition at line 221 of file http2.c.

221 {
222 if (!connection || !*connection)
223 return;
224 nghttp2_session_del((*connection)->session);
225 SAFE_FREE(*connection);
226 *connection = NULL;
227}

References SAFE_FREE.

◆ http2_connection_next_output()

asciichat_error_t http2_connection_next_output ( http2_connection_t * connection,
const uint8_t ** data,
size_t * length )

Definition at line 237 of file http2.c.

237 {
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}
size_t output_length
Definition http2.c:38
const uint8_t * output
Definition http2.c:37

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_NETWORK_PROTOCOL, http2_connection::output, http2_connection::output_length, http2_connection::session, and SET_ERRNO.

◆ http2_connection_receive()

asciichat_error_t http2_connection_receive ( http2_connection_t * connection,
const void * data,
size_t length )

Definition at line 229 of file http2.c.

229 {
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}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_NETWORK_PROTOCOL, http2_connection::session, and SET_ERRNO.

◆ http2_connection_submit_response()

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 at line 249 of file http2.c.

250 {
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 SAFE_MALLOC(size, cast)
unsigned char uint8_t
@ ERROR_MEMORY
Definition error_codes.h:55
#define HTTP2_MAX_HEADERS
Definition http2.c:8
char * data
Definition http2.c:26
size_t length
Definition http2.c:27

References ASCIICHAT_OK, response_state_t::data, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_NETWORK_PROTOCOL, HTTP2_MAX_HEADERS, response_state_t::length, http2_connection::max_response_body_length, SAFE_CALLOC, SAFE_FREE, SAFE_MALLOC, http2_connection::session, SET_ERRNO, and http_header_t::value.

Referenced by http_response_finish().