rust-web-server 17.105.0

A dependency-minimal Rust web platform: HTTP/1.1, HTTP/2, and HTTP/3 server, reverse proxy, and application framework with routing, middleware (auth, rate limiting, tracing), an MCP server, an async ORM, background jobs, object storage, and a mailer. Runs as a zero-code config-driven proxy or as a library crate. No third-party HTTP dependencies.
Documentation
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
use std::sync::atomic::Ordering;

use crate::application::Application;
use crate::http::VERSION;
use crate::metrics::{
    ACTIVE_CONNECTIONS, ERRORS_TOTAL, REQUESTS_TOTAL,
    MetricsLayer, connection_close, connection_open,
    prometheus_text, record_error, record_request, record_route,
};
use crate::middleware::Middleware;
use crate::request::{METHOD, Request};
use crate::response::Response;
use crate::server::{Address, ConnectionInfo};

// ── helpers ───────────────────────────────────────────────────────────────────

fn conn() -> ConnectionInfo {
    ConnectionInfo {
        client: Address { ip: "127.0.0.1".to_string(), port: 9999 },
        server: Address { ip: "127.0.0.1".to_string(), port: 7878 },
        request_size: 8192,
    sni_hostname: None,
    }
}

fn get(uri: &str) -> Request {
    Request {
        method: METHOD.get.to_string(),
        request_uri: uri.to_string(),
        http_version: VERSION.http_1_1.to_string(),
        headers: vec![],
        body: vec![],
    }
}

fn ok_response() -> Response {
    Response {
        http_version: VERSION.http_1_1.to_string(),
        status_code: 200,
        reason_phrase: "OK".to_string(),
        headers: vec![],
        content_range_list: vec![],
        stream_file: None,
        stream_pipe: None,
    }
}

struct FixedApp(Response);

impl Application for FixedApp {
    fn execute(&self, _req: &Request, _conn: &ConnectionInfo) -> Result<Response, String> {
        Ok(self.0.clone())
    }
}

struct ErrorApp;

impl Application for ErrorApp {
    fn execute(&self, _req: &Request, _conn: &ConnectionInfo) -> Result<Response, String> {
        Err("boom".to_string())
    }
}

// ── server-wide counter behaviour ─────────────────────────────────────────────
//
// Static atomics persist across tests.  We assert relative increments (after >=
// before + N) so that concurrent tests don't create false failures.

#[test]
fn record_request_increments_requests_total() {
    let before = REQUESTS_TOTAL.load(Ordering::SeqCst);
    record_request();
    let after = REQUESTS_TOTAL.load(Ordering::SeqCst);
    assert!(after >= before + 1);
}

#[test]
fn record_request_called_multiple_times_accumulates() {
    let before = REQUESTS_TOTAL.load(Ordering::SeqCst);
    record_request();
    record_request();
    record_request();
    let after = REQUESTS_TOTAL.load(Ordering::SeqCst);
    assert!(after >= before + 3);
}

#[test]
fn record_error_increments_errors_total() {
    let before = ERRORS_TOTAL.load(Ordering::SeqCst);
    record_error();
    let after = ERRORS_TOTAL.load(Ordering::SeqCst);
    assert!(after >= before + 1);
}

#[test]
fn connection_open_increments_active_connections() {
    let before = ACTIVE_CONNECTIONS.load(Ordering::SeqCst);
    connection_open();
    let after = ACTIVE_CONNECTIONS.load(Ordering::SeqCst);
    assert!(after >= before + 1);
    connection_close();
}

#[test]
fn connection_close_decrements_active_connections() {
    connection_open();
    let before = ACTIVE_CONNECTIONS.load(Ordering::SeqCst);
    connection_close();
    let after = ACTIVE_CONNECTIONS.load(Ordering::SeqCst);
    assert!(after <= before - 1);
}

#[test]
fn open_and_close_net_zero() {
    let before = ACTIVE_CONNECTIONS.load(Ordering::SeqCst);
    connection_open();
    connection_open();
    connection_close();
    connection_close();
    let after = ACTIVE_CONNECTIONS.load(Ordering::SeqCst);
    assert_eq!(after - before, 0);
}

// ── prometheus_text format ────────────────────────────────────────────────────

#[test]
fn prometheus_text_contains_required_metric_names() {
    let text = prometheus_text();
    assert!(text.contains("rws_requests_total"),    "missing rws_requests_total");
    assert!(text.contains("rws_errors_total"),       "missing rws_errors_total");
    assert!(text.contains("rws_active_connections"), "missing rws_active_connections");
}

#[test]
fn prometheus_text_contains_help_and_type_lines() {
    let text = prometheus_text();
    let help_count = text.lines().filter(|l| l.starts_with("# HELP")).count();
    let type_count = text.lines().filter(|l| l.starts_with("# TYPE")).count();
    // At least 3 server-wide + possibly route metrics headers.
    assert!(help_count >= 3, "expected at least 3 # HELP lines, got {}", help_count);
    assert!(type_count >= 3, "expected at least 3 # TYPE lines, got {}", type_count);
}

