static-web-server 3.0.0-beta.3

A cross-platform, high-performance and asynchronous web server for static files-serving.
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
// SPDX-License-Identifier: MIT OR Apache-2.0
// This file is part of Static Web Server.
// See https://static-web-server.net/ for more information
// Copyright (C) 2019-present Jose Quintana <joseluisq.net>

//! Configurable access logging with format string support.
//!
//! Supports a `%{token}` syntax for user-defined log formats.
//! When no format is configured, the line is logged with structured fields instead.
//!
//! Values taken from the request (URI, `Host`, `User-Agent`, `Referer`) are client-controlled.
//! Bytes outside printable ASCII, `"` and `\` in them are written as `\xHH`, so a client cannot
//! end a quoted field, start a new log line or inject terminal escape sequences. An escaped
//! value longer than 4 KiB is cut and ends with `...`, so a large header cannot make a large line.
//!
//! The line is written when hyper drops the response body: once it is sent, or when the client
//! goes away. So `%{bytes}` and `%{duration}` cover the whole transfer.

use bytes::Bytes;
use http::uri::Authority;
use http_body::{Frame, SizeHint};
use http_body_util::BodyExt;
use hyper::header::{HOST, HeaderValue, REFERER, USER_AGENT};
use hyper::{Method, Request, Response, StatusCode, Uri, Version};
use std::fmt::Write;
use std::io;
use std::net::{IpAddr, SocketAddr};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll, ready};
use std::time::Instant;
use time::{OffsetDateTime, format_description::BorrowedFormatItem, macros::format_description};

use crate::{body::Body, handler::RequestHandlerOpts, health, log_addr};

/// A compiled access log format, parsed once at startup.
#[derive(Debug, Clone)]
pub struct AccessLogFormat {
    segments: Arc<[Segment]>,
}

/// A single segment of a compiled format string.
#[derive(Debug)]
enum Segment {
    Literal(String),
    Token(Token),
}

/// Available log format tokens.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Token {
    Method,
    Uri,
    Status,
    Bytes,
    Duration,
    RemoteAddr,
    XRealIp,
    ForwardedFor,
    Host,
    UserAgent,
    Referer,
    Timestamp,
    Version,
}

impl Token {
    fn from_name(name: &str) -> Option<Self> {
        match name {
            "method" => Some(Self::Method),
            "uri" => Some(Self::Uri),
            "status" => Some(Self::Status),
            "bytes" => Some(Self::Bytes),
            "duration" => Some(Self::Duration),
            "remote_addr" => Some(Self::RemoteAddr),
            "x_real_ip" => Some(Self::XRealIp),
            "forwarded_for" => Some(Self::ForwardedFor),
            "host" => Some(Self::Host),
            "user_agent" => Some(Self::UserAgent),
            "referer" => Some(Self::Referer),
            "timestamp" => Some(Self::Timestamp),
            "version" => Some(Self::Version),
            _ => None,
        }
    }
}

impl AccessLogFormat {
    /// Parses a format string into a compiled representation.
    ///
    /// Tokens use `%{name}` syntax. Use `%%` for a literal `%`.
    pub fn parse(format: &str) -> crate::Result<Self> {
        let mut segments = Vec::new();
        let mut literal = String::new();
        let mut chars = format.chars();

        while let Some(c) = chars.next() {
            if c != '%' {
                literal.push(c);
                continue;
            }
            match chars.next() {
                Some('%') => literal.push('%'),
                Some('{') => {
                    let mut name = String::new();
                    let mut closed = false;
                    for ch in chars.by_ref() {
                        if ch == '}' {
                            closed = true;
                            break;
                        }
                        name.push(ch);
                    }
                    if !closed {
                        bail!("access log format: unclosed '%{{{name}'");
                    }
                    let token = Token::from_name(&name)
                        .ok_or_else(|| anyhow!("access log format: unknown token '%{{{name}}}'"))?;
                    if !literal.is_empty() {
                        segments.push(Segment::Literal(std::mem::take(&mut literal)));
                    }
                    segments.push(Segment::Token(token));
                }
                Some(other) => bail!(
                    "access log format: unexpected character '{other}' after '%'; \
                     use %{{name}} for tokens or %% for a literal %"
                ),
                None => bail!("access log format: trailing '%' at end of string"),
            }
        }

        if !literal.is_empty() {
            segments.push(Segment::Literal(literal));
        }

        Ok(Self {
            segments: segments.into(),
        })
    }

