Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathrequest-flow.html
More file actions
Latest commit
455 lines (420 loc) · 41.6 KB
/
Copy pathrequest-flow.html
File metadata and controls
455 lines (420 loc) · 41.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!doctype html>
<htmllang="en">
<head>
<metacharset="utf-8">
<metaname="viewport" content="width=device-width,initial-scale=1">
<metaname="color-scheme" content="light dark">
</head>
<body>
<title>libhttpserver v2 — request lifecycle & routing</title>
<style>
:root {
--bg:#eaeef3; --surface:#ffffff; --surface-2:#f5f7fa; --inset:#eef2f7;
--border:#d3dae4; --border-strong:#b9c3d1;
--text:#182031; --text-dim:#566072; --text-faint:#8b94a5;
--spine:#2f6fb3; --state:#a9742a; --behavior:#1c8478; --adapter:#6a52c2; --domain:#495a76;
--hook:#b5417f; --flag:#b4562a; --send:#1c8478;
--shadow:01px2pxrgba(20,30,50,.06),03px10pxrgba(20,30,50,.05);
--sans: system-ui, -apple-system,"Segoe UI", Roboto, Helvetica, Arial, sans-serif;
--mono: ui-monospace,"SF Mono","JetBrains Mono","Menlo","Consolas", monospace;
}
@media (prefers-color-scheme: dark) {
:root {
--bg:#0d1117; --surface:#161c25; --surface-2:#1a212c; --inset:#121821;
--border:#29323f; --border-strong:#3a4552;
--text:#d9dfe8; --text-dim:#98a3b4; --text-faint:#667081;
--spine:#63a9ec; --state:#d6a24d; --behavior:#3cb6a5; --adapter:#9c85ec; --domain:#8ba1c4;
--hook:#e173ac; --flag:#dd8a55; --send:#3cb6a5;
--shadow:01px2pxrgba(0,0,0,.3),04px14pxrgba(0,0,0,.28);
}
}
:root[data-theme="light"] {
--bg:#eaeef3; --surface:#ffffff; --surface-2:#f5f7fa; --inset:#eef2f7;
--border:#d3dae4; --border-strong:#b9c3d1;
--text:#182031; --text-dim:#566072; --text-faint:#8b94a5;
--spine:#2f6fb3; --state:#a9742a; --behavior:#1c8478; --adapter:#6a52c2; --domain:#495a76;
--hook:#b5417f; --flag:#b4562a; --send:#1c8478;
--shadow:01px2pxrgba(20,30,50,.06),03px10pxrgba(20,30,50,.05);
}
:root[data-theme="dark"] {
--bg:#0d1117; --surface:#161c25; --surface-2:#1a212c; --inset:#121821;
--border:#29323f; --border-strong:#3a4552;
--text:#d9dfe8; --text-dim:#98a3b4; --text-faint:#667081;
--spine:#63a9ec; --state:#d6a24d; --behavior:#3cb6a5; --adapter:#9c85ec; --domain:#8ba1c4;
--hook:#e173ac; --flag:#dd8a55; --send:#3cb6a5;
--shadow:01px2pxrgba(0,0,0,.3),04px14pxrgba(0,0,0,.28);
}
* { box-sizing: border-box; }
body { margin:0; background:var(--bg); color:var(--text); font-family:var(--sans);
line-height:1.5; -webkit-font-smoothing: antialiased; }
.wrap { max-width:1180px; margin:0 auto; padding:40px24px72px; }
/* header */
header.top { margin-bottom:30px; }
.kicker { font-family:var(--mono); font-size:11.5px; letter-spacing:.16em; text-transform: uppercase;
color:var(--adapter); font-weight:600; margin:0010px; }
h1 { font-size:clamp(26px,4vw,38px); line-height:1.1; margin:0012px; letter-spacing:-.02em;
text-wrap: balance; font-weight:680; }
h1 .mono { font-family:var(--mono); font-weight:600; }
.lede { max-width:70ch; color:var(--text-dim); font-size:15.5px; margin:0; }
.ledeb { color:var(--text); font-weight:600; }
.lede .mono { font-family:var(--mono); font-size:14px; }
/* at-a-glance strip */
.glance { display: flex; flex-wrap: wrap; align-items: stretch; gap:8px; margin-top:22px; }
.gcard { flex:11150px; background:var(--surface); border:1px solid var(--border); border-radius:9px;
padding:10px12px; box-shadow:var(--shadow); display: flex; flex-direction: column; gap:3px; min-width:0; }
.gcard .gk { font-family:var(--mono); font-size:10px; letter-spacing:.08em; text-transform: uppercase;
color:var(--text-faint); font-weight:600; }
.gcard .gv { font-family:var(--mono); font-size:13px; font-weight:600; color:var(--adapter); word-break: break-word; }
.gcard .gs { font-size:11px; color:var(--text-dim); }
.garrow { display: flex; align-items: center; color:var(--text-faint); font-family:var(--mono); font-size:18px; }
/* legend */
.legend { display: flex; flex-wrap: wrap; gap:9px20px; margin-top:20px; padding:13px16px;
background:var(--surface); border:1px solid var(--border); border-radius:10px; box-shadow:var(--shadow); }
.lg { display: inline-flex; align-items: center; gap:7px; font-size:12.5px; color:var(--text-dim); }
.lg .sw { width:11px; height:11px; border-radius:3px; flex: none; }
.lg .pill { width:20px; height:13px; border-radius:7px; flex: none; border:1px solid; }
.legend .sep { width:1px; align-self: stretch; background:var(--border); }
section { margin-top:42px; }
.sec-head { display: flex; align-items: baseline; gap:12px; margin:004px; }
.sec-headh2 { font-size:19px; margin:0; letter-spacing:-.01em; font-weight:640; }
.sec-num { font-family:var(--mono); font-size:12px; color:var(--text-faint); font-weight:600; }
.sec-sub { color:var(--text-dim); font-size:13.5px; margin:2px022px; max-width:78ch; }
.sec-subcode { font-family:var(--mono); font-size:12.5px; background:var(--inset);
padding:1px5px; border-radius:4px; color:var(--text); }
/* ---- lifecycle bands ---- */
.band { position: relative; border:1px solid var(--border); border-left:3px solid var(--accent,var(--adapter));
border-radius:11px; background:var(--surface); box-shadow:var(--shadow); padding:13px15px15px;
margin-bottom:8px; }
.band-head { display: flex; align-items: baseline; gap:10px; flex-wrap: wrap; margin-bottom:3px; }
.band-cb { font-family:var(--mono); font-size:13.5px; font-weight:600; color:var(--accent,var(--adapter)); }
.band-tag { font-family:var(--mono); font-size:9.5px; letter-spacing:.06em; text-transform: uppercase; font-weight:700;
padding:1.5px6px; border-radius:4px; color:var(--accent,var(--adapter));
background:color-mix(in srgb,var(--accent,var(--adapter)) 13%, transparent); }
.band-when { font-size:11.5px; color:var(--text-faint); font-family:var(--mono); }
.band-note { font-size:12px; color:var(--text-dim); margin:009px; max-width:88ch; }
.band-notecode { font-family:var(--mono); font-size:11px; background:var(--inset); padding:04px; border-radius:3px; color:var(--text); }
.steps { display: flex; flex-direction: column; gap:6px; }
.step { display: grid; grid-template-columns:20px1fr; gap:10px; align-items: start; }
.step .sn { font-family:var(--mono); font-size:11px; color:var(--text-faint); font-weight:600; text-align: right; padding-top:2px;
font-variant-numeric: tabular-nums; }
.step .sbody { display: flex; flex-direction: column; gap:4px; min-width:0; }
.step .sline { font-size:12.5px; color:var(--text); display: flex; flex-wrap: wrap; gap:5px7px; align-items: baseline; }
.actor { font-family:var(--mono); font-size:11.5px; font-weight:600; padding:1px6px; border-radius:5px; white-space: nowrap;
color:var(--role,var(--domain)); background:color-mix(in srgb,var(--role,var(--domain)) 12%, transparent);
border:1px solid color-mix(in srgb,var(--role,var(--domain)) 26%, transparent); }
.a-beh { --role:var(--behavior); } .a-state { --role:var(--state); } .a-mhd { --role:var(--adapter); }
.a-dom { --role:var(--domain); } .a-spine { --role:var(--spine); }
.step .txt { color:var(--text-dim); font-size:12.5px; }
.step .txtcode, .slinecode { font-family:var(--mono); font-size:11px; background:var(--inset); padding:04px; border-radius:3px; color:var(--text); }
.step .txtb { color:var(--text); font-weight:600; }
/* hook pill */
.hook { font-family:var(--mono); font-size:10.5px; font-weight:600; padding:1.5px8px1.5px7px; border-radius:10px;
color:var(--hook); background:color-mix(in srgb,var(--hook) 12%, transparent);
border:1px solid color-mix(in srgb,var(--hook) 34%, transparent); white-space: nowrap; display: inline-flex; gap:5px; align-items: center; }
.hook::before { content:"◈"; font-size:9px; opacity:.85; }
.hook.sc { border-style: dashed; }
.hook .sm { color:var(--text-faint); font-weight:500; }
/* branch chip */
.branch { display: inline-flex; gap:6px; align-items: baseline; font-family:var(--mono); font-size:10.5px;
color:var(--flag); background:color-mix(in srgb,var(--flag) 10%, transparent);
border:1px dashed color-mix(in srgb,var(--flag) 40%, transparent); border-radius:6px; padding:2px8px; white-space: normal; }
.branchb { color:var(--flag); font-weight:700; }
/* flow connector */
.flow-arrow { display: flex; justify-content: center; align-items: center; height:20px; color:var(--text-faint);
font-family:var(--mono); font-size:13px; gap:8px; margin:-2px0; }
.flow-arrow::before, .flow-arrow::after { content:""; height:1px; width:40px; background:var(--border-strong); }
/* nested loop / branch inset */
.inset { margin:8px08px30px; border:1.5px dashed color-mix(in srgb,var(--flag) 42%,var(--border));
border-radius:10px; background:color-mix(in srgb,var(--flag) 5%,var(--surface)); padding:12px14px; position: relative; }
.inset> .inset-tag { position: absolute; top:-10px; left:14px; background:var(--surface);
border:1px solid color-mix(in srgb,var(--flag) 42%,var(--border)); border-left:3px solid var(--flag);
border-radius:6px; padding:2px9px; font-family:var(--mono); font-size:11px; font-weight:600; color:var(--flag); }
.inset .loopnote { font-size:12px; color:var(--text-dim); margin:8px2px0; }
.inset .loopnotecode { font-family:var(--mono); font-size:11px; background:var(--inset); padding:04px; border-radius:3px; color:var(--text); }
.ws-inset { --flag:var(--adapter); }
.ws-inset> .inset-tag { color:var(--adapter); border-left-color:var(--adapter); }
/* ---- routing cascade ---- */
.cascade { display: flex; flex-direction: column; gap:0; }
.tier { display: grid; grid-template-columns:84px1fr; gap:12px; align-items: stretch; }
.tier .tlabel { font-family:var(--mono); font-size:11px; font-weight:600; color:var(--state);
display: flex; flex-direction: column; justify-content: center; gap:2px; text-align: right; padding-right:4px; }
.tier .tlabel .tn { color:var(--text-faint); font-size:10px; letter-spacing:.06em; }
.tier .tbox { background:var(--surface); border:1px solid var(--border); border-left:3px solid var(--state);
border-radius:9px; padding:10px13px; box-shadow:var(--shadow); display: flex; flex-direction: column; gap:3px; }
.tier .tbox .tt { font-family:var(--mono); font-size:12.5px; font-weight:600; color:var(--text); display: flex; gap:8px; align-items: baseline; flex-wrap: wrap; }
.tier .tbox .tt .hit { font-family:var(--mono); font-size:9.5px; text-transform: uppercase; letter-spacing:.05em; font-weight:700;
color:var(--behavior); background:color-mix(in srgb,var(--behavior) 13%, transparent); padding:1px5px; border-radius:4px; }
.tier .tbox .td { font-size:12px; color:var(--text-dim); }
.tier .tbox .tdcode { font-family:var(--mono); font-size:11px; background:var(--inset); padding:04px; border-radius:3px; color:var(--text); }
.cascade .miss { display: flex; align-items: center; gap:9px; margin:5px05px96px; color:var(--text-faint);
font-family:var(--mono); font-size:11px; }
.cascade .miss::before { content:"miss ↓"; color:var(--flag); font-weight:600; }
/* ---- hook rail table ---- */
.railwrap { overflow-x: auto; border:1px solid var(--border); border-radius:11px; box-shadow:var(--shadow); }
table.rail { border-collapse: collapse; width:100%; min-width:640px; font-size:12.5px; background:var(--surface); }
table.railth,table.railtd { text-align: left; padding:9px13px; border-bottom:1px solid var(--border); vertical-align: top; }
table.railtheadth { font-family:var(--mono); font-size:10.5px; letter-spacing:.06em; text-transform: uppercase;
color:var(--text-faint); font-weight:600; background:var(--surface-2); }
table.railtbodytr:last-childtd { border-bottom: none; }
table.rail .ph { font-family:var(--mono); font-weight:600; color:var(--hook); white-space: nowrap; }
table.rail .ph .idx { color:var(--text-faint); font-weight:500; margin-right:6px; }
table.rail .where { color:var(--text-dim); }
table.rail .wherecode { font-family:var(--mono); font-size:11px; background:var(--inset); padding:04px; border-radius:3px; color:var(--text); }
.kind-tag { font-family:var(--mono); font-size:9.5px; text-transform: uppercase; letter-spacing:.05em; font-weight:700;
padding:1.5px6px; border-radius:4px; white-space: nowrap; }
.kind-sc { color:var(--flag); background:color-mix(in srgb,var(--flag) 12%, transparent); border:1px dashed color-mix(in srgb,var(--flag) 40%, transparent); }
.kind-ob { color:var(--domain); background:color-mix(in srgb,var(--domain) 12%, transparent); }
.scope { font-family:var(--mono); font-size:11px; color:var(--text-dim); white-space: nowrap; }
.scope .both { color:var(--behavior); }
footer { margin-top:46px; padding-top:20px; border-top:1px solid var(--border); color:var(--text-dim); font-size:12.5px; }
footercode { font-family:var(--mono); background:var(--inset); padding:1px5px; border-radius:4px; color:var(--text); }
footer .note { max-width:84ch; }
footerstrong { color:var(--text); font-weight:600; }
@media (max-width:620px) {
.tier { grid-template-columns:1fr; }
.tier .tlabel { text-align: left; flex-direction: row; gap:8px; padding:002px; }
.cascade .miss { margin-left:4px; }
.inset, .band { padding-left:12px; }
}
</style>
<divclass="wrap">
<headerclass="top">
<pclass="kicker">libhttpserver v2.0 · request lifecycle</p>
<h1>How a request flows — from the <spanclass="mono">MHD</span> callback to the wire</h1>
<pclass="lede">One HTTP request is not one function call. libmicrohttpd drives the exchange through a fixed sequence of <b>C-ABI callbacks</b> (the purple <spanclass="mono">webserver_impl</span> trampolines), each forwarding into a <b>behavior service</b>. This traces that journey end-to-end: the callback spine, the <b>body-accumulation loop</b>, the <b>WebSocket</b> branch, the four-tier <b>route resolution</b>, and where each of the <b>11 hook phases</b> fires. Companion to the <spanclass="mono">class & filesystem map</span>; same colour language.</p>
<divclass="glance" aria-label="entry points at a glance">
<divclass="gcard"><spanclass="gk">accept</span><spanclass="gv">policy_callback</span><spanclass="gs">IP ACL · per connection</span></div>
<divclass="garrow">→</div>
<divclass="gcard"><spanclass="gk">allocate</span><spanclass="gv">connection_notify · uri_log</span><spanclass="gs">arena + con_cls</span></div>
<divclass="garrow">→</div>
<divclass="gcard"><spanclass="gk">drive</span><spanclass="gv">answer_to_connection</span><spanclass="gs">called 1..N times</span></div>
<divclass="garrow">→</div>
<divclass="gcard"><spanclass="gk">finish</span><spanclass="gv">request_completed</span><spanclass="gs">fire + free</span></div>
</div>
<divclass="legend" aria-label="legend">
<spanclass="lg"><spanclass="sw" style="background:var(--adapter)"></span>MHD C-ABI trampoline</span>
<spanclass="lg"><spanclass="sw" style="background:var(--behavior)"></span>behavior service</span>
<spanclass="lg"><spanclass="sw" style="background:var(--state)"></span>state collaborator</span>
<spanclass="lg"><spanclass="sw" style="background:var(--domain)"></span>domain / value object</span>
<spanclass="sep" aria-hidden="true"></span>
<spanclass="lg"><spanclass="pill" style="color:var(--hook);border-color:color-mix(in srgb,var(--hook) 40%,transparent);background:color-mix(in srgb,var(--hook) 12%,transparent)"></span>hook phase fires</span>
<spanclass="lg"><spanclass="pill" style="border-style:dashed;color:var(--flag);border-color:color-mix(in srgb,var(--flag) 45%,transparent);background:color-mix(in srgb,var(--flag) 10%,transparent)"></span>branch / short-circuit</span>
</div>
</header>
<!-- ============ 1. LIFECYCLE SPINE ============ -->
<section>
<divclass="sec-head"><spanclass="sec-num">01</span><h2>The callback spine — a normal request, top to bottom</h2></div>
<pclass="sec-sub">Each band is one libmicrohttpd callback (all <code>static</code> members of <code>webserver_impl</code>, defined in <code>webserver_callbacks.cpp</code>). The trampoline marshals the C ABI and delegates to a service. A <code>GET</code> runs straight down; <code>◈ hook</code> pills mark where a phase fires; dashed chips mark branch points. The shared blackboard threaded through every band is <code>connection_context</code> (<code>*con_cls</code>).</p>
<!-- BAND: policy_callback -->
<divclass="band" style="--accent:var(--adapter)">
<divclass="band-head"><spanclass="band-cb">policy_callback</span><spanclass="band-tag">MHD adapter</span><spanclass="band-when">once · per TCP connection · before accept</span></div>
<pclass="band-note">Only runs when <code>ip_access_control_enabled</code>. Decides whether to accept the peer at all.</p>
<divclass="steps">
<divclass="step"><spanclass="sn">1</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-state">ip_access_control::classify</span><spanclass="txt">build <code>ip_representation(addr)</code> → <code>membership{denied, allowed}</code> under shared locks</span></div></div></div>
<divclass="step"><spanclass="sn">2</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-mhd">classify_decision</span><spanclass="txt">apply <code>default_policy</code> → <code>(accepted, reason)</code></span></div>
<divclass="sline"><spanclass="branch"><b>DENY</b> → return <code>MHD_NO</code>, connection refused</span></div></div></div>
<divclass="step"><spanclass="sn">3</span><divclass="sbody">
<divclass="sline"><spanclass="hook">accept_decision</span><spanclass="txt">fired via <code>hooks_dispatch_.fire_accept_decision</code> → return <code>MHD_YES</code></span></div></div></div>
</div>
</div>
<divclass="flow-arrow">▼</div>
<!-- BAND: connection_notify STARTED -->
<divclass="band" style="--accent:var(--adapter)">
<divclass="band-head"><spanclass="band-cb">connection_notify</span><spanclass="band-tag">MHD adapter</span><spanclass="band-when">STARTED · per connection</span></div>
<divclass="steps">
<divclass="step"><spanclass="sn">4</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-dom">new connection_state</span><spanclass="txt">per-connection PMR arena (8 KB inline buffer) → stored in MHD <code>socket_context</code>; copies <code>max_args_count/bytes</code></span></div></div></div>
<divclass="step"><spanclass="sn">5</span><divclass="sbody">
<divclass="sline"><spanclass="hook">connection_opened</span><spanclass="txt"><code>fire_connection_opened(ctx)</code></span></div></div></div>
</div>
</div>
<divclass="flow-arrow">▼</div>
<!-- BAND: uri_log -->
<divclass="band" style="--accent:var(--adapter)">
<divclass="band-head"><spanclass="band-cb">uri_log</span><spanclass="band-tag">MHD adapter</span><spanclass="band-when">once · per request</span></div>
<divclass="steps">
<divclass="step"><spanclass="sn">6</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-dom">make_unique<connection_context></span><spanclass="txt">stashes <code>complete_uri</code>; the pointer becomes MHD's <code>*con_cls</code>, handed back to every later callback for this request</span></div></div></div>
</div>
</div>
<divclass="flow-arrow">▼</div>
<!-- BAND: answer_to_connection FIRST -->
<divclass="band" style="--accent:var(--adapter)">
<divclass="band-head"><spanclass="band-cb">answer_to_connection</span><spanclass="band-tag">MHD adapter → request_pipeline</span><spanclass="band-when">first call · <codestyle="font-family:var(--mono)">request == nullptr</code></span></div>
<pclass="band-note">First invocation for the request: parse the head, resolve the verb, build the <code>http_request</code>.</p>
<divclass="steps">
<divclass="step"><spanclass="sn">7</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-mhd">answer_to_connection</span><spanclass="txt">stamp <code>start_time</code>, <code>ws=parent</code>; <code>base_unescaper</code>; <b>canonicalize</b>: <code>standardized_url = normalize_path(standardize_url(url))</code> — the single security-critical dot-segment collapse</span></div></div></div>
<divclass="step"><spanclass="sn">8</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-mhd">resolve_method_callback</span><spanclass="txt">verb → <code>callback = &http_resource::render_get</code>, <code>method_enum</code>, sets <code>has_body</code></span></div>
<divclass="sline"><spanclass="branch">unknown verb → <code>callback=nullptr</code>, <b>405</b> path</span></div></div></div>
<divclass="step"><spanclass="sn">9</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">request_pipeline::requests_answer_first_step</span><spanclass="txt">build <code>http_request</code> in the connection arena (<code>pick_resource</code>)</span></div></div></div>
<divclass="step"><spanclass="sn">10</span><divclass="sbody">
<divclass="sline"><spanclass="hook sc">request_received</span><spanclass="txt"><code>hooks_.fire_request_received</code></span><spanclass="branch"><b>respond_with</b> → set <code>response</code>, <code>skip_handler=true</code></span></div></div></div>
<divclass="step"><spanclass="sn">11</span><divclass="sbody">
<divclass="sline"><spanclass="branch"><b>has_body?</b></span><spanclass="txt"><b>GET / no body</b> → return <code>MHD_YES</code>, no post-processor. <b>POST / PUT</b> → set <code>content_size_limit</code>, create <code>MHD_PostProcessor</code> for form types → enters the loop below</span></div></div></div>
</div>
<!-- INSET: body accumulation loop -->
<divclass="inset">
<spanclass="inset-tag">⟳ body-accumulation loop · POST / PUT only</span>
<pclass="band-note" style="margin-top:6px">MHD calls <code>answer_to_connection</code><b>repeatedly</b>, each with a non-empty chunk, routing to <code>request_pipeline::requests_answer_second_step</code>. Loops until MHD delivers a zero-size chunk.</p>
<divclass="steps">
<divclass="step"><spanclass="sn">a</span><divclass="sbody">
<divclass="sline"><spanclass="hook sc">body_chunk</span><spanclass="txt">fired <b>per chunk</b> (<code>body_bytes_seen</code> offset)</span><spanclass="branch">respond_with → <code>skip_handler</code>, drain</span></div></div></div>
<divclass="step"><spanclass="sn">b</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">request_pipeline</span><spanclass="txt">accumulate: <code>grow_content</code> (raw / <code>put_processed_data_to_content</code>) <b>or</b><code>MHD_post_process</code> → drives <code>post_iterator</code></span></div></div></div>
<divclass="step"><spanclass="sn">c</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-mhd">post_iterator</span><spanclass="txt">per form field →</span><spanclass="actor a-beh">upload_pipeline::iterate_file</span><spanclass="txt">memory → <code>set_arg_flat</code>; disk → <code>manage_upload_stream</code> + <code>write</code> into <code>file_info</code></span></div></div></div>
</div>
<pclass="loopnote">Each iteration ends with <code>*upload_data_size = 0</code> to signal the chunk consumed. <b>No handler runs yet.</b></p>
</div>
</div>
<divclass="flow-arrow">▼</div>
<!-- BAND: answer_to_connection TERMINAL -->
<divclass="band" style="--accent:var(--adapter)">
<divclass="band-head"><spanclass="band-cb">answer_to_connection</span><spanclass="band-tag">MHD adapter → request_pipeline</span><spanclass="band-when">terminal call · <codestyle="font-family:var(--mono)">*upload_data_size == 0</code></span></div>
<divclass="steps">
<divclass="step"><spanclass="sn">12</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">requests_answer_second_step → complete_request</span><spanclass="txt"><code>set_path(standardized_url)</code>, <code>set_method</code>, <code>set_version</code> → hand off to the dispatcher</span></div></div></div>
</div>
</div>
<divclass="flow-arrow">▼</div>
<!-- BAND: finalize_answer -->
<divclass="band" style="--accent:var(--behavior)">
<divclass="band-head"><spanclass="band-cb">request_dispatcher::finalize_answer</span><spanclass="band-tag">behavior service</span><spanclass="band-when">route · auth · dispatch</span></div>
<pclass="band-note">The heart of the exchange: try to upgrade, resolve the route, run the gate hooks, invoke the handler, catch what it throws.</p>
<divclass="steps">
<divclass="step"><spanclass="sn">13</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">try_ws_upgrade</span><spanclass="txt">→ <code>websocket_upgrader::try_handle</code></span><spanclass="branch"><b>Upgrade: websocket</b> → 101, bypass everything below ↘ (see §3)</span></div></div></div>
<divclass="step"><spanclass="sn">14</span><divclass="sbody">
<divclass="sline"><spanclass="branch"><b>skip_handler?</b> → jump straight to materialize (step 20)</span></div></div></div>
<divclass="step"><spanclass="sn">15</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">resolve_resource_for_request</span><spanclass="txt">→ <code>route_table::lookup_v2</code> (see §2); replay <code>captured_params</code> via <code>set_arg</code></span></div>
<divclass="sline"><spanclass="branch"><b>not found</b> → <code>errors_.not_found_page</code> · <b>404</b></span></div></div></div>
<divclass="step"><spanclass="sn">16</span><divclass="sbody">
<divclass="sline"><spanclass="hook">route_resolved</span><spanclass="txt">gated; builds <code>route_descriptor{template, methods, is_prefix}</code></span></div></div></div>
<divclass="step"><spanclass="sn">17</span><divclass="sbody">
<divclass="sline"><spanclass="hook sc">before_handler</span><spanclass="scope">server + <spanclass="both">per-route</span></span><spanclass="txt"><b>auth & 405-alias hooks run here</b></span><spanclass="branch">respond_with → skip handler</span></div></div></div>
<divclass="step"><spanclass="sn">18</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">dispatch_resource_handler</span><spanclass="txt"><code>is_allowed(method_enum)</code> →</span><spanclass="branch">no → <b>405</b> + <code>Allow:</code></span></div>
<divclass="sline"><spanclass="txt"><code>(*hrm.*callback)(*request)</code> — <b>pointer-to-member dispatch</b> → your <code>render_get(...)</code> (or <code>lambda_resource::invoke_</code>)</span></div>
<divclass="sline"><spanclass="hook sc">handler_exception</span><spanclass="scope">server + <spanclass="both">per-route</span></span><spanclass="branch">threw & unhandled → <code>internal_error_page</code> · <b>500</b></span></div></div></div>
<divclass="step"><spanclass="sn">19</span><divclass="sbody">
<divclass="sline"><spanclass="hook sc">after_handler</span><spanclass="scope">server + <spanclass="both">per-route</span></span><spanclass="txt">a hook may <b>replace</b> the response</span></div></div></div>
</div>
</div>
<divclass="flow-arrow">▼</div>
<!-- BAND: materialize -->
<divclass="band" style="--accent:var(--behavior)">
<divclass="band-head"><spanclass="band-cb">response_materializer::materialize_and_queue_response</span><spanclass="band-tag">behavior service</span><spanclass="band-when">http_response → the wire</span></div>
<divclass="steps">
<divclass="step"><spanclass="sn">20</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">materialize_response</span><spanclass="txt"><code>response_body::materialize()</code> — polymorphic per <code>body_kind</code> → <code>MHD_Response*</code> (empty · string · file · iovec · pipe · deferred · digest); null/throw → 404/500 fallback</span></div></div></div>
<divclass="step"><spanclass="sn">21</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">decorate_mhd_response</span><spanclass="txt">walk headers / footers / parsed cookies → <code>MHD_add_response_header</code></span></div></div></div>
<divclass="step"><spanclass="sn">22</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh" style="--role:var(--send)">queue_response_dispatching_kind</span><spanclass="txt"><bstyle="color:var(--send)">◀ THE SEND</b> — <code>MHD_queue_response</code> (or <code>MHD_queue_auth_required_response3</code> for a digest challenge)</span></div></div></div>
<divclass="step"><spanclass="sn">23</span><divclass="sbody">
<divclass="sline"><spanclass="hook">response_sent</span><spanclass="scope">server + <spanclass="both">per-route</span> + log_access alias</span><spanclass="txt">after queue, before destroy; computes <code>bytes_queued</code> + <code>elapsed</code></span></div></div></div>
<divclass="step"><spanclass="sn">24</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-mhd">MHD_destroy_response</span><spanclass="txt">releases the caller ref; MHD keeps streaming bodies alive</span></div></div></div>
</div>
</div>
<divclass="flow-arrow">▼</div>
<!-- BAND: request_completed -->
<divclass="band" style="--accent:var(--adapter)">
<divclass="band-head"><spanclass="band-cb">request_completed</span><spanclass="band-tag">MHD adapter</span><spanclass="band-when">MHD completion callback</span></div>
<divclass="steps">
<divclass="step"><spanclass="sn">25</span><divclass="sbody">
<divclass="sline"><spanclass="hook">request_completed</span><spanclass="scope">server + <spanclass="both">per-route</span> via <code>resource_weak_</code></span></div></div></div>
<divclass="step"><spanclass="sn">26</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-dom">delete connection_context</span><spanclass="txt">runs <code>~http_request</code> + arena destructors</span></div></div></div>
<divclass="step"><spanclass="sn">27</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-dom">connection_state::reset_arena</span><spanclass="txt">release + <code>secure_zero</code> for keep-alive reuse</span></div></div></div>
</div>
</div>
<divclass="flow-arrow">▼</div>
<!-- BAND: connection_notify CLOSED -->
<divclass="band" style="--accent:var(--adapter)">
<divclass="band-head"><spanclass="band-cb">connection_notify</span><spanclass="band-tag">MHD adapter</span><spanclass="band-when">CLOSED · per connection</span></div>
<divclass="steps">
<divclass="step"><spanclass="sn">28</span><divclass="sbody">
<divclass="sline"><spanclass="hook">connection_closed</span><spanclass="txt">then <code>delete connection_state</code></span></div></div></div>
</div>
</div>
</section>
<!-- ============ 2. ROUTE RESOLUTION ============ -->
<section>
<divclass="sec-head"><spanclass="sec-num">02</span><h2>Route resolution — <spanclass="mono" style="font-size:16px">route_table::lookup_v2</span></h2></div>
<pclass="sec-sub">Step 15 above. Four tiers, cheapest first; the first hit wins and returns a <code>lookup_result{found, tier, entry, captured_params}</code>. Lookup keys off the raw <code>standardized_url</code> string — <code>http_endpoint</code> parsing is a <b>registration-time</b> concern, not per-request. The entry is returned <b>regardless of method</b> so the 405 path still sees it.</p>
<divclass="cascade">
<divclass="tier"><divclass="tlabel"><span>canon</span><spanclass="tn">pre</span></div>
<divclass="tbox"><divclass="tt">canonicalize_lookup_path</div><divclass="td">ensure leading <code>/</code>, strip trailing <code>/</code>; zero-alloc when already canonical</div></div></div>
<divclass="tier"><divclass="tlabel"><span>Tier 1</span><spanclass="tn">exact</span></div>
<divclass="tbox"><divclass="tt">exact_routes_ <spanclass="hit">hit → bypass cache</span></div><divclass="td"><code>std::map</code> transparent <code>find</code> under <code>shared_lock</code> — the hottest path, deliberately skips the LRU</div></div></div>
<divclass="miss"></div>
<divclass="tier"><divclass="tlabel"><span>Tier 2</span><spanclass="tn">cache</span></div>
<divclass="tbox"><divclass="tt">route_lru_cache.find_by_view <spanclass="hit">hit → promote</span></div><divclass="td">LRU over <b>parameter / regex</b> results only; <code>{method, path}</code> key, moves out <code>entry</code> + <code>captured_params</code></div></div></div>
<divclass="miss"></div>
<divclass="tier"><divclass="tlabel"><span>Tier 3</span><spanclass="tn">radix</span></div>
<divclass="tbox"><divclass="tt">param_and_prefix_routes_ · segment_trie <spanclass="hit">hit → radix</span></div><divclass="td">walk path segments: literal children, then <code>{name}</code> wildcards (captured), applying any <code>{id|regex}</code> constraint; records best prefix terminus for <code>register_prefix</code></div></div></div>
<divclass="miss"></div>
<divclass="tier"><divclass="tlabel"><span>Tier 4</span><spanclass="tn">regex</span></div>
<divclass="tbox"><divclass="tt">regex_routes_ · linear scan <spanclass="hit">hit → regex</span></div><divclass="td"><code>std::regex_match</code> over pre-compiled patterns; last resort</div></div></div>
<divclass="tier"><divclass="tlabel" style="color:var(--behavior)"><span>install</span><spanclass="tn">post</span></div>
<divclass="tbox" style="border-left-color:var(--behavior)"><divclass="tt" style="color:var(--behavior)">route_lru_cache.insert</div><divclass="td">on any param/regex hit, memoize <code>{entry, captured_params}</code> for next time</div></div></div>
</div>
<pclass="sec-sub" style="margin-top:20px"><bstyle="color:var(--text)">Method → handler.</b><code>lookup_v2</code> never filters by method. The verb was already turned into a <b>pointer-to-member</b> (<code>render_get</code>/<code>render_post</code>/…) back in <code>resolve_method_callback</code> (step 8); <code>dispatch_resource_handler</code> then checks <code>http_resource::is_allowed(method_enum)</code> — mismatch yields <b>405</b> with the resource's <code>Allow:</code> header.</p>
</section>
<!-- ============ 3. WEBSOCKET BRANCH ============ -->
<section>
<divclass="sec-head"><spanclass="sec-num">03</span><h2>The WebSocket branch — diverges at step 13</h2></div>
<pclass="sec-sub">Before any routing or handler dispatch, <code>finalize_answer</code> offers the request to the upgrader. A genuine upgrade queues <b>101 Switching Protocols</b> and <b>bypasses routing, all handler hooks, and the response materializer entirely</b>.</p>
<divclass="inset ws-inset" style="margin-left:0">
<spanclass="inset-tag">↘ websocket_upgrader::try_handle → std::optional<MHD_Result></span>
<divclass="steps" style="margin-top:8px">
<divclass="step"><spanclass="sn">1</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">try_handle</span><spanclass="txt">no <code>Upgrade: websocket</code> header → <code>std::nullopt</code> → normal HTTP flow resumes</span></div></div></div>
<divclass="step"><spanclass="sn">2</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">validate_websocket_handshake</span><spanclass="txt">check <code>Connection: Upgrade</code>, <code>Sec-WebSocket-Version: 13</code>, non-empty key</span><spanclass="branch">invalid → <b>400</b></span></div></div></div>
<divclass="step"><spanclass="sn">3</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-state">ws_registry::find</span><spanclass="txt"><code>shared_ptr<websocket_handler></code> for the URL</span><spanclass="branch">none → fall back to normal HTTP</span></div></div></div>
<divclass="step"><spanclass="sn">4</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">complete_websocket_upgrade</span><spanclass="txt"><code>Sec-WebSocket-Accept</code> + <code>MHD_create_response_for_upgrade</code> → <b>101</b> via <code>MHD_queue_response</code></span></div></div></div>
<divclass="step"><spanclass="sn">5</span><divclass="sbody">
<divclass="sline"><spanclass="actor a-beh">upgrade_handler</span><spanclass="txt">on the raw socket: <code>on_open</code>, then a <code>recv()</code> loop → decode frames → <code>on_message</code> / <code>on_binary</code> / <code>on_ping</code> / <code>on_close</code></span></div></div></div>
</div>
</div>
</section>
<!-- ============ 4. HOOK RAIL ============ -->
<section>
<divclass="sec-head"><spanclass="sec-num">04</span><h2>The 11 hook phases, in firing order</h2></div>
<pclass="sec-sub">Server-wide hooks live on <code>hook_bus</code>; five phases are also <b>per-resource</b> (<code>resource_hook_table</code>, registered via <code>http_resource::add_hook</code>). <code>hook_dispatcher</code> fires both chains. Every phase is guarded by a relaxed-atomic <code>has_hooks_for</code> check — <b>zero cost when unused</b>. Short-circuit phases can return <code>respond_with(...)</code> to end the request early.</p>
<divclass="railwrap">
<tableclass="rail">
<thead><tr><th>Phase</th><th>Fires in</th><th>Scope</th><th>Kind</th></tr></thead>
<tbody>
<tr><tdclass="ph"><spanclass="idx">1</span>connection_opened</td><tdclass="where"><code>connection_notify</code> STARTED</td><tdclass="scope">server</td><td><spanclass="kind-tag kind-ob">observe</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">2</span>accept_decision</td><tdclass="where"><code>policy_callback</code>, after ACL classify</td><tdclass="scope">server</td><td><spanclass="kind-tag kind-ob">observe</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">3</span>request_received</td><tdclass="where"><code>requests_answer_first_step</code></td><tdclass="scope">server</td><td><spanclass="kind-tag kind-sc">short-circuit</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">4</span>body_chunk</td><tdclass="where"><code>requests_answer_second_step</code> · <b>per chunk</b></td><tdclass="scope">server</td><td><spanclass="kind-tag kind-sc">short-circuit</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">5</span>route_resolved</td><tdclass="where"><code>finalize_answer</code>, after lookup</td><tdclass="scope">server</td><td><spanclass="kind-tag kind-ob">observe</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">6</span>before_handler</td><tdclass="where"><code>finalize_answer</code>, pre-dispatch · <b>auth</b></td><tdclass="scope"><spanclass="both">server + route</span></td><td><spanclass="kind-tag kind-sc">short-circuit</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">7</span>handler_exception</td><tdclass="where"><code>dispatch_resource_handler</code> catch arms</td><tdclass="scope"><spanclass="both">server + route</span></td><td><spanclass="kind-tag kind-sc">short-circuit</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">8</span>after_handler</td><tdclass="where"><code>finalize_answer</code>, post-handler</td><tdclass="scope"><spanclass="both">server + route</span></td><td><spanclass="kind-tag kind-sc">replace resp</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">9</span>response_sent</td><tdclass="where"><code>materialize_and_queue</code>, after queue</td><tdclass="scope"><spanclass="both">server + route</span> · log_access alias</td><td><spanclass="kind-tag kind-ob">observe</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">10</span>request_completed</td><tdclass="where"><code>request_completed</code> callback</td><tdclass="scope"><spanclass="both">server + route</span></td><td><spanclass="kind-tag kind-ob">observe</span></td></tr>
<tr><tdclass="ph"><spanclass="idx">11</span>connection_closed</td><tdclass="where"><code>connection_notify</code> CLOSED</td><tdclass="scope">server</td><td><spanclass="kind-tag kind-ob">observe</span></td></tr>
</tbody>
</table>
</div>
</section>
<footer>
<pclass="note"><strong>Reading the flow.</strong> Purple bands are the fixed libmicrohttpd callback sequence — <code>webserver_impl</code>'s static C-ABI trampolines, the only code touching MHD's ABI. Everything real happens in the teal <strong>behavior services</strong> they delegate to. The three branch axes that reshape the path: <code>has_body</code> (whether the body loop runs), <code>skip_handler</code> (a request_received/body_chunk short-circuit that skips routing straight to materialize), and the <strong>WebSocket upgrade</strong> (which bypasses the HTTP tail entirely). One request drives <code>answer_to_connection</code><strong>1..N times</strong> — once for a bodyless GET, many times while a POST body streams in — but resolves to exactly one <code>finalize_answer</code>. Companion to the class & filesystem map; both share the state / behavior / adapter colour language.</p>
</footer>
</div>
</body>
</html>