static_web_server/static_files.rs
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// This file is part of Static Web Server.
// See https://static-web-server.net/ for more information
// Copyright (C) 2019-present Jose Quintana <joseluisq.net>
//! The static file module which powers the web server.
//!
// Part of the file is borrowed and adapted at a convenience from
// https://github.com/seanmonstar/warp/blob/master/src/filters/fs.rs
use headers::{AcceptRanges, HeaderMap, HeaderMapExt, HeaderValue};
use hyper::{header::CONTENT_ENCODING, header::CONTENT_LENGTH, Body, Method, Response, StatusCode};
use std::fs::{File, Metadata};
use std::io;
use std::path::PathBuf;
use crate::conditional_headers::ConditionalHeaders;
use crate::fs::meta::{try_metadata, try_metadata_with_html_suffix, FileMetadata};
use crate::fs::path::{sanitize_path, PathExt};
use crate::http_ext::{MethodExt, HTTP_SUPPORTED_METHODS};
use crate::response::response_body;
use crate::Result;
#[cfg(feature = "experimental")]
use crate::mem_cache::{cache, cache::MemCacheOpts};
#[cfg(any(
feature = "compression",
feature = "compression-deflate",
feature = "compression-gzip",
feature = "compression-brotli",
feature = "compression-zstd"
))]
use crate::compression_static;
#[cfg(feature = "directory-listing")]
use crate::{
directory_listing,
directory_listing::{DirListFmt, DirListOpts},
};
const DEFAULT_INDEX_FILES: &[&str; 1] = &["index.html"];
/// Defines all options needed by the static-files handler.
pub struct HandleOpts<'a> {
/// Request method.
pub method: &'a Method,
/// In-memory files cache feature (experimental).
#[cfg(feature = "experimental")]
pub memory_cache: Option<&'a MemCacheOpts>,
/// Request headers.
pub headers: &'a HeaderMap<HeaderValue>,
/// Request base path.
pub base_path: &'a PathBuf,
/// Request base path.
pub uri_path: &'a str,
/// Index files.
pub index_files: &'a [&'a str],
/// Request URI query.
pub uri_query: Option<&'a str>,
/// Directory listing feature.
#[cfg(feature = "directory-listing")]
#[cfg_attr(docsrs, doc(cfg(feature = "directory-listing")))]
pub dir_listing: bool,
/// Directory listing order feature.
#[cfg(feature = "directory-listing")]
#[cfg_attr(docsrs, doc(cfg(feature = "directory-listing")))]
pub dir_listing_order: u8,
/// Directory listing format feature.
#[cfg(feature = "directory-listing")]
#[cfg_attr(docsrs, doc(cfg(feature = "directory-listing")))]
pub dir_listing_format: &'a DirListFmt,
/// Redirect trailing slash feature.
pub redirect_trailing_slash: bool,
/// Compression static feature.
pub compression_static: bool,
/// Ignore hidden files feature.
pub ignore_hidden_files: bool,
/// Prevent following symlinks for files and directories.
pub disable_symlinks: bool,
}
/// Static file response type with additional data.
pub struct StaticFileResponse {
/// Inner HTTP response.
pub resp: Response<Body>,
/// The file path of the inner HTTP response.
pub file_path: PathBuf,
}
/// The server entry point to handle incoming requests which map to specific files
/// on file system and return a file response.
pub async fn handle<'a>(opts: &HandleOpts<'a>) -> Result<StaticFileResponse, StatusCode> {
let method = opts.method;
let uri_path = opts.uri_path;
// Check if current HTTP method for incoming request is supported
if !method.is_allowed() {
return Err(StatusCode::METHOD_NOT_ALLOWED);
}
let headers_opt = opts.headers;
let mut file_path = sanitize_path(opts.base_path, uri_path)?;
// In-memory file cache feature with eviction policy
#[cfg(feature = "experimental")]
if opts.memory_cache.is_some() {
// NOTE: we only support a default auto index for directory requests
// when working on a memory-cache context.
if opts.redirect_trailing_slash && uri_path.ends_with('/') {
file_path.push("index.html");
}
if let Some(result) = cache::get_or_acquire(file_path.as_path(), headers_opt).await {
return Ok(StaticFileResponse {
resp: result?,
// file_path: resp_file_path,
file_path,
});
}
}
let FileMetadata {
file_path,
metadata,
is_dir,
precompressed_variant,
} = get_composed_file_metadata(
&mut file_path,
headers_opt,
opts.compression_static,
opts.index_files,
opts.disable_symlinks,
)?;
// Check for a hidden file/directory (dotfile) and ignore it if feature enabled
if opts.ignore_hidden_files && file_path.is_hidden() {
return Err(StatusCode::NOT_FOUND);
}
let resp_file_path = file_path.to_owned();
// Check for a trailing slash on the current directory path
// and redirect if that path doesn't end with the slash char
if is_dir && opts.redirect_trailing_slash && !uri_path.ends_with('/') {
let uri = [uri_path, "/"].concat();
let loc = match HeaderValue::from_str(uri.as_str()) {
Ok(val) => val,
Err(err) => {
tracing::error!("invalid header value from current uri: {:?}", err);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
};
let mut resp = Response::new(Body::empty());
resp.headers_mut().insert(hyper::header::LOCATION, loc);
*resp.status_mut() = StatusCode::PERMANENT_REDIRECT;
tracing::trace!("uri doesn't end with a slash so redirecting permanently");
return Ok(StaticFileResponse {
resp,
file_path: resp_file_path,
});
}
// Respond with the permitted communication methods
if method.is_options() {
let mut resp = Response::new(Body::empty());
*resp.status_mut() = StatusCode::NO_CONTENT;
resp.headers_mut()
.typed_insert(headers::Allow::from_iter(HTTP_SUPPORTED_METHODS.clone()));
resp.headers_mut().typed_insert(AcceptRanges::bytes());
return Ok(StaticFileResponse {
resp,
file_path: resp_file_path,
});
}
// Directory listing
// Check if "directory listing" feature is enabled,
// if current path is a valid directory and
// if it does not contain an `index.html` file (if a proper auto index is generated)
#[cfg(feature = "directory-listing")]
if is_dir && opts.dir_listing && !file_path.exists() {
let resp = directory_listing::auto_index(DirListOpts {
method,
current_path: uri_path,
uri_query: opts.uri_query,
filepath: file_path,
dir_listing_order: opts.dir_listing_order,
dir_listing_format: opts.dir_listing_format,
ignore_hidden_files: opts.ignore_hidden_files,
disable_symlinks: opts.disable_symlinks,
})?;
return Ok(StaticFileResponse {
resp,
file_path: resp_file_path,
});
}
// Check for a pre-compressed file variant if present under the `opts.compression_static` context
if let Some(precompressed_meta) = precompressed_variant {
let (precomp_path, precomp_encoding) = precompressed_meta;
let mut resp = file_reply(
headers_opt,
file_path,
&metadata,
Some(precomp_path),
#[cfg(feature = "experimental")]
opts.memory_cache,
)?;
// Prepare corresponding headers to let know how to decode the payload
resp.headers_mut().remove(CONTENT_LENGTH);
let encoding = match HeaderValue::from_str(precomp_encoding.as_str()) {
Ok(val) => val,
Err(err) => {
tracing::error!(
"unable to parse header value from content encoding: {:?}",
err
);
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
};
resp.headers_mut().insert(CONTENT_ENCODING, encoding);
return Ok(StaticFileResponse {
resp,
file_path: resp_file_path,
});
}
#[cfg(feature = "experimental")]
let resp = file_reply(headers_opt, file_path, &metadata, None, opts.memory_cache)?;
#[cfg(not(feature = "experimental"))]
let resp = file_reply(headers_opt, file_path, &metadata, None)?;
Ok(StaticFileResponse {
resp,
file_path: resp_file_path,
})
}
/// Returns the final composed metadata containing
/// the current `file_path` with its file metadata
/// as well as its optional pre-compressed variant.
fn get_composed_file_metadata<'a>(
mut file_path: &'a mut PathBuf,
_headers: &'a HeaderMap<HeaderValue>,
_compression_static: bool,
mut index_files: &'a [&'a str],
disable_symlinks: bool,
) -> Result<FileMetadata<'a>, StatusCode> {
tracing::trace!("getting metadata for file {}", file_path.display());
// Prevent symlinks access if option is enabled
if disable_symlinks && file_path.is_symlink() {
tracing::warn!(
"file path {} is a symlink, access denied",
file_path.display()
);
return Err(StatusCode::FORBIDDEN);
}
// Try to find the file path on the file system
match try_metadata(file_path) {
Ok((mut metadata, is_dir)) => {
if is_dir {
// Try every index file variant in order
if index_files.is_empty() {
index_files = DEFAULT_INDEX_FILES;
}
let mut index_found = false;
for index in index_files {
// Append a HTML index page by default if it's a directory path (`autoindex`)
tracing::debug!("dir: appending {} to the directory path", index);
file_path.push(index);
// Pre-compressed variant check for the autoindex
#[cfg(any(
feature = "compression",
feature = "compression-deflate",
feature = "compression-gzip",
feature = "compression-brotli",
feature = "compression-zstd"
))]
if _compression_static {
if let Some(p) =
compression_static::precompressed_variant(file_path, _headers)
{
return Ok(FileMetadata {
file_path,
metadata: p.metadata,
is_dir: false,
precompressed_variant: Some((p.file_path, p.encoding)),
});
}
}
// Otherwise, just fallback to finding the index.html
// and overwrite the current `meta`
// Also noting that it's still a directory request
if let Ok(meta_res) = try_metadata(file_path) {
(metadata, _) = meta_res;
index_found = true;
break;
}
// We remove only the appended index file
file_path.pop();
let new_meta: Option<Metadata>;
(file_path, new_meta) = try_metadata_with_html_suffix(file_path);
if let Some(new_meta) = new_meta {
metadata = new_meta;
index_found = true;
break;
}
}
// In case no index was found then we append the last index
// of the list to preserve the previous behavior
if !index_found && !index_files.is_empty() {
file_path.push(index_files.last().unwrap());
}
} else {
// Fallback pre-compressed variant check for the specific file
#[cfg(any(
feature = "compression",
feature = "compression-deflate",
feature = "compression-gzip",
feature = "compression-brotli",
feature = "compression-zstd"
))]
if _compression_static {
if let Some(p) = compression_static::precompressed_variant(file_path, _headers)
{
return Ok(FileMetadata {
file_path,
metadata: p.metadata,
is_dir: false,
precompressed_variant: Some((p.file_path, p.encoding)),
});
}
}
}
Ok(FileMetadata {
file_path,
metadata,
is_dir,
precompressed_variant: None,
})
}
Err(err) => {
// Pre-compressed variant check for the file not found
#[cfg(any(
feature = "compression",
feature = "compression-deflate",
feature = "compression-gzip",
feature = "compression-brotli",
feature = "compression-zstd"
))]
if _compression_static {
if let Some(p) = compression_static::precompressed_variant(file_path, _headers) {
return Ok(FileMetadata {
file_path,
metadata: p.metadata,
is_dir: false,
precompressed_variant: Some((p.file_path, p.encoding)),
});
}
}
// Otherwise, if the file path doesn't exist then
// we try to find the path suffixed with `.html`.
// For example: `/posts/article` will fallback to `/posts/article.html`
let new_meta: Option<Metadata>;
(file_path, new_meta) = try_metadata_with_html_suffix(file_path);
#[cfg(any(
feature = "compression",
feature = "compression-deflate",
feature = "compression-gzip",
feature = "compression-brotli",
feature = "compression-zstd"
))]
match new_meta {
Some(new_meta) => {
return Ok(FileMetadata {
file_path,
metadata: new_meta,
is_dir: false,
precompressed_variant: None,
})
}
_ => {
// Last pre-compressed variant check or the suffixed file not found
if _compression_static {
if let Some(p) =
compression_static::precompressed_variant(file_path, _headers)
{
return Ok(FileMetadata {
file_path,
metadata: p.metadata,
is_dir: false,
precompressed_variant: Some((p.file_path, p.encoding)),
});
}
}
}
}
#[cfg(not(feature = "compression"))]
if let Some(new_meta) = new_meta {
return Ok(FileMetadata {
file_path,
metadata: new_meta,
is_dir: false,
precompressed_variant: None,
});
}
Err(err)
}
}
}
/// Reply with the corresponding file content taking into account
/// its precompressed variant if any.
/// The `path` param should contains always the original requested file path and
/// the `meta` param value should corresponds to it.
/// However, if `path_precompressed` contains some value then
/// the `meta` param value will belong to the `path_precompressed` (precompressed file variant).
fn file_reply<'a>(
headers: &'a HeaderMap<HeaderValue>,
path: &'a PathBuf,
meta: &'a Metadata,
path_precompressed: Option<PathBuf>,
#[cfg(feature = "experimental")] memory_cache: Option<&'a MemCacheOpts>,
) -> Result<Response<Body>, StatusCode> {
let conditionals = ConditionalHeaders::new(headers);
let file_path = path_precompressed.as_ref().unwrap_or(path);
match File::open(file_path) {
Ok(file) => {
#[cfg(feature = "experimental")]
let resp = response_body(file, path, meta, conditionals, memory_cache);
#[cfg(not(feature = "experimental"))]
let resp = response_body(file, path, meta, conditionals);
resp
}
Err(err) => {
let status = match err.kind() {
io::ErrorKind::NotFound => {
tracing::debug!("file can't be opened or not found: {:?}", path.display());
StatusCode::NOT_FOUND
}
io::ErrorKind::PermissionDenied => {
tracing::warn!("file permission denied: {:?}", path.display());
StatusCode::FORBIDDEN
}
_ => {
tracing::error!("file open error (path={:?}): {} ", path.display(), err);
StatusCode::INTERNAL_SERVER_ERROR
}
};
Err(status)
}
}
}