#[test]
fn prometheus_text_type_annotations_are_correct() {
    let text = prometheus_text();
    assert!(text.contains("# TYPE rws_requests_total counter"));
    assert!(text.contains("# TYPE rws_errors_total counter"));
    assert!(text.contains("# TYPE rws_active_connections gauge"));
}

#[test]
fn prometheus_text_metric_values_are_parseable_numbers() {
    record_request();
    let text = prometheus_text();
    for line in text.lines() {
        if line.starts_with('#') || line.is_empty() {
            continue;
        }
        // Lines look like: name{labels} value  OR  name value
        let value_part = line.rsplit(' ').next().expect("no value on metric line");
        value_part.parse::<f64>().unwrap_or_else(|_| {
            panic!("metric value is not a number on line: '{}'", line)
        });
    }
}

#[test]
fn prometheus_text_requests_reflect_recorded_requests() {
    let before_val: u64 = prometheus_text()
        .lines()
        .find(|l| l.starts_with("rws_requests_total "))
        .and_then(|l| l.split_whitespace().nth(1))
        .and_then(|v| v.parse().ok())
        .expect("rws_requests_total line not found");

    record_request();
    record_request();

    let after_val: u64 = prometheus_text()
        .lines()
        .find(|l| l.starts_with("rws_requests_total "))
        .and_then(|l| l.split_whitespace().nth(1))
        .and_then(|v| v.parse().ok())
        .expect("rws_requests_total line not found");

    assert!(after_val >= before_val + 2);
}

// ── per-route record_route ────────────────────────────────────────────────────

#[test]
fn record_route_appears_in_prometheus_text() {
    record_route("GET", "/__metrics_test_appear__", 200, 0.001);
    let text = prometheus_text();
    assert!(
        text.contains("/__metrics_test_appear__"),
        "route path not found in prometheus output"
    );
}

#[test]
fn record_route_counts_accumulate_for_same_key() {
    let path = "/__metrics_test_count__";
    record_route("GET", path, 200, 0.01);
    record_route("GET", path, 200, 0.01);
    record_route("GET", path, 200, 0.01);

    let text = prometheus_text();
    // Find the counter line for this path with status 200.
    let count: u64 = text.lines()
        .find(|l| l.contains(path) && l.contains("status=\"200\"") && l.contains("rws_route_requests_total"))
        .and_then(|l| l.rsplit(' ').next())
        .and_then(|v| v.parse().ok())
        .expect("counter line not found");
    assert!(count >= 3, "expected at least 3, got {}", count);
}

#[test]
fn record_route_separates_by_status() {
    let path = "/__metrics_test_status__";
    record_route("GET", path, 200, 0.01);
    record_route("GET", path, 404, 0.01);

    let text = prometheus_text();
    assert!(text.contains(&format!("path=\"{}\"", path)));

    let has_200 = text.lines().any(|l| l.contains(path) && l.contains("status=\"200\""));
    let has_404 = text.lines().any(|l| l.contains(path) && l.contains("status=\"404\""));
    assert!(has_200, "missing 200 status line");
    assert!(has_404, "missing 404 status line");
}

#[test]
fn record_route_separates_by_method() {
    let path = "/__metrics_test_method__";
    record_route("GET",  path, 200, 0.01);
    record_route("POST", path, 201, 0.02);

    let text = prometheus_text();
    let has_get  = text.lines().any(|l| l.contains(path) && l.contains("method=\"GET\""));
    let has_post = text.lines().any(|l| l.contains(path) && l.contains("method=\"POST\""));
    assert!(has_get,  "missing GET method line");
    assert!(has_post, "missing POST method line");
}

#[test]
fn histogram_includes_bucket_sum_and_count_lines() {
    let path = "/__metrics_test_histogram__";
    record_route("GET", path, 200, 0.003); // falls in ≤0.005 bucket
    record_route("GET", path, 200, 0.008); // falls in ≤0.01 bucket

    let text = prometheus_text();
    let bucket_line = text.lines()
        .find(|l| l.contains(path) && l.contains("_bucket{") && l.contains("le=\"+Inf\""))
        .expect("no +Inf bucket line found");
    let inf_count: u64 = bucket_line.rsplit(' ').next()
        .and_then(|v| v.parse().ok())
        .expect("could not parse +Inf count");
    assert!(inf_count >= 2);

    assert!(text.lines().any(|l| l.contains(path) && l.contains("rws_route_duration_seconds_sum")));
    assert!(text.lines().any(|l| l.contains(path) && l.contains("rws_route_duration_seconds_count")));
}

