ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
api-server/main.c
Go to the documentation of this file.
1#include "main.h"
3#include "windows_service.h"
7#include <ascii-chat/otel.h>
11#include <ascii-chat/version.h>
12#include <libplatform/filesystem.h>
13#include <libplatform/systemd.h>
14#include <options/options.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <uv.h>
19
26
36
37static void api_finish(http_response_t *response, unsigned status) {
38 (void)http_response_set_status(response, status);
39 (void)http_response_finish(response);
40}
41
42static unsigned api_render_status(asciichat_error_t error) {
43 if (error == ERROR_BUFFER_FULL)
44 return 503;
45 if (error == ERROR_BUFFER_OVERFLOW)
46 return 413;
47 if (error == ERROR_INVALID_PARAM || error == ERROR_MEDIA_INIT ||
48 error == ERROR_MEDIA_DECODE)
49 return 422;
50 return 500;
51}
52
53static const unsigned char *api_find_bytes(const unsigned char *data,
54 size_t length, const char *needle) {
55 size_t needle_length = strlen(needle);
56 if (!needle_length || needle_length > length)
57 return NULL;
58 for (size_t i = 0; i + needle_length <= length; ++i)
59 if (!memcmp(data + i, needle, needle_length))
60 return data + i;
61 return NULL;
62}
63
64static bool api_decode_query_component(const char *src, size_t length,
65 char *dst, size_t capacity) {
66 size_t out = 0;
67 for (size_t i = 0; i < length; ++i) {
68 unsigned char c = (unsigned char)src[i];
69 if (c == '%') {
70 if (i + 2 >= length)
71 return false;
72 int hi =
73 (src[i + 1] >= '0' && src[i + 1] <= '9') ? src[i + 1] - '0'
74 : (src[i + 1] >= 'a' && src[i + 1] <= 'f') ? src[i + 1] - 'a' + 10
75 : (src[i + 1] >= 'A' && src[i + 1] <= 'F') ? src[i + 1] - 'A' + 10
76 : -1;
77 int lo =
78 (src[i + 2] >= '0' && src[i + 2] <= '9') ? src[i + 2] - '0'
79 : (src[i + 2] >= 'a' && src[i + 2] <= 'f') ? src[i + 2] - 'a' + 10
80 : (src[i + 2] >= 'A' && src[i + 2] <= 'F') ? src[i + 2] - 'A' + 10
81 : -1;
82 if (hi < 0 || lo < 0)
83 return false;
84 c = (unsigned char)((hi << 4) | lo);
85 i += 2;
86 } else if (c == '+')
87 c = ' ';
88 if (!c || out + 1 >= capacity)
89 return false;
90 dst[out++] = (char)c;
91 }
92 dst[out] = 0;
93 return true;
94}
95
96static void api_write_result(http_response_t *response, const char *format,
97 const http_render_result_t *result) {
98 if (!strcmp(format, "files")) {
99 static const char boundary[] = "ascii-chat-frame";
101 response, "Content-Type", "multipart/mixed; boundary=ascii-chat-frame");
102 for (size_t i = 0; i < result->frame_count; ++i) {
103 const http_render_frame_t *frame = &result->frames[i];
104 if (!frame->text)
105 continue;
106 char header[256];
107 int length = safe_snprintf(
108 header, sizeof(header),
109 "--%s\r\nContent-Type: text/plain; charset=utf-8\r\n"
110 "Content-Disposition: attachment; filename=frame-%zu.txt\r\n\r\n",
111 boundary, i);
112 (void)http_response_write(response, header, (size_t)length);
113 (void)http_response_write(response, frame->text, frame->text_length);
114 (void)http_response_write(response, "\r\n", 2);
115 }
116 (void)http_response_write(response, "--ascii-chat-frame--\r\n", 22);
117 } else if (!strcmp(format, "json")) {
118 size_t cap = 64;
119 for (size_t frame_index = 0; frame_index < result->frame_count;
120 ++frame_index) {
121 const http_render_frame_t *frame = &result->frames[frame_index];
122 // Every byte may expand to a six-byte JSON escape (for example,
123 // "\u001f").
124 if (cap > SIZE_MAX - 96 ||
125 frame->text_length > (SIZE_MAX - cap - 96) / 6) {
126 (void)http_response_set_status(response, 500);
127 return;
128 }
129 cap += frame->text_length * 6 + 96;
130 }
131 char *json = SAFE_MALLOC(cap, char *);
132 int written = safe_snprintf(json, cap, "{\"frames\":[");
133 if (written < 0) {
134 SAFE_FREE(json);
135 (void)http_response_set_status(response, 500);
136 return;
137 }
138 size_t out = (size_t)written;
139 for (size_t frame_index = 0; frame_index < result->frame_count;
140 ++frame_index) {
141 const http_render_frame_t *frame = &result->frames[frame_index];
142 if (frame_index) {
143 written = safe_snprintf(json + out, cap - out, ",");
144 if (written < 0) {
145 SAFE_FREE(json);
146 (void)http_response_set_status(response, 500);
147 return;
148 }
149 out += (size_t)written;
150 }
151 written = safe_snprintf(json + out, cap - out,
152 "{\"timestamp_ms\":%llu,\"text\":\"",
153 (unsigned long long)frame->timestamp_ms);
154 if (written < 0) {
155 SAFE_FREE(json);
156 (void)http_response_set_status(response, 500);
157 return;
158 }
159 out += (size_t)written;
160 for (size_t i = 0; i < frame->text_length && out + 8 < cap; ++i) {
161 unsigned char c = (unsigned char)frame->text[i];
162 if (c == '"' || c == '\\') {
163 json[out++] = '\\';
164 json[out++] = (char)c;
165 } else if (c == '\n') {
166 json[out++] = '\\';
167 json[out++] = 'n';
168 } else if (c < 0x20) {
169 written = safe_snprintf(json + out, cap - out, "\\u%04x", c);
170 if (written < 0) {
171 SAFE_FREE(json);
172 (void)http_response_set_status(response, 500);
173 return;
174 }
175 out += (size_t)written;
176 } else
177 json[out++] = (char)c;
178 }
179 written = safe_snprintf(json + out, cap - out, "\"}");
180 if (written < 0) {
181 SAFE_FREE(json);
182 (void)http_response_set_status(response, 500);
183 return;
184 }
185 out += (size_t)written;
186 }
187 written = safe_snprintf(json + out, cap - out, "]}");
188 if (written < 0) {
189 SAFE_FREE(json);
190 (void)http_response_set_status(response, 500);
191 return;
192 }
193 out += (size_t)written;
194 (void)http_response_set_header(response, "Content-Type",
195 "application/json");
196 (void)http_response_write(response, json, out);
197 SAFE_FREE(json);
198 } else if (!strcmp(format, "html")) {
199 (void)http_response_set_header(response, "Content-Type",
200 "text/html; charset=utf-8");
201 (void)http_response_write(response, "<pre>", 5);
202 for (size_t i = 0; i < result->ascii_length; ++i) {
203 const char *escaped = result->ascii[i] == '&' ? "&amp;"
204 : result->ascii[i] == '<' ? "&lt;"
205 : result->ascii[i] == '>' ? "&gt;"
206 : NULL;
207 (void)http_response_write(response, escaped ? escaped : &result->ascii[i],
208 escaped ? strlen(escaped) : 1);
209 }
210 (void)http_response_write(response, "</pre>", 6);
211 } else {
212 (void)http_response_set_header(response, "Content-Type",
213 "text/plain; charset=utf-8");
214 (void)http_response_write(response, result->ascii, result->ascii_length);
215 }
216}
217
218static void api_render_complete(http_renderer_job_t *job,
219 const http_render_result_t *result,
220 void *user_data) {
221 (void)result;
222 api_server_t *server = user_data;
223 api_render_job_t *pending = job->user_data;
224 if (pending) {
225 atomic_store_bool(&pending->completed, true);
226 }
227 if (server)
228 (void)http_server_wakeup(server->http);
229}
230
231static void api_render_error(http_renderer_job_t *job, asciichat_error_t error,
232 void *user_data) {
233 (void)error;
234 api_render_complete(job, &job->result, user_data);
235}
236
237static void api_wakeup(void *user_data) {
238 api_server_t *server = user_data;
239 api_render_job_t **link = &server->jobs;
240 while (*link) {
241 api_render_job_t *pending = *link;
242 if (pending->renderer_job && pending->response &&
244 (void)http_renderer_cancel(pending->renderer_job);
246 }
247 if (!pending->renderer_job || !atomic_load_bool(&pending->completed) ||
249 link = &pending->next;
250 continue;
251 }
252 *link = pending->next;
253 if (pending->response) {
254 if (pending->renderer_job->result.error == ASCIICHAT_OK) {
255 api_write_result(pending->response, pending->format,
256 &pending->renderer_job->result);
257 api_finish(pending->response, 200);
258 } else {
259 log_warn("API render failed: %s",
260 asciichat_error_string(pending->renderer_job->result.error));
261 api_finish(pending->response,
262 api_render_status(pending->renderer_job->result.error));
263 }
264 }
265 pending->renderer_job->user_data = NULL;
267 platform_delete_temp_file(pending->path);
268 SAFE_FREE(pending);
269 }
270 /* Every renderer job is owned by a pending entry. Reap only after the
271 * completed entries have been unlinked and no callback can use them. */
272 http_renderer_reap(server->renderer, false);
273}
274
275static void api_discard_jobs(api_server_t *server) {
276 while (server->jobs) {
277 api_render_job_t *pending = server->jobs;
278 server->jobs = pending->next;
280 platform_delete_temp_file(pending->path);
281 SAFE_FREE(pending);
282 }
283}
284
285static void api_request(http_request_t *request, http_response_t *response,
286 void *user_data) {
287 api_server_t *server = user_data;
288 const http_request_info_t *info = http_request_info(request);
289 ASCII_OTEL_SCOPE(HTTP);
290 ASCII_OTEL_ATTR_STRING("service.name", "ascii-chat-api");
291 ASCII_OTEL_ATTR_STRING("http.route", info->path);
292 ASCII_OTEL_ATTR_STRING("http.request.method", info->method);
293 ASCII_OTEL_ATTR_INT("http.protocol", info->protocol);
294 if (!strcmp(info->method, "GET") && !strcmp(info->path, "/health")) {
295 static const char body[] =
296 "{\"status\":\"ok\",\"service\":\"ascii-chat-api\",\"version\":"
297 "\"" ASCII_CHAT_VERSION_FULL "\"}";
298 (void)http_response_set_header(response, "Content-Type",
299 "application/json");
300 (void)http_response_write(response, body, sizeof(body) - 1);
301 api_finish(response, 200);
302 return;
303 }
304 if (strcmp(info->path, "/health") && strcmp(info->path, "/convert")) {
305 api_finish(response, 404);
306 return;
307 }
308 if (!strcmp(info->path, "/health")) {
309 api_finish(response, 405);
310 return;
311 }
312 if (strcmp(info->method, "POST")) {
313 api_finish(response, 405);
314 return;
315 }
316 const char *content_type = NULL;
317 for (size_t i = 0; i < info->header_count; ++i)
318 if (!platform_strcasecmp(info->headers[i].name, "content-type"))
319 content_type = info->headers[i].value;
320 bool multipart =
321 content_type && !strncmp(content_type, "multipart/form-data;", 20);
322 if (!content_type ||
323 (!multipart && strncmp(content_type, "image/", 6) &&
324 strncmp(content_type, "application/octet-stream", 24))) {
325 api_finish(response, 415);
326 return;
327 }
328 size_t length = 0;
329 const unsigned char *body = http_request_body(request, &length);
330 if (!body || !length) {
331 api_finish(response, 400);
332 return;
333 }
334 if (multipart) {
335 const char *boundary = strstr(content_type, "boundary=");
336 if (!boundary || !boundary[9]) {
337 api_finish(response, 400);
338 return;
339 }
340 boundary += 9;
341 char boundary_value[256];
342 size_t boundary_len = strcspn(boundary, ";\r\n");
343 if (boundary_len >= sizeof(boundary_value)) {
344 api_finish(response, 400);
345 return;
346 }
347 memcpy(boundary_value, boundary, boundary_len);
348 boundary_value[boundary_len] = 0;
349 if (boundary_value[0] == '"') {
350 size_t n = strlen(boundary_value);
351 if (n < 2 || boundary_value[n - 1] != '"') {
352 api_finish(response, 400);
353 return;
354 }
355 memmove(boundary_value, boundary_value + 1, n - 2);
356 boundary_value[n - 2] = 0;
357 }
358 boundary = boundary_value;
359 char marker[256];
360 int marker_len = safe_snprintf(marker, sizeof(marker), "--%s", boundary);
361 if (marker_len <= 0 || (size_t)marker_len >= sizeof(marker)) {
362 api_finish(response, 400);
363 return;
364 }
365 const unsigned char *start = api_find_bytes(body, length, "\r\n\r\n");
366 if (!start || (size_t)(start - body) >= length) {
367 api_finish(response, 400);
368 return;
369 }
370 if (!api_find_bytes(body, (size_t)(start - body), "name=\"image\"")) {
371 api_finish(response, 400);
372 return;
373 }
374 start += 4;
375 const unsigned char *end =
376 api_find_bytes(start, length - (size_t)(start - body), marker);
377 if (!end || end <= start || (size_t)(end - body) > length) {
378 api_finish(response, 400);
379 return;
380 }
381 body = start;
382 length = (size_t)(end - start);
383 while (length && (body[length - 1] == '\r' || body[length - 1] == '\n'))
384 --length;
385 if (!length) {
386 api_finish(response, 400);
387 return;
388 }
389 }
390 http_render_request_t render_request;
391 http_render_request_init(&render_request);
392 render_request.max_output_bytes = 8u * 1024u * 1024u;
393 const char *query = info->query ? info->query : "";
394 char format[16] = "text";
395 char output[16] = "files";
396 for (const char *p = query; *p;) {
397 const char *eq = strchr(p, '=');
398 const char *amp = strchr(p, '&');
399 if (!eq || (amp && eq > amp)) {
400 api_finish(response, 400);
401 return;
402 }
403 size_t key_len = (size_t)(eq - p),
404 value_len = (size_t)((amp ? amp : p + strlen(p)) - eq - 1);
405 char key[32], value[256];
406 if (!key_len || key_len >= sizeof(key) || value_len >= sizeof(value)) {
407 api_finish(response, 400);
408 return;
409 }
410 if (!api_decode_query_component(p, key_len, key, sizeof(key)) ||
411 !api_decode_query_component(eq + 1, value_len, value, sizeof(value))) {
412 api_finish(response, 400);
413 return;
414 }
415 char *endptr = NULL;
416 if (!strcmp(key, "width") || !strcmp(key, "height")) {
417 long n = strtol(value, &endptr, 10);
418 if (!endptr || *endptr || n < 1 || n > 3840) {
419 api_finish(response, 422);
420 return;
421 }
422 if (!strcmp(key, "width"))
423 render_request.options.width = (int)n;
424 else
425 render_request.options.height = (int)n;
426 } else if (!strcmp(key, "palette")) {
427 SAFE_STRNCPY(render_request.options.palette, value,
428 sizeof(render_request.options.palette));
429 } else if (!strcmp(key, "format")) {
430 SAFE_STRNCPY(format, value, sizeof(format));
431 if (strcmp(format, "text") && strcmp(format, "json") &&
432 strcmp(format, "html")) {
433 api_finish(response, 422);
434 return;
435 }
436 } else if (!strcmp(key, "output")) {
437 SAFE_STRNCPY(output, value, sizeof(output));
438 if (strcmp(output, "text") && strcmp(output, "files")) {
439 api_finish(response, 422);
440 return;
441 }
442 } else if (!strcmp(key, "palette-chars")) {
443 SAFE_STRNCPY(render_request.options.palette_chars, value,
444 sizeof(render_request.options.palette_chars));
445 } else if (!strcmp(key, "color")) {
446 if (!strcmp(value, "none"))
447 render_request.options.color_mode = TERM_COLOR_NONE;
448 else if (!strcmp(value, "16"))
449 render_request.options.color_mode = TERM_COLOR_16;
450 else if (!strcmp(value, "256"))
451 render_request.options.color_mode = TERM_COLOR_256;
452 else if (!strcmp(value, "truecolor") || !strcmp(value, "24bit"))
453 render_request.options.color_mode = TERM_COLOR_TRUECOLOR;
454 else if (!strcmp(value, "auto"))
455 render_request.options.color_mode = TERM_COLOR_AUTO;
456 else {
457 api_finish(response, 422);
458 return;
459 }
460 } else if (!strcmp(key, "renderer")) {
461 if (strcmp(value, "auto") && strcmp(value, "block") &&
462 strcmp(value, "braille") && strcmp(value, "half-block")) {
463 api_finish(response, 422);
464 return;
465 }
466 SAFE_STRNCPY(render_request.options.render_theme, value,
467 sizeof(render_request.options.render_theme));
468 } else {
469 api_finish(response, 400);
470 return;
471 }
472 p = amp ? amp + 1 : p + strlen(p);
473 }
474 ASCII_OTEL_ATTR_STRING("http.render.format", format);
475 ASCII_OTEL_ATTR_INT("http.render.width", render_request.options.width);
476 ASCII_OTEL_ATTR_INT("http.render.height", render_request.options.height);
477 char path[PLATFORM_MAX_PATH_LENGTH];
478 int fd = -1;
479 if (platform_create_temp_file(path, sizeof(path), "ascii-api", &fd) != 0 ||
480 platform_write(fd, body, length) != (ssize_t)length) {
481 if (fd >= 0)
482 platform_close(fd);
483 api_finish(response, 500);
484 return;
485 }
486 platform_close(fd);
487 render_request.source.type = HTTP_RENDER_SOURCE_FILE;
488 SAFE_STRNCPY(render_request.source.path, path,
489 sizeof(render_request.source.path));
490 render_request.source.body_length = length;
491 render_request.output_type = !strcmp(output, "files")
494 render_request.max_output_bytes = 8u * 1024u * 1024u;
495 SAFE_STRNCPY(render_request.source.mime_type, content_type,
496 sizeof(render_request.source.mime_type));
497 api_render_job_t *pending =
498 SAFE_CALLOC(1, sizeof(*pending), api_render_job_t *);
499 if (!pending) {
500 platform_delete_temp_file(path);
501 api_finish(response, 500);
502 return;
503 }
504 pending->server = server;
505 SAFE_STRNCPY(pending->format, !strcmp(output, "files") ? "files" : format,
506 sizeof(pending->format));
507 SAFE_STRNCPY(pending->path, path, sizeof(pending->path));
508 atomic_store_bool(&pending->completed, false);
509 http_response_t *retained = NULL;
510 if (http_response_retain(response, &retained) != ASCIICHAT_OK) {
511 SAFE_FREE(pending);
512 platform_delete_temp_file(path);
513 api_finish(response, 500);
514 return;
515 }
516 pending->response = retained;
517 render_request.user_data = pending;
518 http_renderer_job_t *render_job = NULL;
520 server->renderer, &render_request, pending, &render_job);
521 if (err != ASCIICHAT_OK) {
523 SAFE_FREE(pending);
524 platform_delete_temp_file(path);
525 api_finish(response, api_render_status(err));
526 return;
527 }
528 pending->renderer_job = render_job;
529 pending->next = server->jobs;
530 server->jobs = pending;
531 if (atomic_load_bool(&pending->completed))
532 (void)http_server_wakeup(server->http);
533}
534
536 http_server_t *server = NULL;
537 api_server_t api = {0};
538 quic_endpoint_t *http3_endpoint = NULL;
539 quic_endpoint_t *http3_endpoint6 = NULL;
540 const char *tls_cert = GET_OPTION(tls_cert);
541 const char *tls_key = GET_OPTION(tls_key);
542 const char *tls_password = GET_OPTION(tls_key_password);
543 if (!tls_cert || !*tls_cert)
544 tls_cert = NULL;
545 if (!tls_key || !*tls_key)
546 tls_key = NULL;
547 if (!tls_password || !*tls_password)
548 tls_password = NULL;
549 http_server_config_t config = {.bind_address = GET_OPTION(address),
550 .bind_address6 = GET_OPTION(address6),
551 .port = (uint16_t)GET_OPTION(port),
552 .enable_http1 = true,
553 .enable_http2 = true,
554 .enable_http3 = false,
555 .tls_cert_path = tls_cert,
556 .tls_key_path = tls_key,
557 .tls_key_password = tls_password,
558 .max_connections =
559 (size_t)GET_OPTION(max_clients),
560 .max_request_body_bytes = 10u * 1024u * 1024u,
561 .max_response_body_bytes = 8u * 1024u * 1024u,
562 .on_request = api_request,
563 .on_wakeup = api_wakeup};
564 if (!config.max_connections)
565 config.max_connections = 1;
567 http_renderer_config_t renderer_config = {
568 .max_jobs = (unsigned)config.max_connections,
569 .max_input_bytes = config.max_request_body_bytes,
570 .max_output_bytes = config.max_response_body_bytes,
571 .timeout_ms = 10000,
572 .restricted = true,
573 .on_complete = api_render_complete,
574 .on_error = api_render_error,
575 .user_data = &api};
576 err = http_renderer_init(&api.renderer, &renderer_config);
577 if (err != ASCIICHAT_OK)
578 return err;
579 config.on_request = api_request;
580 config.user_data = &api;
581 err = http_server_init(&server, &config);
582 if (err != ASCIICHAT_OK) {
583 LOG_ERRNO_IF_SET("API HTTP server initialization failed");
585 return err;
586 }
587 api.http = server;
588 if (tls_cert && tls_key) {
589 struct sockaddr_storage bind_address;
590 socklen_t bind_length = 0;
592 (uint16_t)GET_OPTION(port),
593 &bind_address, &bind_length);
594 if (err == ASCIICHAT_OK) {
595 quic_config_t quic_config = {.alpn = "h3",
596 .certificate_file = tls_cert,
597 .private_key_file = tls_key,
598 .enable_http3 = true,
599 .http_server = server};
600 err =
601 quic_endpoint_open((const struct sockaddr *)&bind_address,
602 bind_length, true, &quic_config, &http3_endpoint);
603 if (err == ASCIICHAT_OK && *GET_OPTION(address6)) {
604 struct sockaddr_storage bind_address6;
605 socklen_t bind_length6 = 0;
607 (uint16_t)GET_OPTION(port),
608 &bind_address6, &bind_length6);
609 if (err == ASCIICHAT_OK)
610 err = quic_endpoint_open((const struct sockaddr *)&bind_address6,
611 bind_length6, true, &quic_config,
612 &http3_endpoint6);
613 }
614 }
615 if (err != ASCIICHAT_OK) {
616 LOG_ERRNO_IF_SET("API HTTP/3 listener initialization failed");
617 http_server_destroy(&server);
619 return err;
620 }
621 }
622 err = http_server_start(server);
623 if (err != ASCIICHAT_OK) {
624 LOG_ERRNO_IF_SET("API HTTP server start failed");
625 if (http3_endpoint) {
626 (void)quic_endpoint_stop(http3_endpoint);
627 (void)quic_endpoint_destroy(&http3_endpoint);
628 }
629 if (http3_endpoint6) {
630 (void)quic_endpoint_stop(http3_endpoint6);
631 (void)quic_endpoint_destroy(&http3_endpoint6);
632 }
633 http_server_destroy(&server);
635 return err;
636 }
637 platform_systemd_notify_status("API HTTP listener started");
638 platform_systemd_notify_ready();
641 uv_sleep(16);
642 http_server_stop(server);
643 if (http3_endpoint) {
644 (void)quic_endpoint_stop(http3_endpoint);
645 (void)quic_endpoint_destroy(&http3_endpoint);
646 }
647 if (http3_endpoint6) {
648 (void)quic_endpoint_stop(http3_endpoint6);
649 (void)quic_endpoint_destroy(&http3_endpoint6);
650 }
652 api_discard_jobs(&api);
653 http_server_destroy(&server);
654 return ASCIICHAT_OK;
655}
struct api_render_job api_render_job_t
int api_server_main(void)
Application-level callbacks for library code.
#define APP_CALLBACK_BOOL(callback_name)
#define LOG_ERRNO_IF_SET(message)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
asciichat_error_t quic_endpoint_open(const struct sockaddr *address, socklen_t length, bool listener, const quic_config_t *config, quic_endpoint_t **out)
Definition endpoint.c:128
asciichat_error_t quic_endpoint_stop(quic_endpoint_t *endpoint)
Definition endpoint.c:289
asciichat_error_t quic_endpoint_destroy(quic_endpoint_t **endpoint)
Definition endpoint.c:334
int platform_close(int fd)
Definition filesystem.c:91
unsigned short uint16_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_CALLOC(count, size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_BUFFER_FULL
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_MEDIA_DECODE
Definition error_codes.h:72
@ ERROR_MEDIA_INIT
Definition error_codes.h:70
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define GET_OPTION(field)
Definition options.h:1339
asciichat_error_t http_renderer_submit(http_renderer_t *renderer, const http_render_request_t *request, void *user_data, http_renderer_job_t **out_job)
void http_render_request_init(http_render_request_t *request)
void http_renderer_reap(http_renderer_t *renderer, bool all)
asciichat_error_t http_renderer_cancel(http_renderer_job_t *job)
asciichat_error_t http_renderer_init(http_renderer_t **out, const http_renderer_config_t *config)
bool http_renderer_job_is_done(const http_renderer_job_t *job)
void http_renderer_destroy(http_renderer_t **renderer)
struct http_renderer_job http_renderer_job_t
@ HTTP_RENDER_SOURCE_FILE
@ HTTP_RENDER_OUTPUT_FILES
@ HTTP_RENDER_OUTPUT_TEXT
struct http_renderer http_renderer_t
struct quic_endpoint quic_endpoint_t
Backend-neutral HTTP request/response contract.
struct http_request http_request_t
struct http_request_info http_request_info_t
struct http_response http_response_t
asciichat_error_t parse_numeric_socket_address(const char *host, uint16_t port, struct sockaddr_storage *address, socklen_t *length)
Definition ip.c:155
🌍 IP Address Parsing and Formatting Utilities
const void * http_request_body(const http_request_t *r, size_t *n)
asciichat_error_t http_server_init(http_server_t **out, const http_server_config_t *cfg)
asciichat_error_t http_response_set_status(http_response_t *r, unsigned s)
asciichat_error_t http_server_wakeup(http_server_t *s)
void http_server_stop(http_server_t *s)
void http_server_destroy(http_server_t **p)
asciichat_error_t http_response_set_header(http_response_t *r, const char *n, const char *v)
asciichat_error_t http_response_retain(http_response_t *r, http_response_t **retained)
bool http_response_is_cancelled(const http_response_t *r)
asciichat_error_t http_server_start(http_server_t *s)
asciichat_error_t http_response_write(http_response_t *r, const void *d, size_t n)
const http_request_info_t * http_request_info(const http_request_t *r)
void http_response_release(http_response_t **response)
asciichat_error_t http_response_finish(http_response_t *r)
bool should_exit(void)
Definition main.c:260
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
#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
static bool size_t gotel_query_t * out
Definition queries.h:10
int platform_strcasecmp(const char *s1, const char *s2)
🔤 String Manipulation and Shell Escaping Utilities
http_renderer_job_t * renderer_job
http_response_t * response
char path[PLATFORM_MAX_PATH_LENGTH]
api_render_job_t * next
api_server_t * server
http_server_t * http
http_renderer_t * renderer
api_render_job_t * jobs
Atomic value wrapper for integral/boolean types.
Definition atomic.h:80
char render_theme[OPTIONS_BUFF_SIZE]
char palette_chars[OPTIONS_BUFF_SIZE]
char palette[OPTIONS_BUFF_SIZE]
http_render_source_t source
http_render_output_type_t output_type
http_render_options_t options
http_render_frame_t * frames
asciichat_error_t error
char path[PLATFORM_MAX_PATH_LENGTH]
http_render_source_type_t type
http_render_result_t result
ssize_t platform_write(int fd, const void *buf, size_t count)
Definition system.c:78
⏱️ High-precision timing utilities using sokol_time.h and uthash
struct http_server http_server_t
Definition tls.h:8
void windows_service_ready(void)