Skip to content

Commit c0cbc1e

Browse files
edsipercosmo0920
authored andcommitted
lib: monkey: upgrade to v1.8.8
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent 3444cf0 commit c0cbc1e

11 files changed

Lines changed: 286 additions & 16 deletions

File tree

lib/monkey/AGENTS.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# AGENTS
2+
3+
This file is the local operating guide for agents working in this repository.
4+
It focuses on two things:
5+
6+
- how the Monkey source tree is organized
7+
- how commits are written in the existing Git history
8+
9+
## Repository map
10+
11+
Monkey is a small HTTP server written in C. The tree is split by subsystem.
12+
13+
- `mk_core/`
14+
Core utilities used by the server and plugins.
15+
Includes memory helpers, strings, files, thread helpers, event loops,
16+
I/O vectors, config parsing, and generic utilities.
17+
18+
- `mk_server/`
19+
The HTTP server implementation.
20+
This is where connection handling, request parsing, header generation,
21+
virtual hosts, MIME resolution, scheduler integration, streams, plugins,
22+
and server lifecycle live.
23+
24+
- `mk_bin/`
25+
The standalone `monkey` executable entrypoints and signal handling.
26+
27+
- `include/monkey/`
28+
Public and internal headers for the core, server, parser, plugins,
29+
config, events, streams, and API types.
30+
31+
- `plugins/`
32+
Optional server features.
33+
Examples in this tree include `liana`, `mandril`, `dirlisting`, `cgi`,
34+
`fastcgi`, `auth`, `logger`, `tls`, and `cheetah`.
35+
36+
- `api/`
37+
Small API-focused programs and tests.
38+
39+
- `test/`
40+
Native unit/integration-style test targets used by CMake.
41+
42+
- `fuzz/`
43+
Fuzzing entrypoints and helpers for parser and request handling.
44+
45+
- `conf/`
46+
Config templates installed or copied at build/install time.
47+
48+
- `htdocs/`
49+
Default static site content used by the standalone server.
50+
51+
- `cmake/`
52+
CMake helper modules and build logic.
53+
54+
- `deps/`
55+
Bundled third-party dependencies such as regex, rbtree, libco,
56+
and mbedtls sources.
57+
58+
- `qa/`
59+
Extra request fixtures and local QA artifacts.
60+
61+
## Main runtime flow
62+
63+
When debugging behavior, the usual path is:
64+
65+
1. `mk_bin/monkey.c`
66+
Starts the binary.
67+
2. `mk_server/monkey.c`, `mk_server/mk_server.c`
68+
Initializes the server and worker threads.
69+
3. `mk_server/mk_scheduler.c`
70+
Drives socket events into protocol handlers.
71+
4. `mk_server/mk_http.c`
72+
Owns HTTP session lifecycle, request preparation, response handling,
73+
range parsing, file serving, keepalive, and teardown.
74+
5. `mk_server/mk_http_parser.c`
75+
Parses the request line, headers, body state, and chunked transfer coding.
76+
6. `mk_server/mk_header.c`, `mk_server/mk_stream.c`
77+
Build and send responses.
78+
79+
Useful supporting code:
80+
81+
- `mk_server/mk_vhost.c`
82+
Virtual host lookup, per-vhost file descriptor table.
83+
- `mk_server/mk_mimetype.c`
84+
File extension to MIME mapping.
85+
- `mk_server/mk_user.c`
86+
`~user` URI handling.
87+
- `mk_server/mk_plugin.c`
88+
Plugin registration and API exposure.
89+
- `mk_core/mk_event_*.c`
90+
Backend-specific event loop implementations.
91+
92+
## Build and verification
93+
94+
Typical local build entrypoint:
95+
96+
```bash
97+
cmake --build build
98+
```
99+
100+
If a fresh build tree is needed, inspect `CMakeLists.txt` and the generated
101+
`build/` layout before changing build flags. The project currently requires
102+
CMake 3.20 and produces `build/bin/monkey`.
103+
104+
## Commit style used in this repository
105+
106+
Follow the existing Git history, not the older wording in `CONTRIBUTING.md`.
107+
108+
### Subject format
109+
110+
Use a short, lowercase, scope-prefixed subject. The common patterns are:
111+
112+
- `build: bump to v1.8.7`
113+
- `server: clean thread destroy on worker loop exit`
114+
- `server: http: move initialization of request headers to request init`
115+
- `core: event: Plug descriptor leaks in an error case.`
116+
- `parser: fixed header loss issue caused by duplicated headers`
117+
- `logger: set log file permissions to 0600, closes CVE-2013-1771 (#413)`
118+
119+
### Prefix rules
120+
121+
Pick the narrowest stable prefix that matches the area being changed.
122+
123+
- `build:` for version bumps, CMake, workflows, packaging
124+
- `core:` for `mk_core/` functionality
125+
- `server:` for `mk_server/` functionality
126+
- `server: http:` for `mk_server/mk_http.c` and closely related request flow
127+
- `server: parser:` or `server: http_parser:` for parser-specific work
128+
- `plugin:` or a plugin-specific prefix when the change is isolated there
129+
- `test:` for tests
130+
- `logger:`, `scheduler:`, `mimetype:`, `config:` when the change is clearly
131+
isolated to that subsystem and history already uses that style
132+
- backend-specific prefixes like `mk_event_kqueue:` are acceptable when the
133+
change is narrow and entirely local to that backend
134+
135+
### Subject style rules
136+
137+
- keep it concise
138+
- prefer lowercase after the prefix
139+
- use colon-separated scopes, not bracket tags
140+
- do not invent long marketing titles
141+
- keep the subject under 80 characters
142+
- match existing nouns already used in history where possible
143+
144+
Good examples for this tree:
145+
146+
- `server: http: reject malformed range delimiters`
147+
- `server: http: avoid reusing invalid request state`
148+
- `server: parser: validate chunk length tokens strictly`
149+
- `core: memory: handle null mk_ptr_to_buf input`
150+
151+
Bad examples for this tree:
152+
153+
- `Fix CVEs`
154+
- `Monkey: important security fixes`
155+
- `HTTP: Add Various Improvements`
156+
- `misc: cleanup`
157+
158+
## Commit body rules
159+
160+
The older contribution guide still applies well here.
161+
162+
- include a body for non-trivial changes
163+
- wrap body lines at about 80 columns
164+
- explain the bug, the fix, and any verification done
165+
- if the change is security-related, describe the faulty path precisely
166+
- if multiple root causes exist, prefer separate commits
167+
168+
When I am asked to commit in this repository, default behavior should be:
169+
170+
1. split unrelated changes into separate commits
171+
2. choose the narrowest prefix from the existing history
172+
3. write a short lowercase subject
173+
4. add a body for anything beyond trivial cleanup
174+
5. use `git commit -s` unless the user explicitly asks otherwise
175+
176+
## Working rules for this repository
177+
178+
- Do not touch unrelated untracked files in the worktree.
179+
- Be careful around parser and request lifecycle code. Many bugs surface later
180+
in teardown, not at the first invalid input.
181+
- Prefer minimal targeted fixes over broad refactors unless requested.
182+
- When a bug crosses files, still group the commit by root cause, not by file.
183+
- For security fixes, verify behavior on the built binary, not only by code
184+
inspection.

