itsi-server 0.1.1 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of itsi-server might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +7 -0
- data/Cargo.lock +4417 -0
- data/Cargo.toml +7 -0
- data/README.md +4 -0
- data/Rakefile +8 -1
- data/_index.md +6 -0
- data/exe/itsi +94 -45
- data/ext/itsi_error/Cargo.toml +2 -0
- data/ext/itsi_error/src/from.rs +68 -0
- data/ext/itsi_error/src/lib.rs +18 -34
- data/ext/itsi_error/target/debug/build/clang-sys-da71b0344e568175/out/common.rs +355 -0
- data/ext/itsi_error/target/debug/build/clang-sys-da71b0344e568175/out/dynamic.rs +276 -0
- data/ext/itsi_error/target/debug/build/clang-sys-da71b0344e568175/out/macros.rs +49 -0
- data/ext/itsi_error/target/debug/build/rb-sys-49f554618693db24/out/bindings-0.9.110-mri-arm64-darwin23-3.4.2.rs +8865 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-1mmt5sux7jb0i/s-h510z7m8v9-0bxu7yd.lock +0 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-2vn3jey74oiw0/s-h5113n0e7e-1v5qzs6.lock +0 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-37uv9dicz7awp/s-h510ykifhe-0tbnep2.lock +0 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-37uv9dicz7awp/s-h510yyocpj-0tz7ug7.lock +0 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-37uv9dicz7awp/s-h510z0xc8g-14ol18k.lock +0 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-3g5qf4y7d54uj/s-h5113n0e7d-1trk8on.lock +0 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-3lpfftm45d3e2/s-h510z7m8r3-1pxp20o.lock +0 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-3o4qownhl3d7n/s-h510ykifek-1uxasnk.lock +0 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-3o4qownhl3d7n/s-h510yyocki-11u37qm.lock +0 -0
- data/ext/itsi_error/target/debug/incremental/itsi_error-3o4qownhl3d7n/s-h510z0xc93-0pmy0zm.lock +0 -0
- data/ext/itsi_instrument_entry/Cargo.toml +15 -0
- data/ext/itsi_instrument_entry/src/lib.rs +31 -0
- data/ext/itsi_rb_helpers/Cargo.toml +3 -0
- data/ext/itsi_rb_helpers/src/heap_value.rs +139 -0
- data/ext/itsi_rb_helpers/src/lib.rs +140 -10
- data/ext/itsi_rb_helpers/target/debug/build/clang-sys-da71b0344e568175/out/common.rs +355 -0
- data/ext/itsi_rb_helpers/target/debug/build/clang-sys-da71b0344e568175/out/dynamic.rs +276 -0
- data/ext/itsi_rb_helpers/target/debug/build/clang-sys-da71b0344e568175/out/macros.rs +49 -0
- data/ext/itsi_rb_helpers/target/debug/build/rb-sys-eb9ed4ff3a60f995/out/bindings-0.9.110-mri-arm64-darwin23-3.4.2.rs +8865 -0
- data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-040pxg6yhb3g3/s-h5113n7a1b-03bwlt4.lock +0 -0
- data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-131g1u4dzkt1a/s-h51113xnh3-1eik1ip.lock +0 -0
- data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-131g1u4dzkt1a/s-h5111704jj-0g4rj8x.lock +0 -0
- data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-1q2d3drtxrzs5/s-h5113n79yl-0bxcqc5.lock +0 -0
- data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-374a9h7ovycj0/s-h51113xoox-10de2hp.lock +0 -0
- data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-374a9h7ovycj0/s-h5111704w7-0vdq7gq.lock +0 -0
- data/ext/itsi_scheduler/Cargo.toml +24 -0
- data/ext/itsi_scheduler/src/itsi_scheduler/io_helpers.rs +56 -0
- data/ext/itsi_scheduler/src/itsi_scheduler/io_waiter.rs +44 -0
- data/ext/itsi_scheduler/src/itsi_scheduler/timer.rs +44 -0
- data/ext/itsi_scheduler/src/itsi_scheduler.rs +308 -0
- data/ext/itsi_scheduler/src/lib.rs +38 -0
- data/ext/itsi_server/Cargo.lock +2956 -0
- data/ext/itsi_server/Cargo.toml +73 -13
- data/ext/itsi_server/extconf.rb +1 -1
- data/ext/itsi_server/src/env.rs +43 -0
- data/ext/itsi_server/src/lib.rs +100 -40
- data/ext/itsi_server/src/ruby_types/itsi_body_proxy/big_bytes.rs +109 -0
- data/ext/itsi_server/src/ruby_types/itsi_body_proxy/mod.rs +141 -0
- data/ext/itsi_server/src/ruby_types/itsi_grpc_request.rs +147 -0
- data/ext/itsi_server/src/ruby_types/itsi_grpc_response.rs +19 -0
- data/ext/itsi_server/src/ruby_types/itsi_grpc_stream/mod.rs +216 -0
- data/ext/itsi_server/src/ruby_types/itsi_http_request.rs +282 -0
- data/ext/itsi_server/src/ruby_types/itsi_http_response.rs +388 -0
- data/ext/itsi_server/src/ruby_types/itsi_server/file_watcher.rs +225 -0
- data/ext/itsi_server/src/ruby_types/itsi_server/itsi_server_config.rs +355 -0
- data/ext/itsi_server/src/ruby_types/itsi_server.rs +82 -0
- data/ext/itsi_server/src/ruby_types/mod.rs +55 -0
- data/ext/itsi_server/src/server/bind.rs +75 -31
- data/ext/itsi_server/src/server/bind_protocol.rs +37 -0
- data/ext/itsi_server/src/server/byte_frame.rs +32 -0
- data/ext/itsi_server/src/server/cache_store.rs +74 -0
- data/ext/itsi_server/src/server/io_stream.rs +104 -0
- data/ext/itsi_server/src/server/itsi_service.rs +172 -0
- data/ext/itsi_server/src/server/lifecycle_event.rs +12 -0
- data/ext/itsi_server/src/server/listener.rs +332 -132
- data/ext/itsi_server/src/server/middleware_stack/middleware.rs +153 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/allow_list.rs +47 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/auth_api_key.rs +58 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/auth_basic.rs +82 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/auth_jwt.rs +321 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/cache_control.rs +139 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/compression.rs +300 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/cors.rs +287 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/deny_list.rs +48 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/error_response.rs +127 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/etag.rs +191 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/grpc_service.rs +72 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/header_interpretation.rs +85 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/intrusion_protection.rs +195 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/log_requests.rs +82 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/mod.rs +82 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/proxy.rs +216 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/rate_limit.rs +124 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/redirect.rs +76 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/request_headers.rs +43 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/response_headers.rs +34 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/ruby_app.rs +93 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/static_assets.rs +162 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/string_rewrite.rs +158 -0
- data/ext/itsi_server/src/server/middleware_stack/middlewares/token_source.rs +12 -0
- data/ext/itsi_server/src/server/middleware_stack/mod.rs +315 -0
- data/ext/itsi_server/src/server/mod.rs +15 -2
- data/ext/itsi_server/src/server/process_worker.rs +229 -0
- data/ext/itsi_server/src/server/rate_limiter.rs +565 -0
- data/ext/itsi_server/src/server/request_job.rs +11 -0
- data/ext/itsi_server/src/server/serve_strategy/cluster_mode.rs +337 -0
- data/ext/itsi_server/src/server/serve_strategy/mod.rs +30 -0
- data/ext/itsi_server/src/server/serve_strategy/single_mode.rs +421 -0
- data/ext/itsi_server/src/server/signal.rs +93 -0
- data/ext/itsi_server/src/server/static_file_server.rs +984 -0
- data/ext/itsi_server/src/server/thread_worker.rs +444 -0
- data/ext/itsi_server/src/server/tls/locked_dir_cache.rs +132 -0
- data/ext/itsi_server/src/server/tls.rs +187 -60
- data/ext/itsi_server/src/server/types.rs +43 -0
- data/ext/itsi_tracing/Cargo.toml +5 -0
- data/ext/itsi_tracing/src/lib.rs +225 -7
- data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-0994n8rpvvt9m/s-h510hfz1f6-1kbycmq.lock +0 -0
- data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-0bob7bf4yq34i/s-h5113125h5-0lh4rag.lock +0 -0
- data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2fcodulrxbbxo/s-h510h2infk-0hp5kjw.lock +0 -0
- data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2iak63r1woi1l/s-h510h2in4q-0kxfzw1.lock +0 -0
- data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2kk4qj9gn5dg2/s-h5113124kv-0enwon2.lock +0 -0
- data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2mwo0yas7dtw4/s-h510hfz1ha-1udgpei.lock +0 -0
- data/lib/itsi/http_request.rb +87 -0
- data/lib/itsi/http_response.rb +39 -0
- data/lib/itsi/server/Itsi.rb +119 -0
- data/lib/itsi/server/config/dsl.rb +506 -0
- data/lib/itsi/server/config.rb +131 -0
- data/lib/itsi/server/default_app/default_app.rb +38 -0
- data/lib/itsi/server/default_app/index.html +91 -0
- data/lib/itsi/server/grpc_interface.rb +213 -0
- data/lib/itsi/server/rack/handler/itsi.rb +27 -0
- data/lib/itsi/server/rack_interface.rb +94 -0
- data/lib/itsi/server/scheduler_interface.rb +21 -0
- data/lib/itsi/server/scheduler_mode.rb +10 -0
- data/lib/itsi/server/signal_trap.rb +29 -0
- data/lib/itsi/server/version.rb +1 -1
- data/lib/itsi/server.rb +90 -9
- data/lib/itsi/standard_headers.rb +86 -0
- metadata +122 -31
- data/ext/itsi_server/src/request/itsi_request.rs +0 -143
- data/ext/itsi_server/src/request/mod.rs +0 -1
- data/ext/itsi_server/src/server/itsi_ca/itsi_ca.crt +0 -32
- data/ext/itsi_server/src/server/itsi_ca/itsi_ca.key +0 -52
- data/ext/itsi_server/src/server/itsi_server.rs +0 -182
- data/ext/itsi_server/src/server/transfer_protocol.rs +0 -23
- data/ext/itsi_server/src/stream_writer/mod.rs +0 -21
- data/lib/itsi/request.rb +0 -39
| @@ -0,0 +1,984 @@ | |
| 1 | 
            +
            use super::{
         | 
| 2 | 
            +
                middleware_stack::ErrorResponse,
         | 
| 3 | 
            +
                types::{HttpRequest, HttpResponse},
         | 
| 4 | 
            +
            };
         | 
