MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_httpd_read_request

Function cbm_httpd_read_request

src/ui/httpd.c:608–698  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

606/* ── Request reading ──────────────────────────────────────────── */
607
608int cbm_httpd_read_request(cbm_http_conn_t *c, cbm_http_req_t *req) {
609 if (!c || !req)
610 return -1;
611
612 char *head = malloc(CBM_HTTP_MAX_HEAD);
613 if (!head)
614 return -1;
615
616 int64_t deadline = now_ms() + c->recv_deadline_ms;
617 size_t have = 0;
618 size_t body_off = 0, clen = 0;
619 int rc = CBM_HTTP_NEED_MORE;
620
621 /* Read until the head parses (or fails to). */
622 for (;;) {
623 int w = wait_connection_ready(c, false, deadline);
624 if (w == 0) {
625 free(head);
626 return 408;
627 }
628 if (w < 0) {
629 free(head);
630 return -1;
631 }
632#ifdef _WIN32
633 int n = recv(c->fd, head + have, (int)(CBM_HTTP_MAX_HEAD - have), 0);
634#else
635 ssize_t n = recv(c->fd, head + have, CBM_HTTP_MAX_HEAD - have, 0);
636#endif
637 if (n < 0 && socket_would_block())
638 continue;
639 if (n <= 0) {
640 free(head); /* peer vanished mid-request — nothing to answer */
641 return -1;
642 }
643 have += (size_t)n;
644
645 rc = cbm_http_parse_head(head, have, req, &body_off, &clen);
646 if (rc == 0)
647 break;
648 if (rc != CBM_HTTP_NEED_MORE) {
649 free(head);
650 return rc;
651 }
652 if (have >= CBM_HTTP_MAX_HEAD) {
653 free(head);
654 return 431;
655 }
656 }
657
658 /* Read the body per Content-Length (already capped by the parser). */
659 if (clen > 0) {
660 char *body = malloc(clen + 1);
661 if (!body) {
662 free(head);
663 return -1;
664 }
665 size_t got = have - body_off;

Callers 1

cbm_http_server_runFunction · 0.85

Calls 4

wait_connection_readyFunction · 0.85
socket_would_blockFunction · 0.85
cbm_http_parse_headFunction · 0.85
now_msFunction · 0.70

Tested by

no test coverage detected