lib/monkey/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ endif()
2323
# Monkey Version
2424
set(MK_VERSION_MAJOR 1)
2525
set(MK_VERSION_MINOR 8)
26-
set(MK_VERSION_PATCH 6)
26+
set(MK_VERSION_PATCH 8)
2727
set(MK_VERSION_STR "${MK_VERSION_MAJOR}.${MK_VERSION_MINOR}.${MK_VERSION_PATCH}")
2828
2929
# Output paths

lib/monkey/CONTRIBUTING.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ You have to pay attention to the code indentation, tabs are 4 spaces, spaces on
2121

2222
When you commit your local changes in your repository (before to push to Github), we need you take care of the following:
2323

24-
- Your principal commit message (one line subject) must be prefixed with the core section name, e.g: If you are adding a new but missing protocol feature it could be __HTTP: add new XYZ method__.
24+
- Your principal commit message (one line subject) must be prefixed with the affected area name. Follow the style used in the existing history, e.g: `build: ...`, `core: ...`, `server: ...`, `server: http: ...`, `server: parser: ...`.
2525
- The Subject of the commit must not be longer than 80 characters.
26+
- Keep the subject short and prefer lowercase wording after the prefix.
27+
- Use the narrowest practical scope prefix for the change.
2628
- On the commit body, each line should not be longer than 80 characters.
2729
- On most of cases we want full description about what your patch is doing, the patch description should be self descriptive.. like for dummies. Do not assume everybody knows what you are doing and on each like do not exceed 80 characters.
2830
- When running the __git commit__ command, make sure you are using the __-s__ flag, that will add a Signed-off comment in the patch description.
@@ -31,19 +33,27 @@ Expanding a bit the example feature message we could use the following command:
3133

