rws-server 18.29.0

The server layer of rust-web-server (internal: use the rust-web-server crate).
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
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
456
457
458
459
460
461
462
463
464
465
use std::net::SocketAddr;
use std::thread;
use crate::entry_point::command_line_args::CommandLineArgument;
use crate::request::Request;
use crate::response::Response;
use crate::server_config::log_privacy::{self, LogPrivacy};

#[cfg(test)]
mod tests;
#[doc(hidden)]
pub mod access_sink;
pub mod syslog;
pub mod tail;

/// Logging helpers for access logs and server info.
///
/// [`Log::combined`] produces standard Combined Log Format lines compatible with
/// GoAccess, AWStats, and similar tools. All three server code paths (HTTP/1.1,
/// HTTP/2, HTTP/3) use it.
pub struct Log;

/// Called for every access-logged request, before the line is written: the
/// traffic insights (`traffic`, above this layer) set it when enabled.
static ACCESS_OBSERVER: std::sync::OnceLock<fn(&Request, &Response, std::net::IpAddr)> = std::sync::OnceLock::new();

/// Set the [`Log::log_access`] observer (once; later calls are ignored).
#[doc(hidden)]
pub fn set_access_observer(observe: fn(&Request, &Response, std::net::IpAddr)) {
    let _ = ACCESS_OBSERVER.set(observe);
}

impl Log {
    /// A multi-line debugging dump of a request and its response, headers
    /// included — the values of `Authorization`, `Proxy-Authorization`,
    /// `Cookie`, `Set-Cookie` and of `RWS_CONFIG_LOG_REDACT_HEADERS` shown as
    /// `REDACTED` (since 18.33.0).
    pub fn request_response(request: &Request, response: &Response, peer_addr: &SocketAddr) -> String {
        let config = crate::server_config::ServerConfig::current();
        let shown = |header: &crate::header::Header| -> String {
            if config.log_privacy.redacts_header(&header.name) {
                log_privacy::REDACTED.to_string()
            } else {
                header.value.to_string()
            }
        };
        let mut request_headers = "".to_string();
        for header in &request.headers {
            if &header.name.chars().count() > &0 {
                request_headers = [
                    request_headers,
                    "\n  ".to_string(),
                    header.name.to_string(),
                    ": ".to_string(),
                    shown(header)
                ].join("");
            }
        }

        let mut response_headers = "".to_string();
        for header in &response.headers {
            if &header.name.chars().count() > &0 {
                response_headers = [
                    response_headers,
                    "\n  ".to_string(),
                    header.name.to_string(),
                    ": ".to_string(),
                    shown(header)
                ].join("");
            }
        }

        let mut response_body_length = 0;
        let mut response_body_parts_number = 0;
        for content_range in &response.content_range_list {
            let boxed_parse = content_range.size.parse::<i32>();
            if boxed_parse.is_ok() {
                response_body_length += boxed_parse.unwrap();
                response_body_parts_number += 1;
            }
        }

        let current_thread = thread::current();
        let thread_id = current_thread.name().unwrap();

        let log_request_response = format!("\n\nRequest (thread id: {} peer address is {}):\n  {} {} {}  {}\n  Body: {} byte(s) total (including default initialization vector)\nEnd of Request\nResponse:\n  {} {} {}\n\n  Body: {} part(s), {} byte(s) total\nEnd of Response",
                                           thread_id,
                                           peer_addr,
                                           &request.http_version,
                                           &request.method,
                                           &request.request_uri,
                                           request_headers,
                                           request.body.len(),

                                           &response.status_code,
                                           &response.reason_phrase,
                                           response_headers,
                                           response_body_parts_number,
                                           response_body_length);

        log_request_response
    }