    /// Whether the format logs `X-Real-IP` or `X-Forwarded-For`.
    pub(crate) fn uses_proxy_headers(&self) -> bool {
        self.segments
            .iter()
            .any(|seg| matches!(seg, Segment::Token(Token::XRealIp | Token::ForwardedFor)))
    }

    /// Renders the format using the given request/response context.
    fn render(&self, ctx: &LogContext<'_>) -> String {
        let mut buf = String::with_capacity(256);
        for seg in self.segments.iter() {
            match seg {
                Segment::Literal(s) => buf.push_str(s),
                Segment::Token(token) => render_token(&mut buf, *token, ctx),
            }
        }
        buf
    }
}

/// The target of the default line, kept from `log_addr` for log filters.
const DEFAULT_TARGET: &str = "static_web_server::log_addr";

/// Whether the line of `req` passes the log level filter, so it is worth building.
pub(crate) fn enabled<T>(opts: &RequestHandlerOpts, req: &Request<T>) -> bool {
    // Health check requests are logged in debug mode only
    if opts.health && health::is_health_endpoint(req.uri()) {
        tracing::enabled!(tracing::Level::DEBUG)
    } else {
        tracing::enabled!(tracing::Level::INFO)
    }
}

/// The request values of an access log line, copied before the pipeline runs: rewrites replace
/// the URI and `Host`, and the line is written after the handler returns.
#[derive(Debug)]
pub(crate) struct AccessLog {
    /// `None` logs the default line with structured fields.
    format: Option<AccessLogFormat>,
    method: Method,
    uri: Uri,
    host: Option<HeaderValue>,
    version: Version,
    user_agent: Option<HeaderValue>,
    referer: Option<HeaderValue>,
    remote_addr: Option<SocketAddr>,
    x_real_ip: Option<IpAddr>,
    forwarded_for: Option<IpAddr>,
    health: bool,
    start: Instant,
}

impl AccessLog {
    /// Copies the request values the access log uses.
    pub(crate) fn new<T>(
        format: Option<&AccessLogFormat>,
        opts: &RequestHandlerOpts,
        req: &Request<T>,
        remote_addr: Option<SocketAddr>,
    ) -> Self {
        let headers = req.headers();
        // The default line logs only the addresses its options enable
        let custom = format.is_some();
        Self {
            format: format.cloned(),
            method: req.method().clone(),
            uri: req.uri().clone(),
            host: headers.get(HOST).cloned(),
            version: req.version(),
            user_agent: headers.get(USER_AGENT).cloned(),
            referer: headers.get(REFERER).cloned(),
            remote_addr: remote_addr.filter(|_| custom || opts.log_remote_address),
            x_real_ip: (custom || opts.log_x_real_ip)
                .then(|| log_addr::x_real_ip(opts, req, remote_addr))
                .flatten(),
            forwarded_for: (custom || opts.log_forwarded_for)
                .then(|| log_addr::forwarded_for(opts, req, remote_addr))
                .flatten(),
            health: opts.health && health::is_health_endpoint(req.uri()),
            start: Instant::now(),
        }
    }

    /// Wraps the response body, so the line is logged when hyper drops it.
    pub(crate) fn wrap(self, resp: Response<Body>) -> Response<Body> {
        let status = resp.status();
        // Like CLF `%b`, these log `-` since no body is sent
        let sends_body = self.method != Method::HEAD
            && status != StatusCode::NO_CONTENT
            && status != StatusCode::NOT_MODIFIED;
        resp.map(|inner| {
            LoggedBody {
                inner,
                log: self,
                status: status.as_u16(),
                bytes: 0,
                sends_body,
            }
            .boxed()
        })
    }

    fn write(&self, status: u16, bytes: u64) {
        let Some(format) = &self.format else {
            self.write_default(status);
            return;
        };
        let ctx = LogContext {
            method: self.method.as_str(),
            uri: &self.uri,
            host: self.host.as_ref(),
            version: http_version_str(self.version),
            status,
            bytes,
            duration_secs: self.start.elapsed().as_secs_f64(),
            remote_addr: self.remote_addr,
            user_agent: self.user_agent.as_ref(),
            referer: self.referer.as_ref(),
            x_real_ip: self.x_real_ip,
            forwarded_for: self.forwarded_for,
        };
        let line = format.render(&ctx);

        // Log health check requests in debug mode only
        if self.health {
            tracing::debug!("{line}");
        } else {
            tracing::info!("{line}");
        }
    }

