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
use crate::{
fs_shims::{fs, File},
options::StaticOptions,
StaticConnExt,
};
use std::path::{Path, PathBuf};
use trillium::{async_trait, conn_unwrap, Conn, Handler};
/**
trillium handler to serve static files from the filesystem
*/
#[derive(Debug)]
pub struct StaticFileHandler {
fs_root: PathBuf,
index_file: Option<String>,
root_is_file: bool,
options: StaticOptions,
}
#[derive(Debug)]
enum Record {
File(PathBuf, File),
Dir(PathBuf),
}
impl StaticFileHandler {
async fn resolve_fs_path(&self, url_path: &str) -> Option<PathBuf> {
let mut file_path = self.fs_root.clone();
log::trace!(
"attempting to resolve {} relative to {}",
url_path,
file_path.to_str().unwrap()
);
for segment in Path::new(url_path) {
match segment.to_str() {
Some("/") => {}
Some(".") => {}
Some("..") => {
file_path.pop();
}
_ => {
file_path.push(segment);
}
};
}
if file_path.starts_with(&self.fs_root) {
fs::canonicalize(file_path).await.ok().map(Into::into)
} else {
None
}
}
async fn resolve(&self, url_path: &str) -> Option<Record> {
let fs_path = self.resolve_fs_path(url_path).await?;
let metadata = fs::metadata(&fs_path).await.ok()?;
if metadata.is_dir() {
log::trace!("resolved {} as dir {}", url_path, fs_path.to_str().unwrap());
Some(Record::Dir(fs_path))
} else if metadata.is_file() {
File::open(&fs_path)
.await
.ok()
.map(|file| Record::File(fs_path, file))
} else {
None
}
}
/**
builds a new StaticFileHandler
If the fs_root is a file instead of a directory, that file will be served at all paths.
```
# #[cfg(not(unix))] fn main() {}
# #[cfg(unix)] fn main() {
# use trillium::Handler;
# trillium_testing::block_on(async {
use trillium_static::{StaticFileHandler, crate_relative_path};
use trillium_testing::prelude::*;
let mut handler = StaticFileHandler::new(crate_relative_path!("examples/files"));
# handler.init(&mut "testing".into()).await;
assert_not_handled!(get("/").run_async(&handler).await); // no index file configured
assert_ok!(
get("/index.html").run_async(&handler).await,
"<h1>hello world</h1>",
"content-type" => "text/html; charset=utf-8"
);
# }); }
```
*/
pub fn new(fs_root: impl AsRef<Path>) -> Self {
let fs_root = fs_root.as_ref().canonicalize().unwrap();
Self {
fs_root,
index_file: None,
root_is_file: false,
options: StaticOptions::default(),
}
}
/// do not set an etag header
pub fn without_etag_header(mut self) -> Self {
self.options.etag = false;
self
}
/// do not set last-modified header
pub fn without_modified_header(mut self) -> Self {
self.options.modified = false;
self
}
/**
sets the index file on this StaticFileHandler
```
# #[cfg(not(unix))] fn main() {}
# #[cfg(unix)] fn main() {
# use trillium::Handler;
# trillium_testing::block_on(async {
use trillium_static::{StaticFileHandler, crate_relative_path};
let mut handler = StaticFileHandler::new(crate_relative_path!("examples/files"))
.with_index_file("index.html");
# handler.init(&mut "testing".into()).await;
use trillium_testing::prelude::*;
assert_ok!(
get("/").run_async(&handler).await,
"<h1>hello world</h1>", "content-type" => "text/html; charset=utf-8"
);
# }); }
```
*/
pub fn with_index_file(mut self, file: &str) -> Self {
self.index_file = Some(file.to_string());
self
}
}
#[async_trait]
impl Handler for StaticFileHandler {
async fn init(&mut self, _info: &mut trillium::Info) {
self.root_is_file = match self.resolve("/").await {
Some(Record::File(path, _)) => {
log::info!("serving {:?} for all paths", path);
true
}
Some(Record::Dir(dir)) => {
log::info!("serving files within {:?}", dir);
false
}
None => {
log::error!(
"could not find {:?} on init, continuing anyway",
self.fs_root
);
false
}
};
}
async fn run(&self, conn: Conn) -> Conn {
match self.resolve(conn.path()).await {
Some(Record::File(path, file)) => conn.send_file(file).await.with_mime_from_path(path),
Some(Record::Dir(path)) => {
let index = conn_unwrap!(self.index_file.as_ref(), conn);
let path = path.join(index);
let file = conn_unwrap!(File::open(path.to_str().unwrap()).await.ok(), conn);
conn.send_file_with_options(file, &self.options)
.await
.with_mime_from_path(path)
}
_ => conn,
}
}
}