    pub fn usage_information() -> String {
        let mut log = "Usage:\n\n".to_string();
        let command_line_arg_list = CommandLineArgument::get_command_line_arg_list();
        for arg in command_line_arg_list {
            let flags = if arg.short_form.is_empty() {
                format!("--{}", arg.long_form)
            } else {
                format!("-{} or --{}", arg.short_form, arg.long_form)
            };
            let argument_info = format!("  {} environment variable\n  {} as command line line argument\n  {}\n\n", arg.environment_variable, flags, arg._hint.unwrap());
            log = [log, argument_info].join("");
        }
        log = [log, "End of usage section\n\n".to_string()].join("");
        log
    }

    /// The user rws runs as, for the startup banner: `$USER`, `$LOGNAME` or
    /// `%USERNAME%`, else `"unknown"`. Deliberately not `whoami` (what
    /// `FileExt::get_current_user` runs): minimal container images such as
    /// distroless have no `whoami`, and the banner's `unwrap()` on that
    /// error crashed the published Docker image at startup.
    pub fn current_user() -> String {
        ["USER", "LOGNAME", "USERNAME"]
            .iter()
            .find_map(|var| std::env::var(var).ok().filter(|v| !v.trim().is_empty()))
            .unwrap_or_else(|| "unknown".to_string())
    }

    pub fn info(name: &str) -> String {
        let mut log = format!("{}\n", name).to_string();
        const VERSION: &str = env!("CARGO_PKG_VERSION");
        const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
        const REPOSITORY: &str = env!("CARGO_PKG_REPOSITORY");
        const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
        const RUST_VERSION: &str = env!("CARGO_PKG_RUST_VERSION");
        const LICENSE: &str = env!("CARGO_PKG_LICENSE");
        let user = Log::current_user();
        let working_directory = crate::app::controller::static_resource::working_directory().to_string();

        let version = format!("Version:           {}\n", VERSION);
        log = [log, version].join("");

        let authors = format!("Authors:           {}\n", AUTHORS);
        log = [log, authors].join("");

        let repository = format!("Repository:        {}\n", REPOSITORY);
        log = [log, repository].join("");

        let description = format!("Description:       {}\n", DESCRIPTION);
        log = [log, description].join("");

        let rust_version = format!("Rust Version:      {}\n", RUST_VERSION);
        log = [log, rust_version].join("");

        let license = format!("License:           {}\n", LICENSE);
        log = [log, license].join("");

        let license = format!("User:              {}\n", user);
        log = [log, license].join("");

        let license = format!("Working Directory: {}\n", working_directory);
        log = [log, license].join("");

        log
    }

    /// Returns a structured JSON log line for one request/response pair:
    /// `time`, `remote_addr`, `method`, `path`, `protocol`, `status`, `bytes`,
    /// then `host`, `user_agent` and `referer` when the request has them.
    ///
    /// Enable with `RWS_CONFIG_LOG_FORMAT=json`. Useful for log aggregators
    /// (Loki, Fluentd, Datadog) that ingest JSON from pod stdout.
    pub fn json(request: &Request, response: &Response, peer_addr: &SocketAddr) -> String {
        let mut line = Vec::with_capacity(160);
        Log::write_json(&mut line, request, response, peer_addr);
        bytes_to_string(line)
    }

    /// [`Log::json`] appended to `out`, without intermediate strings.
    fn write_json(out: &mut Vec<u8>, request: &Request, response: &Response, peer_addr: &SocketAddr) {
        let config = crate::server_config::ServerConfig::current();
        Log::write_json_with(out, request, response, peer_addr, &config.log_privacy)
    }