| 5 | 
            +
            use bytes::Bytes;
         | 
| 6 | 
            +
            use chrono::{DateTime, Utc};
         | 
| 7 | 
            +
            use http::{header, Response, StatusCode};
         | 
| 8 | 
            +
            use http_body_util::{combinators::BoxBody, Full};
         | 
| 9 | 
            +
            use itsi_error::Result;
         | 
| 10 | 
            +
            use moka::sync::Cache;
         | 
| 11 | 
            +
            use serde::Deserialize;
         | 
| 12 | 
            +
            use std::{
         | 
| 13 | 
            +
                collections::HashMap,
         | 
| 14 | 
            +
                convert::Infallible,
         | 
| 15 | 
            +
                fs::Metadata,
         | 
| 16 | 
            +
                path::{Path, PathBuf},
         | 
| 17 | 
            +
                sync::{Arc, LazyLock},
         | 
| 18 | 
            +
                time::{Duration, Instant, SystemTime},
         | 
| 19 | 
            +
            };
         | 
| 20 | 
            +
            use tokio::sync::Mutex;
         | 
| 21 | 
            +
            use tokio::{fs::File, io::AsyncReadExt};
         | 
| 22 | 
            +
            use tracing::{info, warn};
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            pub static ROOT_STATIC_FILE_SERVER: LazyLock<StaticFileServer> = LazyLock::new(|| {
         | 
| 25 | 
            +
                StaticFileServer::new(StaticFileServerConfig {
         | 
| 26 | 
            +
                    root_dir: Path::new("./").to_path_buf(),
         | 
| 27 | 
            +
                    max_file_size: 4096,
         | 
| 28 | 
            +
                    max_entries: 1024 * 1024 * 10,
         | 
| 29 | 
            +
                    recheck_interval: Duration::from_secs(1),
         | 
| 30 | 
            +
                    try_html_extension: true,
         | 
| 31 | 
            +
                    auto_index: true,
         | 
| 32 | 
            +
                    not_found_behavior: NotFoundBehavior::Error(ErrorResponse::default()),
         | 
| 33 | 
            +
                })
         | 
| 34 | 
            +
            });
         | 
| 35 | 
            +
             | 
| 36 | 
            +
            #[derive(Debug, Clone, Deserialize)]
         | 
| 37 | 
            +
            pub struct Redirect {
         | 
| 38 | 
            +
                pub to: String,
         | 
| 39 | 
            +
            }
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            #[derive(Debug, Clone, Deserialize)]
         | 
| 42 | 
            +
            pub enum NotFoundBehavior {
         | 
| 43 | 
            +
                #[serde(rename = "error")]
         | 
| 44 | 
            +
                Error(ErrorResponse),
         | 
| 45 | 
            +
                #[serde(rename = "fallthrough")]
         | 
| 46 | 
            +
                FallThrough,
         | 
| 47 | 
            +
                #[serde(rename = "index")]
         | 
| 48 | 
            +
                IndexFile(PathBuf),
         | 
| 49 | 
            +
                #[serde(rename = "redirect")]
         | 
| 50 | 
            +
                Redirect(Redirect),
         | 
| 51 | 
            +
                #[serde(rename = "internal_server_error")]
         | 
| 52 | 
            +
                InternalServerError,
         | 
| 53 | 
            +
            }
         | 
| 54 | 
            +
             | 
| 55 | 
            +
            #[derive(Debug, Clone)]
         | 
| 56 | 
            +
            pub struct StaticFileServerConfig {
         | 
| 57 | 
            +
                pub root_dir: PathBuf,
         | 
| 58 | 
            +
                pub max_file_size: u64,
         | 
| 59 | 
            +
                pub max_entries: u64,
         | 
| 60 | 
            +
                pub recheck_interval: Duration,
         | 
| 61 | 
            +
                pub try_html_extension: bool,
         | 
| 62 | 
            +
                pub auto_index: bool,
         | 
| 63 | 
            +
                pub not_found_behavior: NotFoundBehavior,
         | 
| 64 | 
            +
            }
         | 
| 65 | 
            +
             | 
| 66 | 
            +
            #[derive(Debug, Clone)]
         | 
| 67 | 
            +
            pub struct StaticFileServer {
         | 
| 68 | 
            +
                config: Arc<StaticFileServerConfig>,
         | 
| 69 | 
            +
                key_to_path: Arc<Mutex<HashMap<String, PathBuf>>>,
         | 
| 70 | 
            +
                cache: Cache<PathBuf, CacheEntry>,
         | 
| 71 | 
            +
            }
         | 
| 72 | 
            +
             | 
| 73 | 
            +
            #[derive(Clone, Debug)]
         | 
| 74 | 
            +
            struct CacheEntry {
         | 
| 75 | 
            +
                content: Arc<Bytes>,
         | 
| 76 | 
            +
                last_modified: SystemTime,
         | 
| 77 | 
            +
                last_checked: Instant,
         | 
| 78 | 
            +
            }
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            #[derive(Debug, Clone)]
         | 
| 81 | 
            +
            pub enum ServeRange {
         | 
| 82 | 
            +
                Range(u64, u64),
         | 
| 83 | 
            +
                Full,
         | 
| 84 | 
            +
            }
         | 
| 85 | 
            +
             | 
| 86 | 
            +
            impl CacheEntry {
         | 
| 87 | 
            +
                async fn new(path: PathBuf) -> Result<Self> {
         | 
| 88 | 
            +
                    let (bytes, last_modified) = read_entire_file(&path).await?;
         | 
| 89 | 
            +
                    Ok(CacheEntry {
         | 
| 90 | 
            +
                        content: Arc::new(bytes),
         | 
| 91 | 
            +
                        last_modified,
         | 
| 92 | 
            +
                        last_checked: Instant::now(),
         | 
| 93 | 
            +
                    })
         | 
| 94 | 
            +
                }
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                async fn new_virtual_listing(path: PathBuf, root_dir: &Path) -> Self {
         | 
| 97 | 
            +
                    let directory_listing: Bytes = generate_directory_listing(path.parent().unwrap(), root_dir)
         | 
| 98 | 
            +
                        .await
         | 
| 99 | 
            +
                        .unwrap_or("".to_owned())
         | 
| 100 | 
            +
                        .into();
         | 
| 101 | 
            +
                    CacheEntry {
         | 
| 102 | 
            +
                        content: Arc::new(directory_listing),
         | 
| 103 | 
            +
                        last_modified: SystemTime::now(),
         | 
| 104 | 
            +
                        last_checked: Instant::now(),
         | 
| 105 | 
            +
                    }
         | 
| 106 | 
            +
                }
         | 
| 107 | 
            +
            }
         | 
| 108 | 
            +
             | 
| 109 | 
            +
            struct ServeStreamArgs(PathBuf, Metadata, u64, u64, bool, Option<SystemTime>, bool);
         | 
| 110 | 
            +
            struct ServeCacheArgs<'a>(
         | 
| 111 | 
            +
                &'a CacheEntry,
         | 
| 112 | 
            +
                u64,
         | 
| 113 | 
            +
                u64,
         | 
| 114 | 
            +
                bool,
         | 
| 115 | 
            +
                Option<SystemTime>,
         | 
| 116 | 
            +
                bool,
         | 
| 117 | 
            +
                &'a Path,
         | 
| 118 | 
            +
            );
         | 
| 119 | 
            +
             | 