    fn write_default(&self, status: u16) {
        let method = self.method.as_str();
        let uri = request_target(&self.uri);
        let remote_addr = self.remote_addr.map(|addr| addr.ip());
        let remote_addr = remote_addr.as_ref().map(tracing::field::display);
        let x_real_ip = self.x_real_ip.as_ref().map(tracing::field::display);
        let real_remote_ip = self.forwarded_for.as_ref().map(tracing::field::display);

        // Log health check requests in debug mode only
        if self.health {
            tracing::debug!(
                target: DEFAULT_TARGET,
                method,
                uri,
                status,
                remote_addr,
                x_real_ip,
                real_remote_ip,
                "response"
            );
        } else {
            tracing::info!(
                target: DEFAULT_TARGET,
                method,
                uri,
                status,
                remote_addr,
                x_real_ip,
                real_remote_ip,
                "response"
            );
        }
    }
}

/// A response body that counts the data bytes it yields and writes the access log line on drop.
struct LoggedBody {
    inner: Body,
    log: AccessLog,
    status: u16,
    bytes: u64,
    sends_body: bool,
}

impl http_body::Body for LoggedBody {
    type Data = Bytes;
    type Error = io::Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Bytes>, io::Error>>> {
        let this = self.get_mut();
        let frame = ready!(Pin::new(&mut this.inner).poll_frame(cx));
        if let Some(data) = frame.as_ref().and_then(|f| f.as_ref().ok()?.data_ref()) {
            this.bytes += data.len() as u64;
        }
        Poll::Ready(frame)
    }

    fn is_end_stream(&self) -> bool {
        self.inner.is_end_stream()
    }

    fn size_hint(&self) -> SizeHint {
        self.inner.size_hint()
    }
}

impl Drop for LoggedBody {
    fn drop(&mut self) {
        let bytes = if self.sends_body { self.bytes } else { 0 };
        self.log.write(self.status, bytes);
    }
}

/// All data available for access log rendering.
struct LogContext<'a> {
    method: &'a str,
    uri: &'a Uri,
    host: Option<&'a HeaderValue>,
    version: &'a str,
    status: u16,
    bytes: u64,
    duration_secs: f64,
    remote_addr: Option<SocketAddr>,
    user_agent: Option<&'a HeaderValue>,
    referer: Option<&'a HeaderValue>,
    x_real_ip: Option<IpAddr>,
    forwarded_for: Option<IpAddr>,
}

fn render_token(buf: &mut String, token: Token, ctx: &LogContext<'_>) {
    match token {
        Token::Method => buf.push_str(ctx.method),
        Token::Uri => push_escaped(buf, request_target(ctx.uri).as_bytes()),
        Token::Version => buf.push_str(ctx.version),
        Token::Status => {
            let _ = write!(buf, "{}", ctx.status);
        }
        Token::Bytes => match ctx.bytes {
            0 => buf.push('-'),
            bytes => {
                let _ = write!(buf, "{bytes}");
            }
        },
        Token::Duration => {
            let _ = write!(buf, "{:.6}", ctx.duration_secs);
        }
        Token::RemoteAddr => push_ip(buf, ctx.remote_addr.map(|addr| addr.ip())),
        Token::XRealIp => push_ip(buf, ctx.x_real_ip),
        Token::ForwardedFor => push_ip(buf, ctx.forwarded_for),
        Token::Host => push_value(buf, request_host(ctx.uri, ctx.host)),
        Token::UserAgent => push_value(buf, ctx.user_agent.map(HeaderValue::as_bytes)),
        Token::Referer => push_value(buf, ctx.referer.map(HeaderValue::as_bytes)),
        Token::Timestamp => {
            // Local time, respecting the TZ environment variable, or UTC if the offset is unknown
            let now = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
            push_clf_time(buf, now);
        }
    }
}

/// Common Log Format time, e.g. `29/May/2026:23:43:47 +0200`.
const CLF_TIME: &[BorrowedFormatItem<'_>] = format_description!(
    "[day]/[month repr:short]/[year]:[hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
);

