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

Go to the source code of this file.

Data Structures

struct  http_write
struct  http_server
struct  http_request
struct  http_response
struct  http_server_client

Macros

#define HTTP_READ_CAPACITY   (1024u * 1024u)
#define HTTP_HEADER_MAX_BYTES   (64u * 1024u)
#define HTTP_HEADER_CAPACITY   64u

Typedefs

typedef struct http_server_client http_server_client_t
typedef struct http_write http_write_t

Functions

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_response_set_status (http_response_t *r, unsigned s)
asciichat_error_t http_response_retain (http_response_t *r, http_response_t **retained)
void http_response_release (http_response_t **response)
asciichat_error_t http_response_set_header (http_response_t *r, const char *n, const char *v)
asciichat_error_t http_response_write (http_response_t *r, const void *d, size_t n)
size_t http_response_body_length (const http_response_t *r)
asciichat_error_t http_response_finish (http_response_t *r)
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)
const http_request_info_t * http_request_info (const http_request_t *r)
const void * http_request_body (const http_request_t *r, size_t *n)
size_t http_server_max_request_body (const http_server_t *server)
bool http_request_is_cancelled (const http_request_t *r)
bool http_response_is_cancelled (const http_response_t *r)
asciichat_error_t http_request_cancel (http_request_t *r)
asciichat_error_t http_server_init (http_server_t **out, const http_server_config_t *cfg)
asciichat_error_t http_server_wakeup (http_server_t *s)
asciichat_error_t http_server_start (http_server_t *s)
asciichat_error_t http_server_adopt_socket (http_server_t *s, intptr_t socket_handle)
asciichat_error_t http_server_run (http_server_t *s)
void http_server_stop (http_server_t *s)
void http_server_destroy (http_server_t **p)

Macro Definition Documentation

◆ HTTP_HEADER_CAPACITY

#define HTTP_HEADER_CAPACITY   64u

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

Referenced by http_response_set_header().

◆ HTTP_HEADER_MAX_BYTES

#define HTTP_HEADER_MAX_BYTES   (64u * 1024u)

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

◆ HTTP_READ_CAPACITY

#define HTTP_READ_CAPACITY   (1024u * 1024u)

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

Typedef Documentation

◆ http_server_client_t

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

◆ http_write_t

typedef struct http_write http_write_t

Function Documentation

◆ http_request_body()

const void * http_request_body ( const http_request_t * r,
size_t * n )

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

855 {
856 if (n)
857 *n = r ? r->body_length : 0;
858 return r && r->body_length ? r->storage + r->body_offset : NULL;
859}

References http_request::body_length, http_request::body_offset, and http_request::storage.

◆ http_request_cancel()

asciichat_error_t http_request_cancel ( http_request_t * r)

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

870 {
871 if (!r)
872 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing HTTP request");
873 if (!r->cancelled)
875 r->cancelled = true;
876 return ASCIICHAT_OK;
877}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
void stats_counter_add(stats_scope_t *scope, stats_counter_id_t id, uint64_t amount)
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:88

References ASCIICHAT_OK, http_request::cancelled, ERROR_INVALID_PARAM, SET_ERRNO, stats_counter_add(), STATS_COUNTER_HTTP_CANCELLED, and stats_runtime_scope().

◆ http_request_info()

const http_request_info_t * http_request_info ( const http_request_t * r)

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

852 {
853 return r ? &r->info : NULL;
854}

References http_request::info.

◆ http_request_is_cancelled()

bool http_request_is_cancelled ( const http_request_t * r)

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

864 {
865 return !r || r->cancelled;
866}

References http_request::cancelled.

◆ http_response_body_length()

size_t http_response_body_length ( const http_response_t * response)

Return the number of bytes buffered for the response body.

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

729 {
730 return r ? r->body_length : 0;
731}

References http_response::body_length.

◆ http_response_finish()