    /// [`Log::write_json`] with the privacy settings at hand: with
    /// everything off, exactly the line of 18.32.
    fn write_json_with(out: &mut Vec<u8>, request: &Request, response: &Response, peer_addr: &SocketAddr, privacy: &LogPrivacy) {
        use std::io::Write;
        let body_size: usize = response.content_range_list.iter().map(|cr| cr.body.len()).sum();
        let plain = privacy.is_off();
        out.extend_from_slice(b"{\"time\":\"");
        with_timestamps(|iso, _| out.extend_from_slice(iso.as_bytes()));
        out.extend_from_slice(b"\",\"remote_addr\":\"");
        if plain {
            let _ = write!(out, "{}", peer_addr.ip());
        } else {
            privacy.write_ip(out, peer_addr.ip());
        }
        out.extend_from_slice(b"\",\"method\":\"");
        push_json_escaped(out, &request.method);
        out.extend_from_slice(b"\",\"path\":\"");
        if plain {
            push_json_escaped(out, &request.request_uri);
        } else {
            privacy.redact_target(&request.request_uri, |piece| push_json_escaped(out, piece));
        }
        out.extend_from_slice(b"\",\"protocol\":\"");
        push_json_escaped(out, &request.http_version);
        out.extend_from_slice(b"\",\"status\":");
        let _ = write!(out, "{}", response.status_code);
        out.extend_from_slice(b",\"bytes\":");
        out.extend_from_slice(crate::ext::decimal::Decimal::new(body_size as u128).as_str().as_bytes());
        // Who asked, for what site, from where (18.15.0): only when sent.
        for (key, name) in [(&b",\"host\":\""[..], "host"), (&b",\"user_agent\":\""[..], "user-agent"), (&b",\"referer\":\""[..], "referer")] {
            if let Some(header) = request.header(name).filter(|h| !h.value.is_empty()) {
                out.extend_from_slice(key);
                if plain {
                    push_json_escaped(out, &header.value);
                } else if privacy.redacts_header(name) {
                    out.extend_from_slice(log_privacy::REDACTED.as_bytes());
                } else if name == "referer" {
                    privacy.redact_target(&header.value, |piece| push_json_escaped(out, piece));
                } else {
                    push_json_escaped(out, &header.value);
                }
                out.push(b'"');
            }
        }
        out.push(b'}');
    }

    /// Writes one access log line to stdout using the format configured by
    /// `RWS_CONFIG_LOG_FORMAT` (`"combined"` or `"json"`). Call this instead of
    /// `Log::combined` so that format selection is centralised.
    ///
    /// Lines are buffered per thread and written by a background thread every
    /// 100 ms (and when a thread's buffer passes 32 KiB, when the thread
    /// exits, and at process exit); [`Log::flush_access_log`] writes them now.
    pub fn log_access(request: &Request, response: &Response, peer_addr: &SocketAddr) {
        Log::log_access_traced(request, response, peer_addr, None)
    }

    /// [`Log::log_access`] for the server's own connection loops: takes the
    /// request's trace context off the response first (`OtelLayer` puts it
    /// there only while OTLP log export is on), so it reaches the OTLP log
    /// record but not the client.
    #[doc(hidden)]
    pub fn log_served(request: &Request, response: &mut Response, peer_addr: &SocketAddr) {
        let trace = crate::otel::logs::take_trace(response);
        Log::log_access_traced(request, response, peer_addr, trace)
    }

    fn log_access_traced(request: &Request, response: &Response, peer_addr: &SocketAddr, trace: Option<crate::otel::logs::TraceIds>) {
        // The client behind a trusted proxy (`ip_filter::client_ip`), not the
        // proxy; only looked up when the request says it was forwarded.
        let client;
        let peer_addr = if request.header("x-forwarded-for").is_some() || request.header("forwarded").is_some() {
            client = crate::ip_filter::client_ip(request, &peer_addr.ip().to_string())
                .parse::<std::net::IpAddr>()
                .map_or(*peer_addr, |ip| SocketAddr::new(ip, peer_addr.port()));
            &client
        } else {
            peer_addr
        };
        // Traffic insights count every request, whether lines are written.
        if let Some(observe) = ACCESS_OBSERVER.get() {
            observe(request, response, peer_addr.ip());
        }
        let config = crate::server_config::ServerConfig::current();
        let format = &config.log_format;
        let off = format.eq_ignore_ascii_case("off") || format.eq_ignore_ascii_case("none");
        let otlp = crate::otel::logs::enabled();
        if off && !otlp {
            return;
        }
        let json = format == "json";
        let privacy = &config.log_privacy;
        if !off {
            access_sink::write_line(|out| {
                if json {
                    Log::write_json_with(out, request, response, peer_addr, privacy)
                } else {
                    Log::write_combined_with(out, request, response, peer_addr, privacy)
                }
            });
        }
        if otlp {
            // The same line as the record's body (JSON while the access log
            // itself is off), beside semantic-convention attributes.
            crate::otel::logs::record(request, response, peer_addr.ip(), privacy, trace, |out| {
                if json || off {
                    Log::write_json_with(out, request, response, peer_addr, privacy)
                } else {
                    Log::write_combined_with(out, request, response, peer_addr, privacy)
                }
            });
        }
    }