| 120 | 
            +
            impl StaticFileServer {
         | 
| 121 | 
            +
                pub fn new(config: StaticFileServerConfig) -> Self {
         | 
| 122 | 
            +
                    let cache = Cache::builder().max_capacity(config.max_entries).build();
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                    StaticFileServer {
         | 
| 125 | 
            +
                        config: Arc::new(config),
         | 
| 126 | 
            +
                        cache,
         | 
| 127 | 
            +
                        key_to_path: Arc::new(Mutex::new(HashMap::new())),
         | 
| 128 | 
            +
                    }
         | 
| 129 | 
            +
                }
         | 
| 130 | 
            +
             | 
| 131 | 
            +
                pub async fn serve(
         | 
| 132 | 
            +
                    &self,
         | 
| 133 | 
            +
                    request: &HttpRequest,
         | 
| 134 | 
            +
                    path: &str,
         | 
| 135 | 
            +
                    abs_path: &str,
         | 
| 136 | 
            +
                    serve_range: ServeRange,
         | 
| 137 | 
            +
                    if_modified_since: Option<SystemTime>,
         | 
| 138 | 
            +
                    is_head_request: bool,
         | 
| 139 | 
            +
                ) -> Option<HttpResponse> {
         | 
| 140 | 
            +
                    let resolved = self.resolve(path, abs_path).await;
         | 
| 141 | 
            +
                    Some(match resolved {
         | 
| 142 | 
            +
                        Ok(ResolvedAsset {
         | 
| 143 | 
            +
                            path,
         | 
| 144 | 
            +
                            cache_entry,
         | 
| 145 | 
            +
                            metadata,
         | 
| 146 | 
            +
                            redirect_to: None,
         | 
| 147 | 
            +
                        }) => {
         | 
| 148 | 
            +
                            let (start, end) = match serve_range {
         | 
| 149 | 
            +
                                ServeRange::Full => (0, u64::MAX),
         | 
| 150 | 
            +
                                ServeRange::Range(start, end) => (start, end),
         | 
| 151 | 
            +
                            };
         | 
| 152 | 
            +
                            let is_range_request = matches!(serve_range, ServeRange::Range { .. });
         | 
| 153 | 
            +
             | 
| 154 | 
            +
                            if let Some(cache_entry) = cache_entry {
         | 
| 155 | 
            +
                                self.serve_cached_content(ServeCacheArgs(
         | 
| 156 | 
            +
                                    &cache_entry,
         | 
| 157 | 
            +
                                    start,
         | 
| 158 | 
            +
                                    end,
         | 
| 159 | 
            +
                                    is_range_request,
         | 
| 160 | 
            +
                                    if_modified_since,
         | 
| 161 | 
            +
                                    is_head_request,
         | 
| 162 | 
            +
                                    &path,
         | 
| 163 | 
            +
                                ))
         | 
| 164 | 
            +
                            } else {
         | 
| 165 | 
            +
                                self.serve_stream_content(ServeStreamArgs(
         | 
| 166 | 
            +
                                    path,
         | 
| 167 | 
            +
                                    metadata.unwrap(),
         | 
| 168 | 
            +
                                    start,
         | 
| 169 | 
            +
                                    end,
         | 
| 170 | 
            +
                                    is_range_request,
         | 
| 171 | 
            +
                                    if_modified_since,
         | 
| 172 | 
            +
                                    is_head_request,
         | 
| 173 | 
            +
                                ))
         | 
| 174 | 
            +
                                .await
         | 
| 175 | 
            +
                            }
         | 
| 176 | 
            +
                        }
         | 
| 177 | 
            +
                        Ok(ResolvedAsset {
         | 
| 178 | 
            +
                            redirect_to: Some(redirect_to),
         | 
| 179 | 
            +
                            ..
         | 
| 180 | 
            +
                        }) => Response::builder()
         | 
| 181 | 
            +
                            .status(StatusCode::MOVED_PERMANENTLY)
         | 
| 182 | 
            +
                            .header(header::LOCATION, redirect_to)
         | 
| 183 | 
            +
                            .body(BoxBody::new(Full::new(Bytes::new())))
         | 
| 184 | 
            +
                            .unwrap(),
         | 
| 185 | 
            +
                        Err(not_found_behavior) => match not_found_behavior {
         | 
| 186 | 
            +
                            NotFoundBehavior::Error(error_response) => {
         | 
| 187 | 
            +
                                error_response.to_http_response(request).await
         | 
| 188 | 
            +
                            }
         | 
| 189 | 
            +
                            NotFoundBehavior::FallThrough => return None,
         | 
| 190 | 
            +
                            NotFoundBehavior::IndexFile(index_file) => {
         | 
| 191 | 
            +
                                self.serve_single(index_file.to_str().unwrap()).await
         | 
| 192 | 
            +
                            }
         | 
| 193 | 
            +
                            NotFoundBehavior::Redirect(redirect) => Response::builder()
         | 
| 194 | 
            +
                                .status(StatusCode::MOVED_PERMANENTLY)
         | 
| 195 | 
            +
                                .header(header::LOCATION, redirect.to)
         | 
| 196 | 
            +
                                .body(BoxBody::new(Full::new(Bytes::new())))
         | 
| 197 | 
            +
                                .unwrap(),
         | 
| 198 | 
            +
                            NotFoundBehavior::InternalServerError => Response::builder()
         | 
| 199 | 
            +
                                .status(StatusCode::INTERNAL_SERVER_ERROR)
         | 
| 200 | 
            +
                                .body(BoxBody::new(Full::new(Bytes::new())))
         | 
| 201 | 
            +
                                .unwrap(),
         | 
| 202 | 
            +
                        },
         | 
| 203 | 
            +
                    })
         | 
| 204 | 
            +
                }
         | 
| 205 | 
            +
             | 
| 206 | 
            +
                pub async fn serve_single(&self, path: &str) -> HttpResponse {
         | 
| 207 | 
            +
                    let resolved = self.resolve(path, path).await;
         | 
| 208 | 
            +
                    if let Ok(ResolvedAsset {
         | 
| 209 | 
            +
                        path,
         | 
| 210 | 
            +
                        cache_entry: Some(cache_entry),
         | 
| 211 | 
            +
                        ..
         | 
| 212 | 
            +
                    }) = resolved
         | 
| 213 | 
            +
                    {
         | 
| 214 | 
            +
                        return self.serve_cached_content(ServeCacheArgs(
         | 
| 215 | 
            +
                            &cache_entry,
         | 
| 216 | 
            +
                            0,
         | 
| 217 | 
            +
                            u64::MAX,
         | 
| 218 | 
            +
                            false,
         | 
| 219 | 
            +
                            None,
         | 
| 220 | 
            +
                            false,
         | 
| 221 | 
            +
                            &path,
         | 
| 222 | 
            +
                        ));
         | 
| 223 | 
            +
                    } else if let Ok(ResolvedAsset { path, metadata, .. }) = resolved {
         | 
| 224 | 
            +
                        return self
         | 
| 225 | 
            +
                            .serve_stream_content(ServeStreamArgs(
         | 
| 226 | 
            +
                                path,
         | 
| 227 | 
            +
                                metadata.unwrap(),
         | 
| 228 | 
            +
                                0,
         | 
| 229 | 
            +
                                u64::MAX,
         | 
| 230 | 
            +
                                false,
         | 
| 231 | 
            +
                                None,
         | 
| 232 | 
            +
                                false,
         | 
| 233 | 
            +
                            ))
         | 
| 234 | 
            +
                            .await;
         | 
| 235 | 
            +
                    }
         | 
| 236 | 
            +
             | 
| 237 | 
            +
                    Response::builder()
         | 
| 238 | 
            +
                        .status(StatusCode::NOT_FOUND)
         | 
| 239 | 
            +
                        .body(BoxBody::new(Full::new(Bytes::new())))
         | 
| 240 | 
            +
                        .unwrap()
         | 
| 241 | 
            +
                }
         | 
| 242 | 
            +
             | 
| 243 | 
            +
                /// Resolves a request key to an actual file path and determines if it needs to be cached
         | 
| 244 | 
            +
                async fn resolve(
         | 
| 245 | 
            +
                    &self,
         | 
| 246 | 
            +
                    key: &str,
         | 
| 247 | 
            +
                    abs_path: &str,
         | 
| 248 | 
            +
                ) -> std::result::Result<ResolvedAsset, NotFoundBehavior> {
         | 
| 249 | 
            +
                    // First check if we have a cached mapping for this key
         | 
| 250 | 
            +
                    if let Some(path) = self.key_to_path.lock().await.get(key) {
         | 
| 251 | 
            +
                        // Check if the cached entry is still valid
         | 
| 252 | 
            +
                        if let Some(entry) = self.cache.get(path) {
         | 
| 253 | 
            +
                            let last_check_elapsed = entry.last_checked.elapsed();
         | 
| 254 | 
            +
                            if last_check_elapsed < self.config.recheck_interval {
         | 
| 255 | 
            +
                                // Entry is still fresh, use it
         | 
| 256 | 
            +
                                return Ok(ResolvedAsset {
         | 
| 257 | 
            +
                                    path: path.clone(),
         | 
| 258 | 
            +
                                    cache_entry: Some(entry.clone()),
         | 
| 259 | 
            +
                                    metadata: None,
         | 
| 260 | 
            +
                                    redirect_to: None,
         | 
| 261 | 
            +
                                });
         | 
| 262 | 
            +
                            }
         | 
| 263 | 
            +
             | 
| 264 | 
            +
                            // Entry is stale, check if file has changed
         | 
| 265 | 
            +
                            if let Ok(metadata) = tokio::fs::metadata(path).await {
         | 
| 266 | 
            +
                                if metadata
         | 
| 267 | 
            +
                                    .modified()
         | 
| 268 | 
            +
                                    .is_ok_and(|modified| modified == entry.last_modified)
         | 
| 269 | 
            +
                                {
         | 
| 270 | 
            +
                                    // File hasn't changed, just update last_checked
         | 
| 271 | 
            +
                                    let mut entry = entry;
         | 
| 272 | 
            +
                                    entry.last_checked = Instant::now();
         | 
| 273 | 
            +
                                    self.cache.insert(path.clone(), entry.clone());
         | 
| 274 | 
            +
                                    return Ok(ResolvedAsset {
         | 
| 275 | 
            +
                                        path: path.clone(),
         | 
| 276 | 
            +
                                        cache_entry: Some(entry.clone()),
         | 
| 277 | 
            +
                                        metadata: None,
         | 
| 278 | 
            +
                                        redirect_to: None,
         | 
| 279 | 
            +
                                    });
         | 
| 280 | 
            +
                                }
         | 
| 281 | 
            +
             | 
| 282 | 
            +
                                // File has changed, check if it's still cacheable
         | 
| 283 | 
            +
                                if metadata.len() > self.config.max_file_size {
         | 
| 284 | 
            +
                                    // File is now too large, remove from cache
         | 
| 285 | 
            +
                                    self.cache.invalidate(path);
         | 
| 286 | 
            +
                                    self.key_to_path.lock().await.remove(key);
         | 
| 287 | 
            +
                                }
         | 
| 288 | 
            +
                            }
         | 
| 289 | 
            +
                        }
         | 
| 290 | 
            +
                    }
         | 
| 291 | 
            +
             | 
| 292 | 
            +
                    // No valid cached entry, resolve the key to a file path
         | 
| 293 | 
            +
                    let normalized_path = normalize_path(key).ok_or(NotFoundBehavior::InternalServerError)?;
         | 
| 294 | 
            +
                    let mut full_path = self.config.root_dir.clone();
         | 
| 295 | 
            +
                    full_path.push(normalized_path);
         | 
| 296 | 
            +
                    // Check if path exists and is a file
         | 
| 297 | 
            +
                    match tokio::fs::metadata(&full_path).await {
         | 
| 298 | 
            +
                        Ok(metadata) => {
         | 
| 299 | 
            +
                            if metadata.is_file() {
         | 
| 300 | 
            +
                                let cache_entry = if metadata.len() <= self.config.max_file_size {
         | 
| 301 | 
            +
                                    self.key_to_path
         | 
| 302 | 
            +
                                        .lock()
         | 
| 303 | 
            +
                                        .await
         | 
| 304 | 
            +
                                        .insert(key.to_string(), full_path.clone());
         | 
| 305 | 
            +
                                    let cache_entry = CacheEntry::new(full_path.clone()).await.unwrap();
         | 
| 306 | 
            +
                                    self.cache.insert(full_path.clone(), cache_entry.clone());
         | 
| 307 | 
            +
                                    Some(cache_entry)
         | 
| 308 | 
            +
                                } else {
         | 
| 309 | 
            +
                                    None
         | 
| 310 | 
            +
                                };
         | 
| 311 | 
            +
                                return Ok(ResolvedAsset {
         | 
| 312 | 
            +
                                    path: full_path,
         | 
| 313 | 
            +
                                    cache_entry,
         | 
| 314 | 
            +
                                    metadata: Some(metadata),
         | 
| 315 | 
            +
                                    redirect_to: None,
         | 
| 316 | 
            +
                                });
         | 
| 317 | 
            +
                            } else if metadata.is_dir() {
         | 
| 318 | 
            +
                                if !abs_path.ends_with("/") {
         | 
| 319 | 
            +
                                    return Ok(ResolvedAsset {
         | 
| 320 | 
            +
                                        path: full_path,
         | 
| 321 | 
            +
                                        cache_entry: None,
         | 
| 322 | 
            +
                                        metadata: Some(metadata),
         | 
| 323 | 
            +
                                        redirect_to: Some(format!("{}/", abs_path)),
         | 
| 324 | 
            +
                                    });
         | 
| 325 | 
            +
                                }
         | 
| 326 | 
            +
                                let mut index_file = None;
         | 
| 327 | 
            +
             | 
| 328 | 
            +
                                let index_path = full_path.join("index.html");
         | 
| 329 | 
            +
                                if let Ok(idx_meta) = tokio::fs::metadata(&index_path).await {
         | 
| 330 | 
            +
                                    if idx_meta.is_file() {
         | 
| 331 | 
            +
                                        index_file = Some(index_path);
         | 
| 332 | 
            +
                                    }
         | 
| 333 | 
            +
                                }
         | 
| 334 | 
            +
             | 
| 335 | 
            +
                                if index_file.is_none() {
         | 
| 336 | 
            +
                                    // Check for case insensitive index.html
         | 
| 337 | 
            +
                                    let entries = match tokio::fs::read_dir(&full_path).await {
         | 
| 338 | 
            +
                                        Ok(entries) => entries,
         | 
| 339 | 
            +
                                        Err(_) => return Err(NotFoundBehavior::InternalServerError),
         | 
| 340 | 
            +
                                    };
         | 
| 341 | 
            +
             | 
| 342 | 
            +
                                    tokio::pin!(entries);
         | 
| 343 | 
            +
                                    while let Some(entry) = entries.next_entry().await.unwrap_or(None) {
         | 
| 344 | 
            +
                                        if entry
         | 
| 345 | 
            +
                                            .file_name()
         | 
| 346 | 
            +
                                            .to_str()
         | 
| 347 | 
            +
                                            .is_some_and(|name| name.eq_ignore_ascii_case("index.html"))
         | 
| 348 | 
            +
                                            && entry.metadata().await.unwrap().is_file()
         | 
| 349 | 
            +
                                        {
         | 
| 350 | 
            +
                                            index_file = Some(entry.path());
         | 
| 351 | 
            +
                                            break;
         | 
| 352 | 
            +
                                        }
         | 
| 353 | 
            +
                                    }
         | 
| 354 | 
            +
                                }
         | 
| 355 | 
            +
                                if index_file.is_some() {
         | 
| 356 | 
            +
                                    let index_path = index_file.unwrap();
         | 
| 357 | 
            +
                                    self.key_to_path
         | 
| 358 | 
            +
                                        .lock()
         | 
| 359 | 
            +
                                        .await
         | 
| 360 | 
            +
                                        .insert(key.to_string(), index_path.clone());
         | 
| 361 | 
            +
                                    let cache_entry = CacheEntry::new(index_path.clone()).await.unwrap();
         | 
| 362 | 
            +
                                    self.cache.insert(index_path.clone(), cache_entry.clone());
         | 
| 363 | 
            +
                                    return Ok(ResolvedAsset {
         | 
| 364 | 
            +
                                        path: index_path,
         | 
| 365 | 
            +
                                        cache_entry: Some(cache_entry),
         | 
| 366 | 
            +
                                        metadata: None,
         | 
| 367 | 
            +
                                        redirect_to: None,
         | 
| 368 | 
            +
                                    });
         | 
| 369 | 
            +
                                }
         | 
| 370 | 
            +
             | 
| 371 | 
            +
                                // No index.html, check if auto_index is enabled
         | 
| 372 | 
            +
                                if self.config.auto_index {
         | 
| 373 | 
            +
                                    // Create a virtual path for the directory listing
         | 
| 374 | 
            +
                                    let virtual_path = full_path.join(".directory_listing.dir_list");
         | 
| 375 | 
            +
             | 
| 376 | 
            +
                                    let cache_entry = CacheEntry::new_virtual_listing(
         | 
| 377 | 
            +
                                        virtual_path.clone(),
         | 
| 378 | 
            +
                                        &self.config.root_dir,
         | 
| 379 | 
            +
                                    )
         | 
| 380 | 
            +
                                    .await;
         | 
| 381 | 
            +
                                    self.key_to_path
         | 
| 382 | 
            +
                                        .lock()
         | 
| 383 | 
            +
                                        .await
         | 
| 384 | 
            +
                                        .insert(key.to_string(), virtual_path.clone());
         | 
| 385 | 
            +
                                    self.cache.insert(virtual_path.clone(), cache_entry.clone());
         | 
| 386 | 
            +
                                    return Ok(ResolvedAsset {
         | 
| 387 | 
            +
                                        path: virtual_path.clone(),
         | 
| 388 | 
            +
                                        cache_entry: Some(cache_entry.clone()),
         | 
| 389 | 
            +
                                        metadata: None,
         | 
| 390 | 
            +
                                        redirect_to: None,
         | 
| 391 | 
            +
                                    });
         | 
| 392 | 
            +
                                }
         | 
| 393 | 
            +
                            }
         | 
| 394 | 
            +
                        }
         | 
| 395 | 
            +
                        Err(_) => {
         | 
| 396 | 
            +
                            // Path doesn't exist, try with .html extension if configured
         | 
| 397 | 
            +
                            if self.config.try_html_extension {
         | 
| 398 | 
            +
                                let mut html_path = full_path.clone();
         | 
| 399 | 
            +
                                html_path.set_extension("html");
         | 
| 400 | 
            +
             | 
| 401 | 
            +
                                if let Ok(html_meta) = tokio::fs::metadata(&html_path).await {
         | 
| 402 | 
            +
                                    if html_meta.is_file() {
         | 
| 403 | 
            +
                                        self.key_to_path
         | 
| 404 | 
            +
                                            .lock()
         | 
| 405 | 
            +
                                            .await
         | 
| 406 | 
            +
                                            .insert(key.to_string(), html_path.clone());
         | 
| 407 | 
            +
                                        let cache_entry = if html_meta.len() <= self.config.max_file_size {
         | 
| 408 | 
            +
                                            let cache_entry = CacheEntry::new(html_path.clone()).await.unwrap();
         | 
| 409 | 
            +
                                            self.cache.insert(html_path.clone(), cache_entry.clone());
         | 
| 410 | 
            +
                                            Some(cache_entry)
         | 
| 411 | 
            +
                                        } else {
         | 
| 412 | 
            +
                                            None
         | 
| 413 | 
            +
                                        };
         | 
| 414 | 
            +
                                        return Ok(ResolvedAsset {
         | 
| 415 | 
            +
                                            path: html_path,
         | 
| 416 | 
            +
                                            cache_entry,
         | 
| 417 | 
            +
                                            metadata: Some(html_meta),
         | 
| 418 | 
            +
                                            redirect_to: None,
         | 
| 419 | 
            +
                                        });
         | 
| 420 | 
            +
                                    }
         | 
| 421 | 
            +
                                }
         | 
| 422 | 
            +
                            }
         | 
| 423 | 
            +
                        }
         | 
| 424 | 
            +
                    }
         | 
| 425 | 
            +
             | 
| 426 | 
            +
                    // If we get here, we couldn't resolve the key to a file
         | 
| 427 | 
            +
                    Err(self.config.not_found_behavior.clone())
         | 
| 428 | 
            +
                }
         | 
| 429 | 
            +
             | 
| 430 | 
            +
                async fn stream_file_range(
         | 
| 431 | 
            +
                    &self,
         | 
| 432 | 
            +
                    path: PathBuf,
         | 
| 433 | 
            +
                    start: u64,
         | 
| 434 | 
            +
                    end: u64,
         | 
| 435 | 
            +
                ) -> Option<BoxBody<Bytes, Infallible>> {
         | 
| 436 | 
            +
                    use futures::TryStreamExt;
         | 
| 437 | 
            +
                    use http_body_util::StreamBody;
         | 
| 438 | 
            +
                    use hyper::body::Frame;
         | 
| 439 | 
            +
                    use tokio::io::AsyncSeekExt;
         | 
| 440 | 
            +
                    use tokio_util::io::ReaderStream;
         | 
| 441 | 
            +
             | 
| 442 | 
            +
                    let mut file = match File::open(&path).await {
         | 
| 443 | 
            +
                        Ok(f) => f,
         | 
| 444 | 
            +
                        Err(e) => {
         | 
| 445 | 
            +
                            warn!(
         | 
| 446 | 
            +
                                "Failed to open file for streaming: {}: {}",
         | 
| 447 | 
            +
                                path.display(),
         | 
| 448 | 
            +
                                e
         | 
| 449 | 
            +
                            );
         | 
| 450 | 
            +
                            return None;
         | 
| 451 | 
            +
                        }
         | 
| 452 | 
            +
                    };
         | 
| 453 | 
            +
             | 
| 454 | 
            +
                    // Seek to the start position
         | 
| 455 | 
            +
                    if let Err(e) = file.seek(std::io::SeekFrom::Start(start)).await {
         | 
| 456 | 
            +
                        warn!(
         | 
| 457 | 
            +
                            "Failed to seek to position {} in file {}: {}",
         | 
| 458 | 
            +
                            start,
         | 
| 459 | 
            +
                            path.display(),
         | 
| 460 | 
            +
                            e
         | 
| 461 | 
            +
                        );
         | 
| 462 | 
            +
                        return None;
         | 
| 463 | 
            +
                    }
         | 
| 464 | 
            +
             | 
| 465 | 
            +
                    // Create a limited reader that will only read up to range_length bytes
         | 
| 466 | 
            +
                    let range_length = end - start + 1;
         | 
| 467 | 
            +
                    let limited_reader = tokio::io::AsyncReadExt::take(file, range_length);
         | 
| 468 | 
            +
                    let path_clone = path.clone();
         | 
| 469 | 
            +
                    let stream = ReaderStream::new(limited_reader)
         | 
| 470 | 
            +
                        .map_ok(Frame::data)
         | 
| 471 | 
            +
                        .map_err(move |e| {
         | 
| 472 | 
            +
                            warn!("Error streaming file {}: {}", path_clone.display(), e);
         | 
| 473 | 
            +
                            unreachable!("We handle IO errors above")
         | 
| 474 | 
            +
                        });
         | 
| 475 | 
            +
             | 
| 476 | 
            +
                    Some(BoxBody::new(StreamBody::new(stream)))
         | 
| 477 | 
            +
                }
         | 
| 478 | 
            +
             | 
| 479 | 
            +
                async fn stream_file(&self, path: PathBuf) -> Option<BoxBody<Bytes, Infallible>> {
         | 
| 480 | 
            +
                    use futures::TryStreamExt;
         | 
| 481 | 
            +
                    use http_body_util::StreamBody;
         | 
| 482 | 
            +
                    use hyper::body::Frame;
         | 
| 483 | 
            +
                    use tokio_util::io::ReaderStream;
         | 
| 484 | 
            +
             | 
| 485 | 
            +
                    match File::open(&path).await {
         | 
| 486 | 
            +
                        Ok(file) => {
         | 
| 487 | 
            +
                            let path_clone = path.clone();
         | 
| 488 | 
            +
                            let stream = ReaderStream::new(file)
         | 
| 489 | 
            +
                                .map_ok(Frame::data)
         | 
| 490 | 
            +
                                .map_err(move |e| {
         | 
| 491 | 
            +
                                    warn!("Error streaming file {}: {}", path_clone.display(), e);
         | 
| 492 | 
            +
                                    unreachable!("We handle IO errors above")
         | 
| 493 | 
            +
                                });
         | 
| 494 | 
            +
                            Some(BoxBody::new(StreamBody::new(stream)))
         | 
| 495 | 
            +
                        }
         | 
| 496 | 
            +
                        Err(e) => {
         | 
| 497 | 
            +
                            warn!(
         | 
| 498 | 
            +
                                "Failed to open file for streaming: {}: {}",
         | 
| 499 | 
            +
                                path.display(),
         | 
| 500 | 
            +
                                e
         | 
| 501 | 
            +
                            );
         | 
| 502 | 
            +
                            None
         | 
| 503 | 
            +
                        }
         | 
| 504 | 
            +
                    }
         | 
| 505 | 
            +
                }
         | 
| 506 | 
            +
             | 
| 507 | 
            +
                async fn serve_stream_content(&self, stream_args: ServeStreamArgs) -> HttpResponse {
         | 
| 508 | 
            +
                    let ServeStreamArgs(
         | 
| 509 | 
            +
                        file,
         | 
| 510 | 
            +
                        metadata,
         | 
| 511 | 
            +
                        start,
         | 
| 512 | 
            +
                        end,
         | 
| 513 | 
            +
                        is_range_request,
         | 
| 514 | 
            +
                        if_modified_since,
         | 
| 515 | 
            +
                        is_head_request,
         | 
| 516 | 
            +
                    ) = stream_args;
         | 
| 517 | 
            +
             | 
| 518 | 
            +
                    let content_length = metadata.len();
         | 
| 519 | 
            +
                    let last_modified = metadata.modified().unwrap();
         | 
| 520 | 
            +
             | 
| 521 | 
            +
                    // Handle If-Modified-Since header
         | 
| 522 | 
            +
                    if is_not_modified(last_modified, if_modified_since) {
         | 
| 523 | 
            +
                        return build_not_modified_response();
         | 
| 524 | 
            +
                    }
         | 
| 525 | 
            +
             | 
| 526 | 
            +
                    // For range requests, validate the range bounds
         | 
| 527 | 
            +
                    if is_range_request && start >= content_length {
         | 
| 528 | 
            +
                        return Response::builder()
         | 
| 529 | 
            +
                            .status(StatusCode::RANGE_NOT_SATISFIABLE)
         | 
| 530 | 
            +
                            .header("Content-Range", format!("bytes */{}", content_length))
         | 
| 531 | 
            +
                            .body(BoxBody::new(Full::new(Bytes::new())))
         | 
| 532 | 
            +
                            .unwrap();
         | 
| 533 | 
            +
                    }
         | 
| 534 | 
            +
             | 
| 535 | 
            +
                    // Adjust end bound for open-ended ranges or to not exceed file size
         | 
| 536 | 
            +
                    let adjusted_end = if end == u64::MAX {
         | 
| 537 | 
            +
                        content_length - 1
         | 
| 538 | 
            +
                    } else {
         | 
| 539 | 
            +
                        std::cmp::min(end, content_length - 1)
         | 
| 540 | 
            +
                    };
         | 
| 541 | 
            +
             | 
| 542 | 
            +
                    // Create response based on request type
         | 
| 543 | 
            +
                    let status = if is_range_request {
         | 
| 544 | 
            +
                        StatusCode::PARTIAL_CONTENT
         | 
| 545 | 
            +
                    } else {
         | 
| 546 | 
            +
                        StatusCode::OK
         | 
| 547 | 
            +
                    };
         | 
| 548 | 
            +
             | 
| 549 | 
            +
                    let content_range = if is_range_request {
         | 
| 550 | 
            +
                        Some(format!(
         | 
| 551 | 
            +
                            "bytes {}-{}/{}",
         | 
| 552 | 
            +
                            start, adjusted_end, content_length
         | 
| 553 | 
            +
                        ))
         | 
| 554 | 
            +
                    } else {
         | 
| 555 | 
            +
                        None
         | 
| 556 | 
            +
                    };
         | 
| 557 | 
            +
             | 
| 558 | 
            +
                    // For HEAD requests, return just the headers
         | 
| 559 | 
            +
                    if is_head_request {
         | 
| 560 | 
            +
                        let mut builder = Response::builder()
         | 
| 561 | 
            +
                            .status(status)
         | 
| 562 | 
            +
                            .header("Content-Type", get_mime_type(&file))
         | 
| 563 | 
            +
                            .header(
         | 
| 564 | 
            +
                                "Content-Length",
         | 
| 565 | 
            +
                                if is_range_request {
         | 
| 566 | 
            +
                                    (adjusted_end - start + 1).to_string()
         | 
| 567 | 
            +
                                } else {
         | 
| 568 | 
            +
                                    content_length.to_string()
         | 
| 569 | 
            +
                                },
         | 
| 570 | 
            +
                            )
         | 
| 571 | 
            +
                            .header("Last-Modified", format_http_date(last_modified));
         | 
| 572 | 
            +
             | 
| 573 | 
            +
                        if let Some(range) = content_range {
         | 
| 574 | 
            +
                            builder = builder.header("Content-Range", range);
         | 
| 575 | 
            +
                        }
         | 
| 576 | 
            +
             | 
| 577 | 
            +
                        return builder.body(BoxBody::new(Full::new(Bytes::new()))).unwrap();
         | 
| 578 | 
            +
                    }
         | 
| 579 | 
            +
             | 
| 580 | 
            +
                    // For GET requests, prepare the actual content
         | 
| 581 | 
            +
                    if is_range_request {
         | 
| 582 | 
            +
                        // Extract the requested range from the cached content
         | 
| 583 | 
            +
                        let end_idx = std::cmp::min((adjusted_end + 1) as u64, content_length);
         | 
| 584 | 
            +
             | 
| 585 | 
            +
                        build_file_response(
         | 
| 586 | 
            +
                            status,
         | 
| 587 | 
            +
                            get_mime_type(&file),
         | 
| 588 | 
            +
                            (end_idx - start) as usize,
         | 
| 589 | 
            +
                            last_modified,
         | 
| 590 | 
            +
                            content_range,
         | 
| 591 | 
            +
                            self.stream_file_range(file, start, end_idx).await.unwrap(),
         | 
| 592 | 
            +
                        )
         | 
| 593 | 
            +
                    } else {
         | 
| 594 | 
            +
                        build_file_response(
         | 
| 595 | 
            +
                            status,
         | 
| 596 | 
            +
                            get_mime_type(&file),
         | 
| 597 | 
            +
                            content_length as usize,
         | 
| 598 | 
            +
                            last_modified,
         | 
| 599 | 
            +
                            content_range,
         | 
| 600 | 
            +
                            self.stream_file(file).await.unwrap(),
         | 
| 601 | 
            +
                        )
         | 
| 602 | 
            +
                    }
         | 
| 603 | 
            +
                }
         | 
| 604 | 
            +
             | 
| 605 | 
            +
                fn serve_cached_content(
         | 
| 606 | 
            +
                    &self,
         | 
| 607 | 
            +
                    serve_cache_args: ServeCacheArgs,
         | 
| 608 | 
            +
                ) -> http::Response<BoxBody<Bytes, Infallible>> {
         | 
| 609 | 
            +
                    let ServeCacheArgs(
         | 
| 610 | 
            +
                        cache_entry,
         | 
| 611 | 
            +
                        start,
         | 
| 612 | 
            +
                        end,
         | 
| 613 | 
            +
                        is_range_request,
         | 
| 614 | 
            +
                        if_modified_since,
         | 
| 615 | 
            +
                        is_head_request,
         | 
| 616 | 
            +
                        path,
         | 
| 617 | 
            +
                    ) = serve_cache_args;
         | 
| 618 | 
            +
             | 
| 619 | 
            +
                    let content_length = cache_entry.content.len() as u64;
         | 
| 620 | 
            +
             | 
| 621 | 
            +
                    // Handle If-Modified-Since header
         | 
| 622 | 
            +
                    if is_not_modified(cache_entry.last_modified, if_modified_since) {
         | 
| 623 | 
            +
                        return build_not_modified_response();
         | 
| 624 | 
            +
                    }
         | 
| 625 | 
            +
             | 
| 626 | 
            +
                    // For range requests, validate the range bounds
         | 
| 627 | 
            +
                    if is_range_request && start >= content_length {
         | 
| 628 | 
            +
                        return Response::builder()
         | 
| 629 | 
            +
                            .status(StatusCode::RANGE_NOT_SATISFIABLE)
         | 
| 630 | 
            +
                            .header("Content-Range", format!("bytes */{}", content_length))
         | 
| 631 | 
            +
                            .body(BoxBody::new(Full::new(Bytes::new())))
         | 
| 632 | 
            +
                            .unwrap();
         | 
| 633 | 
            +
                    }
         | 
| 634 | 
            +
             | 
| 635 | 
            +
                    // Adjust end bound for open-ended ranges or to not exceed file size
         | 
| 636 | 
            +
                    let adjusted_end = if end == u64::MAX {
         | 
| 637 | 
            +
                        content_length.saturating_sub(1)
         | 
| 638 | 
            +
                    } else {
         | 
| 639 | 
            +
                        std::cmp::min(end, content_length.saturating_sub(1))
         | 
| 640 | 
            +
                    };
         | 
| 641 | 
            +
             | 
| 642 | 
            +
                    // Create response based on request type
         | 
| 643 | 
            +
                    let status = if is_range_request {
         | 
| 644 | 
            +
                        StatusCode::PARTIAL_CONTENT
         | 
| 645 | 
            +
                    } else {
         | 
| 646 | 
            +
                        StatusCode::OK
         | 
| 647 | 
            +
                    };
         | 
| 648 | 
            +
             | 
| 649 | 
            +
                    let content_range = if is_range_request {
         | 
| 650 | 
            +
                        Some(format!(
         | 
| 651 | 
            +
                            "bytes {}-{}/{}",
         | 
| 652 | 
            +
                            start, adjusted_end, content_length
         | 
| 653 | 
            +
                        ))
         | 
| 654 | 
            +
                    } else {
         | 
| 655 | 
            +
                        None
         | 
| 656 | 
            +
                    };
         | 
| 657 | 
            +
             | 
| 658 | 
            +
                    // For HEAD requests, return just the headers
         | 
| 659 | 
            +
                    if is_head_request {
         | 
| 660 | 
            +
                        let mut builder = Response::builder()
         | 
| 661 | 
            +
                            .status(status)
         | 
| 662 | 
            +
                            .header("Content-Type", get_mime_type(path))
         | 
| 663 | 
            +
                            .header(
         | 
| 664 | 
            +
                                "Content-Length",
         | 
| 665 | 
            +
                                if is_range_request {
         | 
| 666 | 
            +
                                    (adjusted_end - start + 1).to_string()
         | 
| 667 | 
            +
                                } else {
         | 
| 668 | 
            +
                                    content_length.to_string()
         | 
| 669 | 
            +
                                },
         | 
| 670 | 
            +
                            )
         | 
| 671 | 
            +
                            .header("Last-Modified", format_http_date(cache_entry.last_modified));
         | 
| 672 | 
            +
             | 
| 673 | 
            +
                        if let Some(range) = content_range {
         | 
| 674 | 
            +
                            builder = builder.header("Content-Range", range);
         | 
| 675 | 
            +
                        }
         | 
| 676 | 
            +
             | 
| 677 | 
            +
                        return builder.body(BoxBody::new(Full::new(Bytes::new()))).unwrap();
         | 
| 678 | 
            +
                    }
         | 
| 679 | 
            +
             | 
| 680 | 
            +
                    // For GET requests, prepare the actual content
         | 
| 681 | 
            +
                    if is_range_request {
         | 
| 682 | 
            +
                        // Extract the requested range from the cached content
         | 
| 683 | 
            +
                        let start_idx = start as usize;
         | 
| 684 | 
            +
                        let end_idx = std::cmp::min((adjusted_end + 1) as usize, cache_entry.content.len());
         | 
| 685 | 
            +
                        let range_bytes = cache_entry.content.slice(start_idx..end_idx);
         | 
| 686 | 
            +
             | 
| 687 | 
            +
                        build_file_response(
         | 
| 688 | 
            +
                            status,
         | 
| 689 | 
            +
                            get_mime_type(path),
         | 
| 690 | 
            +
                            range_bytes.len(),
         | 
| 691 | 
            +
                            cache_entry.last_modified,
         | 
| 692 | 
            +
                            content_range,
         | 
| 693 | 
            +
                            BoxBody::new(Full::new(range_bytes)),
         | 
| 694 | 
            +
                        )
         | 
| 695 | 
            +
                    } else {
         | 
| 696 | 
            +
                        // Return the full content
         | 
| 697 | 
            +
                        let content_clone = cache_entry.content.clone();
         | 
| 698 | 
            +
                        let body = build_ok_body(content_clone);
         | 
| 699 | 
            +
                        build_file_response(
         | 
| 700 | 
            +
                            status,
         | 
| 701 | 
            +
                            get_mime_type(path),
         | 
| 702 | 
            +
                            content_length as usize,
         | 
| 703 | 
            +
                            cache_entry.last_modified,
         | 
| 704 | 
            +
                            content_range,
         | 
| 705 | 
            +
                            body,
         | 
| 706 | 
            +
                        )
         | 
| 707 | 
            +
                    }
         | 
| 708 | 
            +
                }
         | 
| 709 | 
            +
             | 
| 710 | 
            +
                pub async fn invalidate_cache(&self, path: &Path) {
         | 
| 711 | 
            +
                    if let Ok(path_buf) = path.to_path_buf().canonicalize() {
         | 
| 712 | 
            +
                        self.cache.invalidate(&path_buf);
         | 
| 713 | 
            +
                    }
         | 
| 714 | 
            +
                }
         | 
| 715 | 
            +
            }
         | 