asciichat_error_t http_response_finish ( http_response_t * r)

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

732 {
733 if (!r || r->finished)
734 return SET_ERRNO(ERROR_INVALID_STATE, "HTTP response already finished");
735 r->finished = true;
736 if (r->status >= 400)
738 if (r->cancelled) {
739 if (r->inflight_accounted && !r->deferred) {
740 http_request_release(r->server);
741 r->inflight_accounted = false;
742 }
743 return SET_ERRNO(ERROR_INVALID_STATE, "HTTP response was cancelled");
744 }
745 asciichat_error_t result;
746 if (r->http3_submit)
747 result = r->http3_submit(r->http3_context, r->status, r->headers, r->header_count, r->body, r->body_length);
748 else if (r->client && r->client->http2_mode) {
750 if (result == ASCIICHAT_OK)
751 result = http2_flush(r->client);
752 } else {
753 size_t cap = 256 + r->body_length;
754 for (size_t i = 0; i < r->header_count; i++)
755 cap += strlen(r->headers[i].name) + strlen(r->headers[i].value) + 4;
756 char *out = SAFE_MALLOC(cap, char *);
757 if (!out) {
758 if (r->inflight_accounted && !r->deferred) {
759 http_request_release(r->server);
760 r->inflight_accounted = false;
761 }
762 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate HTTP response");
763 }
764 const char *version = r->protocol == HTTP_PROTOCOL_10 ? "HTTP/1.0" : "HTTP/1.1";
765 int n = safe_snprintf(out, cap, "%s %u %s\r\nContent-Length: %zu\r\nConnection: close\r\n", version, r->status, reason(r->status), r->body_length);
766 for (size_t i = 0; i < r->header_count; i++)
767 n += safe_snprintf(out + n, cap - (size_t)n, "%s: %s\r\n", r->headers[i].name, r->headers[i].value);
768 n += safe_snprintf(out + n, cap - (size_t)n, "\r\n");
769 if (r->body_length)
770 memcpy(out + n, r->body, r->body_length);
771 n += (int)r->body_length;
772 result = client_write_plain(r->client, out, (size_t)n, true);
773 SAFE_FREE(out);
774 }
775 if (r->inflight_accounted && !r->deferred) {
776 http_request_release(r->server);
777 r->inflight_accounted = false;
778 }
779 return result;
780}
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
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
static bool size_t gotel_query_t * out
Definition queries.h:10
http_server_client_t * client
http3_submit_response_fn http3_submit

References ASCIICHAT_OK, http_response::body, http_response::body_length, http_response::cancelled, http_response::client, http_response::deferred, ERROR_INVALID_STATE, ERROR_MEMORY, http_response::finished, http_response::header_count, http_response::headers, http_server_client::http2, http2_connection_submit_response(), http_server_client::http2_mode, http_response::http2_stream_id, http_response::http3_context, http_response::http3_submit, HTTP_PROTOCOL_10, http_response::inflight_accounted, http_header_t::name, out, http_response::protocol, SAFE_FREE, SAFE_MALLOC, http_response::server, SET_ERRNO, stats_counter_add(), STATS_COUNTER_HTTP_ERRORS, stats_runtime_scope(), http_response::status, and http_header_t::value.

Referenced by http_server_authorize(), and http_server_dispatch_http3().

◆ http_response_is_cancelled()

bool http_response_is_cancelled ( const http_response_t * r)

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

867 {
868 return !r || r->cancelled;
869}

References http_response::cancelled.

◆ http_response_release()

void http_response_release ( http_response_t ** response)

Release a response retained with http_response_retain().

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