    /// Have `SIGUSR1` reopen the access-log file (`RWS_CONFIG_ACCESS_LOG`)
    /// before its next write, for logrotate; does nothing when the log goes
    /// to stdout. The `rws` binary calls it at startup.
    pub fn reopen_access_log_on_sigusr1() {
        access_sink::install_reopen_signal();
    }

    /// Reopen the access-log file before its next write — what `SIGUSR1`
    /// does, for a program that moves the file itself.
    pub fn reopen_access_log() {
        access_sink::reopen();
    }

    /// Write out the buffered access-log lines now (they're otherwise written
    /// within 100 ms) — e.g. before exiting without returning from `main`.
    pub fn flush_access_log() {
        access_sink::flush();
    }

    /// The access-log line for `RWS_CONFIG_LOG_FORMAT`: `json` (the `rws`
    /// binary's default), Combined Log Format for any other value (including
    /// unset, in library use), or `None` for `off`/`none` — no per-request
    /// line, e.g. behind a proxy that already logs or when benchmarking.
    /// Taken from `ServerConfig::current()`, so it's hot-reloadable.
    pub fn access_log_line(request: &Request, response: &Response, peer_addr: &SocketAddr) -> Option<String> {
        let config = crate::server_config::ServerConfig::current();
        let format = &config.log_format;
        if format.eq_ignore_ascii_case("off") || format.eq_ignore_ascii_case("none") {
            return None;
        }
        Some(if format == "json" {
            Log::json(request, response, peer_addr)
        } else {
            Log::combined(request, response, peer_addr)
        })
    }

    fn format_iso8601(secs: u64) -> String {
        let sec = secs % 60;
        let min = (secs / 60) % 60;
        let hour = (secs / 3600) % 24;
        let days = secs / 86400;
        let (year, month, day) = Log::days_to_ymd(days);
        format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z", year, month, day, hour, min, sec)
    }

    /// Returns a Combined Log Format (CLF) line for one request/response pair:
    /// `IP - - [DD/Mon/YYYY:HH:MM:SS +0000] "METHOD URI VERSION" STATUS SIZE`
    pub fn combined(request: &Request, response: &Response, peer_addr: &SocketAddr) -> String {
        let mut line = Vec::with_capacity(128);
        Log::write_combined(&mut line, request, response, peer_addr);
        bytes_to_string(line)
    }

    /// [`Log::combined`] appended to `out`, without intermediate strings.
    fn write_combined(out: &mut Vec<u8>, request: &Request, response: &Response, peer_addr: &SocketAddr) {
        let config = crate::server_config::ServerConfig::current();
        Log::write_combined_with(out, request, response, peer_addr, &config.log_privacy)
    }

