tower-http 0.7.0

Tower middleware and utilities for HTTP clients and servers
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
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
use super::{
    backend::{Backend, File as _, Metadata as _},
    headers::{ETag, IfMatch, IfModifiedSince, IfNoneMatch, IfUnmodifiedSince, LastModified},
    ServeVariant,
};
use crate::content_encoding::{Encoding, QValue};
use bytes::Bytes;
use http::{header, HeaderValue, Method, Request, Uri};
use http_body_util::Empty;
use http_range_header::RangeUnsatisfiableError;
use std::{
    ffi::OsStr,
    io::{self, ErrorKind, SeekFrom},
    ops::RangeInclusive,
    path::{Path, PathBuf},
};
use tokio::io::AsyncSeekExt;

pub(super) enum OpenFileOutput {
    FileOpened(Box<FileOpened>),
    Redirect {
        location: HeaderValue,
    },
    FileNotFound,
    PreconditionFailed,
    NotModified {
        etag: Option<ETag>,
        last_modified: Option<LastModified>,
    },
    InvalidRedirectUri,
    InvalidFilename,
}

pub(super) struct FileOpened {
    pub(super) extent: FileRequestExtent,
    pub(super) chunk_size: usize,
    pub(super) mime_header_value: HeaderValue,
    pub(super) maybe_encoding: Option<Encoding>,
    pub(super) maybe_range: Option<Result<Vec<RangeInclusive<u64>>, RangeUnsatisfiableError>>,
    pub(super) last_modified: Option<LastModified>,
    pub(super) precompression_configured: bool,
    pub(super) etag: Option<ETag>,
}

pub(super) enum FileRequestExtent {
    Full(Box<dyn tokio::io::AsyncRead + Unpin + Send>, u64),
    Head(u64),
}

pub(super) struct OpenFileRequest<B> {
    pub(super) variant: ServeVariant,
    pub(super) redirect_path_prefix: String,
    pub(super) path_to_file: PathBuf,
    pub(super) req: Request<Empty<Bytes>>,
    pub(super) negotiated_encodings: Vec<(Encoding, QValue)>,
    pub(super) range_header: Option<String>,
    pub(super) buf_chunk_size: usize,
    pub(super) precompression_configured: bool,
    pub(super) backend: B,
}