678 {
679 if (!response || !*response)
680 return;
681 if ((*response)->retained) {
682 http_server_t *server = (*response)->server;
683 if ((*response)->inflight_accounted) {
684 http_request_release(server);
685 (*response)->inflight_accounted = false;
686 }
687 if (server) {
688 mutex_lock(&server->response_mutex);
689 http_response_t **cursor = &server->retained_responses;
690 while (*cursor && *cursor != *response)
691 cursor = &(*cursor)->next_retained;
692 if (*cursor == *response)
693 *cursor = (*response)->next_retained;
694 mutex_unlock(&server->response_mutex);
695 }
696 SAFE_FREE((*response)->body);
697 SAFE_FREE(*response);
698 }
699 *response = NULL;
700}
struct http_response http_response_t
http_response_t * retained_responses
struct http_server http_server_t
Definition tls.h:8

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

◆ http_response_retain()

asciichat_error_t http_response_retain ( http_response_t * response,
http_response_t ** retained )

Retain a response for completion after the request callback returns.

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

640 {
641 if (!r || !retained || !r->server || r->finished || r->retained)
642 return SET_ERRNO(ERROR_INVALID_STATE, "HTTP response cannot be retained");
643 http_response_t *copy = SAFE_CALLOC(1, sizeof(*copy), http_response_t *);
644 if (!copy)
645 return SET_ERRNO(ERROR_MEMORY, "Cannot retain HTTP response");
646 copy->stream = r->stream;
647 copy->client = r->client;
649 copy->protocol = r->protocol;
651 copy->http3_submit = r->http3_submit;
652 copy->http3_context = r->http3_context;
653 copy->server = r->server;
655 copy->status = r->status;
656 copy->header_count = r->header_count;
657 memcpy(copy->headers, r->headers, r->header_count * sizeof(r->headers[0]));
658 if (r->body_length) {
659 copy->body = SAFE_MALLOC(r->body_length, char *);
660 if (!copy->body) {
661 SAFE_FREE(copy);
662 return SET_ERRNO(ERROR_MEMORY, "Cannot retain HTTP response body");
663 }
664 memcpy(copy->body, r->body, r->body_length);
665 copy->body_length = r->body_length;
666 }
667 copy->retained = true;
668 mutex_lock(&r->server->response_mutex);
670 r->server->retained_responses = copy;
671 mutex_unlock(&r->server->response_mutex);
672 r->deferred = true;
673 r->retained = true;
674 r->inflight_accounted = false;
675 *retained = copy;
676 return ASCIICHAT_OK;
677}
#define SAFE_CALLOC(count, size, cast)

References ASCIICHAT_OK, http_response::body, http_response::body_length, http_response::client, http_response::deferred, ERROR_INVALID_STATE, ERROR_MEMORY, http_response::finished, http_response::header_count, http_response::headers, http_response::http2_stream_id, http_response::http3_context, http_response::http3_submit, http_response::inflight_accounted, http_response::max_body_length, http_response::next_retained, http_response::protocol, http_server::response_mutex, http_response::retained, http_server::retained_responses, SAFE_CALLOC, SAFE_FREE, SAFE_MALLOC, http_response::server, SET_ERRNO, http_response::status, and http_response::stream.

◆ http_response_set_header()

asciichat_error_t http_response_set_header ( http_response_t * r,
const char * n,
const char * v )

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

701 {
702 if (!r || !n || !v)
703 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid HTTP response header");
704 for (size_t i = 0; i < r->header_count; ++i) {
705 if (platform_strcasecmp(r->headers[i].name, n) == 0) {
706 r->headers[i] = (http_header_t){n, v};
707 return ASCIICHAT_OK;
708 }
709 }
711 return SET_ERRNO(ERROR_INVALID_PARAM, "HTTP response header limit exceeded");
712 r->headers[r->header_count++] = (http_header_t){n, v};
713 return ASCIICHAT_OK;
714}
#define HTTP_HEADER_CAPACITY
int platform_strcasecmp(const char *s1, const char *s2)

References ASCIICHAT_OK, ERROR_INVALID_PARAM, http_response::header_count, http_response::headers, HTTP_HEADER_CAPACITY, http_header_t::name, platform_strcasecmp(), and SET_ERRNO.