fn push_clf_time(buf: &mut String, time: OffsetDateTime) {
    match time.format(CLF_TIME) {
        Ok(time) => buf.push_str(&time),
        Err(_) => buf.push('-'),
    }
}

fn push_ip(buf: &mut String, ip: Option<IpAddr>) {
    match ip {
        Some(ip) => {
            let _ = write!(buf, "{ip}");
        }
        None => buf.push('-'),
    }
}

/// The host the client asked for, as `virtual_hosts` resolves it: the URI authority (HTTP/2
/// `:authority` or an absolute-form target) without userinfo, else the `Host` header.
fn request_host<'a>(uri: &'a Uri, host: Option<&'a HeaderValue>) -> Option<&'a [u8]> {
    match uri.authority() {
        Some(authority) => Some(host_port(authority).as_bytes()),
        None => host.map(HeaderValue::as_bytes),
    }
}

/// The path and query of `uri`, without the scheme and authority of an absolute-form or
/// HTTP/2 request URI.
fn request_target(uri: &Uri) -> &str {
    match (uri.path_and_query(), uri.authority()) {
        (Some(path), _) => path.as_str(),
        // Authority-form `CONNECT` target, e.g. `example.com:443`
        (None, Some(authority)) if uri.scheme().is_none() => host_port(authority),
        _ => "/",
    }
}

/// The authority without userinfo.
fn host_port(authority: &Authority) -> &str {
    let authority = authority.as_str();
    authority.rsplit_once('@').map_or(authority, |(_, h)| h)
}

fn push_value(buf: &mut String, value: Option<&[u8]>) {
    match value {
        Some(value) if !value.is_empty() => push_escaped(buf, value),
        _ => buf.push('-'),
    }
}

/// Maximum length of an escaped value, before the `...` that marks it as cut.
const MAX_ESCAPED_LEN: usize = 4096;

/// Appends a client-controlled value, writing bytes outside printable ASCII, `"` and `\` as `\xHH`.
/// The escaped value is cut at [`MAX_ESCAPED_LEN`] and ends with `...`, never inside an `\xHH`.
fn push_escaped(buf: &mut String, value: &[u8]) {
    let start = buf.len();
    for &b in value {
        let printable = (0x20..=0x7e).contains(&b) && b != b'"' && b != b'\\';
        let len = if printable { 1 } else { 4 };
        if buf.len() - start + len > MAX_ESCAPED_LEN {
            buf.push_str("...");
            return;
        }
        if printable {
            buf.push(b as char);
        } else {
            let _ = write!(buf, "\\x{b:02X}");
        }
    }
}

