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

Function cbm_http_parse_head

src/ui/httpd.c:462–604  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

460}
461
462int cbm_http_parse_head(const char *data, size_t len, cbm_http_req_t *req, size_t *body_offset,
463 size_t *content_length) {
464 memset(req, 0, sizeof(*req));
465 *body_offset = 0;
466 *content_length = 0;
467
468 /* Scan for the CRLFCRLF terminator. While scanning, enforce the strict
469 * byte rules: no raw NUL, no LF that is not preceded by CR. */
470 size_t scan = len < CBM_HTTP_MAX_HEAD ? len : CBM_HTTP_MAX_HEAD;
471 size_t head_end = 0; /* offset just past CRLFCRLF */
472 for (size_t i = 0; i < scan; i++) {
473 char ch = data[i];
474 if (ch == '\0')
475 return 400;
476 if (ch == '\n' && (i == 0 || data[i - 1] != '\r'))
477 return 400;
478 if (ch == '\n' && i >= 3 && data[i - 3] == '\r' && data[i - 2] == '\n') {
479 head_end = i + 1;
480 break;
481 }
482 }
483 if (head_end == 0)
484 return len >= CBM_HTTP_MAX_HEAD ? 431 : CBM_HTTP_NEED_MORE;
485
486 /* ── Request line: METHOD SP request-target SP HTTP/1.x ── */
487 const char *line = data;
488 const char *line_end = memchr(line, '\r', head_end);
489 if (!line_end)
490 return 400;
491
492 const char *sp1 = memchr(line, ' ', (size_t)(line_end - line));
493 if (!sp1)
494 return 400;
495 size_t mlen = (size_t)(sp1 - line);
496 if (mlen == 0 || mlen >= sizeof(req->method))
497 return 400;
498 for (size_t i = 0; i < mlen; i++) {
499 if (!isupper((unsigned char)line[i]))
500 return 400;
501 }
502 memcpy(req->method, line, mlen);
503 req->method[mlen] = '\0';
504
505 const char *target = sp1 + 1;
506 const char *sp2 = memchr(target, ' ', (size_t)(line_end - target));
507 if (!sp2)
508 return 400;
509 const char *version = sp2 + 1;
510 size_t vlen = (size_t)(line_end - version);
511 /* Exactly HTTP/1.0 or HTTP/1.1 */
512 if (vlen != 8 || memcmp(version, "HTTP/1.", 7) != 0 || (version[7] != '0' && version[7] != '1'))
513 return 400;
514 req->http_minor = (unsigned char)(version[7] - '0');
515
516 size_t tlen = (size_t)(sp2 - target);
517 if (tlen == 0 || target[0] != '/')
518 return 400;
519 /* Reject percent-encoded NUL anywhere in the request target. */

Callers 2

cbm_httpd_read_requestFunction · 0.85
test_httpd.cFile · 0.85

Calls 2

header_name_isFunction · 0.85
copy_header_valueFunction · 0.85

Tested by

no test coverage detected