Referenced by http_server_authorize().

◆ http_response_set_status()

asciichat_error_t http_response_set_status ( http_response_t * r,
unsigned s )

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

634 {
635 if (!r || s < 100 || s > 599)
636 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid HTTP status");
637 r->status = s;
638 return ASCIICHAT_OK;
639}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, SET_ERRNO, and http_response::status.

Referenced by http_server_authorize().

◆ http_response_write()

asciichat_error_t http_response_write ( http_response_t * r,
const void * d,
size_t n )

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

715 {
716 if (!r || (!d && n) || r->finished)
717 return SET_ERRNO(ERROR_INVALID_STATE, "HTTP response is not writable");
718 if (n > r->max_body_length || r->body_length > r->max_body_length - n)
719 return SET_ERRNO(ERROR_NETWORK_SIZE, "HTTP response body exceeds configured limit");
720 char *p = SAFE_REALLOC(r->body, r->body_length + n + 1, char *);
721 if (!p)
722 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate HTTP response body");
723 r->body = p;
724 memcpy(p + r->body_length, d, n);
725 r->body_length += n;
726 p[r->body_length] = 0;
727 return ASCIICHAT_OK;
728}
#define SAFE_REALLOC(ptr, size, cast)
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83

References ASCIICHAT_OK, http_response::body, http_response::body_length, ERROR_INVALID_STATE, ERROR_MEMORY, ERROR_NETWORK_SIZE, http_response::finished, http_response::max_body_length, SAFE_REALLOC, and SET_ERRNO.

◆ http_server_adopt_socket()

asciichat_error_t http_server_adopt_socket ( http_server_t * server,
intptr_t socket_handle )

Adopt an already-listening native socket before starting the server.

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

1297 {
1298 if (!s || s->running || s->adopted_socket || socket_handle < 0)
1299 return SET_ERRNO(ERROR_INVALID_STATE, "HTTP listener cannot adopt this socket");
1300 if (uv_tcp_open(&s->listener, (uv_os_sock_t)socket_handle) != 0)
1301 return SET_ERRNO(ERROR_NETWORK, "Cannot adopt HTTP listener socket");
1302 s->adopted_socket = true;
1303 return ASCIICHAT_OK;
1304}
@ ERROR_NETWORK
Definition error_codes.h:77

References http_server::adopted_socket, ASCIICHAT_OK, ERROR_INVALID_STATE, ERROR_NETWORK, http_server::listener, http_server::running, and SET_ERRNO.

◆ http_server_destroy()

void http_server_destroy ( http_server_t ** p)

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

1337 {
1338 if (!p || !*p)
1339 return;
1340 http_server_t *s = *p;
1342 while (uv_run(&s->loop, UV_RUN_NOWAIT) != 0)
1343 ;
1344 uv_loop_close(&s->loop);
1345 http_server_detach_http3(s);
1347 mutex_destroy(&s->response_mutex);
1348 SSL_CTX_free(s->tls_context);
1349 SAFE_FREE(s);
1350 *p = NULL;
1351}
void http_server_stop(http_server_t *s)
void rate_limiter_destroy(rate_limiter_t *limiter)
Destroy rate limiter and free resources.
Definition rate_limit.c:126
rate_limiter_t * rate_limiter

References http_server_stop(), http_server::loop, http_server::rate_limiter, rate_limiter_destroy(), http_server::response_mutex, SAFE_FREE, and http_server::tls_context.

Referenced by api_server_main(), and mcp_http_run().

◆ 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}
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
unsigned long long uint64_t
struct http_request http_request_t
struct http_request_info http_request_info_t
@ STATS_COUNTER_HTTP_BYTES_RECEIVED
@ STATS_GAUGE_HTTP_STREAMS_ACTIVE
asciichat_error_t http_response_finish(http_response_t *r)
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
int platform_strncpy(char *dest, size_t dest_size, const char *src, size_t count)
http_server_config_t config
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_init()