3234
> $ git commit -a -s
3335
>
34-
> HTTP: add new XYZ method
36+
> server: http: add new xyz method
3537
>
36-
> This patch adds the missing XYZ method described in RCF2616 in the
38+
> This patch adds the missing XYZ method described in RFC2616 in the
3739
> section 12.4.x.a, it do not alter the core behavior but if the new
3840
> method is requested it will take care of the proper handling.
3941
>
40-
> the patch have been tested using tools A & B.
42+
> The patch has been tested using tools A & B.
4143
>
4244
> Signed-off-by: Your Name <your@email.com>
4345
46+
Some recent examples from this repository are:
47+
48+
- `server: clean thread destroy on worker loop exit`
49+
- `server: http: move initialization of request headers to request init`
50+
- `server: parser: remove unnecessary index updater`
51+
- `build: bump to v1.8.7`
52+
- `core: event: Plug descriptor leaks in an error case.`
53+
4454
If you want to see a real example, run the following command:
4555

46-
> $ git log 4efbc11bafeb56fbe2b4f0f6925671630ce84125
56+
> $ git log --oneline --no-merges -20
4757
4858
Your path/patches should be fully documented, that will make the review process faster for us, and a faster merge for you.
4959

lib/monkey/include/monkey/mk_http_parser.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,11 @@ int mk_http_parser_chunked_decode_buf(struct mk_http_parser *p,
389389

390390
static inline int mk_http_parser_more(struct mk_http_parser *p, int len)
391391
{
392-
if (abs(len - p->i) - 1 > 0) {
392+
if (len <= 0 || p->i < 0) {
393+
return MK_FALSE;
394+
}
395+
396+
if ((p->i + 1) < len) {
393397
return MK_TRUE;
394398
}
395399

lib/monkey/mk_core/mk_memory.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ char *mk_ptr_to_buf(mk_ptr_t p)
5252
{
5353
char *buf;
5454

55+
if (!p.data || p.len == 0) {
56+
buf = mk_mem_alloc(1);
57+
if (!buf) {
58+
return NULL;
59+
}
60+
61+
buf[0] = '\0';
62+
return buf;
63+
}
64+
5565
buf = mk_mem_alloc(p.len + 1);
5666
if (!buf) return NULL;
5767

lib/monkey/mk_server/mk_http.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ static int mk_http_range_parse(struct mk_http_request *sr)
457457
if ((sep_pos = mk_string_char_search(sr->range.data, '-', sr->range.len)) < 0)
458458
return -1;
459459

460+
if (sep_pos < eq_pos) {
461+
return -1;
462+
}
463+
460464
len = sr->range.len;
461465
sh = &sr->headers;
462466

@@ -476,10 +480,16 @@ static int mk_http_range_parse(struct mk_http_request *sr)
476480
/* =yyy-xxx */
477481
if ((eq_pos + 1 != sep_pos) && (len > sep_pos + 1)) {
478482
buffer = mk_string_copy_substr(sr->range.data, eq_pos + 1, sep_pos);
483+
if (!buffer) {
484+
return -1;
485+
}
479486
sh->ranges[0] = (unsigned long) atol(buffer);
480487
mk_mem_free(buffer);
481488

482489
buffer = mk_string_copy_substr(sr->range.data, sep_pos + 1, len);
490+
if (!buffer) {
491+
return -1;
492+
}
483493
sh->ranges[1] = (unsigned long) atol(buffer);
484494
mk_mem_free(buffer);
485495

@@ -493,6 +503,9 @@ static int mk_http_range_parse(struct mk_http_request *sr)
493503
/* =yyy- */
494504
if ((eq_pos + 1 != sep_pos) && (len == sep_pos + 1)) {
495505
buffer = mk_string_copy_substr(sr->range.data, eq_pos + 1, len);
506+
if (!buffer) {
507+
return -1;
508+
}
496509
sr->headers.ranges[0] = (unsigned long) atol(buffer);
497510
mk_mem_free(buffer);
498511

@@ -522,7 +535,16 @@ static int mk_http_directory_redirect_check(struct mk_http_session *cs,
522535
return 0;
523536
}
524537

538+
if (!sr->host.data || sr->host.len <= 0) {
539+
mk_http_error(MK_CLIENT_BAD_REQUEST, cs, sr, server);
540+
return -1;
541+
}
542+
525543
host = mk_ptr_to_buf(sr->host);
544+
if (!host) {
545+
mk_http_error(MK_CLIENT_BAD_REQUEST, cs, sr, server);
546+
return -1;
547+
}
526548

527549
/*
528550
* Add ending slash to the location string
@@ -588,6 +610,9 @@ static inline char *mk_http_index_lookup(mk_ptr_t *path_base,
588610
}
589611

590612
off = path_base->len;
613+
if ((size_t) off >= buf_size) {
614+
return NULL;
615+
}
591616
memcpy(buf, path_base->data, off);
592617

593618
mk_list_foreach(head, server->index_files) {
@@ -1138,15 +1163,27 @@ int mk_http_request_end(struct mk_http_session *cs, struct mk_server *server)
11381163
ret = mk_http_parser_more(&cs->parser, cs->body_length);
11391164
if (ret == MK_TRUE) {
11401165
/* Our pipeline request limit is the same that our keepalive limit */
1166+
if (cs->parser.i < 0 ||
1167+
(unsigned int) (cs->parser.i + 1) >= cs->body_length) {
1168+
goto shutdown;
1169+
}
1170+
11411171
cs->counter_connections++;
11421172
len = (cs->body_length - cs->parser.i) -1;
1173+
if (len <= 0) {
1174+
goto shutdown;
1175+
}
11431176
memmove(cs->body,
11441177
cs->body + cs->parser.i + 1,
11451178
len);
11461179
cs->body_length = len;
11471180

11481181
/* Prepare for next one */
1149-
sr = mk_list_entry_first(&cs->request_list, struct mk_http_request, _head);
1182+
if (mk_list_is_empty(&cs->request_list) == 0) {
1183+
cs->close_now = MK_TRUE;
1184+
goto shutdown;
1185+
}
1186+
sr = &cs->sr_fixed;
11501187
mk_http_request_free(sr, server);
11511188
mk_http_request_init(cs, sr, server);
11521189
mk_http_parser_init(&cs->parser);
@@ -1626,9 +1663,10 @@ int mk_http_sched_done(struct mk_sched_conn *conn,
16261663
struct mk_http_request *sr;
16271664

16281665
session = mk_http_session_get(conn);
1629-
sr = mk_list_entry_first(&session->request_list,
1630-
struct mk_http_request, _head);
1631-
mk_plugin_stage_run_40(session, sr, server);
1666+
if (mk_list_is_empty(&session->request_list) != 0) {
1667+
sr = &session->sr_fixed;
1668+
mk_plugin_stage_run_40(session, sr, server);
1669+
}
16321670

16331671
return mk_http_request_end(session, server);
16341672
}

0 commit comments

Comments
 (0)