odd-box 0.1.10

a dead simple reverse proxy server and web server
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
use std::{
    borrow::Cow,
    fs,
    path::{Path, PathBuf},
    sync::Arc,
    time::{Duration, Instant},
};
use hyper::Response;
use mime_guess::mime;
use crate::{
    configuration::DirServer,
    http_proxy::{create_simple_response_from_bytes, EpicResponse},
    CustomError,
};


use once_cell::sync::Lazy;
type CacheValue = (String, Instant, Response<bytes::Bytes>); // (Content-Type, Cache Time, Response)
use dashmap::DashMap;
static RESPONSE_CACHE: Lazy<DashMap<String, CacheValue>> = Lazy::new(|| {
    DashMap::new()
});

// todo:
// - this endpoint is not counted towards any statistics or rate limits ... need to fix.
// - cors headers and such.
// - actual cache support using etag and such
// - markdown rendering (configurable)
// - directory listing opt in via config
// - compression
// - make sure we keep perf ok'ish. current impl sits around ~200k requests per second on an 8 core machine
//   serving some basic example site from "https://github.com/cloudacademy/static-website-example"
pub async fn handle(
    target: DirServer,
    req: hyper::Request<hyper::body::Incoming>,
) -> Result<EpicResponse, CustomError> {
    use mime_guess::from_path;

    let root_dir = Path::new(&target.dir);
let req_path : String = urlencoding::decode(req.uri().path()).map_err(|e|CustomError(format!("{e:?}")))?.to_string();
        
    let cache_key = req_path.trim_end_matches('/').to_string();
    {
        //tracing::trace!("checking cache for {}", cache_key);
        let mut expired_in_cache = false;
        if let Some(guard) = RESPONSE_CACHE.get(&cache_key) {
            
            let (_content_type, cache_time,res) = guard.value();
            // todo - configurable cache time
            if cache_time.elapsed() < Duration::from_secs(10) {
                tracing::trace!("Cache hit for {}", if cache_key.is_empty() {"/"} else {&cache_key});
                return create_simple_response_from_bytes(res.clone());
            } else {
                // tracing::trace!("Cache expired for {}", cache_key);
                expired_in_cache = true;
            }
        }
        if expired_in_cache {
            RESPONSE_CACHE.remove(&cache_key);
        }
    }

    tracing::trace!("Fetching cold file: {}", req_path);

    let requested_path = Path::new(&req_path);
    let full_path = root_dir.join(requested_path.strip_prefix("/").unwrap_or(requested_path));

    let full_path = match fs::canonicalize(&full_path) {
        Ok(path) => path,
        Err(e) => {
            match e.kind() {
                std::io::ErrorKind::NotFound => {
                    let response_body = "sorry, there is nothing here..";
                    let response = Response::builder()
                        .status(404)
                        .header("Content-Type", "text/plain")
                        .body(response_body.into())
                        .expect("should always be possible to create 404 reply");

                    return create_simple_response_from_bytes(response);
                }
                _ => {}
            }
            return Err(CustomError(format!("Failed to canonicalize path: {}", e).into()));
        }
    };

    if !full_path.starts_with(root_dir) {
        return Err(CustomError("Attempted directory traversal".into()));
    }

    if full_path.is_file() {
        let file_content = fs::read(&full_path)
            .map_err(|e| CustomError(format!("Failed to read file: {}", e).into()))?;

        if target.render_markdown.unwrap_or_default() && full_path.extension().and_then(|ext| {
            if let Some(v) = ext.to_str() {
                if v.eq_ignore_ascii_case("md") {
                    Some(v)
                } else {
                    None
                }
            } else {
                None
            }
        }).is_some() {
            // Convert Markdown to HTML
            let markdown = String::from_utf8_lossy(&file_content);
            let html = super::markdown_to_html(full_path.file_name().unwrap_or_default().to_str().unwrap_or("unnamed"),&markdown)
                .map_err(|e| CustomError(format!("Failed to convert Markdown to HTML: {}", e).into()))?;

            let response = hyper::Response::builder()
                .status(200)
                .header("Content-Type", "text/html; charset=utf-8")
                .body(html.into_bytes().into())
                .map_err(|e| CustomError(format!("Failed to create response: {}", e).into()))?;

            // Cache the response
            {
                RESPONSE_CACHE.insert(
                    cache_key.clone(),
                    (
                        "text/html; charset=utf-8".to_string(),
                        Instant::now(),
                        response.clone()
                    ),
                );
            }

            return create_simple_response_from_bytes(response);
        }


        let mut mime_type = from_path(&full_path).first_or_octet_stream();

        if let Some(extension) = full_path.extension().and_then(|ext| ext.to_str()) {
            if extension.eq_ignore_ascii_case("md") {
                mime_type = mime::TEXT_PLAIN_UTF_8;
            }
        }

        let response_bytes : Arc<[u8]> = Arc::from(file_content);

        let response = hyper::Response::builder()
            .status(200)
            .header("Content-Type", mime_type.to_string())
            .body(response_bytes.to_vec().into())
            .map_err(|e| CustomError(format!("Failed to create response: {}", e).into()))?;

        // Cache the response
        {
            RESPONSE_CACHE.insert(
                cache_key.clone(),
                (
                   // response_bytes.clone(),
                    mime_type.to_string(),
                    Instant::now(),
                    response.clone()
                ),
            );
        }


        create_simple_response_from_bytes(response)
    } else if full_path.is_dir() {

        if req_path.ends_with("/") == false {
            let response = hyper::Response::builder()
                .status(301)
                .header("Location", format!("{}/", req_path))
                .body("".into())
                .map_err(|e| CustomError(format!("Failed to create response: {}", e).into()))?;

            return create_simple_response_from_bytes(response);
        }

        // Check for default files before listing the directory
        let default_files = ["index.html", "index.htm", "index.md"];

        for default_file in &default_files {
            let default_file_path = full_path.join(default_file);

            if default_file_path.is_file() {

                // Serve the default file
                let file_content = fs::read(&default_file_path)
                    .map_err(|e| CustomError(format!("Failed to read default file: {}", e).into()))?;

                if default_file_path.extension().and_then(|ext| ext.to_str()) == Some("md") {
                    // Convert Markdown to HTML
                    let markdown = String::from_utf8_lossy(&file_content);
                    let html = super::markdown_to_html("Index.md",&markdown)
                        .map_err(|e| CustomError(format!("Failed to convert Markdown to HTML: {}", e).into()))?;

                    let response = hyper::Response::builder()
                        .status(200)
                        .header("Content-Type", "text/html; charset=utf-8")
                        .body(html.into_bytes().into())
                        .map_err(|e| CustomError(format!("Failed to create response: {}", e).into()))?;

                    // Cache the response
                    {
                        RESPONSE_CACHE.insert(
                            cache_key.clone(),
                            (
                                "text/html; charset=utf-8".to_string(),
                                Instant::now(),
                                response.clone()
                            ),
                        );
                    }

                    return create_simple_response_from_bytes(response);
                }

                // Guess the MIME type
                let mime_type = from_path(&default_file_path).first_or_octet_stream();

                let response_bytes : Arc<[u8]> = Arc::from(file_content);


                let response = hyper::Response::builder()
                    .status(200)
                    .header("Content-Type", mime_type.to_string())
                    .body(response_bytes.to_vec().into())
                    .map_err(|e| CustomError(format!("Failed to create response: {}", e).into()))?;

                // Cache the response
                {
                    RESPONSE_CACHE.insert(
                        cache_key.clone(),
                        (
                            mime_type.to_string(),
                            Instant::now(),
                            response.clone()
                        ),
                    );
                }

                return create_simple_response_from_bytes(response);
            }
        }

        // prevent listing directories if not allowed by dir server configuration
        if target.enable_directory_browsing.unwrap_or_default() == false{
            let response_body = "Directory browsing is disabled";
            let response = hyper::Response::builder()
                .status(403)
                .header("Content-Type", "text/plain")
                .body(response_body.into())
                .map_err(|e| CustomError(format!("Failed to create response: {}", e).into()))?;
    
            return create_simple_response_from_bytes(response)
        }    

        
        create_index_page(&full_path, &req_path,cache_key)

    } else {
        // Return 404 Not Found if the path is neither a file nor a directory
        let response_body = "Not Found";
        let response = hyper::Response::builder()
            .status(404)
            .header("Content-Type", "text/plain")
            .body(response_body.into())
            .map_err(|e| CustomError(format!("Failed to create response: {}", e).into()))?;

        create_simple_response_from_bytes(response)
    }
}