asciichat_error_t http_server_init ( http_server_t ** out,
const http_server_config_t * cfg )

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

1178 {
1179 if (!out || !cfg || !cfg->on_request || (!cfg->enable_http1 && !cfg->enable_http2 && !cfg->enable_http3))
1180 return SET_ERRNO(ERROR_INVALID_PARAM, "At least one HTTP protocol and a request handler are required");
1181 if (cfg->max_connections == 0 || cfg->max_request_body_bytes == 0)
1182 return SET_ERRNO(ERROR_INVALID_PARAM, "HTTP limits must be non-zero");
1183 if (cfg->basic_auth.enabled && (!cfg->basic_auth.username || !cfg->basic_auth.password || !cfg->basic_auth.username[0] || !cfg->basic_auth.password[0]))
1184 return SET_ERRNO(ERROR_INVALID_PARAM, "Basic authentication requires non-empty credentials");
1185 if ((cfg->enable_http3 && !cfg->http3_endpoint) || ((cfg->tls_cert_path == NULL) != (cfg->tls_key_path == NULL)))
1186 return SET_ERRNO(ERROR_INVALID_PARAM, "HTTP/3 requires a pre-opened endpoint and TLS requires certificate and key");
1187 http_server_t *s = SAFE_CALLOC(1, sizeof(*s), http_server_t *);
1188 if (mutex_init(&s->response_mutex, "http_responses") != 0) {
1189 SAFE_FREE(s);
1190 return SET_ERRNO(ERROR_NETWORK, "Cannot initialize HTTP response synchronization");
1191 }
1193 if (!s->rate_limiter) {
1194 mutex_destroy(&s->response_mutex);
1195 SAFE_FREE(s);
1196 return SET_ERRNO(ERROR_MEMORY, "Cannot initialize HTTP rate limiter");
1197 }
1198 s->config = *cfg;
1199 if (cfg->tls_cert_path) {
1200 s->tls_context = SSL_CTX_new(TLS_server_method());
1201 if (!s->tls_context) {
1203 mutex_destroy(&s->response_mutex);
1204 SAFE_FREE(s);
1205 return SET_ERRNO(ERROR_NETWORK, "Cannot initialize TLS context");
1206 }
1207 SSL_CTX_set_app_data(s->tls_context, s);
1208 SSL_CTX_set_alpn_select_cb(s->tls_context, http_select_alpn, s);
1209 if (cfg->tls_key_password) {
1210 SSL_CTX_set_default_passwd_cb_userdata(s->tls_context, (void *)cfg->tls_key_password);
1211 }
1212 if (cfg->tls_key_password)
1213 SSL_CTX_set_default_passwd_cb(s->tls_context, tls_password_cb);
1214 if (SSL_CTX_use_certificate_chain_file(s->tls_context, cfg->tls_cert_path) != 1 || SSL_CTX_use_PrivateKey_file(s->tls_context, cfg->tls_key_path, SSL_FILETYPE_PEM) != 1 ||
1215 SSL_CTX_check_private_key(s->tls_context) != 1) {
1216 SSL_CTX_free(s->tls_context);
1217 s->tls_context = NULL;
1219 mutex_destroy(&s->response_mutex);
1220 SAFE_FREE(s);
1221 return SET_ERRNO(ERROR_NETWORK, "Cannot load HTTP TLS certificate or key");
1222 }
1223 }
1227 if (uv_loop_init(&s->loop) != 0) {
1229 mutex_destroy(&s->response_mutex);
1230 SSL_CTX_free(s->tls_context);
1231 SAFE_FREE(s);
1232 return SET_ERRNO(ERROR_NETWORK, "Cannot initialize HTTP event loop");
1233 }
1234 if (uv_tcp_init(&s->loop, &s->listener) != 0) {
1235 uv_loop_close(&s->loop);
1237 mutex_destroy(&s->response_mutex);
1238 SSL_CTX_free(s->tls_context);
1239 SAFE_FREE(s);
1240 return SET_ERRNO(ERROR_NETWORK, "Cannot initialize HTTP listener");
1241 }
1242 s->listener_initialized = true;
1243 s->listener.data = s;
1244 if (uv_tcp_init(&s->loop, &s->listener6) != 0) {
1245 uv_close((uv_handle_t *)&s->listener, NULL);
1246 return SET_ERRNO(ERROR_NETWORK, "Cannot initialize IPv6 HTTP listener");
1247 }
1248 s->listener6_initialized = true;
1249 s->listener6.data = s;
1250 if (uv_async_init(&s->loop, &s->stop_async, stop_async_cb) != 0) {
1251 uv_close((uv_handle_t *)&s->listener, NULL);
1252 uv_run(&s->loop, UV_RUN_DEFAULT);
1253 uv_loop_close(&s->loop);
1255 mutex_destroy(&s->response_mutex);
1256 SSL_CTX_free(s->tls_context);
1257 SAFE_FREE(s);
1258 return SET_ERRNO(ERROR_NETWORK, "Cannot initialize HTTP stop handle");
1259 }
1260 s->stop_async_initialized = true;
1261 s->stop_async.data = s;
1262 if (uv_async_init(&s->loop, &s->wake_async, wake_async_cb) != 0) {
1263 uv_close((uv_handle_t *)&s->stop_async, NULL);
1264 uv_close((uv_handle_t *)&s->listener, NULL);
1265 uv_run(&s->loop, UV_RUN_DEFAULT);
1266 uv_loop_close(&s->loop);
1267 mutex_destroy(&s->response_mutex);
1269 SSL_CTX_free(s->tls_context);
1270 SAFE_FREE(s);
1271 return SET_ERRNO(ERROR_NETWORK, "Cannot initialize HTTP wakeup handle");
1272 }
1273 s->wake_async_initialized = true;
1274 s->wake_async.data = s;
1275 *out = s;
1276 return ASCIICHAT_OK;
1277}
unsigned int uint32_t
rate_limiter_t * rate_limiter_create_memory(void)
Create in-memory rate limiter.
Definition rate_limit.c:83
const rate_limit_config_t DEFAULT_RATE_LIMITS[RATE_EVENT_MAX]
Default rate limits for each event type.
Definition rate_limit.c:33
const char * password
Definition basic_auth.h:18
const char * username
Definition basic_auth.h:17
uint32_t max_events
Maximum events allowed.
Definition rate_limit.h:78
uint32_t window_secs
Time window in seconds.
Definition rate_limit.h:79

