ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
server_internal.h File Reference

Go to the source code of this file.

Typedefs

typedef asciichat_error_t(* http3_submit_response_fn) (void *context, unsigned status, const http_header_t *headers, size_t header_count, const void *body, size_t body_length)

Functions

size_t http_server_max_request_body (const http_server_t *server)
size_t http_server_max_response_body (const http_server_t *server)
void http_server_invalidate_http3_context (http_server_t *server, void *context)
asciichat_error_t http_server_dispatch_http3 (http_server_t *server, const char *method, const char *path, const http_header_t *headers, size_t header_count, const void *body, size_t body_length, const char *peer_address, http3_submit_response_fn submit, void *submit_context)

Typedef Documentation

◆ http3_submit_response_fn

typedef asciichat_error_t(* http3_submit_response_fn) (void *context, unsigned status, const http_header_t *headers, size_t header_count, const void *body, size_t body_length)

Definition at line 9 of file server_internal.h.

Function Documentation

◆ http_server_dispatch_http3()

asciichat_error_t http_server_dispatch_http3 ( http_server_t * server,
const char * method,
const char * path,
const http_header_t * headers,
size_t header_count,
const void * body,
size_t body_length,
const char * peer_address,
http3_submit_response_fn submit,
void * submit_context )

Definition at line 782 of file lib/network/http/server/server.c.

783 {
784 if (!server || !method || !path || (header_count && !headers) || (!body && body_length) || !submit || body_length > server->config.max_request_body_bytes)
785 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid HTTP/3 request");
786 char path_copy[2048];
787 if (strlen(path) >= sizeof(path_copy))
788 return SET_ERRNO(ERROR_NETWORK_SIZE, "HTTP/3 request path is too long");
789 platform_strncpy(path_copy, sizeof(path_copy), path, sizeof(path_copy) - 1);
790 char *query = strchr(path_copy, '?');
791 if (query)
792 *query++ = 0;
793 http_request_t request = {0};
794 request.info = (http_request_info_t){.method = method,
795 .path = path_copy,
796 .query = query,
797 .protocol = HTTP_PROTOCOL_3,
798 .headers = headers,
799 .header_count = header_count,
800 .peer_address = peer_address,
801 .user_data = server->config.user_data};
802 request.storage = (char *)body;
803 request.body_length = body_length;
805 const char *peer = peer_address ? peer_address : "unknown";
806 bool allowed = false;
807 if (rate_limiter_check(server->rate_limiter, peer, RATE_EVENT_HTTP_REQUEST, &server->request_rate_limit, &allowed) != ASCIICHAT_OK || !allowed) {
809 http_response_t limited = {
810 .server = server, .protocol = HTTP_PROTOCOL_3, .status = 429, .max_body_length = http_max_response_body(server), .http3_submit = submit, .http3_context = submit_context};
811 return http_response_finish(&limited);
812 }
816 uint64_t request_start = time_get_ns();
817 if (!http_request_admit(server)) {
818 http_response_t limited = {
819 .server = server, .protocol = HTTP_PROTOCOL_3, .status = 429, .max_body_length = http_max_response_body(server), .http3_submit = submit, .http3_context = submit_context};
820 return http_response_finish(&limited);
821 }
822 http_response_t response = {.server = server,
823 .protocol = HTTP_PROTOCOL_3,
824 .status = 200,
825 .max_body_length = http_max_response_body(server),
826 .inflight_accounted = true,
827 .http3_submit = submit,
828 .http3_context = submit_context};
829 ASCII_OTEL_SCOPE(HTTP);
830 ASCII_OTEL_ATTR_STRING("http.method", method);
831 ASCII_OTEL_ATTR_STRING("http.route", request.info.path);
832 ASCII_OTEL_ATTR_STRING("http.protocol", "3");
833 ASCII_OTEL_ATTR_STRING("http.backend", "quic-ngtcp2-nghttp3");
834 if (http_server_authorize(&server->config.basic_auth, &request.info, &response))
835 server->config.on_request(&request, &response, server->config.user_data);
836 if (!response.finished && !response.deferred) {
837 asciichat_error_t result = http_response_finish(&response);
838 ASCII_OTEL_ATTR_INT("http.status", response.status);
839 ASCII_OTEL_ATTR_INT("http.response_bytes", (int64_t)response.body_length);
842 if (result == ASCIICHAT_OK && response.protocol == HTTP_PROTOCOL_3)
844 SAFE_FREE(response.body);
845 return result;
846 }
849 SAFE_FREE(response.body);
850 return ASCIICHAT_OK;
851}
#define SET_ERRNO(code,...)
bool http_server_authorize(const http_basic_auth_config_t *config, const struct http_request_info *request, struct http_response *response)
Definition basic_auth.c:114
#define SAFE_FREE(ptr)
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ ERROR_INVALID_PARAM
struct http_request http_request_t
struct http_request_info http_request_info_t
struct http_response http_response_t
@ STATS_COUNTER_HTTP_BYTES_RECEIVED
@ STATS_GAUGE_HTTP_STREAMS_ACTIVE
asciichat_error_t http_response_finish(http_response_t *r)
void stats_counter_add(stats_scope_t *scope, stats_counter_id_t id, uint64_t amount)
void stats_duration_record(stats_scope_t *scope, stats_duration_id_t id, uint64_t elapsed_ns)
void stats_gauge_set(stats_scope_t *scope, stats_gauge_id_t id, uint64_t value)
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:102
#define ASCII_OTEL_ATTR_INT(key, value)
Definition otel.h:94
#define ASCII_OTEL_ATTR_STRING(key, value)
Definition otel.h:92
asciichat_error_t rate_limiter_record(rate_limiter_t *limiter, const char *ip_address, rate_event_type_t event_type)
Record a rate limit event.
Definition rate_limit.c:161
asciichat_error_t rate_limiter_check(rate_limiter_t *limiter, const char *ip_address, rate_event_type_t event_type, const rate_limit_config_t *config, bool *allowed)
Check if an event from an IP address should be rate limited.
Definition rate_limit.c:138
@ RATE_EVENT_HTTP_REQUEST
HTTP requests from a client identity.
Definition rate_limit.h:65
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:88
int platform_strncpy(char *dest, size_t dest_size, const char *src, size_t count)
http_server_config_t config
rate_limiter_t * rate_limiter
rate_limit_config_t request_rate_limit