fn http_version_str(version: hyper::Version) -> &'static str {
    match version {
        hyper::Version::HTTP_09 => "HTTP/0.9",
        hyper::Version::HTTP_10 => "HTTP/1.0",
        hyper::Version::HTTP_11 => "HTTP/1.1",
        hyper::Version::HTTP_2 => "HTTP/2",
        hyper::Version::HTTP_3 => "HTTP/3",
        _ => "HTTP/?",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hyper::HeaderMap;
    use proptest::prelude::*;
    use std::net::Ipv4Addr;
    use time::macros::datetime;

    fn context<'a>(uri: &'a Uri, headers: &'a HeaderMap) -> LogContext<'a> {
        LogContext {
            method: Method::GET.as_str(),
            uri,
            host: headers.get(HOST),
            version: "HTTP/1.1",
            status: 200,
            bytes: 0,
            duration_secs: 0.0,
            remote_addr: None,
            user_agent: headers.get(USER_AGENT),
            referer: headers.get(REFERER),
            x_real_ip: None,
            forwarded_for: None,
        }
    }

    #[test]
    fn parse_empty() {
        let fmt = AccessLogFormat::parse("").unwrap();
        assert!(fmt.segments.is_empty());
    }

    #[test]
    fn parse_literal_only() {
        let fmt = AccessLogFormat::parse("hello world").unwrap();
        assert_eq!(fmt.segments.len(), 1);
        assert!(matches!(&fmt.segments[0], Segment::Literal(s) if s == "hello world"));
    }

    #[test]
    fn parse_keeps_non_ascii_literals() {
        let fmt = AccessLogFormat::parse("→ %{status} ✓").unwrap();
        assert!(matches!(&fmt.segments[0], Segment::Literal(s) if s == "→ "));
        assert!(matches!(&fmt.segments[2], Segment::Literal(s) if s == " ✓"));
    }

    #[test]
    fn parse_single_token() {
        let fmt = AccessLogFormat::parse("%{method}").unwrap();
        assert_eq!(fmt.segments.len(), 1);
        assert!(matches!(&fmt.segments[0], Segment::Token(Token::Method)));
    }

    #[test]
    fn parse_mixed() {
        let fmt = AccessLogFormat::parse("%{remote_addr} \"%{method} %{uri}\" %{status}").unwrap();
        assert_eq!(fmt.segments.len(), 7);
        assert!(matches!(
            &fmt.segments[0],
            Segment::Token(Token::RemoteAddr)
        ));
        assert!(matches!(&fmt.segments[1], Segment::Literal(s) if s == " \""));
        assert!(matches!(&fmt.segments[2], Segment::Token(Token::Method)));
        assert!(matches!(&fmt.segments[3], Segment::Literal(s) if s == " "));
        assert!(matches!(&fmt.segments[4], Segment::Token(Token::Uri)));
        assert!(matches!(&fmt.segments[5], Segment::Literal(s) if s == "\" "));
        assert!(matches!(&fmt.segments[6], Segment::Token(Token::Status)));
    }

    #[test]
    fn parse_percent_escape() {
        let fmt = AccessLogFormat::parse("100%% done").unwrap();
        assert_eq!(fmt.segments.len(), 1);
        assert!(matches!(&fmt.segments[0], Segment::Literal(s) if s == "100% done"));
    }

    #[test]
    fn parse_all_tokens() {
        let all = "%{method} %{uri} %{status} %{bytes} %{duration} %{remote_addr} \
                   %{x_real_ip} %{forwarded_for} %{host} %{user_agent} %{referer} \
                   %{timestamp} %{version}";
        let fmt = AccessLogFormat::parse(all).unwrap();
        let token_count = fmt
            .segments
            .iter()
            .filter(|s| matches!(s, Segment::Token(_)))
            .count();
        assert_eq!(token_count, 13);
    }

    #[test]
    fn uses_proxy_headers_only_with_proxy_tokens() {
        let uses = |f: &str| AccessLogFormat::parse(f).unwrap().uses_proxy_headers();
        assert!(uses("%{x_real_ip}"));
        assert!(uses("%{method} %{forwarded_for}"));
        assert!(!uses("%{remote_addr} x_real_ip forwarded_for"));
    }

    #[test]
    fn parse_unknown_token_fails() {
        let result = AccessLogFormat::parse("%{bogus}");
        assert!(result.unwrap_err().to_string().contains("bogus"));
    }

    #[test]
    fn parse_empty_token_fails() {
        let result = AccessLogFormat::parse("%{}");
        assert!(result.unwrap_err().to_string().contains("unknown token"));
    }

    #[test]
    fn parse_unclosed_brace_fails() {
        let result = AccessLogFormat::parse("%{method");
        assert!(result.unwrap_err().to_string().contains("unclosed"));
    }

    #[test]
    fn parse_unclosed_brace_after_a_closed_one_fails() {
        let result = AccessLogFormat::parse("%{method} %{method");
        assert!(result.unwrap_err().to_string().contains("unclosed"));
    }

    #[test]
    fn parse_trailing_percent_fails() {
        let result = AccessLogFormat::parse("hello%");
        assert!(result.is_err());
    }

    #[test]
    fn parse_bare_percent_char_fails() {
        let result = AccessLogFormat::parse("%x");
        assert!(result.is_err());
    }

    #[test]
    fn render_basic_format() {
        let fmt = AccessLogFormat::parse("%{method} %{uri} %{status} %{bytes}").unwrap();
        let uri = Uri::from_static("/index.html");
        let headers = HeaderMap::new();
        let ctx = LogContext {
            bytes: 1024,
            remote_addr: Some(SocketAddr::new(
                IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)),
                54321,
            )),
            ..context(&uri, &headers)
        };
        assert_eq!(fmt.render(&ctx), "GET /index.html 200 1024");
    }

    #[test]
    fn render_uri_logs_the_path_and_query_only() {
        // Over HTTP/2 the request URI carries the scheme and authority
        let fmt = AccessLogFormat::parse("%{uri}").unwrap();
        let uri = Uri::from_static("https://127.0.0.1:8443/a/b.css?v=1");
        let headers = HeaderMap::new();
        assert_eq!(fmt.render(&context(&uri, &headers)), "/a/b.css?v=1");
    }

    #[test]
    fn render_uri_logs_the_connect_authority() {
        let fmt = AccessLogFormat::parse("%{uri}").unwrap();
        let headers = HeaderMap::new();
        let cases = [
            ("example.com:443", "example.com:443"),
            ("user:secret@example.com:443", "example.com:443"),
            ("http://example.com", "/"),
        ];
        for (uri, target) in cases {
            let uri = Uri::from_static(uri);
            assert_eq!(fmt.render(&context(&uri, &headers)), target, "{uri}");
        }
    }

    #[test]
    fn render_missing_values_show_dash() {
        let fmt = AccessLogFormat::parse(
            "%{remote_addr} %{x_real_ip} %{forwarded_for} %{host} %{user_agent} %{referer} %{bytes}",
        )
        .unwrap();
        let uri = Uri::from_static("/");
        let headers = HeaderMap::new();
        assert_eq!(fmt.render(&context(&uri, &headers)), "- - - - - - -");
    }

    #[test]
    fn render_percent_literal() {
        let fmt = AccessLogFormat::parse("100%% %{method}").unwrap();
        let uri = Uri::from_static("/");
        let headers = HeaderMap::new();
        assert_eq!(fmt.render(&context(&uri, &headers)), "100% GET");
    }

    #[test]
    fn render_escapes_client_controlled_values() {
        let fmt = AccessLogFormat::parse("\"%{user_agent}\" \"%{referer}\"").unwrap();
        let uri = Uri::from_static("/");
        let mut headers = HeaderMap::new();
        headers.insert(
            hyper::header::USER_AGENT,
            "curl\" 200 \\ \tx".parse().unwrap(),
        );
        headers.insert(
            hyper::header::REFERER,
            hyper::header::HeaderValue::from_bytes(b"caf\xc3\xa9").unwrap(),
        );
        assert_eq!(
            fmt.render(&context(&uri, &headers)),
            r#""curl\x22 200 \x5C \x09x" "caf\xC3\xA9""#
        );
    }

    #[test]
    fn render_host_prefers_the_uri_authority() {
        let fmt = AccessLogFormat::parse("%{host}").unwrap();
        let mut headers = HeaderMap::new();
        headers.insert(HOST, "other.example".parse().unwrap());
        let cases = [
            ("http://vhost.example/", "vhost.example"),
            ("https://example.com:8443/", "example.com:8443"),
            ("http://[::1]:8080/", "[::1]:8080"),
            ("http://user:secret@example.com/", "example.com"),
            ("/", "other.example"),
        ];
        for (uri, host) in cases {
            let uri = Uri::from_static(uri);
            assert_eq!(fmt.render(&context(&uri, &headers)), host, "{uri}");
        }
    }

    #[test]
    fn push_escaped_cuts_long_values() {
        let mut buf = String::new();
        push_escaped(&mut buf, "a".repeat(MAX_ESCAPED_LEN).as_bytes());
        assert_eq!(buf, "a".repeat(MAX_ESCAPED_LEN));

        let mut buf = String::new();
        push_escaped(&mut buf, "a".repeat(MAX_ESCAPED_LEN + 1).as_bytes());
        assert_eq!(buf, format!("{}...", "a".repeat(MAX_ESCAPED_LEN)));
    }

    #[test]
    fn push_escaped_never_cuts_an_escape() {
        let mut buf = String::new();
        push_escaped(&mut buf, &[0xff; MAX_ESCAPED_LEN / 4]);
        assert_eq!(buf, "\\xFF".repeat(MAX_ESCAPED_LEN / 4));

        let mut value = "a".repeat(MAX_ESCAPED_LEN - 3).into_bytes();
        value.push(0xff);
        let mut buf = String::from("ua=");
        push_escaped(&mut buf, &value);
        assert_eq!(buf, format!("ua={}...", "a".repeat(MAX_ESCAPED_LEN - 3)));
    }

    #[test]
    fn render_all_tokens() {
        let fmt = AccessLogFormat::parse(
            "%{method}|%{uri}|%{version}|%{status}|%{bytes}|%{duration}|%{remote_addr}|\
             %{x_real_ip}|%{forwarded_for}|%{host}|%{user_agent}|%{referer}",
        )
        .unwrap();
        let uri = Uri::from_static("/a/b.css?v=1");
        let mut headers = HeaderMap::new();
        headers.insert(hyper::header::HOST, "example.com".parse().unwrap());
        headers.insert(hyper::header::USER_AGENT, "curl/8.0".parse().unwrap());
        headers.insert(
            hyper::header::REFERER,
            "https://example.com/".parse().unwrap(),
        );
        let ctx = LogContext {
            version: http_version_str(hyper::Version::HTTP_2),
            status: 304,
            bytes: 1024,
            duration_secs: 0.0012345,
            remote_addr: Some("[2001:db8::1]:443".parse().unwrap()),
            x_real_ip: Some(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 7))),
            forwarded_for: Some(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195))),
            ..context(&uri, &headers)
        };
        assert_eq!(
            fmt.render(&ctx),
            "GET|/a/b.css?v=1|HTTP/2|304|1024|0.001234|2001:db8::1|\
             203.0.113.7|203.0.113.195|example.com|curl/8.0|https://example.com/"
        );
    }

    #[test]
    fn render_timestamp_uses_the_common_log_format() {
        let fmt = AccessLogFormat::parse("[%{timestamp}]").unwrap();
        let uri = Uri::from_static("/");
        let headers = HeaderMap::new();
        let line = fmt.render(&context(&uri, &headers));
        let clf =
            regex_lite::Regex::new(r"^\[\d{2}/[A-Z][a-z]{2}/\d{4}:\d{2}:\d{2}:\d{2} [+-]\d{4}\]$")
                .unwrap();
        assert!(clf.is_match(&line), "{line}");
    }

    #[test]
    fn push_clf_time_formats_the_offset() {
        let cases = [
            (
                datetime!(2026-05-29 23:43:47 +02:00),
                "29/May/2026:23:43:47 +0200",
            ),
            (
                datetime!(2026-01-02 03:04:05 -05:30),
                "02/Jan/2026:03:04:05 -0530",
            ),
            (
                datetime!(2026-12-31 00:00:00 UTC),
                "31/Dec/2026:00:00:00 +0000",
            ),
        ];
        for (time, expected) in cases {
            let mut buf = String::new();
            push_clf_time(&mut buf, time);
            assert_eq!(buf, expected);
        }
    }

    proptest! {
        /// Escaped values never contain a byte that could end a quoted field, start a new line or
        /// form a terminal escape sequence, and they decode back to the original bytes.
        #[test]
        fn prop_push_escaped_is_printable_and_lossless(value in proptest::collection::vec(any::<u8>(), 0..64)) {
            let mut buf = String::new();
            push_escaped(&mut buf, &value);

            let mut decoded = Vec::new();
            let mut rest = buf.as_bytes();
            while let Some((&b, tail)) = rest.split_first() {
                prop_assert!((0x20..=0x7e).contains(&b) && b != b'"', "{buf}");
                if b == b'\\' {
                    prop_assert_eq!(tail.first(), Some(&b'x'), "{}", buf);
                    let hex = std::str::from_utf8(&tail[1..3]).unwrap();
                    decoded.push(u8::from_str_radix(hex, 16).unwrap());
                    rest = &tail[3..];
                } else {
                    decoded.push(b);
                    rest = tail;
                }
            }
            prop_assert_eq!(decoded, value);
        }

        /// Escaped values stop at `MAX_ESCAPED_LEN` plus `...`, after the longest prefix that fits.
        #[test]
        fn prop_push_escaped_is_bounded(value in proptest::collection::vec(any::<u8>(), 0..2048)) {
            let width = |b: u8| {
                if (0x20..=0x7e).contains(&b) && b != b'"' && b != b'\\' { 1 } else { 4 }
            };
            let mut buf = String::new();
            push_escaped(&mut buf, &value);

            let full_len: usize = value.iter().map(|&b| width(b)).sum();
            if full_len <= MAX_ESCAPED_LEN {
                prop_assert_eq!(buf.len(), full_len);
            } else {
                let body = buf.strip_suffix("...").expect("a cut value ends with `...`");
                prop_assert!(body.len() <= MAX_ESCAPED_LEN);
                let kept = body.len() - 3 * body.matches('\\').count();
                prop_assert!(body.len() + width(value[kept]) > MAX_ESCAPED_LEN);
            }
        }
    }
}