References ASCIICHAT_OK, http_server_config_t::basic_auth, http_server::config, DEFAULT_RATE_LIMITS, http_server_config_t::enable_http1, http_server_config_t::enable_http2, http_server_config_t::enable_http3, http_basic_auth_config::enabled, ERROR_INVALID_PARAM, ERROR_MEMORY, ERROR_NETWORK, http_server_config_t::http3_endpoint, http_server::listener, http_server::listener6, http_server::listener6_initialized, http_server::listener_initialized, http_server::loop, http_server_config_t::max_connections, rate_limit_config_t::max_events, http_server_config_t::max_request_body_bytes, http_server_config_t::max_requests_per_peer, http_server_config_t::on_request, out, http_basic_auth_config::password, RATE_EVENT_HTTP_REQUEST, http_server::rate_limiter, rate_limiter_create_memory(), rate_limiter_destroy(), http_server::request_rate_limit, http_server_config_t::request_rate_window_seconds, http_server::response_mutex, SAFE_CALLOC, SAFE_FREE, SET_ERRNO, http_server::stop_async, http_server::stop_async_initialized, http_server_config_t::tls_cert_path, http_server::tls_context, http_server_config_t::tls_key_password, http_server_config_t::tls_key_path, http_basic_auth_config::username, http_server::wake_async, http_server::wake_async_initialized, and rate_limit_config_t::window_secs.