pub(super) async fn open_file<B: Backend>(
    request: OpenFileRequest<B>,
) -> io::Result<OpenFileOutput> {
    let OpenFileRequest {
        variant,
        redirect_path_prefix,
        mut path_to_file,
        req,
        negotiated_encodings,
        range_header,
        buf_chunk_size,
        precompression_configured,
        backend,
    } = request;
    let preconditions = Preconditions {
        if_match: req
            .headers()
            .get(header::IF_MATCH)
            .and_then(IfMatch::from_header_value),
        if_unmodified_since: req
            .headers()
            .get(header::IF_UNMODIFIED_SINCE)
            .and_then(IfUnmodifiedSince::from_header_value),
        if_none_match: req
            .headers()
            .get(header::IF_NONE_MATCH)
            .and_then(IfNoneMatch::from_header_value),
        if_modified_since: req
            .headers()
            .get(header::IF_MODIFIED_SINCE)
            .and_then(IfModifiedSince::from_header_value),
    };

    let mime = match variant {
        ServeVariant::Directory {
            append_index_html_on_directories,
            html_as_default_extension,
        } => {
            // Might already at this point know a redirect or not found result should be
            // returned which corresponds to a Some(output). Otherwise the path might be
            // modified and proceed to the open file/metadata future.
            if let Some(output) = maybe_redirect_or_append_path(
                &redirect_path_prefix,
                &mut path_to_file,
                req.uri(),
                append_index_html_on_directories,
                html_as_default_extension,
                &backend,
            )
            .await
            {
                return Ok(output);
            }

            mime_guess::from_path(&path_to_file)
                .first_raw()
                .map(HeaderValue::from_static)
                .unwrap_or_else(|| {
                    HeaderValue::from_str(mime::APPLICATION_OCTET_STREAM.as_ref()).unwrap()
                })
        }

        ServeVariant::SingleFile { mime } => mime,
    };

    if req.method() == Method::HEAD {
        #[cfg(feature = "tracing")]
        let _path_str = path_to_file.display().to_string();
        let (meta, maybe_encoding) =
            file_metadata_with_fallback(&backend, path_to_file, negotiated_encodings).await?;

        let last_modified = meta.modified().ok().map(LastModified::from);
        let etag = meta
            .modified()
            .ok()
            .and_then(|mtime| ETag::from_metadata(meta.len(), mtime));

        #[cfg(feature = "tracing")]
        if etag.is_none() {
            rate_limited!(
                std::time::Duration::from_secs(60),
                tracing::warn!(path = %_path_str, "ETag generation failed (mtime unavailable or pre-epoch)")
            );
        }

        if let Some(output) = preconditions.check(etag.as_ref(), last_modified.as_ref()) {
            return Ok(output);
        }

        let maybe_range = try_parse_range(range_header.as_deref(), meta.len());

        Ok(OpenFileOutput::FileOpened(Box::new(FileOpened {
            extent: FileRequestExtent::Head(meta.len()),
            chunk_size: buf_chunk_size,
            mime_header_value: mime,
            maybe_encoding,
            maybe_range,
            last_modified,
            precompression_configured,
            etag,
        })))
    } else {
        #[cfg(feature = "tracing")]
        let _path_str = path_to_file.display().to_string();
        let (mut file, maybe_encoding) =
            match open_file_with_fallback(&backend, path_to_file, negotiated_encodings).await {
                Ok(result) => result,

                Err(err) if is_invalid_filename_error(&err) => {
                    return Ok(OpenFileOutput::InvalidFilename)
                }
                Err(err) => return Err(err),
            };

        let meta = file.metadata().await?;

        let last_modified = meta.modified().ok().map(LastModified::from);
        let etag = meta
            .modified()
            .ok()
            .and_then(|mtime| ETag::from_metadata(meta.len(), mtime));

        #[cfg(feature = "tracing")]
        if etag.is_none() {
            rate_limited!(
                std::time::Duration::from_secs(60),
                tracing::warn!(path = %_path_str, "ETag generation failed (mtime unavailable or pre-epoch)")
            );
        }

        if let Some(output) = preconditions.check(etag.as_ref(), last_modified.as_ref()) {
            return Ok(output);
        }

        let size = meta.len();
        let maybe_range = try_parse_range(range_header.as_deref(), size);
        if let Some(Ok(ranges)) = maybe_range.as_ref() {
            // if there is any other amount of ranges than 1 we'll return an
            // unsatisfiable later as there isn't yet support for multipart ranges
            if ranges.len() == 1 {
                file.seek(SeekFrom::Start(*ranges[0].start())).await?;
            }
        }

        Ok(OpenFileOutput::FileOpened(Box::new(FileOpened {
            extent: FileRequestExtent::Full(Box::new(file), size),
            chunk_size: buf_chunk_size,
            mime_header_value: mime,
            maybe_encoding,
            maybe_range,
            last_modified,
            precompression_configured,
            etag,
        })))
    }
}

fn is_invalid_filename_error(err: &io::Error) -> bool {
    // Only applies to NULL bytes
    if err.kind() == ErrorKind::InvalidInput {
        return true;
    }

    // FIXME: Remove when MSRV >= 1.87.
    // `io::ErrorKind::InvalidFilename` is stabilized in v1.87
    #[cfg(windows)]
    if let Some(raw_err) = err.raw_os_error() {
        // https://github.com/rust-lang/rust/blob/70e2b4a4d197f154bed0eb3dcb5cac6a948ff3a3/library/std/src/sys/pal/windows/mod.rs
        // Lines 81 and 115
        if (raw_err == 123) || (raw_err == 161) || (raw_err == 206) {
            return true;
        }
    }

    false
}

/// Precondition headers parsed from the request.
struct Preconditions {
    if_match: Option<IfMatch>,
    if_unmodified_since: Option<IfUnmodifiedSince>,
    if_none_match: Option<IfNoneMatch>,
    if_modified_since: Option<IfModifiedSince>,
}