// Function to format file sizes in a human-readable format
fn format_file_size(size: u64) -> String {
    let sizes = ["B", "KB", "MB", "GB", "TB"];
    let mut size = size as f64;
    let mut i = 0;
    while size >= 1024.0 && i < sizes.len() - 1 {
        size /= 1024.0;
        i += 1;
    }
    format!("{:.2} {}", size, sizes[i])
}


use std::collections::HashMap;

fn create_index_page(full_path: &PathBuf, req_path: &str, cache_key: String) -> Result<EpicResponse, CustomError> {

    let content_in_the_directory = fs::read_dir(&full_path)
        .map_err(|e| CustomError(format!("Failed to read directory: {}", e).into()))?;

    // Helper function to map file extensions to icons
    fn get_icon_for_extension(extension: &str) -> &'static str {
        let icons: HashMap<&str, &str> = [
            ("png", "🖼️"),
            ("jpg", "🖼️"),
            ("jpeg", "🖼️"),
            ("gif", "🖼️"),
            ("bmp", "🖼️"),
            ("txt", "📄"),
            ("md", "📝"),
            ("html", "🌐"),
            ("css", "🎨"),
            ("js", "📜"),
            ("json", "📄"),
            ("rs", "🦀"),
            ("py", "🐍"),
            ("java", ""),
            ("c", "📘"),
            ("cpp", "📘"),
            ("cs", "💻"),
            ("pdf", "📕"),
            ("zip", "📦"),
            ("tar", "📦"),
            ("gz", "📦"),
            ("mp3", "🎵"),
            ("mp4", "🎥"),
            ("wav", "🎶"),
            ("doc", "📄"),
            ("docx", "📄"),
            ("xls", "📊"),
            ("xlsx", "📊"),
        ]
        .iter()
        .cloned()
        .collect();

        icons.get(extension).cloned().unwrap_or("📄") // Default icon if not found
    }

    // Build the HTML response
    let html = Cow::Borrowed(
        r#"<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Directory Listing</title>
<style>
    body {
        padding: 2em;
        background-color: #121212;
        color: #ffffff;
        font-family: Arial, sans-serif;
        margin: 0;
    }
    a {
        color: #1e90ff;
        text-decoration: none;
    }
    a:hover {
        text-decoration: underline;
    }
    ul {
        list-style-type: none;
        max-width: 1400px;
        margin: 0 auto; /* Centers the element horizontally */
        border-radius: 8px;
        padding: 4px; /* Adds padding inside the element for inner spacing */
        background-color: #121212;
        background-image: 
            linear-gradient(45deg, #ff0000, #ff7f00, #ffff00, #00ff00, #0000ff, #4b0082, #8f00ff, #ff0000);
        background-size: 200% 200%;
        animation: rainbowBorder 10s alternate-reverse infinite;
        border: 4px solid transparent; /* Thick "border" effect */
        background-clip: padding-box, border-box; /* Keeps inner padding color separate */
    }
    li {
        display: flex;
        align-items: center;
        padding: 10px;
        border-bottom: 1px solid #333;
        background-color: #121212; /* Matches background for inner padding effect */
    }
    li:nth-child(even) {
        background-color: #1d1d1d;
    }
    .icon {
        margin-right: 10px;
        width: 16px;
        height: 16px;
    }
    .file-size {
        margin-left: auto;
        color: #888;
    }
    h1 {
        text-align: center;
        font-size: 1.5em;
        margin: 20px;
    }
    @media (max-width: 600px) {
        body {
            font-size: 0.9em;
        }
        .file-size {
            font-size: 0.8em;
        }
    }
    /* Keyframes for smooth gradient animation */
    @keyframes rainbowBorder {
        0% { background-position: 0% 50%; }
        100% { background-position: 100% 50%; }
    }
</style>



    </head>
    <body>
        <h1>Directory Listing for "#,
    );

    let mut html_owned = html.into_owned();
    html_owned.push_str(&req_path);
    html_owned.push_str(r#"</h1>
        <ul>"#);

    // Add a link to the parent directory if not at the root
    if req_path != "/" {
        let parent_path = Path::new(&req_path).parent().unwrap_or(Path::new("/"));
        let parent_path_str = parent_path.to_str().unwrap_or("/");
        html_owned.push_str(&format!(
            r#"<li><a href="{0}">.. (Parent Directory)</a></li>"#,
            parent_path_str
        ));
    }

    for entry in content_in_the_directory {
        let entry = entry.map_err(|e| CustomError(format!("Failed to read entry: {}", e).into()))?;
        let metadata = entry.metadata().map_err(|e| CustomError(format!("Failed to get metadata: {}", e).into()))?;
        let file_name = entry.file_name().into_string().unwrap_or_else(|_| "Unknown".into());
        let file_size = if metadata.is_file() {
            format!(" ({})", format_file_size(metadata.len()))
        } else {
            " (Directory)".to_string()
        };

        let entry_path = format!("{}/{}", req_path.trim_end_matches('/'), file_name);
        let icon = if metadata.is_file() {
            let extension = file_name.rsplit('.').next().unwrap_or("");
            get_icon_for_extension(extension)
        } else {
            "📁" // Folder icon
        };

        html_owned.push_str(&format!(
            r#"<li><span class="icon">{2}</span><a href="{0}">{1}</a><span class="file-size">{3}</span></li>"#,
            entry_path,
            file_name,
            icon,
            file_size
        ));
    }

    html_owned.push_str(
        r#"
        </ul>
    </body>
    </html>"#,
    );

    let response = hyper::Response::builder()
        .status(200)
        .header("Content-Type", "text/html; charset=utf-8")
        .body(html_owned.into_bytes().into())
        .map_err(|e| CustomError(format!("Failed to create response: {}", e).into()))?;

    // Cache the response
    {
        RESPONSE_CACHE.insert(
            cache_key.clone(),
            (
                "text/html; charset=utf-8".to_string(),
                Instant::now(),
                response.clone()
            ),
        );
    }
    create_simple_response_from_bytes(response)
}