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
use std::{
    ffi::OsStr,
    path::{Path, PathBuf},
};

use askama::Template;
use tokio::fs::File;

use crate::{
    http::{Method, StatusCode},
    Body, Endpoint, Request, Response,
};

#[derive(Template)]
#[template(
    ext = "html",
    source = r#"
<html>
    <head>
        <title>Index of {{ path }}</title>
    </head>
    <body>
        <h1>Index of {{ path }}</h1>
        <ul>
            {% for file in files %}
            <li>
                {% if file.is_dir %} 
                <a href="{{ file.url }}">{{ file.filename | e }}/</a>
                {% else %}
                <a href="{{ file.url }}">{{ file.filename | e }}</a>
                {% endif %}
            </li>
            {% endfor %}
        </ul>
    </body>
    </html>
"#
)]
struct DirectoryTemplate<'a> {
    path: &'a str,
    files: Vec<FileRef>,
}

struct FileRef {
    url: String,
    filename: String,
    is_dir: bool,
}

/// Static files handling service.
pub struct Files {
    path: PathBuf,
    show_files_listing: bool,
    index_file: Option<String>,
}

impl Files {
    /// Create new Files service for a specified base directory.
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self {
            path: path.into(),
            show_files_listing: false,
            index_file: None,
        }
    }

    /// Show files listing for directories.
    ///
    /// By default show files listing is disabled.
    pub fn show_files_listing(self) -> Self {
        Self {
            show_files_listing: true,
            ..self
        }
    }

    /// Set index file
    ///
    /// Shows specific index file for directories instead of showing files
    /// listing.
    ///
    /// If the index file is not found, files listing is shown as a fallback if
    /// Files::show_files_listing() is set.
    pub fn index_file(self, index: impl Into<String>) -> Self {
        Self {
            index_file: Some(index.into()),
            ..self
        }
    }
}

#[async_trait::async_trait]
impl Endpoint for Files {
    type Output = Response;

    async fn call(&self, req: Request) -> Self::Output {
        if req.method() != Method::GET {
            return StatusCode::METHOD_NOT_ALLOWED.into();
        }

        let path = req.uri().path();
        let path = path.trim_start_matches('/');
        let path = path.trim_end_matches('/');
        let mut file_path = self.path.clone();
        for p in Path::new(path) {
            if p == OsStr::new(".") {
                continue;
            } else if p == OsStr::new("..") {
                file_path.pop();
            } else {
                file_path.push(&p);
            }
        }

        if !file_path.starts_with(&self.path) {
            return StatusCode::FORBIDDEN.into();
        }

        if !file_path.exists() {
            return StatusCode::NOT_FOUND.into();
        }

        if file_path.is_file() {
            create_file_response(&file_path).await
        } else {
            if let Some(index_file) = &self.index_file {
                let index_path = file_path.join(index_file);
                if index_path.is_file() {
                    return create_file_response(&index_path).await;
                }
            }

            if self.show_files_listing {
                let read_dir = match file_path.read_dir() {
                    Ok(d) => d,
                    Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into(),
                };
                let mut template = DirectoryTemplate {
                    path: req.uri().path(),
                    files: Vec::new(),
                };

                for res in read_dir {
                    let entry = match res {
                        Ok(entry) => entry,
                        Err(err) => {
                            return (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into()
                        }
                    };

                    if let Some(filename) = entry.file_name().to_str() {
                        let mut base_url = req.original_uri().path().to_string();
                        if !base_url.ends_with('/') {
                            base_url.push('/');
                        }
                        template.files.push(FileRef {
                            url: format!("{}{}", base_url, filename),
                            filename: filename.to_string(),
                            is_dir: entry.path().is_dir(),
                        });
                    }
                }

                let html = match template.render() {
                    Ok(html) => html,
                    Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into(),
                };
                Response::builder().body(Body::from_string(html))
            } else {
                StatusCode::NOT_FOUND.into()
            }
        }
    }
}

async fn create_file_response(path: &Path) -> Response {
    let file = match File::open(path).await {
        Ok(file) => file,
        Err(err) => return (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into(),
    };
    Response::builder().body(Body::from_async_read(file))
}