cMCP 0.4.1
Model Context Protocol library in pure C11
Loading...
Searching...
No Matches
cmcp_http_parser.h
Go to the documentation of this file.
1/**
2 * @file cmcp_http_parser.h
3 * @brief Minimal HTTP/1.1 request-line + headers parser.
4 *
5 * Extracted from `src/transport_http.c` into a standalone translation
6 * unit so the libFuzzer harness (`fuzz/fuzz_http.c`) can drive it
7 * directly without sockets. The parser deliberately does *not*
8 * understand transfer-encoding chunks or HTTP/2 — those belong to
9 * the transport layer's body-reading logic. Bounded buffers throughout;
10 * caps documented in `src/transport_http.c`.
11 */
12#ifndef CMCP_HTTP_PARSER_H
13#define CMCP_HTTP_PARSER_H
14
15#include <stddef.h>
16
17#define CMCP_HTTP_MAX_HEADERS 64
18
19typedef struct {
20 char *name; /* trimmed, owned */
21 char *value; /* trimmed, owned */
23
24typedef struct {
25 char *method; /* "POST", "GET", ... — owned */
26 char *target; /* "/mcp", "/mcp?foo=bar", ... — owned */
28 size_t n_headers;
29 char *body; /* NULL when Content-Length absent / 0 */
30 size_t body_len;
32
33/* Free all owned strings in `r` and zero the struct. Safe on a zeroed
34 * struct and on a struct populated only partway through parsing. */
36
37/* Case-insensitive header lookup. Returns NULL if absent. */
39 const char *name);
40
41/* Parse a request line + headers section in-place.
42 *
43 * block caller-owned writable buffer holding the head bytes.
44 * MUST remain valid until the call returns. The parser
45 * writes NULs into it to terminate parsed substrings.
46 * body_offset byte offset of the start of the body (i.e. one past
47 * the terminating "\r\n\r\n" or "\n\n"). Used to bound
48 * the head's working range.
49 * out OUT. On success, populated with owned strings.
50 * On error, partially populated — caller must always
51 * call cmcp_http_request_clear() to release whatever
52 * was set.
53 *
54 * Returns 0 on success or a negative cmcp_err_t (CMCP_EPROTOCOL on
55 * malformed input, CMCP_ENOMEM on allocation failure). */
56int cmcp_http_parse_head(char *block, size_t body_offset,
58
59#endif
int cmcp_http_parse_head(char *block, size_t body_offset, cmcp_http_request_t *out)
void cmcp_http_request_clear(cmcp_http_request_t *r)
#define CMCP_HTTP_MAX_HEADERS
const char * cmcp_http_header_get(const cmcp_http_request_t *r, const char *name)