References ASCII_OTEL_ATTR_INT, ASCII_OTEL_ATTR_STRING, ASCII_OTEL_SCOPE, ASCIICHAT_OK, http_server_config_t::basic_auth, http_response::body, http_request::body_length, http_response::body_length, http_server::config, http_response::deferred, ERROR_INVALID_PARAM, ERROR_NETWORK_SIZE, http_response::finished, HTTP_PROTOCOL_3, http_response_finish(), http_server_authorize(), http_request::info, http_server_config_t::max_request_body_bytes, http_server_config_t::on_request, http_request_info::path, platform_strncpy(), http_response::protocol, RATE_EVENT_HTTP_REQUEST, http_server::rate_limiter, rate_limiter_check(), rate_limiter_record(), http_server::request_rate_limit, SAFE_FREE, SET_ERRNO, stats_counter_add(), STATS_COUNTER_HTTP_BYTES_RECEIVED, STATS_COUNTER_HTTP_BYTES_SENT, STATS_COUNTER_HTTP_REJECTED, STATS_COUNTER_HTTP_REQUESTS, STATS_DURATION_HTTP_REQUEST, stats_duration_record(), STATS_GAUGE_HTTP_STREAMS_ACTIVE, stats_gauge_set(), stats_runtime_scope(), http_response::status, http_request::storage, and http_server_config_t::user_data.

◆ http_server_invalidate_http3_context()

void http_server_invalidate_http3_context ( http_server_t * server,
void * context )

Definition at line 136 of file lib/network/http/server/server.c.

136 {
137 if (!server || !context)
138 return;
139 mutex_lock(&server->response_mutex);
140 for (http_response_t *response = server->retained_responses; response; response = response->next_retained) {
141 if (response->http3_context == context) {
142 response->http3_submit = NULL;
143 response->http3_context = NULL;
144 response->cancelled = true;
145 }
146 }
147 mutex_unlock(&server->response_mutex);
148}
http_response_t * retained_responses

References http_response::next_retained, http_server::response_mutex, and http_server::retained_responses.

Referenced by http_server_http3_destroy().

◆ http_server_max_request_body()

size_t http_server_max_request_body ( const http_server_t * server)

Definition at line 861 of file lib/network/http/server/server.c.

861 {
862 return server ? server->config.max_request_body_bytes : 0;
863}

References http_server::config, and http_server_config_t::max_request_body_bytes.

◆ http_server_max_response_body()

size_t http_server_max_response_body ( const http_server_t * server)

Definition at line 133 of file lib/network/http/server/server.c.

133 {
134 return server ? http_max_response_body(server) : 0;
135}