mirror of
https://github.com/sigoden/dufs.git
synced 2026-04-09 17:13:02 +03:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9384cc8587 | ||
|
|
df48021757 | ||
|
|
af866aaaf4 | ||
|
|
68d238d34d | ||
|
|
a10150f2f8 | ||
|
|
5b11bb75dd | ||
|
|
6d7da0363c | ||
|
|
d8f7335053 |
13
CHANGELOG.md
13
CHANGELOG.md
@@ -2,6 +2,19 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [0.18.0] - 2022-06-18
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Add option --render-try-index ([#47](https://github.com/sigoden/duf/issues/47))
|
||||||
|
- Add slash to end of dir href
|
||||||
|
|
||||||
|
## [0.17.1] - 2022-06-16
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- Range request ([#44](https://github.com/sigoden/duf/issues/44))
|
||||||
|
|
||||||
## [0.17.0] - 2022-06-15
|
## [0.17.0] - 2022-06-15
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|||||||
24
Cargo.lock
generated
24
Cargo.lock
generated
@@ -188,6 +188,27 @@ dependencies = [
|
|||||||
"wasm-bindgen-futures",
|
"wasm-bindgen-futures",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "async-stream"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dad5c83079eae9969be7fadefe640a1c566901f05ff91ab221de4b6f68d9507e"
|
||||||
|
dependencies = [
|
||||||
|
"async-stream-impl",
|
||||||
|
"futures-core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "async-stream-impl"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "10f203db73a71dfa2fb6dd22763990fa26f3d2625a6da2da900d23b87d26be27"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-task"
|
name = "async-task"
|
||||||
version = "4.2.0"
|
version = "4.2.0"
|
||||||
@@ -550,10 +571,11 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "duf"
|
name = "duf"
|
||||||
version = "0.17.0"
|
version = "0.18.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"assert_cmd",
|
"assert_cmd",
|
||||||
"assert_fs",
|
"assert_fs",
|
||||||
|
"async-stream",
|
||||||
"async-walkdir",
|
"async-walkdir",
|
||||||
"async_zip",
|
"async_zip",
|
||||||
"base64",
|
"base64",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "duf"
|
name = "duf"
|
||||||
version = "0.17.0"
|
version = "0.18.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["sigoden <sigoden@gmail.com>"]
|
authors = ["sigoden <sigoden@gmail.com>"]
|
||||||
description = "Duf is a simple file server."
|
description = "Duf is a simple file server."
|
||||||
@@ -15,7 +15,7 @@ clap = { version = "3", default-features = false, features = ["std"] }
|
|||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"]}
|
tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"]}
|
||||||
tokio-rustls = "0.23"
|
tokio-rustls = "0.23"
|
||||||
tokio-util = { version = "0.7", features = ["codec", "io-util"] }
|
tokio-util = { version = "0.7", features = ["io-util"] }
|
||||||
hyper = { version = "0.14", features = ["http1", "server", "tcp", "stream"] }
|
hyper = { version = "0.14", features = ["http1", "server", "tcp", "stream"] }
|
||||||
percent-encoding = "2.1"
|
percent-encoding = "2.1"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
@@ -37,6 +37,7 @@ xml-rs = "0.8"
|
|||||||
env_logger = { version = "0.9", default-features = false, features = ["humantime"] }
|
env_logger = { version = "0.9", default-features = false, features = ["humantime"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
socket2 = "0.4"
|
socket2 = "0.4"
|
||||||
|
async-stream = "0.3"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
assert_cmd = "2"
|
assert_cmd = "2"
|
||||||
|
|||||||
39
README.md
39
README.md
@@ -30,7 +30,7 @@ cargo install duf
|
|||||||
### With docker
|
### With docker
|
||||||
|
|
||||||
```
|
```
|
||||||
docker run -v /tmp:/tmp -p 5000:5000 --rm -it docker.io/sigoden/duf /tmp
|
docker run -v `pwd`:/data -p 5000:5000 --rm -it sigoden/duf /data
|
||||||
```
|
```
|
||||||
|
|
||||||
### Binaries on macOS, Linux, Windows
|
### Binaries on macOS, Linux, Windows
|
||||||
@@ -49,51 +49,52 @@ ARGS:
|
|||||||
<path> Path to a root directory for serving files [default: .]
|
<path> Path to a root directory for serving files [default: .]
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
|
-b, --bind <addr>... Specify bind address
|
||||||
|
-p, --port <port> Specify port to listen on [default: 5000]
|
||||||
|
--path-prefix <path> Specify an url path prefix
|
||||||
-a, --auth <user:pass> Use HTTP authentication
|
-a, --auth <user:pass> Use HTTP authentication
|
||||||
--no-auth-access Not required auth when access static files
|
--no-auth-access Not required auth when access static files
|
||||||
-A, --allow-all Allow all operations
|
-A, --allow-all Allow all operations
|
||||||
|
--allow-upload Allow upload files/folders
|
||||||
--allow-delete Allow delete files/folders
|
--allow-delete Allow delete files/folders
|
||||||
--allow-symlink Allow symlink to files/folders outside root directory
|
--allow-symlink Allow symlink to files/folders outside root directory
|
||||||
--allow-upload Allow upload files/folders
|
|
||||||
-b, --bind <address>... Specify bind address
|
|
||||||
--cors Enable CORS, sets `Access-Control-Allow-Origin: *`
|
|
||||||
-h, --help Print help information
|
|
||||||
-p, --port <port> Specify port to listen on [default: 5000]
|
|
||||||
--path-prefix <path> Specify an url path prefix
|
|
||||||
--render-index Render index.html when requesting a directory
|
--render-index Render index.html when requesting a directory
|
||||||
|
--render-try-index Render index.html if it exists when requesting a directory
|
||||||
--render-spa Render for single-page application
|
--render-spa Render for single-page application
|
||||||
|
--cors Enable CORS, sets `Access-Control-Allow-Origin: *`
|
||||||
--tls-cert <path> Path to an SSL/TLS certificate to serve with HTTPS
|
--tls-cert <path> Path to an SSL/TLS certificate to serve with HTTPS
|
||||||
--tls-key <path> Path to the SSL/TLS certificate's private key
|
--tls-key <path> Path to the SSL/TLS certificate's private key
|
||||||
|
-h, --help Print help information
|
||||||
-V, --version Print version information
|
-V, --version Print version information
|
||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
You can run this command to start serving your current working directory on 127.0.0.1:5000 by default.
|
Serve current working directory, no upload/delete
|
||||||
|
|
||||||
```
|
```
|
||||||
duf
|
duf
|
||||||
```
|
```
|
||||||
|
|
||||||
...or specify which folder you want to serve.
|
Allow upload/delete
|
||||||
|
|
||||||
```
|
```
|
||||||
duf folder_name
|
duf -A
|
||||||
```
|
```
|
||||||
|
|
||||||
Allow all operations such as upload, delete
|
Listen on a specific port
|
||||||
|
|
||||||
```sh
|
|
||||||
duf --allow-all
|
|
||||||
```
|
|
||||||
|
|
||||||
Only allow upload operation
|
|
||||||
|
|
||||||
```
|
```
|
||||||
duf --allow-upload
|
duf -p 80
|
||||||
```
|
```
|
||||||
|
|
||||||
Serve a single page application (SPA)
|
Protect with authentication
|
||||||
|
|
||||||
|
```
|
||||||
|
duf -a admin:admin
|
||||||
|
```
|
||||||
|
|
||||||
|
For a single page application (SPA)
|
||||||
|
|
||||||
```
|
```
|
||||||
duf --render-spa
|
duf --render-spa
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @typedef {object} PathItem
|
* @typedef {object} PathItem
|
||||||
* @property {"Dir"|"SymlinkDir"|"File"|"SymlinkFile"} path_type
|
* @property {"Dir"|"SymlinkDir"|"File"|"SymlinkFile"} path_type
|
||||||
* @property {boolean} is_symlink
|
|
||||||
* @property {string} name
|
* @property {string} name
|
||||||
* @property {number} mtime
|
* @property {number} mtime
|
||||||
* @property {number} size
|
* @property {number} size
|
||||||
@@ -153,10 +152,11 @@ function addBreadcrumb(value) {
|
|||||||
* @param {number} index
|
* @param {number} index
|
||||||
*/
|
*/
|
||||||
function addPath(file, index) {
|
function addPath(file, index) {
|
||||||
const url = getUrl(file.name)
|
let url = getUrl(file.name)
|
||||||
let actionDelete = "";
|
let actionDelete = "";
|
||||||
let actionDownload = "";
|
let actionDownload = "";
|
||||||
if (file.path_type.endsWith("Dir")) {
|
if (file.path_type.endsWith("Dir")) {
|
||||||
|
url += "/";
|
||||||
actionDownload = `
|
actionDownload = `
|
||||||
<div class="action-btn">
|
<div class="action-btn">
|
||||||
<a href="${url}?zip" title="Download folder as a .zip file">
|
<a href="${url}?zip" title="Download folder as a .zip file">
|
||||||
|
|||||||
43
src/args.rs
43
src/args.rs
@@ -1,4 +1,4 @@
|
|||||||
use clap::{Arg, ArgMatches, Command};
|
use clap::{AppSettings, Arg, ArgMatches, Command};
|
||||||
use rustls::{Certificate, PrivateKey};
|
use rustls::{Certificate, PrivateKey};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
@@ -17,14 +17,15 @@ fn app() -> Command<'static> {
|
|||||||
" - ",
|
" - ",
|
||||||
env!("CARGO_PKG_REPOSITORY")
|
env!("CARGO_PKG_REPOSITORY")
|
||||||
))
|
))
|
||||||
|
.global_setting(AppSettings::DeriveDisplayOrder)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("address")
|
Arg::new("bind")
|
||||||
.short('b')
|
.short('b')
|
||||||
.long("bind")
|
.long("bind")
|
||||||
.help("Specify bind address")
|
.help("Specify bind address")
|
||||||
.multiple_values(true)
|
.multiple_values(true)
|
||||||
.multiple_occurrences(true)
|
.multiple_occurrences(true)
|
||||||
.value_name("address"),
|
.value_name("addr"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("port")
|
Arg::new("port")
|
||||||
@@ -46,6 +47,18 @@ fn app() -> Command<'static> {
|
|||||||
.value_name("path")
|
.value_name("path")
|
||||||
.help("Specify an url path prefix"),
|
.help("Specify an url path prefix"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("auth")
|
||||||
|
.short('a')
|
||||||
|
.long("auth")
|
||||||
|
.help("Use HTTP authentication")
|
||||||
|
.value_name("user:pass"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("no-auth-access")
|
||||||
|
.long("no-auth-access")
|
||||||
|
.help("Not required auth when access static files"),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("allow-all")
|
Arg::new("allow-all")
|
||||||
.short('A')
|
.short('A')
|
||||||
@@ -72,25 +85,16 @@ fn app() -> Command<'static> {
|
|||||||
.long("render-index")
|
.long("render-index")
|
||||||
.help("Render index.html when requesting a directory"),
|
.help("Render index.html when requesting a directory"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("render-try-index")
|
||||||
|
.long("render-try-index")
|
||||||
|
.help("Render index.html if it exists when requesting a directory"),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("render-spa")
|
Arg::new("render-spa")
|
||||||
.long("render-spa")
|
.long("render-spa")
|
||||||
.help("Render for single-page application"),
|
.help("Render for single-page application"),
|
||||||
)
|
)
|
||||||
.arg(
|
|
||||||
Arg::new("auth")
|
|
||||||
.short('a')
|
|
||||||
.display_order(1)
|
|
||||||
.long("auth")
|
|
||||||
.help("Use HTTP authentication")
|
|
||||||
.value_name("user:pass"),
|
|
||||||
)
|
|
||||||
.arg(
|
|
||||||
Arg::new("no-auth-access")
|
|
||||||
.display_order(1)
|
|
||||||
.long("no-auth-access")
|
|
||||||
.help("Not required auth when access static files"),
|
|
||||||
)
|
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("cors")
|
Arg::new("cors")
|
||||||
.long("cors")
|
.long("cors")
|
||||||
@@ -128,6 +132,7 @@ pub struct Args {
|
|||||||
pub allow_symlink: bool,
|
pub allow_symlink: bool,
|
||||||
pub render_index: bool,
|
pub render_index: bool,
|
||||||
pub render_spa: bool,
|
pub render_spa: bool,
|
||||||
|
pub render_try_index: bool,
|
||||||
pub cors: bool,
|
pub cors: bool,
|
||||||
pub tls: Option<(Vec<Certificate>, PrivateKey)>,
|
pub tls: Option<(Vec<Certificate>, PrivateKey)>,
|
||||||
}
|
}
|
||||||
@@ -140,7 +145,7 @@ impl Args {
|
|||||||
pub fn parse(matches: ArgMatches) -> BoxResult<Args> {
|
pub fn parse(matches: ArgMatches) -> BoxResult<Args> {
|
||||||
let port = matches.value_of_t::<u16>("port")?;
|
let port = matches.value_of_t::<u16>("port")?;
|
||||||
let addrs = matches
|
let addrs = matches
|
||||||
.values_of("address")
|
.values_of("bind")
|
||||||
.map(|v| v.collect())
|
.map(|v| v.collect())
|
||||||
.unwrap_or_else(|| vec!["0.0.0.0", "::"]);
|
.unwrap_or_else(|| vec!["0.0.0.0", "::"]);
|
||||||
let addrs: Vec<IpAddr> = Args::parse_addrs(&addrs)?;
|
let addrs: Vec<IpAddr> = Args::parse_addrs(&addrs)?;
|
||||||
@@ -164,6 +169,7 @@ impl Args {
|
|||||||
let allow_delete = matches.is_present("allow-all") || matches.is_present("allow-delete");
|
let allow_delete = matches.is_present("allow-all") || matches.is_present("allow-delete");
|
||||||
let allow_symlink = matches.is_present("allow-all") || matches.is_present("allow-symlink");
|
let allow_symlink = matches.is_present("allow-all") || matches.is_present("allow-symlink");
|
||||||
let render_index = matches.is_present("render-index");
|
let render_index = matches.is_present("render-index");
|
||||||
|
let render_try_index = matches.is_present("render-try-index");
|
||||||
let render_spa = matches.is_present("render-spa");
|
let render_spa = matches.is_present("render-spa");
|
||||||
let tls = match (matches.value_of("tls-cert"), matches.value_of("tls-key")) {
|
let tls = match (matches.value_of("tls-cert"), matches.value_of("tls-key")) {
|
||||||
(Some(certs_file), Some(key_file)) => {
|
(Some(certs_file), Some(key_file)) => {
|
||||||
@@ -187,6 +193,7 @@ impl Args {
|
|||||||
allow_upload,
|
allow_upload,
|
||||||
allow_symlink,
|
allow_symlink,
|
||||||
render_index,
|
render_index,
|
||||||
|
render_try_index,
|
||||||
render_spa,
|
render_spa,
|
||||||
tls,
|
tls,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
mod args;
|
mod args;
|
||||||
mod auth;
|
mod auth;
|
||||||
mod server;
|
mod server;
|
||||||
|
mod streamer;
|
||||||
mod tls;
|
mod tls;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
|||||||
167
src/server.rs
167
src/server.rs
@@ -1,4 +1,5 @@
|
|||||||
use crate::auth::{generate_www_auth, valid_digest};
|
use crate::auth::{generate_www_auth, valid_digest};
|
||||||
|
use crate::streamer::Streamer;
|
||||||
use crate::{Args, BoxResult};
|
use crate::{Args, BoxResult};
|
||||||
use xml::escape::escape_str_pcdata;
|
use xml::escape::escape_str_pcdata;
|
||||||
|
|
||||||
@@ -10,26 +11,26 @@ use futures::stream::StreamExt;
|
|||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use headers::{
|
use headers::{
|
||||||
AcceptRanges, AccessControlAllowCredentials, AccessControlAllowHeaders,
|
AcceptRanges, AccessControlAllowCredentials, AccessControlAllowHeaders,
|
||||||
AccessControlAllowOrigin, Connection, ContentLength, ContentRange, ContentType, ETag,
|
AccessControlAllowOrigin, Connection, ContentLength, ContentType, ETag, HeaderMap,
|
||||||
HeaderMap, HeaderMapExt, IfModifiedSince, IfNoneMatch, IfRange, LastModified, Range,
|
HeaderMapExt, IfModifiedSince, IfNoneMatch, IfRange, LastModified, Range,
|
||||||
};
|
};
|
||||||
use hyper::header::{
|
use hyper::header::{
|
||||||
HeaderValue, ACCEPT, AUTHORIZATION, CONTENT_DISPOSITION, CONTENT_TYPE, ORIGIN, RANGE,
|
HeaderValue, ACCEPT, AUTHORIZATION, CONTENT_DISPOSITION, CONTENT_LENGTH, CONTENT_RANGE,
|
||||||
WWW_AUTHENTICATE,
|
CONTENT_TYPE, ORIGIN, RANGE, WWW_AUTHENTICATE,
|
||||||
};
|
};
|
||||||
use hyper::{Body, Method, StatusCode, Uri};
|
use hyper::{Body, Method, StatusCode, Uri};
|
||||||
use percent_encoding::percent_decode;
|
use percent_encoding::percent_decode;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::fs::Metadata;
|
use std::fs::Metadata;
|
||||||
|
use std::io::SeekFrom;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWrite};
|
use tokio::io::{AsyncSeekExt, AsyncWrite};
|
||||||
use tokio::{fs, io};
|
use tokio::{fs, io};
|
||||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
use tokio_util::io::StreamReader;
|
||||||
use tokio_util::io::{ReaderStream, StreamReader};
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub type Request = hyper::Request<Body>;
|
pub type Request = hyper::Request<Body>;
|
||||||
@@ -40,7 +41,7 @@ const INDEX_CSS: &str = include_str!("../assets/index.css");
|
|||||||
const INDEX_JS: &str = include_str!("../assets/index.js");
|
const INDEX_JS: &str = include_str!("../assets/index.js");
|
||||||
const FAVICON_ICO: &[u8] = include_bytes!("../assets/favicon.ico");
|
const FAVICON_ICO: &[u8] = include_bytes!("../assets/favicon.ico");
|
||||||
const INDEX_NAME: &str = "index.html";
|
const INDEX_NAME: &str = "index.html";
|
||||||
const BUF_SIZE: usize = 1024 * 16;
|
const BUF_SIZE: usize = 65536;
|
||||||
|
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
args: Arc<Args>,
|
args: Arc<Args>,
|
||||||
@@ -118,6 +119,7 @@ impl Server {
|
|||||||
let allow_delete = self.args.allow_delete;
|
let allow_delete = self.args.allow_delete;
|
||||||
let render_index = self.args.render_index;
|
let render_index = self.args.render_index;
|
||||||
let render_spa = self.args.render_spa;
|
let render_spa = self.args.render_spa;
|
||||||
|
let render_try_index = self.args.render_try_index;
|
||||||
|
|
||||||
if !self.args.allow_symlink && !is_miss && !self.is_root_contained(path).await {
|
if !self.args.allow_symlink && !is_miss && !self.is_root_contained(path).await {
|
||||||
status_not_found(&mut res);
|
status_not_found(&mut res);
|
||||||
@@ -128,7 +130,9 @@ impl Server {
|
|||||||
Method::GET | Method::HEAD => {
|
Method::GET | Method::HEAD => {
|
||||||
let head_only = method == Method::HEAD;
|
let head_only = method == Method::HEAD;
|
||||||
if is_dir {
|
if is_dir {
|
||||||
if render_index || render_spa {
|
if render_try_index && query == "zip" {
|
||||||
|
self.handle_zip_dir(path, head_only, &mut res).await?;
|
||||||
|
} else if render_index || render_spa || render_try_index {
|
||||||
self.handle_render_index(path, headers, head_only, &mut res)
|
self.handle_render_index(path, headers, head_only, &mut res)
|
||||||
.await?;
|
.await?;
|
||||||
} else if query == "zip" {
|
} else if query == "zip" {
|
||||||
@@ -353,8 +357,8 @@ impl Server {
|
|||||||
error!("Failed to zip {}, {}", path.display(), e);
|
error!("Failed to zip {}, {}", path.display(), e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let stream = ReaderStream::new(reader);
|
let reader = Streamer::new(reader, BUF_SIZE);
|
||||||
*res.body_mut() = Body::wrap_stream(stream);
|
*res.body_mut() = Body::wrap_stream(reader.into_stream());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,15 +369,17 @@ impl Server {
|
|||||||
head_only: bool,
|
head_only: bool,
|
||||||
res: &mut Response,
|
res: &mut Response,
|
||||||
) -> BoxResult<()> {
|
) -> BoxResult<()> {
|
||||||
let path = path.join(INDEX_NAME);
|
let index_path = path.join(INDEX_NAME);
|
||||||
if fs::metadata(&path)
|
if fs::metadata(&index_path)
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
.map(|v| v.is_file())
|
.map(|v| v.is_file())
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
{
|
{
|
||||||
self.handle_send_file(&path, headers, head_only, res)
|
self.handle_send_file(&index_path, headers, head_only, res)
|
||||||
.await?;
|
.await?;
|
||||||
|
} else if self.args.render_try_index {
|
||||||
|
self.handle_ls_dir(path, true, head_only, res).await?;
|
||||||
} else {
|
} else {
|
||||||
status_not_found(res)
|
status_not_found(res)
|
||||||
}
|
}
|
||||||
@@ -425,7 +431,7 @@ impl Server {
|
|||||||
) -> BoxResult<()> {
|
) -> BoxResult<()> {
|
||||||
let (file, meta) = tokio::join!(fs::File::open(path), fs::metadata(path),);
|
let (file, meta) = tokio::join!(fs::File::open(path), fs::metadata(path),);
|
||||||
let (mut file, meta) = (file?, meta?);
|
let (mut file, meta) = (file?, meta?);
|
||||||
let mut maybe_range = true;
|
let mut use_range = true;
|
||||||
if let Some((etag, last_modified)) = extract_cache_headers(&meta) {
|
if let Some((etag, last_modified)) = extract_cache_headers(&meta) {
|
||||||
let cached = {
|
let cached = {
|
||||||
if let Some(if_none_match) = headers.typed_get::<IfNoneMatch>() {
|
if let Some(if_none_match) = headers.typed_get::<IfNoneMatch>() {
|
||||||
@@ -436,55 +442,77 @@ impl Server {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
res.headers_mut().typed_insert(last_modified);
|
|
||||||
res.headers_mut().typed_insert(etag.clone());
|
|
||||||
if cached {
|
if cached {
|
||||||
*res.status_mut() = StatusCode::NOT_MODIFIED;
|
*res.status_mut() = StatusCode::NOT_MODIFIED;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
res.headers_mut().typed_insert(last_modified);
|
||||||
|
res.headers_mut().typed_insert(etag.clone());
|
||||||
|
|
||||||
if headers.typed_get::<Range>().is_some() {
|
if headers.typed_get::<Range>().is_some() {
|
||||||
maybe_range = headers
|
use_range = headers
|
||||||
.typed_get::<IfRange>()
|
.typed_get::<IfRange>()
|
||||||
.map(|if_range| !if_range.is_modified(Some(&etag), Some(&last_modified)))
|
.map(|if_range| !if_range.is_modified(Some(&etag), Some(&last_modified)))
|
||||||
// Always be fresh if there is no validators
|
// Always be fresh if there is no validators
|
||||||
.unwrap_or(true);
|
.unwrap_or(true);
|
||||||
} else {
|
} else {
|
||||||
maybe_range = false;
|
use_range = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let file_range = if maybe_range {
|
|
||||||
if let Some(content_range) = headers
|
let range = if use_range {
|
||||||
.typed_get::<Range>()
|
parse_range(headers)
|
||||||
.and_then(|range| to_content_range(&range, meta.len()))
|
|
||||||
{
|
|
||||||
res.headers_mut().typed_insert(content_range.clone());
|
|
||||||
*res.status_mut() = StatusCode::PARTIAL_CONTENT;
|
|
||||||
content_range.bytes_range()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(mime) = mime_guess::from_path(&path).first() {
|
if let Some(mime) = mime_guess::from_path(&path).first() {
|
||||||
res.headers_mut().typed_insert(ContentType::from(mime));
|
res.headers_mut().typed_insert(ContentType::from(mime));
|
||||||
}
|
} else {
|
||||||
res.headers_mut().typed_insert(AcceptRanges::bytes());
|
res.headers_mut().insert(
|
||||||
res.headers_mut()
|
CONTENT_TYPE,
|
||||||
.typed_insert(ContentLength(meta.len() as u64));
|
HeaderValue::from_static("application/octet-stream"),
|
||||||
if head_only {
|
);
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = if let Some((begin, end)) = file_range {
|
res.headers_mut().typed_insert(AcceptRanges::bytes());
|
||||||
file.seek(io::SeekFrom::Start(begin)).await?;
|
|
||||||
let stream = FramedRead::new(file.take(end - begin + 1), BytesCodec::new());
|
let size = meta.len();
|
||||||
Body::wrap_stream(stream)
|
|
||||||
|
if let Some(range) = range {
|
||||||
|
if range
|
||||||
|
.end
|
||||||
|
.map_or_else(|| range.start < size, |v| v >= range.start)
|
||||||
|
&& file.seek(SeekFrom::Start(range.start)).await.is_ok()
|
||||||
|
{
|
||||||
|
let end = range.end.unwrap_or(size - 1).min(size - 1);
|
||||||
|
let part_size = end - range.start + 1;
|
||||||
|
let reader = Streamer::new(file, BUF_SIZE);
|
||||||
|
*res.status_mut() = StatusCode::PARTIAL_CONTENT;
|
||||||
|
let content_range = format!("bytes {}-{}/{}", range.start, end, size);
|
||||||
|
res.headers_mut()
|
||||||
|
.insert(CONTENT_RANGE, content_range.parse().unwrap());
|
||||||
|
res.headers_mut()
|
||||||
|
.insert(CONTENT_LENGTH, format!("{}", part_size).parse().unwrap());
|
||||||
|
if head_only {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
*res.body_mut() = Body::wrap_stream(reader.into_stream_sized(part_size));
|
||||||
|
} else {
|
||||||
|
*res.status_mut() = StatusCode::RANGE_NOT_SATISFIABLE;
|
||||||
|
res.headers_mut()
|
||||||
|
.insert(CONTENT_RANGE, format!("bytes */{}", size).parse().unwrap());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let stream = FramedRead::new(file, BytesCodec::new());
|
res.headers_mut()
|
||||||
Body::wrap_stream(stream)
|
.insert(CONTENT_LENGTH, format!("{}", size).parse().unwrap());
|
||||||
};
|
if head_only {
|
||||||
*res.body_mut() = body;
|
return Ok(());
|
||||||
|
}
|
||||||
|
let reader = Streamer::new(file, BUF_SIZE);
|
||||||
|
*res.body_mut() = Body::wrap_stream(reader.into_stream());
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -965,32 +993,34 @@ fn extract_cache_headers(meta: &Metadata) -> Option<(ETag, LastModified)> {
|
|||||||
Some((etag, last_modified))
|
Some((etag, last_modified))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_content_range(range: &Range, complete_length: u64) -> Option<ContentRange> {
|
#[derive(Debug)]
|
||||||
use core::ops::Bound::{Included, Unbounded};
|
struct RangeValue {
|
||||||
let mut iter = range.iter();
|
start: u64,
|
||||||
let bounds = iter.next();
|
end: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
if iter.next().is_some() {
|
fn parse_range(headers: &HeaderMap<HeaderValue>) -> Option<RangeValue> {
|
||||||
// Found multiple byte-range-spec. Drop.
|
let range_hdr = headers.get(RANGE)?;
|
||||||
return None;
|
let hdr = range_hdr.to_str().ok()?;
|
||||||
|
let mut sp = hdr.splitn(2, '=');
|
||||||
|
let units = sp.next().unwrap();
|
||||||
|
if units == "bytes" {
|
||||||
|
let range = sp.next()?;
|
||||||
|
let mut sp_range = range.splitn(2, '-');
|
||||||
|
let start: u64 = sp_range.next().unwrap().parse().ok()?;
|
||||||
|
let end: Option<u64> = if let Some(end) = sp_range.next() {
|
||||||
|
if end.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(end.parse().ok()?)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
Some(RangeValue { start, end })
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
bounds.and_then(|b| match b {
|
|
||||||
(Included(start), Included(end)) if start <= end && start < complete_length => {
|
|
||||||
ContentRange::bytes(
|
|
||||||
start..=end.min(complete_length.saturating_sub(1)),
|
|
||||||
complete_length,
|
|
||||||
)
|
|
||||||
.ok()
|
|
||||||
}
|
|
||||||
(Included(start), Unbounded) if start < complete_length => {
|
|
||||||
ContentRange::bytes(start.., complete_length).ok()
|
|
||||||
}
|
|
||||||
(Unbounded, Included(end)) if end > 0 => {
|
|
||||||
ContentRange::bytes(complete_length.saturating_sub(end).., complete_length).ok()
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode_uri(v: &str) -> String {
|
fn encode_uri(v: &str) -> String {
|
||||||
@@ -1004,6 +1034,7 @@ fn status_forbid(res: &mut Response) {
|
|||||||
|
|
||||||
fn status_not_found(res: &mut Response) {
|
fn status_not_found(res: &mut Response) {
|
||||||
*res.status_mut() = StatusCode::NOT_FOUND;
|
*res.status_mut() = StatusCode::NOT_FOUND;
|
||||||
|
*res.body_mut() = Body::from("Not Found");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn status_no_content(res: &mut Response) {
|
fn status_no_content(res: &mut Response) {
|
||||||
|
|||||||
68
src/streamer.rs
Normal file
68
src/streamer.rs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
use async_stream::stream;
|
||||||
|
use futures::{Stream, StreamExt};
|
||||||
|
use std::io::Error;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use tokio::io::{AsyncRead, AsyncReadExt};
|
||||||
|
|
||||||
|
pub struct Streamer<R>
|
||||||
|
where
|
||||||
|
R: AsyncRead + Unpin + Send + 'static,
|
||||||
|
{
|
||||||
|
reader: R,
|
||||||
|
buf_size: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R> Streamer<R>
|
||||||
|
where
|
||||||
|
R: AsyncRead + Unpin + Send + 'static,
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
pub fn new(reader: R, buf_size: usize) -> Self {
|
||||||
|
Self { reader, buf_size }
|
||||||
|
}
|
||||||
|
pub fn into_stream(
|
||||||
|
mut self,
|
||||||
|
) -> Pin<Box<impl ?Sized + Stream<Item = Result<Vec<u8>, Error>> + 'static>> {
|
||||||
|
let stream = stream! {
|
||||||
|
loop {
|
||||||
|
let mut buf = vec![0; self.buf_size];
|
||||||
|
let r = self.reader.read(&mut buf).await?;
|
||||||
|
if r == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
buf.truncate(r);
|
||||||
|
yield Ok(buf);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
stream.boxed()
|
||||||
|
}
|
||||||
|
// allow truncation as truncated remaining is always less than buf_size: usize
|
||||||
|
pub fn into_stream_sized(
|
||||||
|
mut self,
|
||||||
|
max_length: u64,
|
||||||
|
) -> Pin<Box<impl ?Sized + Stream<Item = Result<Vec<u8>, Error>> + 'static>> {
|
||||||
|
let stream = stream! {
|
||||||
|
let mut remaining = max_length;
|
||||||
|
loop {
|
||||||
|
if remaining == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let bs = if remaining >= self.buf_size as u64 {
|
||||||
|
self.buf_size
|
||||||
|
} else {
|
||||||
|
remaining as usize
|
||||||
|
};
|
||||||
|
let mut buf = vec![0; bs];
|
||||||
|
let r = self.reader.read(&mut buf).await?;
|
||||||
|
if r == 0 {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
buf.truncate(r);
|
||||||
|
yield Ok(buf);
|
||||||
|
}
|
||||||
|
remaining -= r as u64;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
stream.boxed()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,7 +29,11 @@ pub static FILES: &[&str] = &[
|
|||||||
"foo\\bar.test",
|
"foo\\bar.test",
|
||||||
];
|
];
|
||||||
|
|
||||||
/// Directory names for testing purpose
|
/// Directory names for testing diretory don't exist
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub static DIR_NO_FOUND: &str = "dir-no-found/";
|
||||||
|
|
||||||
|
/// Directory names for testing diretory don't have index.html
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub static DIR_NO_INDEX: &str = "dir-no-index/";
|
pub static DIR_NO_INDEX: &str = "dir-no-index/";
|
||||||
|
|
||||||
@@ -55,7 +59,7 @@ pub fn tmpdir() -> TempDir {
|
|||||||
}
|
}
|
||||||
for directory in DIRECTORIES {
|
for directory in DIRECTORIES {
|
||||||
for file in FILES {
|
for file in FILES {
|
||||||
if *directory == DIR_NO_INDEX {
|
if *directory == DIR_NO_INDEX && *file == "index.html" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
tmpdir
|
tmpdir
|
||||||
|
|||||||
45
tests/range.rs
Normal file
45
tests/range.rs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use fixtures::{server, Error, TestServer};
|
||||||
|
use headers::HeaderValue;
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn get_file_range(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"GET", format!("{}index.html", server.url()))
|
||||||
|
.header("range", HeaderValue::from_static("bytes=0-6"))
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 206);
|
||||||
|
assert_eq!(resp.headers().get("content-range").unwrap(), "bytes 0-6/18");
|
||||||
|
assert_eq!(resp.headers().get("accept-ranges").unwrap(), "bytes");
|
||||||
|
assert_eq!(resp.headers().get("content-length").unwrap(), "7");
|
||||||
|
assert_eq!(resp.text()?, "This is");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn get_file_range_beyond(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"GET", format!("{}index.html", server.url()))
|
||||||
|
.header("range", HeaderValue::from_static("bytes=12-20"))
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 206);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("content-range").unwrap(),
|
||||||
|
"bytes 12-17/18"
|
||||||
|
);
|
||||||
|
assert_eq!(resp.headers().get("accept-ranges").unwrap(), "bytes");
|
||||||
|
assert_eq!(resp.headers().get("content-length").unwrap(), "6");
|
||||||
|
assert_eq!(resp.text()?, "x.html");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn get_file_range_invalid(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"GET", format!("{}index.html", server.url()))
|
||||||
|
.header("range", HeaderValue::from_static("bytes=20-"))
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 416);
|
||||||
|
assert_eq!(resp.headers().get("content-range").unwrap(), "bytes */18");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
mod fixtures;
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
use fixtures::{server, Error, TestServer, DIR_NO_INDEX};
|
use fixtures::{server, Error, TestServer, DIR_NO_FOUND, DIR_NO_INDEX};
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
@@ -12,12 +13,43 @@ fn render_index(#[with(&["--render-index"])] server: TestServer) -> Result<(), E
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn render_index_404(#[with(&["--render-index"])] server: TestServer) -> Result<(), Error> {
|
fn render_index2(#[with(&["--render-index"])] server: TestServer) -> Result<(), Error> {
|
||||||
let resp = reqwest::blocking::get(format!("{}/{}", server.url(), DIR_NO_INDEX))?;
|
let resp = reqwest::blocking::get(format!("{}{}", server.url(), DIR_NO_INDEX))?;
|
||||||
assert_eq!(resp.status(), 404);
|
assert_eq!(resp.status(), 404);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn render_try_index(#[with(&["--render-try-index"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
|
let text = resp.text()?;
|
||||||
|
assert_eq!(text, "This is index.html");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn render_try_index2(#[with(&["--render-try-index"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}", server.url(), DIR_NO_INDEX))?;
|
||||||
|
let files: Vec<&str> = self::fixtures::FILES
|
||||||
|
.iter()
|
||||||
|
.filter(|v| **v != "index.html")
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
assert_index_resp!(resp, files);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn render_try_index3(#[with(&["--render-try-index"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}?zip", server.url(), DIR_NO_INDEX))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("content-type").unwrap(),
|
||||||
|
"application/zip"
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
|
fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
|
||||||
let resp = reqwest::blocking::get(server.url())?;
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
@@ -27,8 +59,8 @@ fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
fn render_spa_no_404(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
|
fn render_spa2(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
|
||||||
let resp = reqwest::blocking::get(format!("{}/{}", server.url(), DIR_NO_INDEX))?;
|
let resp = reqwest::blocking::get(format!("{}{}", server.url(), DIR_NO_FOUND))?;
|
||||||
let text = resp.text()?;
|
let text = resp.text()?;
|
||||||
assert_eq!(text, "This is index.html");
|
assert_eq!(text, "This is index.html");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user