impl Preconditions {
    /// Evaluate preconditions per [RFC 9110 §13.2.2](https://www.rfc-editor.org/rfc/rfc9110#section-13.2.2).
    ///
    /// Precedence order:
    /// 1. If-Match (strong comparison) → 412 on failure
    /// 2. If-Unmodified-Since (only if If-Match absent) → 412 on failure
    /// 3. If-None-Match (weak comparison) → 304 on failure (for GET/HEAD)
    /// 4. If-Modified-Since (only if If-None-Match absent) → 304 on failure
    fn check(
        self,
        etag: Option<&ETag>,
        last_modified: Option<&LastModified>,
    ) -> Option<OpenFileOutput> {
        // Step 1: If-Match
        if let Some(if_match) = self.if_match {
            // RFC 9110 §13.1.1: "If the field value is '*', the condition is FALSE
            // if the origin server does not have a current representation."
            // No ETag means no current representation → fail.
            let passes = etag
                .map(|etag| if_match.precondition_passes(etag))
                .unwrap_or(false);
            if !passes {
                return Some(OpenFileOutput::PreconditionFailed);
            }
        } else {
            // Step 2: If-Unmodified-Since (only when If-Match is absent)
            // RFC 9110 §13.1.4: "MUST ignore if the resource does not have a
            // modification date available."
            if let Some(since) = self.if_unmodified_since {
                let passes = last_modified
                    .map(|lm| since.precondition_passes(lm))
                    .unwrap_or(true);
                if !passes {
                    return Some(OpenFileOutput::PreconditionFailed);
                }
            }
        }

        // Step 3: If-None-Match
        if let Some(if_none_match) = self.if_none_match {
            // No ETag available → condition is vacuously true (passes), serve normally.
            let passes = etag
                .map(|etag| if_none_match.precondition_passes(etag))
                .unwrap_or(true);
            if !passes {
                return Some(OpenFileOutput::NotModified {
                    etag: etag.cloned(),
                    last_modified: last_modified.map(|lm| LastModified(lm.0)),
                });
            }
        } else {
            // Step 4: If-Modified-Since (only when If-None-Match is absent)
            // No Last-Modified → treat as modified (serve normally).
            if let Some(since) = self.if_modified_since {
                let unmodified = last_modified
                    .map(|lm| !since.is_modified(lm))
                    .unwrap_or(false);
                if unmodified {
                    return Some(OpenFileOutput::NotModified {
                        etag: etag.cloned(),
                        last_modified: last_modified.map(|lm| LastModified(lm.0)),
                    });
                }
            }
        }

        None
    }
}

// Returns the preferred_encoding encoding and modifies the path extension
// to the corresponding file extension for the encoding.
fn preferred_encoding(
    path: &mut PathBuf,
    negotiated_encoding: &[(Encoding, QValue)],
) -> Option<Encoding> {
    let preferred_encoding = Encoding::preferred_encoding(negotiated_encoding.iter().copied());

    if let Some(file_extension) =
        preferred_encoding.and_then(|encoding| encoding.to_file_extension())
    {
        let new_file_name = path
            .file_name()
            .map(|file_name| {
                let mut os_string = file_name.to_os_string();
                os_string.push(file_extension);
                os_string
            })
            .unwrap_or_else(|| file_extension.to_os_string());

        path.set_file_name(new_file_name);
    }

    preferred_encoding
}

// Attempts to open the file with any of the possible negotiated_encodings in the
// preferred order. If none of the negotiated_encodings have a corresponding precompressed
// file the uncompressed file is used as a fallback.
async fn open_file_with_fallback<B: Backend>(
    backend: &B,
    mut path: PathBuf,
    mut negotiated_encoding: Vec<(Encoding, QValue)>,
) -> io::Result<(B::File, Option<Encoding>)> {
    let (file, encoding) = loop {
        // Get the preferred encoding among the negotiated ones.
        let encoding = preferred_encoding(&mut path, &negotiated_encoding);
        match (backend.open(path.clone()).await, encoding) {
            (Ok(file), maybe_encoding) => break (file, maybe_encoding),
            (Err(err), Some(encoding))
                if err.kind() == io::ErrorKind::NotFound && encoding != Encoding::Identity =>
            {
                // Remove the extension corresponding to a precompressed file (.gz, .br, .zz)
                // to reset the path before the next iteration.
                path.set_extension(OsStr::new(""));
                // Remove the encoding from the negotiated_encodings since the file doesn't exist
                negotiated_encoding
                    .retain(|(negotiated_encoding, _)| *negotiated_encoding != encoding);
            }
            (Err(err), _) => return Err(err),
        }
    };
    Ok((file, encoding))
}