#[test]
fn histogram_buckets_are_cumulative() {
    let path = "/__metrics_test_cumulative__";
    // Both observations are ≤ 0.05 s, so all buckets ≥ 0.05 must have count ≥ 2.
    record_route("GET", path, 200, 0.001);
    record_route("GET", path, 200, 0.04);

    let text = prometheus_text();

    // Bucket ≤ 0.005: only the 0.001 observation fits.
    let bucket_005: u64 = text.lines()
        .find(|l| l.contains(path) && l.contains("le=\"0.005\""))
        .and_then(|l| l.rsplit(' ').next())
        .and_then(|v| v.parse().ok())
        .unwrap_or(0);

    // Bucket ≤ 0.05: both observations fit.
    let bucket_05: u64 = text.lines()
        .find(|l| l.contains(path) && l.contains("le=\"0.05\""))
        .and_then(|l| l.rsplit(' ').next())
        .and_then(|v| v.parse().ok())
        .unwrap_or(0);

    assert!(bucket_005 >= 1, "≤0.005 bucket should have at least 1");
    assert!(bucket_05  >= 2, "≤0.05 bucket should have at least 2");
    assert!(bucket_05 >= bucket_005, "buckets must be non-decreasing");
}

// ── MetricsLayer middleware ───────────────────────────────────────────────────

#[test]
fn metrics_layer_records_route_on_success() {
    let path = "/__metrics_test_layer_ok__";
    let app = FixedApp(ok_response());
    let layer = MetricsLayer;
    layer.handle(&get(path), &conn(), &app).unwrap();

    let text = prometheus_text();
    assert!(text.contains(path), "route path not found after MetricsLayer call");
}

#[test]
fn metrics_layer_records_500_on_handler_error() {
    let path = "/__metrics_test_layer_err__";
    let layer = MetricsLayer;
    let _ = layer.handle(&get(path), &conn(), &ErrorApp);

    let text = prometheus_text();
    let has_500 = text.lines()
        .any(|l| l.contains(path) && l.contains("status=\"500\""));
    assert!(has_500, "expected status=500 after handler error");
}

#[test]
fn metrics_layer_strips_query_string_from_path() {
    let layer = MetricsLayer;
    let app = FixedApp(ok_response());
    let req = get("/__metrics_test_query__?page=2&limit=10");
    layer.handle(&req, &conn(), &app).unwrap();

    let text = prometheus_text();
    // Path stored must not include the query string.
    assert!(
        text.contains("/__metrics_test_query__"),
        "stripped path not found"
    );
    assert!(
        !text.contains("page=2"),
        "query string must not appear in the metric path label"
    );
}

#[test]
fn metrics_layer_returns_handler_response_unchanged() {
    let mut expected = ok_response();
    expected.status_code = 201;
    expected.reason_phrase = "Created".to_string();

    let layer = MetricsLayer;
    let app = FixedApp(expected.clone());
    let result = layer.handle(&get("/__metrics_test_passthrough__"), &conn(), &app).unwrap();

    assert_eq!(201, result.status_code);
    assert_eq!("Created", result.reason_phrase);
}

#[test]
fn metrics_layer_propagates_handler_error() {
    let layer = MetricsLayer;
    let result = layer.handle(&get("/__metrics_test_err_prop__"), &conn(), &ErrorApp);
    assert!(result.is_err());
}

#[test]
fn route_prometheus_text_contains_correct_help_and_type_lines() {
    // Trigger at least one route observation so the section is emitted.
    record_route("GET", "/__metrics_test_headers__", 200, 0.01);
    let text = prometheus_text();
    assert!(text.contains("# HELP rws_route_requests_total"));
    assert!(text.contains("# TYPE rws_route_requests_total counter"));
    assert!(text.contains("# HELP rws_route_duration_seconds"));
    assert!(text.contains("# TYPE rws_route_duration_seconds histogram"));
}

#[test]
fn circuit_breaker_state_appears_in_prometheus_text() {
    // A distinctive key scoped to this test — `circuit_breaker::global()` is
    // a process-wide singleton shared by the whole test binary, so this must
    // not collide with any key another test might use.
    let key = "metrics-test-only-circuit-breaker-backend:60606";
    {
        let mut guard = crate::circuit_breaker::global().lock().unwrap();
        guard.reset(key); // start from a known Closed state regardless of prior runs
        for _ in 0..5 {
            // global()'s threshold is fixed at 5
            guard.record_failure(key);
        }
    }

    let text = prometheus_text();
    assert!(text.contains("# HELP rws_circuit_breaker_state"));
    assert!(text.contains("# TYPE rws_circuit_breaker_state gauge"));
    assert!(
        text.contains(&format!("rws_circuit_breaker_state{{backend=\"{}\"}} 2", key)),
        "expected an Open (2) line for {}, got:\n{}",
        key,
        text
    );
}

#[test]
fn circuit_breaker_state_reflects_closed_backend_as_zero() {
    let key = "metrics-test-only-circuit-breaker-backend-closed:60607";
    crate::circuit_breaker::global().lock().unwrap().reset(key);

    let text = prometheus_text();
    assert!(
        text.contains(&format!("rws_circuit_breaker_state{{backend=\"{}\"}} 0", key)),
        "expected a Closed (0) line for {}, got:\n{}",
        key,
        text
    );
}