| 716 | 
            +
             | 
| 717 | 
            +
            fn format_http_date(last_modified: SystemTime) -> String {
         | 
| 718 | 
            +
                let datetime = DateTime::<Utc>::from(last_modified);
         | 
| 719 | 
            +
                datetime.format("%a, %d %b %Y %H:%M:%S GMT").to_string()
         | 
| 720 | 
            +
            }
         | 
| 721 | 
            +
             | 
| 722 | 
            +
            async fn read_entire_file(path: &Path) -> std::io::Result<(Bytes, SystemTime)> {
         | 
| 723 | 
            +
                let metadata = tokio::fs::metadata(path).await?;
         | 
| 724 | 
            +
                let last_modified = metadata.modified()?;
         | 
| 725 | 
            +
                let mut file = File::open(path).await?;
         | 
| 726 | 
            +
                let mut buf = Vec::with_capacity(metadata.len().try_into().unwrap_or(4096));
         | 
| 727 | 
            +
                file.read_to_end(&mut buf).await?;
         | 
| 728 | 
            +
                Ok((Bytes::from(buf), last_modified))
         | 
| 729 | 
            +
            }
         | 
| 730 | 
            +
             | 
| 731 | 
            +
            fn build_ok_body(bytes: Arc<Bytes>) -> BoxBody<Bytes, Infallible> {
         | 
| 732 | 
            +
                BoxBody::new(Full::new(bytes.as_ref().clone()))
         | 
| 733 | 
            +
            }
         | 