// Attempts to get the file metadata with any of the possible negotiated_encodings in the
// preferred order. If none of the negotiated_encodings have a corresponding precompressed
// file the uncompressed file is used as a fallback.
async fn file_metadata_with_fallback<B: Backend>(
    backend: &B,
    mut path: PathBuf,
    mut negotiated_encoding: Vec<(Encoding, QValue)>,
) -> io::Result<(B::Metadata, Option<Encoding>)> {
    let (meta, encoding) = loop {
        // Get the preferred encoding among the negotiated ones.
        let encoding = preferred_encoding(&mut path, &negotiated_encoding);
        match (backend.metadata(path.clone()).await, encoding) {
            (Ok(meta), maybe_encoding) => break (meta, maybe_encoding),
            (Err(err), Some(encoding))
                if err.kind() == io::ErrorKind::NotFound && encoding != Encoding::Identity =>
            {
                // Remove the extension corresponding to a precompressed file (.gz, .br, .zz)
                // to reset the path before the next iteration.
                path.set_extension(OsStr::new(""));
                // Remove the encoding from the negotiated_encodings since the file doesn't exist
                negotiated_encoding
                    .retain(|(negotiated_encoding, _)| *negotiated_encoding != encoding);
            }
            (Err(err), _) => return Err(err),
        }
    };
    Ok((meta, encoding))
}

async fn maybe_redirect_or_append_path<B: Backend>(
    redirect_path_prefix: &str,
    path_to_file: &mut PathBuf,
    uri: &Uri,
    append_index_html_on_directories: bool,
    html_as_default_extension: bool,
    backend: &B,
) -> Option<OpenFileOutput> {
    let uri_path = uri.path();

    let is_directory = is_dir(path_to_file, backend).await;

    if uri_path.ends_with('/') && uri_path != "/" && is_directory != Some(true) {
        return Some(OpenFileOutput::FileNotFound);
    }

    // If the path has no extension and doesn't exist as a file, try appending .html
    if html_as_default_extension && is_directory.is_none() && path_to_file.extension().is_none() {
        path_to_file.set_extension("html");
        return None;
    }

    if is_directory != Some(true) {
        return None;
    }

    if !append_index_html_on_directories {
        return Some(OpenFileOutput::FileNotFound);
    }

    if uri_path.ends_with('/') {
        path_to_file.push("index.html");
        None
    } else {
        let uri = match append_slash_on_path(uri.clone(), redirect_path_prefix) {
            Ok(uri) => uri,
            Err(err) => return Some(err),
        };
        let location = HeaderValue::from_str(&uri.to_string()).unwrap();
        Some(OpenFileOutput::Redirect { location })
    }
}

fn try_parse_range(
    maybe_range_ref: Option<&str>,
    file_size: u64,
) -> Option<Result<Vec<RangeInclusive<u64>>, RangeUnsatisfiableError>> {
    maybe_range_ref.map(|header_value| {
        http_range_header::parse_range_header(header_value)
            .and_then(|first_pass| first_pass.validate(file_size))
    })
}

async fn is_dir<B: Backend>(path_to_file: &Path, backend: &B) -> Option<bool> {
    backend
        .metadata(path_to_file.to_owned())
        .await
        .ok()
        .map(|meta_data| meta_data.is_dir())
}

fn append_slash_on_path(uri: Uri, redirect_path_prefix: &str) -> Result<Uri, OpenFileOutput> {
    let http::uri::Parts {
        scheme,
        authority,
        path_and_query,
        ..
    } = uri.into_parts();

    let mut uri_builder = Uri::builder();

    if let Some(scheme) = scheme {
        uri_builder = uri_builder.scheme(scheme);
    }

    if let Some(authority) = authority {
        uri_builder = uri_builder.authority(authority);
    }

    let uri_builder = if let Some(path_and_query) = path_and_query {
        if let Some(query) = path_and_query.query() {
            uri_builder.path_and_query(format!(
                "{redirect_path_prefix}{}/?{}",
                path_and_query.path(),
                query
            ))
        } else {
            uri_builder.path_and_query(format!("{redirect_path_prefix}{}/", path_and_query.path()))
        }
    } else {
        uri_builder.path_and_query(format!("{redirect_path_prefix}/"))
    };

    uri_builder.build().map_err(|_err| {
        #[cfg(feature = "tracing")]
        tracing::error!(err = ?_err, "redirect uri failed to build");

        OpenFileOutput::InvalidRedirectUri
    })
}

#[test]
fn preferred_encoding_with_extension() {
    let mut path = PathBuf::from("hello.txt");
    preferred_encoding(&mut path, &[(Encoding::Gzip, QValue::one())]);
    assert_eq!(path, PathBuf::from("hello.txt.gz"));
}

#[test]
fn preferred_encoding_without_extension() {
    let mut path = PathBuf::from("hello");
    preferred_encoding(&mut path, &[(Encoding::Gzip, QValue::one())]);
    assert_eq!(path, PathBuf::from("hello.gz"));
}