    fn write_combined_with(out: &mut Vec<u8>, request: &Request, response: &Response, peer_addr: &SocketAddr, privacy: &LogPrivacy) {
        use std::io::Write;
        let body_size: usize = response.content_range_list.iter().map(|cr| cr.body.len()).sum();
        if privacy.is_off() {
            let _ = write!(out, "{}", peer_addr.ip());
        } else {
            privacy.write_ip(out, peer_addr.ip());
        }
        out.extend_from_slice(b" - - [");
        with_timestamps(|_, clf| out.extend_from_slice(clf.as_bytes()));
        out.extend_from_slice(b"] \"");
        out.extend_from_slice(request.method.as_bytes());
        out.push(b' ');
        if privacy.redact_query.is_empty() {
            out.extend_from_slice(request.request_uri.as_bytes());
        } else {
            privacy.redact_target(&request.request_uri, |piece| out.extend_from_slice(piece.as_bytes()));
        }
        out.push(b' ');
        out.extend_from_slice(request.http_version.as_bytes());
        out.extend_from_slice(b"\" ");
        let _ = write!(out, "{}", response.status_code);
        out.push(b' ');
        if body_size > 0 {
            out.extend_from_slice(crate::ext::decimal::Decimal::new(body_size as u128).as_str().as_bytes());
        } else {
            out.push(b'-');
        }
    }

    fn format_clf_timestamp(secs: u64) -> String {
        let sec = secs % 60;
        let min = (secs / 60) % 60;
        let hour = (secs / 3600) % 24;
        let days = secs / 86400;
        let (year, month, day) = Log::days_to_ymd(days);
        const MONTHS: [&str; 12] = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
        format!("{:02}/{}/{:04}:{:02}:{:02}:{:02} +0000",
            day, MONTHS[(month - 1) as usize], year, hour, min, sec)
    }

    fn days_to_ymd(days: u64) -> (u64, u64, u64) {
        let z = days + 719468;
        let era = z / 146097;
        let doe = z % 146097;
        let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
        let y = yoe + era * 400;
        let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
        let mp = (5 * doy + 2) / 153;
        let d = doy - (153 * mp + 2) / 5 + 1;
        let m = if mp < 10 { mp + 3 } else { mp - 9 };
        let y = if m <= 2 { y + 1 } else { y };
        (y, m, d)
    }

    pub fn server_url_thread_count(protocol: &str, bind_addr: &String, thread_count: i32) -> String {
        let url = format!("Server is up and running at: {}://{}\n", protocol, &bind_addr);
        let thread_count = format!("Spawned {} thread(s) to handle incoming requests\n", thread_count);
        [url, thread_count].join("")
    }
}
/// The current second as ISO 8601 (`2026-10-29T12:00:00Z`), cached per
/// thread like the access log's.
#[doc(hidden)]
pub fn iso8601_now(use_it: impl FnOnce(&str)) {
    with_timestamps(|iso, _| use_it(iso));
}

/// The current second's ISO 8601 and Combined Log Format timestamps,
/// formatted at most once per second per thread.
fn with_timestamps(use_them: impl FnOnce(&str, &str)) {
    use std::cell::RefCell;
    use std::time::{SystemTime, UNIX_EPOCH};
    thread_local! {
        static CACHED: RefCell<(u64, String, String)> = const { RefCell::new((u64::MAX, String::new(), String::new())) };
    }
    let secs = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
    CACHED.with(|cached| {
        let mut cached = cached.borrow_mut();
        if cached.0 != secs {
            *cached = (secs, Log::format_iso8601(secs), Log::format_clf_timestamp(secs));
        }
        use_them(&cached.1, &cached.2);
    });
}

/// `s` JSON-escaped as the access log always did it: backslash, double
/// quote, newline and carriage return.
fn push_json_escaped(out: &mut Vec<u8>, s: &str) {
    for b in s.bytes() {
        match b {
            b'\\' => out.extend_from_slice(b"\\\\"),
            b'"' => out.extend_from_slice(b"\\\""),
            b'\n' => out.extend_from_slice(b"\\n"),
            b'\r' => out.extend_from_slice(b"\\r"),
            _ => out.push(b),
        }
    }
}

/// A line built only from `&str` pieces is UTF-8.
fn bytes_to_string(bytes: Vec<u8>) -> String {
    String::from_utf8(bytes).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
}