| 734 | 
            +
             | 
| 735 | 
            +
            // Helper for mime type mapping from file extension
         | 
| 736 | 
            +
            fn get_mime_type(path: &Path) -> &'static str {
         | 
| 737 | 
            +
                match path.extension().and_then(|e| e.to_str()) {
         | 
| 738 | 
            +
                    Some("html") | Some("htm") => "text/html",
         | 
| 739 | 
            +
                    Some("css") => "text/css",
         | 
| 740 | 
            +
                    Some("js") => "application/javascript",
         | 
| 741 | 
            +
                    Some("json") => "application/json",
         | 
| 742 | 
            +
                    Some("txt") => "text/plain",
         | 
| 743 | 
            +
                    Some("png") => "image/png",
         | 
| 744 | 
            +
                    Some("jpg") | Some("jpeg") => "image/jpeg",
         | 
| 745 | 
            +
                    Some("gif") => "image/gif",
         | 
| 746 | 
            +
                    Some("svg") => "image/svg+xml",
         | 
| 747 | 
            +
                    Some("ico") => "image/x-icon",
         | 
| 748 | 
            +
                    Some("webp") => "image/webp",
         | 
| 749 | 
            +
                    Some("pdf") => "application/pdf",
         | 
| 750 | 
            +
                    Some("xml") => "application/xml",
         | 
| 751 | 
            +
                    Some("zip") => "application/zip",
         | 
| 752 | 
            +
                    Some("gz") => "application/gzip",
         | 
| 753 | 
            +
                    Some("mp3") => "audio/mpeg",
         | 
| 754 | 
            +
                    Some("mp4") => "video/mp4",
         | 
| 755 | 
            +
                    Some("webm") => "video/webm",
         | 
| 756 | 
            +
                    Some("woff") => "font/woff",
         | 
| 757 | 
            +
                    Some("woff2") => "font/woff2",
         | 
| 758 | 
            +
                    Some("ttf") => "font/ttf",
         | 
| 759 | 
            +
                    Some("otf") => "font/otf",
         | 
| 760 | 
            +
                    Some("dir_list") => "text/html",
         | 
| 761 | 
            +
                    _ => "application/octet-stream",
         | 
| 762 | 
            +
                }
         | 