Referenced by api_server_main(), and mcp_http_run().

◆ 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}

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}

◆ http_server_run()

asciichat_error_t http_server_run ( http_server_t * server)

Run the server loop when embedding the server without a background thread.

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

1305 {
1306 if (!s || s->running || s->thread_started)
1307 return SET_ERRNO(ERROR_INVALID_STATE, "HTTP server loop is already running in its worker thread");
1308 asciichat_error_t result = http_server_start_listener(s);
1309 if (result != ASCIICHAT_OK)
1310 return result;
1311 s->running = true;
1312 s->foreground_running = true;
1313 uv_run(&s->loop, UV_RUN_DEFAULT);
1314 s->foreground_running = false;
1315 s->running = false;
1316 http_server_detach_http3(s);
1317 return ASCIICHAT_OK;
1318}

References ASCIICHAT_OK, ERROR_INVALID_STATE, http_server::foreground_running, http_server::loop, http_server::running, SET_ERRNO, and http_server::thread_started.

◆ http_server_start()

asciichat_error_t http_server_start ( http_server_t * s)

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

1283 {
1284 if (!s || s->running)
1285 return SET_ERRNO(ERROR_INVALID_STATE, "HTTP server is already running");
1286 asciichat_error_t result = http_server_start_listener(s);
1287 if (result != ASCIICHAT_OK)
1288 return result;
1289 s->running = true;
1290 if (platform_thread_create(&s->thread, "http_io", http_loop_thread, s) != 0) {
1291 http_server_rollback_start(s);
1292 return SET_ERRNO(ERROR_THREAD, "Cannot create HTTP event-loop thread");
1293 }
1294 s->thread_started = true;
1295 return ASCIICHAT_OK;
1296}
@ ERROR_THREAD

References ASCIICHAT_OK, ERROR_INVALID_STATE, ERROR_THREAD, http_server::running, SET_ERRNO, http_server::thread, and http_server::thread_started.

Referenced by api_server_main(), and mcp_http_run().

◆ http_server_stop()

void http_server_stop ( http_server_t * s)

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

1319 {
1320 if (!s || !s->running)
1321 return;
1322 s->running = false;
1323 if (s->thread_started) {
1324 uv_async_send(&s->stop_async);
1325 thread_join(&s->thread, NULL);
1326 s->thread_started = false;
1327 http_server_detach_http3(s);
1328 } else if (s->foreground_running) {
1329 (void)uv_async_send(&s->stop_async);
1330 } else {
1331 stop_async_cb(&s->stop_async);
1332 while (uv_run(&s->loop, UV_RUN_NOWAIT) != 0)
1333 ;
1334 http_server_detach_http3(s);
1335 }
1336}

References http_server::foreground_running, http_server::loop, http_server::running, http_server::stop_async, http_server::thread, and http_server::thread_started.

Referenced by api_server_main(), http_server_destroy(), and mcp_http_run().

◆ http_server_wakeup()

asciichat_error_t http_server_wakeup ( http_server_t * server)

Wake the private event loop from a worker thread.

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

1278 {
1279 if (!s || !s->running || uv_is_closing((uv_handle_t *)&s->wake_async))
1280 return SET_ERRNO(ERROR_INVALID_STATE, "HTTP server is not running");
1281 return uv_async_send(&s->wake_async) == 0 ? ASCIICHAT_OK : SET_ERRNO(ERROR_NETWORK, "Cannot wake HTTP event loop");
1282}

References ASCIICHAT_OK, ERROR_INVALID_STATE, ERROR_NETWORK, http_server::running, SET_ERRNO, and http_server::wake_async.

Referenced by mcp_http_wake().