cMCP
0.4.1
Model Context Protocol library in pure C11
Loading...
Searching...
No Matches
include
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
19
typedef
struct
{
20
char
*
name
;
/* trimmed, owned */
21
char
*
value
;
/* trimmed, owned */
22
}
cmcp_http_header_t
;
23
24
typedef
struct
{
25
char
*
method
;
/* "POST", "GET", ... — owned */
26
char
*
target
;
/* "/mcp", "/mcp?foo=bar", ... — owned */
27
cmcp_http_header_t
headers[
CMCP_HTTP_MAX_HEADERS
];
28
size_t
n_headers
;
29
char
*
body
;
/* NULL when Content-Length absent / 0 */
30
size_t
body_len
;
31
}
cmcp_http_request_t
;
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. */
35
void
cmcp_http_request_clear
(
cmcp_http_request_t
*r);
36
37
/* Case-insensitive header lookup. Returns NULL if absent. */
38
const
char
*
cmcp_http_header_get
(
const
cmcp_http_request_t
*r,
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). */
56
int
cmcp_http_parse_head
(
char
*block,
size_t
body_offset,
57
cmcp_http_request_t
*out);
58
59
#endif
cmcp_http_parse_head
int cmcp_http_parse_head(char *block, size_t body_offset, cmcp_http_request_t *out)
cmcp_http_request_clear
void cmcp_http_request_clear(cmcp_http_request_t *r)
CMCP_HTTP_MAX_HEADERS
#define CMCP_HTTP_MAX_HEADERS
Definition
cmcp_http_parser.h:17
cmcp_http_header_get
const char * cmcp_http_header_get(const cmcp_http_request_t *r, const char *name)
cmcp_http_header_t
Definition
cmcp_http_parser.h:19
cmcp_http_header_t::name
char * name
Definition
cmcp_http_parser.h:20
cmcp_http_header_t::value
char * value
Definition
cmcp_http_parser.h:21
cmcp_http_request_t
Definition
cmcp_http_parser.h:24
cmcp_http_request_t::body
char * body
Definition
cmcp_http_parser.h:29
cmcp_http_request_t::n_headers
size_t n_headers
Definition
cmcp_http_parser.h:28
cmcp_http_request_t::target
char * target
Definition
cmcp_http_parser.h:26
cmcp_http_request_t::method
char * method
Definition
cmcp_http_parser.h:25
cmcp_http_request_t::body_len
size_t body_len
Definition
cmcp_http_parser.h:30
Generated by
1.9.8