| 763 | 
            +
            }
         | 
| 764 | 
            +
             | 
| 765 | 
            +
            // Helper function to handle not modified responses
         | 
| 766 | 
            +
            fn build_not_modified_response() -> http::Response<BoxBody<Bytes, Infallible>> {
         | 
| 767 | 
            +
                Response::builder()
         | 
| 768 | 
            +
                    .status(StatusCode::NOT_MODIFIED)
         | 
| 769 | 
            +
                    .body(BoxBody::new(Full::new(Bytes::new())))
         | 
| 770 | 
            +
                    .unwrap()
         | 
| 771 | 
            +
            }
         | 
| 772 | 
            +
             | 
| 773 | 
            +
            // Helper function to build a file response with common headers
         | 
| 774 | 
            +
            fn build_file_response(
         | 
| 775 | 
            +
                status: StatusCode,
         | 
| 776 | 
            +
                content_type: &str,
         | 
| 777 | 
            +
                content_length: usize,
         | 
| 778 | 
            +
                last_modified: SystemTime,
         | 
| 779 | 
            +
                range_header: Option<String>,
         | 
| 780 | 
            +
                body: BoxBody<Bytes, Infallible>,
         | 
| 781 | 
            +
            ) -> http::Response<BoxBody<Bytes, Infallible>> {
         | 
| 782 | 
            +
                let mut builder = Response::builder()
         | 
| 783 | 
            +
                    .status(status)
         | 
| 784 | 
            +
                    .header("Content-Type", content_type)
         | 
| 785 | 
            +
                    .header("Content-Length", content_length)
         | 
| 786 | 
            +
                    .header("Last-Modified", format_http_date(last_modified));
         | 
| 787 | 
            +
             | 
| 788 | 
            +
                if let Some(range) = range_header {
         | 
| 789 | 
            +
                    builder = builder.header("Content-Range", range);
         | 
| 790 | 
            +
                }
         | 
| 791 | 
            +
             | 
| 792 | 
            +
                builder.body(body).unwrap()
         | 
| 793 | 
            +
            }
         | 
| 794 | 
            +
             | 
| 795 | 
            +
            // Helper function to check if a file is too old based on If-Modified-Since
         | 
| 796 | 
            +
            fn is_not_modified(last_modified: SystemTime, if_modified_since: Option<SystemTime>) -> bool {
         | 
| 797 | 
            +
                if let Some(ims) = if_modified_since {
         | 
| 798 | 
            +
                    if ims >= last_modified {
         | 
| 799 | 
            +
                        return true;
         | 
| 800 | 
            +
                    }
         | 
| 801 | 
            +
                }
         | 
| 802 | 
            +
                false
         | 
| 803 | 
            +
            }
         | 
| 804 | 
            +
             | 
| 805 | 
            +
            fn normalize_path(path: &str) -> Option<PathBuf> {
         | 
| 806 | 
            +
                let mut normalized = PathBuf::new();
         | 
| 807 | 
            +
                let path = path.trim_start_matches('/');
         | 
| 808 | 
            +
             | 
| 809 | 
            +
                for segment in path.split('/') {
         | 
| 810 | 
            +
                    if segment.is_empty() || segment == "." {
         | 
| 811 | 
            +
                        continue;
         | 
| 812 | 
            +
                    }
         | 
| 813 | 
            +
             | 
| 814 | 
            +
                    if segment == ".." {
         | 
| 815 | 
            +
                        return None;
         | 
| 816 | 
            +
                    }
         | 
| 817 | 
            +
             | 
| 818 | 
            +
                    // Reject Windows-style backslash separators just in case
         | 
| 819 | 
            +
                    if segment.contains('\\') {
         | 
| 820 | 
            +
                        return None;
         | 
| 821 | 
            +
                    }
         | 
| 822 | 
            +
             | 
| 823 | 
            +
                    normalized.push(segment);
         | 
| 824 | 
            +
                }
         | 
| 825 | 
            +
             | 
| 826 | 
            +
                Some(normalized)
         | 
| 827 | 
            +
            }
         | 
| 828 | 
            +
             | 
| 829 | 
            +
            #[derive(Debug)]
         | 
| 830 | 
            +
            struct ResolvedAsset {
         | 
| 831 | 
            +
                path: PathBuf,
         | 
| 832 | 
            +
                cache_entry: Option<CacheEntry>,
         | 
| 833 | 
            +
                metadata: Option<Metadata>,
         | 
| 834 | 
            +
                redirect_to: Option<String>,
         | 
| 835 | 
            +
            }
         | 
| 836 | 
            +
             | 
| 837 | 
            +
            impl std::fmt::Display for StaticFileServer {
         | 
| 838 | 
            +
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         | 
| 839 | 
            +
                    write!(f, "StaticFileServer(root_dir: {:?})", self.config.root_dir)
         | 
| 840 | 
            +
                }
         | 
| 841 | 
            +
            }
         | 
| 842 | 
            +
             | 
| 843 | 
            +
            impl Default for StaticFileServer {
         | 
| 844 | 
            +
                fn default() -> Self {
         | 
| 845 | 
            +
                    let config = StaticFileServerConfig {
         | 
| 846 | 
            +
                        root_dir: "public".into(),
         | 
| 847 | 
            +
                        max_file_size: 10 * 1024 * 1024,
         | 
| 848 | 
            +
                        max_entries: 100,
         | 
| 849 | 
            +
                        recheck_interval: Duration::from_secs(60),
         | 
| 850 | 
            +
                        try_html_extension: true,
         | 
| 851 | 
            +
                        auto_index: true,
         | 
| 852 | 
            +
                        not_found_behavior: NotFoundBehavior::Error(ErrorResponse::default()),
         | 
| 853 | 
            +
                    };
         | 
| 854 | 
            +
                    Self::new(config)
         | 
| 855 | 
            +
                }
         | 
| 856 | 
            +
            }
         | 
| 857 | 
            +
             | 
| 858 | 
            +
            async fn generate_directory_listing(dir_path: &Path, root_dir: &Path) -> std::io::Result<String> {
         | 
| 859 | 
            +
                let mut html = String::new();
         | 
| 860 | 
            +
             | 
| 861 | 
            +
                html.push_str("<!DOCTYPE html>\n<html>\n<head>\n");
         | 
| 862 | 
            +
                html.push_str("<title>Directory listing for ");
         | 
| 863 | 
            +
                html.push_str(
         | 
| 864 | 
            +
                    dir_path
         | 
| 865 | 
            +
                        .strip_prefix(root_dir)
         | 
| 866 | 
            +
                        .unwrap_or(Path::new(""))
         | 
| 867 | 
            +
                        .to_str()
         | 
| 868 | 
            +
                        .unwrap_or(""),
         | 
| 869 | 
            +
                );
         | 
| 870 | 
            +
                html.push_str("</title>\n");
         | 
| 871 | 
            +
                html.push_str("<meta charset=\"UTF-8\">\n");
         | 
| 872 | 
            +
                html.push_str("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
         | 
| 873 | 
            +
                html.push_str("<style>\n");
         | 
| 874 | 
            +
                html.push_str("  body { font-family: sans-serif; margin: 0; padding: 20px; }\n");
         | 
| 875 | 
            +
                html.push_str("  h1 { border-bottom: 1px solid #ccc; margin-top: 0; padding-bottom: 5px; }\n");
         | 
| 876 | 
            +
                html.push_str("  table { border-collapse: collapse; width: 100%; }\n");
         | 
| 877 | 
            +
                html.push_str("  th, td { text-align: left; padding: 8px; }\n");
         | 
| 878 | 
            +
                html.push_str("  tr:nth-child(even) { background-color: #f2f2f2; }\n");
         | 
| 879 | 
            +
                html.push_str("  th { background-color: #4CAF50; color: white; }\n");
         | 
| 880 | 
            +
                html.push_str("  a { text-decoration: none; color: #0366d6; }\n");
         | 
| 881 | 
            +
                html.push_str("  a:hover { text-decoration: underline; }\n");
         | 
| 882 | 
            +
                html.push_str("  .size { text-align: right; }\n");
         | 
| 883 | 
            +
                html.push_str("  .date { white-space: nowrap; }\n");
         | 
| 884 | 
            +
                html.push_str("</style>\n");
         | 
| 885 | 
            +
                html.push_str("</head>\n<body>\n");
         | 
| 886 | 
            +
             | 
| 887 | 
            +
                html.push_str("<h1>Directory listing for ");
         | 
| 888 | 
            +
                html.push_str(&dir_path.display().to_string());
         | 
| 889 | 
            +
                html.push_str("</h1>\n");
         | 
| 890 | 
            +
             | 
| 891 | 
            +
                html.push_str("<table>\n");
         | 
| 892 | 
            +
                html.push_str("<tr><th>Name</th><th>Size</th><th>Last Modified</th></tr>\n");
         | 
| 893 | 
            +
             | 
| 894 | 
            +
                // Add parent directory link if not in root
         | 
| 895 | 
            +
             | 
| 896 | 
            +
                if dir_path != root_dir {
         | 
| 897 | 
            +
                    info!("{} != {}", dir_path.display(), root_dir.display());
         | 
| 898 | 
            +
                    html.push_str("<tr><td><a href=\"..\">..</a></td><td class=\"size\">-</td><td class=\"date\">-</td></tr>\n");
         | 
| 899 | 
            +
                }
         | 
| 900 | 
            +
             | 
| 901 | 
            +
                // Read the directory entries
         | 
| 902 | 
            +
                let mut entries = tokio::fs::read_dir(dir_path).await.unwrap();
         | 
| 903 | 
            +
                let mut files = Vec::new();
         | 
| 904 | 
            +
                let mut dirs = Vec::new();
         | 
| 905 | 
            +
             | 
| 906 | 
            +
                while let Some(entry) = entries.next_entry().await? {
         | 
| 907 | 
            +
                    let entry_path = entry.path();
         | 
| 908 | 
            +
                    let metadata = entry.metadata().await?;
         | 
| 909 | 
            +
                    let name = entry_path.file_name().unwrap().to_string_lossy();
         | 
| 910 | 
            +
             | 
| 911 | 
            +
                    if metadata.is_dir() {
         | 
| 912 | 
            +
                        dirs.push((name.to_string(), metadata));
         | 
| 913 | 
            +
                    } else {
         | 
| 914 | 
            +
                        files.push((name.to_string(), metadata));
         | 
| 915 | 
            +
                    }
         | 
| 916 | 
            +
                }
         | 
| 917 | 
            +
             | 
| 918 | 
            +
                // Sort directories and files
         | 
| 919 | 
            +
                dirs.sort_by(|a, b| a.0.cmp(&b.0));
         | 
| 920 | 
            +
                files.sort_by(|a, b| a.0.cmp(&b.0));
         | 
| 921 | 
            +
             | 
| 922 | 
            +
                // Add directories to HTML
         | 
| 923 | 
            +
                for (name, metadata) in dirs {
         | 
| 924 | 
            +
                    html.push_str("<tr>");
         | 
| 925 | 
            +
                    html.push_str("<td><a href=\"");
         | 
| 926 | 
            +
                    html.push_str(&name);
         | 
| 927 | 
            +
                    html.push_str("/\">");
         | 
| 928 | 
            +
                    html.push_str(&name);
         | 
| 929 | 
            +
                    html.push_str("/</a></td>");
         | 
| 930 | 
            +
                    html.push_str("<td class=\"size\">-</td>");
         | 
| 931 | 
            +
             | 
| 932 | 
            +
                    if let Ok(modified) = metadata.modified() {
         | 
| 933 | 
            +
                        let formatted_time = DateTime::<Utc>::from(modified)
         | 
| 934 | 
            +
                            .format("%Y-%m-%d %H:%M:%S")
         | 
| 935 | 
            +
                            .to_string();
         | 
| 936 | 
            +
                        html.push_str(&format!("<td class=\"date\">{}</td>", formatted_time));
         | 
| 937 | 
            +
                    } else {
         | 
| 938 | 
            +
                        html.push_str("<td class=\"date\">-</td>");
         | 
| 939 | 
            +
                    }
         | 
| 940 | 
            +
             | 
| 941 | 
            +
                    html.push_str("</tr>\n");
         | 
| 942 | 
            +
                }
         | 
| 943 | 
            +
             | 
| 944 | 
            +
                // Add files to HTML
         | 
| 945 | 
            +
                for (name, metadata) in files {
         | 
| 946 | 
            +
                    html.push_str("<tr>");
         | 
| 947 | 
            +
                    html.push_str("<td><a href=\"");
         | 
| 948 | 
            +
                    html.push_str(&name);
         | 
| 949 | 
            +
                    html.push_str("\">");
         | 
| 950 | 
            +
                    html.push_str(&name);
         | 
| 951 | 
            +
                    html.push_str("</a></td>");
         | 
| 952 | 
            +
             | 
| 953 | 
            +
                    // Format file size
         | 
| 954 | 
            +
                    let file_size = metadata.len();
         | 
| 955 | 
            +
                    let formatted_size = if file_size < 1024 {
         | 
| 956 | 
            +
                        format!("{} B", file_size)
         | 
| 957 | 
            +
                    } else if file_size < 1024 * 1024 {
         | 
| 958 | 
            +
                        format!("{:.1} KB", file_size as f64 / 1024.0)
         | 
| 959 | 
            +
                    } else if file_size < 1024 * 1024 * 1024 {
         | 
| 960 | 
            +
                        format!("{:.1} MB", file_size as f64 / (1024.0 * 1024.0))
         | 
| 961 | 
            +
                    } else {
         | 
| 962 | 
            +
                        format!("{:.1} GB", file_size as f64 / (1024.0 * 1024.0 * 1024.0))
         | 
| 963 | 
            +
                    };
         | 
| 964 | 
            +
             | 
| 965 | 
            +
                    html.push_str(&format!("<td class=\"size\">{}</td>", formatted_size));
         | 
| 966 | 
            +
             | 
| 967 | 
            +
                    if let Ok(modified) = metadata.modified() {
         | 
| 968 | 
            +
                        let formatted_time = DateTime::<Utc>::from(modified)
         | 
| 969 | 
            +
                            .format("%Y-%m-%d %H:%M:%S")
         | 
| 970 | 
            +
                            .to_string();
         | 
| 971 | 
            +
                        html.push_str(&format!("<td class=\"date\">{}</td>", formatted_time));
         | 
| 972 | 
            +
                    } else {
         | 
| 973 | 
            +
                        html.push_str("<td class=\"date\">-</td>");
         | 
| 974 | 
            +
                    }
         | 
| 975 | 
            +
             | 
| 976 | 
            +
                    html.push_str("</tr>\n");
         | 
| 977 | 
            +
                }
         | 
| 978 | 
            +
             | 
| 979 | 
            +
                html.push_str("</table>\n");
         | 
| 980 | 
            +
                html.push_str("<hr><p>Served by Itsi Static Assets</p>\n");
         | 
| 981 | 
            +
                html.push_str("</body>\n</html>");
         | 
| 982 | 
            +
             | 
| 983 | 
            +
                Ok(html)
         | 
| 984 | 
            +
            }
         |