mirror of
https://github.com/sigoden/dufs.git
synced 2026-04-09 17:13:02 +03:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5142430e93 | ||
|
|
dd8b21f3a6 | ||
|
|
471bca86c6 | ||
|
|
6b01c143d9 | ||
|
|
127c90a45e | ||
|
|
b3890ea094 | ||
|
|
99f0de6ca0 | ||
|
|
b48a7473fc | ||
|
|
a84c3b353d | ||
|
|
e9383d71ed | ||
|
|
8258dabe4a | ||
|
|
0e236b61f6 | ||
|
|
09788ed031 | ||
|
|
46ebe978ae | ||
|
|
e01f2030e1 | ||
|
|
8d03ec151a | ||
|
|
870e92e306 | ||
|
|
261c8b6ee5 | ||
|
|
5ce7bde05c | ||
|
|
63a7b530bb |
2
.github/workflows/release.yaml
vendored
2
.github/workflows/release.yaml
vendored
@@ -145,7 +145,7 @@ jobs:
|
|||||||
uses: docker/build-push-action@v2
|
uses: docker/build-push-action@v2
|
||||||
with:
|
with:
|
||||||
push: ${{ needs.release.outputs.rc == 'false' }}
|
push: ${{ needs.release.outputs.rc == 'false' }}
|
||||||
tags: sigoden/duf:latest, sigoden/duf:${{ github.ref_name }}
|
tags: ${{ github.repository }}:latest, ${{ github.repository }}:${{ github.ref_name }}
|
||||||
|
|
||||||
publish-crate:
|
publish-crate:
|
||||||
name: Publish to crates.io
|
name: Publish to crates.io
|
||||||
|
|||||||
42
CHANGELOG.md
42
CHANGELOG.md
@@ -2,6 +2,48 @@
|
|||||||
|
|
||||||
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.16.0] - 2022-06-12
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Implement head method ([#33](https://github.com/sigoden/duf/issues/33))
|
||||||
|
- Display upload speed and time left ([#34](https://github.com/sigoden/duf/issues/34))
|
||||||
|
- Support tls-key in pkcs#8 format ([#35](https://github.com/sigoden/duf/issues/35))
|
||||||
|
- Options method return status 200
|
||||||
|
|
||||||
|
### Testing
|
||||||
|
|
||||||
|
- Add integration tests ([#36](https://github.com/sigoden/duf/issues/36))
|
||||||
|
|
||||||
|
## [0.15.1] - 2022-06-11
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- Cannot upload ([#32](https://github.com/sigoden/duf/issues/32))
|
||||||
|
|
||||||
|
## [0.15.0] - 2022-06-10
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- Encode webdav href as uri ([#28](https://github.com/sigoden/duf/issues/28))
|
||||||
|
- Query dir param
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Add basic dark theme ([#29](https://github.com/sigoden/duf/issues/29))
|
||||||
|
- Add empty state placeholder to page([#30](https://github.com/sigoden/duf/issues/30))
|
||||||
|
|
||||||
|
## [0.14.0] - 2022-06-07
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- Send index page with content-type ([#26](https://github.com/sigoden/duf/issues/26))
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Support ipv6 ([#25](https://github.com/sigoden/duf/issues/25))
|
||||||
|
- Add favicon ([#27](https://github.com/sigoden/duf/issues/27))
|
||||||
|
|
||||||
## [0.13.2] - 2022-06-06
|
## [0.13.2] - 2022-06-06
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|||||||
1410
Cargo.lock
generated
1410
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
36
Cargo.toml
36
Cargo.toml
@@ -1,13 +1,12 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "duf"
|
name = "duf"
|
||||||
version = "0.13.2"
|
version = "0.16.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."
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
homepage = "https://github.com/sigoden/duf"
|
homepage = "https://github.com/sigoden/duf"
|
||||||
repository = "https://github.com/sigoden/duf"
|
repository = "https://github.com/sigoden/duf"
|
||||||
autotests = false
|
|
||||||
categories = ["command-line-utilities", "web-programming::http-server"]
|
categories = ["command-line-utilities", "web-programming::http-server"]
|
||||||
keywords = ["static", "file", "server", "webdav", "cli"]
|
keywords = ["static", "file", "server", "webdav", "cli"]
|
||||||
|
|
||||||
@@ -25,19 +24,32 @@ serde_json = "1"
|
|||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
base64 = "0.13"
|
base64 = "0.13"
|
||||||
async_zip = "0.0.7"
|
async_zip = "0.0.7"
|
||||||
async-walkdir = "0.2.0"
|
async-walkdir = "0.2"
|
||||||
headers = "0.3.7"
|
headers = "0.3"
|
||||||
mime_guess = "2.0.4"
|
mime_guess = "2.0"
|
||||||
get_if_addrs = "0.5.3"
|
get_if_addrs = "0.5"
|
||||||
rustls = { version = "0.20", default-features = false, features = ["tls12"] }
|
rustls = { version = "0.20", default-features = false, features = ["tls12"] }
|
||||||
rustls-pemfile = "1"
|
rustls-pemfile = "1"
|
||||||
md5 = "0.7.0"
|
md5 = "0.7"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4"
|
||||||
uuid = { version = "1.1.1", features = ["v4", "fast-rng"] }
|
uuid = { version = "1.1", features = ["v4", "fast-rng"] }
|
||||||
urlencoding = "2.1.0"
|
urlencoding = "2.1"
|
||||||
xml-rs = "0.8"
|
xml-rs = "0.8"
|
||||||
env_logger = { version = "0.9.0", default-features = false, features = ["humantime"] }
|
env_logger = { version = "0.9", default-features = false, features = ["humantime"] }
|
||||||
log = "0.4.17"
|
log = "0.4"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
assert_cmd = "2"
|
||||||
|
reqwest = { version = "0.11", features = ["blocking", "multipart", "rustls-tls"], default-features = false }
|
||||||
|
assert_fs = "1"
|
||||||
|
select = "0.5"
|
||||||
|
port_check = "0.1"
|
||||||
|
rstest = "0.13"
|
||||||
|
regex = "1"
|
||||||
|
pretty_assertions = "1.2"
|
||||||
|
url = "2"
|
||||||
|
diqwest = { version = "1", features = ["blocking"] }
|
||||||
|
predicates = "2"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
|
|||||||
BIN
assets/favicon.ico
Executable file
BIN
assets/favicon.ico
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 9.1 KiB |
@@ -93,10 +93,20 @@ body {
|
|||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.upload-status span {
|
||||||
|
width: 70px;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
|
||||||
.main {
|
.main {
|
||||||
padding: 0 1em;
|
padding: 0 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.empty-folder {
|
||||||
|
padding-top: 1rem;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
||||||
.uploaders-table th,
|
.uploaders-table th,
|
||||||
.paths-table th {
|
.paths-table th {
|
||||||
text-align: left;
|
text-align: left;
|
||||||
@@ -173,3 +183,39 @@ body {
|
|||||||
.uploader {
|
.uploader {
|
||||||
padding-right: 1em;
|
padding-right: 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* dark theme */
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
body {
|
||||||
|
background-color: #000;
|
||||||
|
}
|
||||||
|
|
||||||
|
html,
|
||||||
|
.breadcrumb>b,
|
||||||
|
.searchbar #search {
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.uploaders-table th,
|
||||||
|
.paths-table th {
|
||||||
|
color: #ddd;
|
||||||
|
}
|
||||||
|
|
||||||
|
svg,
|
||||||
|
.path svg {
|
||||||
|
fill: #d0e6ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.searchbar {
|
||||||
|
background-color: #111;
|
||||||
|
border-color: #fff6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.searchbar svg {
|
||||||
|
fill: #fff6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.path a {
|
||||||
|
color: #3191ff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -31,11 +31,12 @@
|
|||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<div class="main">
|
<div class="main">
|
||||||
|
<div class="empty-folder hidden"></div>
|
||||||
<table class="uploaders-table hidden">
|
<table class="uploaders-table hidden">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th class="cell-name">Name</th>
|
<th class="cell-name">Name</th>
|
||||||
<th class="cell-status">Status</th>
|
<th class="cell-status">Speed - Progress - Time Left</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -7,11 +7,29 @@
|
|||||||
* @property {number} size
|
* @property {number} size
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// https://stackoverflow.com/a/901144/3642588
|
||||||
|
const params = new Proxy(new URLSearchParams(window.location.search), {
|
||||||
|
get: (searchParams, prop) => searchParams.get(prop),
|
||||||
|
});
|
||||||
|
|
||||||
|
const dirEmptyNote = params.q ? 'No results' : DATA.dir_exists ? 'Empty folder' : 'Folder will be created when a file is uploaded';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type Element
|
* @type Element
|
||||||
*/
|
*/
|
||||||
let $pathsTable, $pathsTableBody, $uploadersTable;
|
let $pathsTable;
|
||||||
|
/**
|
||||||
|
* @type Element
|
||||||
|
*/
|
||||||
|
let $pathsTableBody;
|
||||||
|
/**
|
||||||
|
* @type Element
|
||||||
|
*/
|
||||||
|
let $uploadersTable;
|
||||||
|
/**
|
||||||
|
* @type Element
|
||||||
|
*/
|
||||||
|
let $emptyFolder;
|
||||||
/**
|
/**
|
||||||
* @type string
|
* @type string
|
||||||
*/
|
*/
|
||||||
@@ -34,6 +52,14 @@ class Uploader {
|
|||||||
* @type Element
|
* @type Element
|
||||||
*/
|
*/
|
||||||
$uploadStatus;
|
$uploadStatus;
|
||||||
|
/**
|
||||||
|
* @type number
|
||||||
|
*/
|
||||||
|
uploaded = 0;
|
||||||
|
/**
|
||||||
|
* @type number
|
||||||
|
*/
|
||||||
|
lastUptime = 0;
|
||||||
static globalIdx = 0;
|
static globalIdx = 0;
|
||||||
constructor(file, dirs) {
|
constructor(file, dirs) {
|
||||||
this.name = [...dirs, file.name].join("/");
|
this.name = [...dirs, file.name].join("/");
|
||||||
@@ -50,10 +76,12 @@ class Uploader {
|
|||||||
<div>${getSvg("File")}</div>
|
<div>${getSvg("File")}</div>
|
||||||
<a href="${url}">${name}</a>
|
<a href="${url}">${name}</a>
|
||||||
</td>
|
</td>
|
||||||
<td class="cell-status" id="uploadStatus${idx}"></td>
|
<td class="cell-status upload-status" id="uploadStatus${idx}"></td>
|
||||||
</tr>`);
|
</tr>`);
|
||||||
$uploadersTable.classList.remove("hidden");
|
$uploadersTable.classList.remove("hidden");
|
||||||
|
$emptyFolder.classList.add("hidden");
|
||||||
this.$uploadStatus = document.getElementById(`uploadStatus${idx}`);
|
this.$uploadStatus = document.getElementById(`uploadStatus${idx}`);
|
||||||
|
this.lastUptime = Date.now();
|
||||||
|
|
||||||
const ajax = new XMLHttpRequest();
|
const ajax = new XMLHttpRequest();
|
||||||
ajax.upload.addEventListener("progress", e => this.progress(e), false);
|
ajax.upload.addEventListener("progress", e => this.progress(e), false);
|
||||||
@@ -73,8 +101,15 @@ class Uploader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
progress(event) {
|
progress(event) {
|
||||||
const percent = (event.loaded / event.total) * 100;
|
let now = Date.now();
|
||||||
this.$uploadStatus.innerHTML = `${percent.toFixed(2)}%`;
|
let speed = (event.loaded - this.uploaded) / (now - this.lastUptime) * 1000;
|
||||||
|
let [speedValue, speedUnit] = formatSize(speed);
|
||||||
|
const speedText = `${speedValue}${speedUnit.toLowerCase()}/s`;
|
||||||
|
const progress = formatPercent((event.loaded / event.total) * 100);
|
||||||
|
const duration = formatDuration((event.total - event.loaded) / speed)
|
||||||
|
this.$uploadStatus.innerHTML = `<span>${speedText}</span><span>${progress}</span><span>${duration}</span>`;
|
||||||
|
this.uploaded = event.loaded;
|
||||||
|
this.lastUptime = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
complete() {
|
complete() {
|
||||||
@@ -155,7 +190,7 @@ function addPath(file, index) {
|
|||||||
<a href="${url}" title="${file.name}">${file.name}</a>
|
<a href="${url}" title="${file.name}">${file.name}</a>
|
||||||
</td>
|
</td>
|
||||||
<td class="cell-mtime">${formatMtime(file.mtime)}</td>
|
<td class="cell-mtime">${formatMtime(file.mtime)}</td>
|
||||||
<td class="cell-size">${formatSize(file.size)}</td>
|
<td class="cell-size">${formatSize(file.size).join(" ")}</td>
|
||||||
${actionCell}
|
${actionCell}
|
||||||
</tr>`)
|
</tr>`)
|
||||||
}
|
}
|
||||||
@@ -180,6 +215,8 @@ async function deletePath(index) {
|
|||||||
DATA.paths[index] = null;
|
DATA.paths[index] = null;
|
||||||
if (!DATA.paths.find(v => !!v)) {
|
if (!DATA.paths.find(v => !!v)) {
|
||||||
$pathsTable.classList.add("hidden");
|
$pathsTable.classList.add("hidden");
|
||||||
|
$emptyFolder.textContent = dirEmptyNote;
|
||||||
|
$emptyFolder.classList.remove("hidden");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error(await res.text())
|
throw new Error(await res.text())
|
||||||
@@ -263,17 +300,38 @@ function padZero(value, size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function formatSize(size) {
|
function formatSize(size) {
|
||||||
if (!size) return ""
|
if (!size) return []
|
||||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||||
if (size == 0) return '0 Byte';
|
if (size == 0) return [0, "Byte"];
|
||||||
const i = parseInt(Math.floor(Math.log(size) / Math.log(1024)));
|
const i = parseInt(Math.floor(Math.log(size) / Math.log(1024)));
|
||||||
return Math.round(size / Math.pow(1024, i), 2) + ' ' + sizes[i];
|
return [Math.round(size / Math.pow(1024, i), 2), sizes[i]];
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatDuration(seconds) {
|
||||||
|
seconds = Math.ceil(seconds);
|
||||||
|
let h = Math.floor(seconds / 3600);
|
||||||
|
let m = Math.floor((seconds - h * 3600) / 60);
|
||||||
|
let s = seconds - h * 3600 - m * 60
|
||||||
|
return `${padZero(h, 2)}:${padZero(m, 2)}:${padZero(s, 2)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatPercent(precent) {
|
||||||
|
if (precent > 10) {
|
||||||
|
return precent.toFixed(1) + "%";
|
||||||
|
} else {
|
||||||
|
return precent.toFixed(2) + "%";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ready() {
|
function ready() {
|
||||||
$pathsTable = document.querySelector(".paths-table")
|
$pathsTable = document.querySelector(".paths-table")
|
||||||
$pathsTableBody = document.querySelector(".paths-table tbody");
|
$pathsTableBody = document.querySelector(".paths-table tbody");
|
||||||
$uploadersTable = document.querySelector(".uploaders-table");
|
$uploadersTable = document.querySelector(".uploaders-table");
|
||||||
|
$emptyFolder = document.querySelector(".empty-folder");
|
||||||
|
|
||||||
|
if (params.q) {
|
||||||
|
document.getElementById('search').value = params.q;
|
||||||
|
}
|
||||||
|
|
||||||
addBreadcrumb(DATA.breadcrumb);
|
addBreadcrumb(DATA.breadcrumb);
|
||||||
if (Array.isArray(DATA.paths)) {
|
if (Array.isArray(DATA.paths)) {
|
||||||
@@ -284,6 +342,10 @@ function ready() {
|
|||||||
for (let i = 0; i < len; i++) {
|
for (let i = 0; i < len; i++) {
|
||||||
addPath(DATA.paths[i], i);
|
addPath(DATA.paths[i], i);
|
||||||
}
|
}
|
||||||
|
if (len == 0) {
|
||||||
|
$emptyFolder.textContent = dirEmptyNote;
|
||||||
|
$emptyFolder.classList.remove("hidden");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (DATA.allow_upload) {
|
if (DATA.allow_upload) {
|
||||||
dropzone();
|
dropzone();
|
||||||
|
|||||||
50
src/args.rs
50
src/args.rs
@@ -1,7 +1,7 @@
|
|||||||
use clap::crate_description;
|
use clap::crate_description;
|
||||||
use clap::{Arg, ArgMatches};
|
use clap::{Arg, ArgMatches};
|
||||||
use rustls::{Certificate, PrivateKey};
|
use rustls::{Certificate, PrivateKey};
|
||||||
use std::net::SocketAddr;
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::{env, fs, io};
|
use std::{env, fs, io};
|
||||||
|
|
||||||
@@ -111,8 +111,7 @@ pub fn matches() -> ArgMatches {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
pub address: String,
|
pub addr: SocketAddr,
|
||||||
pub port: u16,
|
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub path_prefix: String,
|
pub path_prefix: String,
|
||||||
pub uri_prefix: String,
|
pub uri_prefix: String,
|
||||||
@@ -133,8 +132,9 @@ impl Args {
|
|||||||
/// If a parsing error ocurred, exit the process and print out informative
|
/// If a parsing error ocurred, exit the process and print out informative
|
||||||
/// error message to user.
|
/// error message to user.
|
||||||
pub fn parse(matches: ArgMatches) -> BoxResult<Args> {
|
pub fn parse(matches: ArgMatches) -> BoxResult<Args> {
|
||||||
let address = matches.value_of("address").unwrap_or_default().to_owned();
|
let ip = matches.value_of("address").unwrap_or_default();
|
||||||
let port = matches.value_of_t::<u16>("port")?;
|
let port = matches.value_of_t::<u16>("port")?;
|
||||||
|
let addr = to_addr(ip, port)?;
|
||||||
let path = Args::parse_path(matches.value_of_os("path").unwrap_or_default())?;
|
let path = Args::parse_path(matches.value_of_os("path").unwrap_or_default())?;
|
||||||
let path_prefix = matches
|
let path_prefix = matches
|
||||||
.value_of("path-prefix")
|
.value_of("path-prefix")
|
||||||
@@ -166,8 +166,7 @@ impl Args {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Ok(Args {
|
Ok(Args {
|
||||||
address,
|
addr,
|
||||||
port,
|
|
||||||
path,
|
path,
|
||||||
path_prefix,
|
path_prefix,
|
||||||
uri_prefix,
|
uri_prefix,
|
||||||
@@ -197,43 +196,44 @@ impl Args {
|
|||||||
})
|
})
|
||||||
.map_err(|err| format!("Failed to access path `{}`: {}", path.display(), err,).into())
|
.map_err(|err| format!("Failed to access path `{}`: {}", path.display(), err,).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct socket address from arguments.
|
|
||||||
pub fn address(&self) -> BoxResult<SocketAddr> {
|
|
||||||
format!("{}:{}", self.address, self.port)
|
|
||||||
.parse()
|
|
||||||
.map_err(|_| format!("Invalid bind address `{}:{}`", self.address, self.port).into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn to_addr(ip: &str, port: u16) -> BoxResult<SocketAddr> {
|
||||||
|
let ip: IpAddr = ip.parse()?;
|
||||||
|
Ok(SocketAddr::new(ip, port))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load public certificate from file.
|
// Load public certificate from file.
|
||||||
pub fn load_certs(filename: &str) -> BoxResult<Vec<Certificate>> {
|
fn load_certs(filename: &str) -> BoxResult<Vec<Certificate>> {
|
||||||
// Open certificate file.
|
// Open certificate file.
|
||||||
let certfile =
|
let certfile = fs::File::open(&filename)
|
||||||
fs::File::open(&filename).map_err(|e| format!("Failed to open {}: {}", &filename, e))?;
|
.map_err(|e| format!("Failed to access `{}`, {}", &filename, e))?;
|
||||||
let mut reader = io::BufReader::new(certfile);
|
let mut reader = io::BufReader::new(certfile);
|
||||||
|
|
||||||
// Load and return certificate.
|
// Load and return certificate.
|
||||||
let certs = rustls_pemfile::certs(&mut reader).map_err(|_| "Failed to load certificate")?;
|
let certs = rustls_pemfile::certs(&mut reader).map_err(|_| "Failed to load certificate")?;
|
||||||
if certs.is_empty() {
|
if certs.is_empty() {
|
||||||
return Err("Expected at least one certificate".into());
|
return Err("No supported certificate in file".into());
|
||||||
}
|
}
|
||||||
Ok(certs.into_iter().map(Certificate).collect())
|
Ok(certs.into_iter().map(Certificate).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load private key from file.
|
// Load private key from file.
|
||||||
pub fn load_private_key(filename: &str) -> BoxResult<PrivateKey> {
|
fn load_private_key(filename: &str) -> BoxResult<PrivateKey> {
|
||||||
// Open keyfile.
|
// Open keyfile.
|
||||||
let keyfile =
|
let keyfile = fs::File::open(&filename)
|
||||||
fs::File::open(&filename).map_err(|e| format!("Failed to open {}: {}", &filename, e))?;
|
.map_err(|e| format!("Failed to access `{}`, {}", &filename, e))?;
|
||||||
let mut reader = io::BufReader::new(keyfile);
|
let mut reader = io::BufReader::new(keyfile);
|
||||||
|
|
||||||
// Load and return a single private key.
|
// Load and return a single private key.
|
||||||
let keys = rustls_pemfile::rsa_private_keys(&mut reader)
|
let keys = rustls_pemfile::read_all(&mut reader)
|
||||||
.map_err(|e| format!("There was a problem with reading private key: {:?}", e))?;
|
.map_err(|e| format!("There was a problem with reading private key: {:?}", e))?
|
||||||
|
.into_iter()
|
||||||
|
.find_map(|item| match item {
|
||||||
|
rustls_pemfile::Item::RSAKey(key) | rustls_pemfile::Item::PKCS8Key(key) => Some(key),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.ok_or("No supported private key in file")?;
|
||||||
|
|
||||||
if keys.len() != 1 {
|
Ok(PrivateKey(keys))
|
||||||
return Err("Expected a single private key".into());
|
|
||||||
}
|
|
||||||
Ok(PrivateKey(keys[0].to_owned()))
|
|
||||||
}
|
}
|
||||||
|
|||||||
269
src/server.rs
269
src/server.rs
@@ -25,7 +25,7 @@ use rustls::ServerConfig;
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use std::fs::Metadata;
|
use std::fs::Metadata;
|
||||||
use std::net::IpAddr;
|
use std::net::{IpAddr, 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;
|
||||||
@@ -44,6 +44,7 @@ type Response = hyper::Response<Body>;
|
|||||||
const INDEX_HTML: &str = include_str!("../assets/index.html");
|
const INDEX_HTML: &str = include_str!("../assets/index.html");
|
||||||
const INDEX_CSS: &str = include_str!("../assets/index.css");
|
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 INDEX_NAME: &str = "index.html";
|
const INDEX_NAME: &str = "index.html";
|
||||||
const BUF_SIZE: usize = 1024 * 16;
|
const BUF_SIZE: usize = 1024 * 16;
|
||||||
|
|
||||||
@@ -56,7 +57,6 @@ macro_rules! status {
|
|||||||
|
|
||||||
pub async fn serve(args: Args) -> BoxResult<()> {
|
pub async fn serve(args: Args) -> BoxResult<()> {
|
||||||
let args = Arc::new(args);
|
let args = Arc::new(args);
|
||||||
let socket_addr = args.address()?;
|
|
||||||
let inner = Arc::new(InnerService::new(args.clone()));
|
let inner = Arc::new(InnerService::new(args.clone()));
|
||||||
match args.tls.clone() {
|
match args.tls.clone() {
|
||||||
Some((certs, key)) => {
|
Some((certs, key)) => {
|
||||||
@@ -66,7 +66,7 @@ pub async fn serve(args: Args) -> BoxResult<()> {
|
|||||||
.with_single_cert(certs, key)?;
|
.with_single_cert(certs, key)?;
|
||||||
let tls_acceptor = TlsAcceptor::from(Arc::new(config));
|
let tls_acceptor = TlsAcceptor::from(Arc::new(config));
|
||||||
let arc_acceptor = Arc::new(tls_acceptor);
|
let arc_acceptor = Arc::new(tls_acceptor);
|
||||||
let listener = TcpListener::bind(&socket_addr).await?;
|
let listener = TcpListener::bind(&args.addr).await?;
|
||||||
let incoming = tokio_stream::wrappers::TcpListenerStream::new(listener);
|
let incoming = tokio_stream::wrappers::TcpListenerStream::new(listener);
|
||||||
let incoming =
|
let incoming =
|
||||||
hyper::server::accept::from_stream(incoming.filter_map(|socket| async {
|
hyper::server::accept::from_stream(incoming.filter_map(|socket| async {
|
||||||
@@ -87,11 +87,11 @@ pub async fn serve(args: Args) -> BoxResult<()> {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
print_listening(args.address.as_str(), args.port, &args.uri_prefix, true);
|
print_listening(&args.addr, &args.uri_prefix, true);
|
||||||
server.await?;
|
server.await?;
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let server = hyper::Server::try_bind(&socket_addr)?.serve(make_service_fn(move |_| {
|
let server = hyper::Server::try_bind(&args.addr)?.serve(make_service_fn(move |_| {
|
||||||
let inner = inner.clone();
|
let inner = inner.clone();
|
||||||
async move {
|
async move {
|
||||||
Ok::<_, Infallible>(service_fn(move |req| {
|
Ok::<_, Infallible>(service_fn(move |req| {
|
||||||
@@ -100,7 +100,7 @@ pub async fn serve(args: Args) -> BoxResult<()> {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
print_listening(args.address.as_str(), args.port, &args.uri_prefix, false);
|
print_listening(&args.addr, &args.uri_prefix, false);
|
||||||
server.await?;
|
server.await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,6 +149,13 @@ impl InnerService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let req_path = req.uri().path();
|
let req_path = req.uri().path();
|
||||||
|
let headers = req.headers();
|
||||||
|
let method = req.method().clone();
|
||||||
|
|
||||||
|
if req_path == "/favicon.ico" && method == Method::GET {
|
||||||
|
self.handle_send_favicon(req.headers(), &mut res).await?;
|
||||||
|
return Ok(res);
|
||||||
|
}
|
||||||
|
|
||||||
let path = match self.extract_path(req_path) {
|
let path = match self.extract_path(req_path) {
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
@@ -176,41 +183,43 @@ impl InnerService {
|
|||||||
return Ok(res);
|
return Ok(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
let headers = req.headers();
|
match method {
|
||||||
|
Method::GET | Method::HEAD => {
|
||||||
match req.method() {
|
let head_only = method == Method::HEAD;
|
||||||
&Method::GET => {
|
|
||||||
if is_dir {
|
if is_dir {
|
||||||
if render_index || render_spa {
|
if render_index || render_spa {
|
||||||
self.handle_render_index(path, headers, &mut res).await?;
|
self.handle_render_index(path, headers, head_only, &mut res)
|
||||||
|
.await?;
|
||||||
} else if query == "zip" {
|
} else if query == "zip" {
|
||||||
self.handle_zip_dir(path, &mut res).await?;
|
self.handle_zip_dir(path, head_only, &mut res).await?;
|
||||||
} else if query.starts_with("q=") {
|
} else if let Some(q) = query.strip_prefix("q=") {
|
||||||
self.handle_query_dir(path, &query[3..], &mut res).await?;
|
self.handle_query_dir(path, q, head_only, &mut res).await?;
|
||||||
} else {
|
} else {
|
||||||
self.handle_ls_dir(path, true, &mut res).await?;
|
self.handle_ls_dir(path, true, head_only, &mut res).await?;
|
||||||
}
|
}
|
||||||
} else if is_file {
|
} else if is_file {
|
||||||
self.handle_send_file(path, headers, &mut res).await?;
|
self.handle_send_file(path, headers, head_only, &mut res)
|
||||||
|
.await?;
|
||||||
} else if render_spa {
|
} else if render_spa {
|
||||||
self.handle_render_spa(path, headers, &mut res).await?;
|
self.handle_render_spa(path, headers, head_only, &mut res)
|
||||||
|
.await?;
|
||||||
} else if allow_upload && req_path.ends_with('/') {
|
} else if allow_upload && req_path.ends_with('/') {
|
||||||
self.handle_ls_dir(path, false, &mut res).await?;
|
self.handle_ls_dir(path, false, head_only, &mut res).await?;
|
||||||
} else {
|
} else {
|
||||||
status!(res, StatusCode::NOT_FOUND);
|
status!(res, StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&Method::OPTIONS => {
|
Method::OPTIONS => {
|
||||||
self.handle_method_options(&mut res);
|
self.handle_options(&mut res);
|
||||||
}
|
}
|
||||||
&Method::PUT => {
|
Method::PUT => {
|
||||||
if !allow_upload || (!allow_delete && is_file && size > 0) {
|
if !allow_upload || (!allow_delete && is_file && size > 0) {
|
||||||
status!(res, StatusCode::FORBIDDEN);
|
status!(res, StatusCode::FORBIDDEN);
|
||||||
} else {
|
} else {
|
||||||
self.handle_upload(path, req, &mut res).await?;
|
self.handle_upload(path, req, &mut res).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&Method::DELETE => {
|
Method::DELETE => {
|
||||||
if !allow_delete {
|
if !allow_delete {
|
||||||
status!(res, StatusCode::FORBIDDEN);
|
status!(res, StatusCode::FORBIDDEN);
|
||||||
} else if !is_miss {
|
} else if !is_miss {
|
||||||
@@ -219,13 +228,6 @@ impl InnerService {
|
|||||||
status!(res, StatusCode::NOT_FOUND);
|
status!(res, StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&Method::HEAD => {
|
|
||||||
if is_miss {
|
|
||||||
status!(res, StatusCode::NOT_FOUND);
|
|
||||||
} else {
|
|
||||||
status!(res, StatusCode::OK);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
method => match method.as_str() {
|
method => match method.as_str() {
|
||||||
"PROPFIND" => {
|
"PROPFIND" => {
|
||||||
if is_dir {
|
if is_dir {
|
||||||
@@ -243,13 +245,31 @@ impl InnerService {
|
|||||||
status!(res, StatusCode::NOT_FOUND);
|
status!(res, StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"MKCOL" if allow_upload && is_miss => self.handle_mkcol(path, &mut res).await?,
|
"MKCOL" => {
|
||||||
"COPY" if allow_upload && !is_miss => {
|
if !allow_upload || !is_miss {
|
||||||
|
status!(res, StatusCode::FORBIDDEN);
|
||||||
|
} else {
|
||||||
|
self.handle_mkcol(path, &mut res).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"COPY" => {
|
||||||
|
if !allow_upload {
|
||||||
|
status!(res, StatusCode::FORBIDDEN);
|
||||||
|
} else if is_miss {
|
||||||
|
status!(res, StatusCode::NOT_FOUND);
|
||||||
|
} else {
|
||||||
self.handle_copy(path, headers, &mut res).await?
|
self.handle_copy(path, headers, &mut res).await?
|
||||||
}
|
}
|
||||||
"MOVE" if allow_upload && allow_delete && !is_miss => {
|
}
|
||||||
|
"MOVE" => {
|
||||||
|
if !allow_upload || !allow_delete {
|
||||||
|
status!(res, StatusCode::FORBIDDEN);
|
||||||
|
} else if is_miss {
|
||||||
|
status!(res, StatusCode::NOT_FOUND);
|
||||||
|
} else {
|
||||||
self.handle_move(path, headers, &mut res).await?
|
self.handle_move(path, headers, &mut res).await?
|
||||||
}
|
}
|
||||||
|
}
|
||||||
"LOCK" => {
|
"LOCK" => {
|
||||||
// Fake lock
|
// Fake lock
|
||||||
if is_file {
|
if is_file {
|
||||||
@@ -282,7 +302,13 @@ impl InnerService {
|
|||||||
) -> BoxResult<()> {
|
) -> BoxResult<()> {
|
||||||
ensure_path_parent(path).await?;
|
ensure_path_parent(path).await?;
|
||||||
|
|
||||||
let mut file = fs::File::create(&path).await?;
|
let mut file = match fs::File::create(&path).await {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(_) => {
|
||||||
|
status!(res, StatusCode::FORBIDDEN);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let body_with_io_error = req
|
let body_with_io_error = req
|
||||||
.body_mut()
|
.body_mut()
|
||||||
@@ -308,7 +334,13 @@ impl InnerService {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_ls_dir(&self, path: &Path, exist: bool, res: &mut Response) -> BoxResult<()> {
|
async fn handle_ls_dir(
|
||||||
|
&self,
|
||||||
|
path: &Path,
|
||||||
|
exist: bool,
|
||||||
|
head_only: bool,
|
||||||
|
res: &mut Response,
|
||||||
|
) -> BoxResult<()> {
|
||||||
let mut paths = vec![];
|
let mut paths = vec![];
|
||||||
if exist {
|
if exist {
|
||||||
paths = match self.list_dir(path, path).await {
|
paths = match self.list_dir(path, path).await {
|
||||||
@@ -319,13 +351,14 @@ impl InnerService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.send_index(path, paths, res)
|
self.send_index(path, paths, exist, head_only, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_query_dir(
|
async fn handle_query_dir(
|
||||||
&self,
|
&self,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
query: &str,
|
query: &str,
|
||||||
|
head_only: bool,
|
||||||
res: &mut Response,
|
res: &mut Response,
|
||||||
) -> BoxResult<()> {
|
) -> BoxResult<()> {
|
||||||
let mut paths: Vec<PathItem> = vec![];
|
let mut paths: Vec<PathItem> = vec![];
|
||||||
@@ -348,23 +381,20 @@ impl InnerService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.send_index(path, paths, res)
|
self.send_index(path, paths, true, head_only, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_zip_dir(&self, path: &Path, res: &mut Response) -> BoxResult<()> {
|
async fn handle_zip_dir(
|
||||||
|
&self,
|
||||||
|
path: &Path,
|
||||||
|
head_only: bool,
|
||||||
|
res: &mut Response,
|
||||||
|
) -> BoxResult<()> {
|
||||||
let (mut writer, reader) = tokio::io::duplex(BUF_SIZE);
|
let (mut writer, reader) = tokio::io::duplex(BUF_SIZE);
|
||||||
let filename = path
|
let filename = path
|
||||||
.file_name()
|
.file_name()
|
||||||
.and_then(|v| v.to_str())
|
.and_then(|v| v.to_str())
|
||||||
.ok_or_else(|| format!("Failed to get name of `{}`", path.display()))?;
|
.ok_or_else(|| format!("Failed to get name of `{}`", path.display()))?;
|
||||||
let path = path.to_owned();
|
|
||||||
tokio::spawn(async move {
|
|
||||||
if let Err(e) = zip_dir(&mut writer, &path).await {
|
|
||||||
error!("Failed to zip {}, {}", path.display(), e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let stream = ReaderStream::new(reader);
|
|
||||||
*res.body_mut() = Body::wrap_stream(stream);
|
|
||||||
res.headers_mut().insert(
|
res.headers_mut().insert(
|
||||||
CONTENT_DISPOSITION,
|
CONTENT_DISPOSITION,
|
||||||
HeaderValue::from_str(&format!(
|
HeaderValue::from_str(&format!(
|
||||||
@@ -373,6 +403,19 @@ impl InnerService {
|
|||||||
))
|
))
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
res.headers_mut()
|
||||||
|
.insert("content-type", "application/zip".parse().unwrap());
|
||||||
|
if head_only {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let path = path.to_owned();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if let Err(e) = zip_dir(&mut writer, &path).await {
|
||||||
|
error!("Failed to zip {}, {}", path.display(), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let stream = ReaderStream::new(reader);
|
||||||
|
*res.body_mut() = Body::wrap_stream(stream);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,6 +423,7 @@ impl InnerService {
|
|||||||
&self,
|
&self,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
headers: &HeaderMap<HeaderValue>,
|
headers: &HeaderMap<HeaderValue>,
|
||||||
|
head_only: bool,
|
||||||
res: &mut Response,
|
res: &mut Response,
|
||||||
) -> BoxResult<()> {
|
) -> BoxResult<()> {
|
||||||
let path = path.join(INDEX_NAME);
|
let path = path.join(INDEX_NAME);
|
||||||
@@ -389,7 +433,8 @@ impl InnerService {
|
|||||||
.map(|v| v.is_file())
|
.map(|v| v.is_file())
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
{
|
{
|
||||||
self.handle_send_file(&path, headers, res).await?;
|
self.handle_send_file(&path, headers, head_only, res)
|
||||||
|
.await?;
|
||||||
} else {
|
} else {
|
||||||
status!(res, StatusCode::NOT_FOUND);
|
status!(res, StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
@@ -400,21 +445,43 @@ impl InnerService {
|
|||||||
&self,
|
&self,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
headers: &HeaderMap<HeaderValue>,
|
headers: &HeaderMap<HeaderValue>,
|
||||||
|
head_only: bool,
|
||||||
res: &mut Response,
|
res: &mut Response,
|
||||||
) -> BoxResult<()> {
|
) -> BoxResult<()> {
|
||||||
if path.extension().is_none() {
|
if path.extension().is_none() {
|
||||||
let path = self.args.path.join(INDEX_NAME);
|
let path = self.args.path.join(INDEX_NAME);
|
||||||
self.handle_send_file(&path, headers, res).await?;
|
self.handle_send_file(&path, headers, head_only, res)
|
||||||
|
.await?;
|
||||||
} else {
|
} else {
|
||||||
status!(res, StatusCode::NOT_FOUND);
|
status!(res, StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn handle_send_favicon(
|
||||||
|
&self,
|
||||||
|
headers: &HeaderMap<HeaderValue>,
|
||||||
|
res: &mut Response,
|
||||||
|
) -> BoxResult<()> {
|
||||||
|
let path = self.args.path.join("favicon.ico");
|
||||||
|
let meta = fs::metadata(&path).await.ok();
|
||||||
|
let is_file = meta.map(|v| v.is_file()).unwrap_or_default();
|
||||||
|
if is_file {
|
||||||
|
self.handle_send_file(path.as_path(), headers, false, res)
|
||||||
|
.await?;
|
||||||
|
} else {
|
||||||
|
*res.body_mut() = Body::from(FAVICON_ICO);
|
||||||
|
res.headers_mut()
|
||||||
|
.insert("content-type", "image/x-icon".parse().unwrap());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn handle_send_file(
|
async fn handle_send_file(
|
||||||
&self,
|
&self,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
headers: &HeaderMap<HeaderValue>,
|
headers: &HeaderMap<HeaderValue>,
|
||||||
|
head_only: bool,
|
||||||
res: &mut Response,
|
res: &mut Response,
|
||||||
) -> 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),);
|
||||||
@@ -463,6 +530,13 @@ impl InnerService {
|
|||||||
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));
|
||||||
}
|
}
|
||||||
|
res.headers_mut().typed_insert(AcceptRanges::bytes());
|
||||||
|
res.headers_mut()
|
||||||
|
.typed_insert(ContentLength(meta.len() as u64));
|
||||||
|
if head_only {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
let body = if let Some((begin, end)) = file_range {
|
let body = if let Some((begin, end)) = file_range {
|
||||||
file.seek(io::SeekFrom::Start(begin)).await?;
|
file.seek(io::SeekFrom::Start(begin)).await?;
|
||||||
let stream = FramedRead::new(file.take(end - begin + 1), BytesCodec::new());
|
let stream = FramedRead::new(file.take(end - begin + 1), BytesCodec::new());
|
||||||
@@ -472,14 +546,10 @@ impl InnerService {
|
|||||||
Body::wrap_stream(stream)
|
Body::wrap_stream(stream)
|
||||||
};
|
};
|
||||||
*res.body_mut() = body;
|
*res.body_mut() = body;
|
||||||
res.headers_mut().typed_insert(AcceptRanges::bytes());
|
|
||||||
res.headers_mut()
|
|
||||||
.typed_insert(ContentLength(meta.len() as u64));
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_method_options(&self, res: &mut Response) {
|
fn handle_options(&self, res: &mut Response) {
|
||||||
res.headers_mut().insert(
|
res.headers_mut().insert(
|
||||||
"Allow",
|
"Allow",
|
||||||
"GET,HEAD,PUT,OPTIONS,DELETE,PROPFIND,COPY,MOVE"
|
"GET,HEAD,PUT,OPTIONS,DELETE,PROPFIND,COPY,MOVE"
|
||||||
@@ -487,8 +557,6 @@ impl InnerService {
|
|||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
res.headers_mut().insert("DAV", "1".parse().unwrap());
|
res.headers_mut().insert("DAV", "1".parse().unwrap());
|
||||||
|
|
||||||
status!(res, StatusCode::NO_CONTENT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_propfind_dir(
|
async fn handle_propfind_dir(
|
||||||
@@ -505,10 +573,10 @@ impl InnerService {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => 0,
|
None => 1,
|
||||||
};
|
};
|
||||||
let mut paths = vec![self.to_pathitem(path, &self.args.path).await?.unwrap()];
|
let mut paths = vec![self.to_pathitem(path, &self.args.path).await?.unwrap()];
|
||||||
if depth > 0 {
|
if depth != 0 {
|
||||||
match self.list_dir(path, &self.args.path).await {
|
match self.list_dir(path, &self.args.path).await {
|
||||||
Ok(child) => paths.extend(child),
|
Ok(child) => paths.extend(child),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@@ -559,7 +627,7 @@ impl InnerService {
|
|||||||
|
|
||||||
let meta = fs::symlink_metadata(path).await?;
|
let meta = fs::symlink_metadata(path).await?;
|
||||||
if meta.is_dir() {
|
if meta.is_dir() {
|
||||||
status!(res, StatusCode::BAD_REQUEST);
|
status!(res, StatusCode::FORBIDDEN);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -638,6 +706,8 @@ impl InnerService {
|
|||||||
&self,
|
&self,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
mut paths: Vec<PathItem>,
|
mut paths: Vec<PathItem>,
|
||||||
|
exist: bool,
|
||||||
|
head_only: bool,
|
||||||
res: &mut Response,
|
res: &mut Response,
|
||||||
) -> BoxResult<()> {
|
) -> BoxResult<()> {
|
||||||
paths.sort_unstable();
|
paths.sort_unstable();
|
||||||
@@ -650,6 +720,7 @@ impl InnerService {
|
|||||||
paths,
|
paths,
|
||||||
allow_upload: self.args.allow_upload,
|
allow_upload: self.args.allow_upload,
|
||||||
allow_delete: self.args.allow_delete,
|
allow_delete: self.args.allow_delete,
|
||||||
|
dir_exists: exist,
|
||||||
};
|
};
|
||||||
let data = serde_json::to_string(&data).unwrap();
|
let data = serde_json::to_string(&data).unwrap();
|
||||||
let output = INDEX_HTML.replace(
|
let output = INDEX_HTML.replace(
|
||||||
@@ -658,7 +729,10 @@ impl InnerService {
|
|||||||
r#"
|
r#"
|
||||||
<title>Files in {}/ - Duf</title>
|
<title>Files in {}/ - Duf</title>
|
||||||
<style>{}</style>
|
<style>{}</style>
|
||||||
<script>var DATA = {}; {}</script>
|
<script>
|
||||||
|
const DATA =
|
||||||
|
{}
|
||||||
|
{}</script>
|
||||||
"#,
|
"#,
|
||||||
rel_path.display(),
|
rel_path.display(),
|
||||||
INDEX_CSS,
|
INDEX_CSS,
|
||||||
@@ -666,8 +740,14 @@ impl InnerService {
|
|||||||
INDEX_JS
|
INDEX_JS
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
res.headers_mut()
|
||||||
|
.typed_insert(ContentType::from(mime_guess::mime::TEXT_HTML_UTF_8));
|
||||||
|
res.headers_mut()
|
||||||
|
.typed_insert(ContentLength(output.as_bytes().len() as u64));
|
||||||
|
if head_only {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
*res.body_mut() = output.into();
|
*res.body_mut() = output.into();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -773,15 +853,9 @@ impl InnerService {
|
|||||||
PathType::Dir | PathType::SymlinkDir => None,
|
PathType::Dir | PathType::SymlinkDir => None,
|
||||||
PathType::File | PathType::SymlinkFile => Some(meta.len()),
|
PathType::File | PathType::SymlinkFile => Some(meta.len()),
|
||||||
};
|
};
|
||||||
let base_name = rel_path
|
|
||||||
.file_name()
|
|
||||||
.and_then(|v| v.to_str())
|
|
||||||
.unwrap_or("/")
|
|
||||||
.to_owned();
|
|
||||||
let name = normalize_path(rel_path);
|
let name = normalize_path(rel_path);
|
||||||
Ok(Some(PathItem {
|
Ok(Some(PathItem {
|
||||||
path_type,
|
path_type,
|
||||||
base_name,
|
|
||||||
name,
|
name,
|
||||||
mtime,
|
mtime,
|
||||||
size,
|
size,
|
||||||
@@ -795,12 +869,12 @@ struct IndexData {
|
|||||||
paths: Vec<PathItem>,
|
paths: Vec<PathItem>,
|
||||||
allow_upload: bool,
|
allow_upload: bool,
|
||||||
allow_delete: bool,
|
allow_delete: bool,
|
||||||
|
dir_exists: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd)]
|
#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
struct PathItem {
|
struct PathItem {
|
||||||
path_type: PathType,
|
path_type: PathType,
|
||||||
base_name: String,
|
|
||||||
name: String,
|
name: String,
|
||||||
mtime: u64,
|
mtime: u64,
|
||||||
size: Option<u64>,
|
size: Option<u64>,
|
||||||
@@ -809,10 +883,12 @@ struct PathItem {
|
|||||||
impl PathItem {
|
impl PathItem {
|
||||||
pub fn to_dav_xml(&self, prefix: &str) -> String {
|
pub fn to_dav_xml(&self, prefix: &str) -> String {
|
||||||
let mtime = Utc.timestamp_millis(self.mtime as i64).to_rfc2822();
|
let mtime = Utc.timestamp_millis(self.mtime as i64).to_rfc2822();
|
||||||
|
let href = encode_uri(&format!("{}{}", prefix, &self.name));
|
||||||
|
let displayname = escape_str_pcdata(self.base_name());
|
||||||
match self.path_type {
|
match self.path_type {
|
||||||
PathType::Dir | PathType::SymlinkDir => format!(
|
PathType::Dir | PathType::SymlinkDir => format!(
|
||||||
r#"<D:response>
|
r#"<D:response>
|
||||||
<D:href>{}{}</D:href>
|
<D:href>{}</D:href>
|
||||||
<D:propstat>
|
<D:propstat>
|
||||||
<D:prop>
|
<D:prop>
|
||||||
<D:displayname>{}</D:displayname>
|
<D:displayname>{}</D:displayname>
|
||||||
@@ -822,14 +898,11 @@ impl PathItem {
|
|||||||
<D:status>HTTP/1.1 200 OK</D:status>
|
<D:status>HTTP/1.1 200 OK</D:status>
|
||||||
</D:propstat>
|
</D:propstat>
|
||||||
</D:response>"#,
|
</D:response>"#,
|
||||||
escape_str_pcdata(prefix),
|
href, displayname, mtime
|
||||||
escape_str_pcdata(&self.name),
|
|
||||||
escape_str_pcdata(&self.base_name),
|
|
||||||
mtime
|
|
||||||
),
|
),
|
||||||
PathType::File | PathType::SymlinkFile => format!(
|
PathType::File | PathType::SymlinkFile => format!(
|
||||||
r#"<D:response>
|
r#"<D:response>
|
||||||
<D:href>{}{}</D:href>
|
<D:href>{}</D:href>
|
||||||
<D:propstat>
|
<D:propstat>
|
||||||
<D:prop>
|
<D:prop>
|
||||||
<D:displayname>{}</D:displayname>
|
<D:displayname>{}</D:displayname>
|
||||||
@@ -840,14 +913,19 @@ impl PathItem {
|
|||||||
<D:status>HTTP/1.1 200 OK</D:status>
|
<D:status>HTTP/1.1 200 OK</D:status>
|
||||||
</D:propstat>
|
</D:propstat>
|
||||||
</D:response>"#,
|
</D:response>"#,
|
||||||
escape_str_pcdata(prefix),
|
href,
|
||||||
escape_str_pcdata(&self.name),
|
displayname,
|
||||||
escape_str_pcdata(&self.base_name),
|
|
||||||
self.size.unwrap_or_default(),
|
self.size.unwrap_or_default(),
|
||||||
mtime
|
mtime
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn base_name(&self) -> &str {
|
||||||
|
Path::new(&self.name)
|
||||||
|
.file_name()
|
||||||
|
.and_then(|v| v.to_str())
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd)]
|
#[derive(Debug, Serialize, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
@@ -974,37 +1052,46 @@ fn to_content_range(range: &Range, complete_length: u64) -> Option<ContentRange>
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_listening(address: &str, port: u16, prefix: &str, tls: bool) {
|
fn print_listening(addr: &SocketAddr, prefix: &str, tls: bool) {
|
||||||
let prefix = encode_uri(prefix.trim_end_matches('/'));
|
let prefix = encode_uri(prefix.trim_end_matches('/'));
|
||||||
let addrs = retrieve_listening_addrs(address);
|
let addrs = retrieve_listening_addrs(addr);
|
||||||
let protocol = if tls { "https" } else { "http" };
|
let protocol = if tls { "https" } else { "http" };
|
||||||
if addrs.len() == 1 {
|
if addrs.len() == 1 {
|
||||||
eprintln!(
|
println!("Listening on {}://{}{}", protocol, addr, prefix);
|
||||||
"Listening on {}://{}:{}{}",
|
|
||||||
protocol, addrs[0], port, prefix
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Listening on:");
|
let message = addrs
|
||||||
for addr in addrs {
|
.iter()
|
||||||
eprintln!(" {}://{}:{}{}", protocol, addr, port, prefix);
|
.map(|addr| format!(" {}://{}{}", protocol, addr, prefix))
|
||||||
}
|
.collect::<Vec<String>>()
|
||||||
eprintln!();
|
.join("\n");
|
||||||
|
println!("Listening on:\n{}\n", message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn retrieve_listening_addrs(address: &str) -> Vec<String> {
|
fn retrieve_listening_addrs(addr: &SocketAddr) -> Vec<SocketAddr> {
|
||||||
if address == "0.0.0.0" {
|
let ip = addr.ip();
|
||||||
|
let port = addr.port();
|
||||||
|
if ip.is_unspecified() {
|
||||||
if let Ok(interfaces) = get_if_addrs() {
|
if let Ok(interfaces) = get_if_addrs() {
|
||||||
let mut ifaces: Vec<IpAddr> = interfaces
|
let mut ifaces: Vec<IpAddr> = interfaces
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|v| v.ip())
|
.map(|v| v.ip())
|
||||||
.filter(|v| v.is_ipv4())
|
.filter(|v| {
|
||||||
|
if ip.is_ipv4() {
|
||||||
|
v.is_ipv4()
|
||||||
|
} else {
|
||||||
|
v.is_ipv6()
|
||||||
|
}
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
ifaces.sort();
|
ifaces.sort();
|
||||||
return ifaces.into_iter().map(|v| v.to_string()).collect();
|
return ifaces
|
||||||
|
.into_iter()
|
||||||
|
.map(|v| SocketAddr::new(v, port))
|
||||||
|
.collect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vec![address.to_owned()]
|
vec![addr.to_owned()]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode_uri(v: &str) -> String {
|
fn encode_uri(v: &str) -> String {
|
||||||
|
|||||||
61
tests/allow.rs
Normal file
61
tests/allow.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use fixtures::{server, Error, TestServer};
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn default_not_allow_upload(server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}file1", server.url());
|
||||||
|
let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 403);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn default_not_allow_delete(server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}test.html", server.url());
|
||||||
|
let resp = fetch!(b"DELETE", &url).send()?;
|
||||||
|
assert_eq!(resp.status(), 403);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn default_not_exist_dir(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}404/", server.url()))?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn allow_upload_not_exist_dir(
|
||||||
|
#[with(&["--allow-upload"])] server: TestServer,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}404/", server.url()))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn allow_upload_no_override(#[with(&["--allow-upload"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}index.html", server.url());
|
||||||
|
let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 403);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn allow_delete_no_override(#[with(&["--allow-delete"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}index.html", server.url());
|
||||||
|
let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 403);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn allow_upload_delete_can_override(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}index.html", server.url());
|
||||||
|
let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 201);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
38
tests/auth.rs
Normal file
38
tests/auth.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use diqwest::blocking::WithDigestAuth;
|
||||||
|
use fixtures::{server, Error, TestServer};
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn no_auth(#[with(&["--auth", "user:pass", "-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
|
assert_eq!(resp.status(), 401);
|
||||||
|
assert!(resp.headers().contains_key("www-authenticate"));
|
||||||
|
let url = format!("{}file1", server.url());
|
||||||
|
let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 401);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn auth(#[with(&["--auth", "user:pass", "-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}file1", server.url());
|
||||||
|
let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 401);
|
||||||
|
let resp = fetch!(b"PUT", &url)
|
||||||
|
.body(b"abc".to_vec())
|
||||||
|
.send_with_digest_auth("user", "pass")?;
|
||||||
|
assert_eq!(resp.status(), 201);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn auth_skip_access(
|
||||||
|
#[with(&["--auth", "user:pass", "--no-auth-access"])] server: TestServer,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
80
tests/bind.rs
Normal file
80
tests/bind.rs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
mod fixtures;
|
||||||
|
|
||||||
|
use fixtures::{port, server, tmpdir, Error, TestServer};
|
||||||
|
|
||||||
|
use assert_cmd::prelude::*;
|
||||||
|
use assert_fs::fixture::TempDir;
|
||||||
|
use regex::Regex;
|
||||||
|
use rstest::rstest;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
use std::process::{Command, Stdio};
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case(&["-b", "20.205.243.166"])]
|
||||||
|
fn bind_fails(tmpdir: TempDir, port: u16, #[case] args: &[&str]) -> Result<(), Error> {
|
||||||
|
Command::cargo_bin("duf")?
|
||||||
|
.env("RUST_LOG", "false")
|
||||||
|
.arg(tmpdir.path())
|
||||||
|
.arg("-p")
|
||||||
|
.arg(port.to_string())
|
||||||
|
.args(args)
|
||||||
|
.assert()
|
||||||
|
.stderr(predicates::str::contains("creating server listener"))
|
||||||
|
.failure();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn bind_ipv4(server: TestServer) -> Result<(), Error> {
|
||||||
|
assert!(reqwest::blocking::get(format!("http://127.0.0.1:{}", server.port()).as_str()).is_ok());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn bind_ipv6(#[with(&["-b", "::"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
assert_eq!(
|
||||||
|
reqwest::blocking::get(format!("http://127.0.0.1:{}", server.port()).as_str()).is_ok(),
|
||||||
|
!cfg!(windows)
|
||||||
|
);
|
||||||
|
assert!(reqwest::blocking::get(format!("http://[::1]:{}", server.port()).as_str()).is_ok());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case(&[] as &[&str])]
|
||||||
|
#[case(&["--path-prefix", "/prefix"])]
|
||||||
|
fn validate_printed_urls(tmpdir: TempDir, port: u16, #[case] args: &[&str]) -> Result<(), Error> {
|
||||||
|
let mut child = Command::cargo_bin("duf")?
|
||||||
|
.env("RUST_LOG", "false")
|
||||||
|
.arg(tmpdir.path())
|
||||||
|
.arg("-p")
|
||||||
|
.arg(port.to_string())
|
||||||
|
.args(args)
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.spawn()?;
|
||||||
|
|
||||||
|
// WARN assumes urls list is terminated by an empty line
|
||||||
|
let url_lines = BufReader::new(child.stdout.take().unwrap())
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.expect("Error reading stdout"))
|
||||||
|
.take_while(|line| !line.is_empty()) /* non-empty lines */
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let url_lines = url_lines.join("\n");
|
||||||
|
|
||||||
|
let urls = Regex::new(r"http://[a-zA-Z0-9\.\[\]:/]+")
|
||||||
|
.unwrap()
|
||||||
|
.captures_iter(url_lines.as_str())
|
||||||
|
.map(|caps| caps.get(0).unwrap().as_str())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert!(!urls.is_empty());
|
||||||
|
|
||||||
|
for url in urls {
|
||||||
|
reqwest::blocking::get(url)?.error_for_status()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
child.kill()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
37
tests/cors.rs
Normal file
37
tests/cors.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use fixtures::{server, Error, TestServer};
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn cors(#[with(&["--cors"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("access-control-allow-origin").unwrap(),
|
||||||
|
"*"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("access-control-allow-headers").unwrap(),
|
||||||
|
"range, content-type, accept, origin, www-authenticate"
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn cors_options(#[with(&["--cors"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"OPTIONS", server.url()).send()?;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("access-control-allow-origin").unwrap(),
|
||||||
|
"*"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("access-control-allow-headers").unwrap(),
|
||||||
|
"range, content-type, accept, origin, www-authenticate"
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
29
tests/data/cert.pem
Normal file
29
tests/data/cert.pem
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
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
|
||||||
|
-----END CERTIFICATE-----
|
||||||
3
tests/data/generate_tls_certs.sh
Executable file
3
tests/data/generate_tls_certs.sh
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
openssl req -subj '/CN=localhost' -x509 -newkey rsa:4096 -keyout key_pkcs8.pem -out cert.pem -nodes -days 3650
|
||||||
|
openssl rsa -in key_pkcs8.pem -out key_pkcs1.pem
|
||||||
51
tests/data/key_pkcs1.pem
Normal file
51
tests/data/key_pkcs1.pem
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
-----BEGIN RSA PRIVATE KEY-----
|
||||||
|
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
|
||||||
|
-----END RSA PRIVATE KEY-----
|
||||||
52
tests/data/key_pkcs8.pem
Normal file
52
tests/data/key_pkcs8.pem
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
-----BEGIN PRIVATE KEY-----
|
||||||
|
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
|
||||||
|
afHJpnY0yZEqN1PeDaKVOCQAHbD8qQ==
|
||||||
|
-----END PRIVATE KEY-----
|
||||||
25
tests/favicon.rs
Normal file
25
tests/favicon.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use fixtures::{server, Error, TestServer};
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn default_favicon(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}favicon.ico", server.url()))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(resp.headers().get("content-type").unwrap(), "image/x-icon");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn exist_favicon(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}favicon.ico", server.url());
|
||||||
|
let data = b"abc";
|
||||||
|
let resp = fetch!(b"PUT", &url).body(data.to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 201);
|
||||||
|
let resp = reqwest::blocking::get(url)?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(resp.bytes()?, data.to_vec());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
191
tests/fixtures.rs
Normal file
191
tests/fixtures.rs
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
use assert_cmd::prelude::*;
|
||||||
|
use assert_fs::fixture::TempDir;
|
||||||
|
use assert_fs::prelude::*;
|
||||||
|
use port_check::free_local_port;
|
||||||
|
use reqwest::Url;
|
||||||
|
use rstest::fixture;
|
||||||
|
use std::process::{Child, Command, Stdio};
|
||||||
|
use std::thread::sleep;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub type Error = Box<dyn std::error::Error>;
|
||||||
|
|
||||||
|
/// File names for testing purpose
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub static FILES: &[&str] = &[
|
||||||
|
"test.txt",
|
||||||
|
"test.html",
|
||||||
|
"index.html",
|
||||||
|
"test.mkv",
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
"test \" \' & < >.csv",
|
||||||
|
"😀.data",
|
||||||
|
"⎙.mp4",
|
||||||
|
"#[]{}()@!$&'`+,;= %20.test",
|
||||||
|
#[cfg(unix)]
|
||||||
|
":?#[]{}<>()@!$&'`|*+,;= %20.test",
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
"foo\\bar.test",
|
||||||
|
];
|
||||||
|
|
||||||
|
/// Directory names for testing purpose
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub static DIR_NO_INDEX: &str = "dir-no-index/";
|
||||||
|
|
||||||
|
/// Directory names for testing purpose
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub static DIRECTORIES: &[&str] = &["dira/", "dirb/", "dirc/", DIR_NO_INDEX];
|
||||||
|
|
||||||
|
/// Name of a deeply nested file
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub static DEEPLY_NESTED_FILE: &str = "very/deeply/nested/test.rs";
|
||||||
|
|
||||||
|
/// Test fixture which creates a temporary directory with a few files and directories inside.
|
||||||
|
/// The directories also contain files.
|
||||||
|
#[fixture]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn tmpdir() -> TempDir {
|
||||||
|
let tmpdir = assert_fs::TempDir::new().expect("Couldn't create a temp dir for tests");
|
||||||
|
for file in FILES {
|
||||||
|
tmpdir
|
||||||
|
.child(file)
|
||||||
|
.write_str(&format!("This is {}", file))
|
||||||
|
.expect("Couldn't write to file");
|
||||||
|
}
|
||||||
|
for directory in DIRECTORIES {
|
||||||
|
for file in FILES {
|
||||||
|
if *directory == DIR_NO_INDEX {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
tmpdir
|
||||||
|
.child(format!("{}{}", directory, file))
|
||||||
|
.write_str(&format!("This is {}{}", directory, file))
|
||||||
|
.expect("Couldn't write to file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpdir
|
||||||
|
.child(&DEEPLY_NESTED_FILE)
|
||||||
|
.write_str("File in a deeply nested directory.")
|
||||||
|
.expect("Couldn't write to file");
|
||||||
|
tmpdir
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a free port.
|
||||||
|
#[fixture]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn port() -> u16 {
|
||||||
|
free_local_port().expect("Couldn't find a free local port")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run miniserve as a server; Start with a temporary directory, a free port and some
|
||||||
|
/// optional arguments then wait for a while for the server setup to complete.
|
||||||
|
#[fixture]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn server<I>(#[default(&[] as &[&str])] args: I) -> TestServer
|
||||||
|
where
|
||||||
|
I: IntoIterator + Clone,
|
||||||
|
I::Item: AsRef<std::ffi::OsStr>,
|
||||||
|
{
|
||||||
|
let port = port();
|
||||||
|
let tmpdir = tmpdir();
|
||||||
|
let child = Command::cargo_bin("duf")
|
||||||
|
.expect("Couldn't find test binary")
|
||||||
|
.env("RUST_LOG", "false")
|
||||||
|
.arg(tmpdir.path())
|
||||||
|
.arg("-p")
|
||||||
|
.arg(port.to_string())
|
||||||
|
.args(args.clone())
|
||||||
|
.stdout(Stdio::null())
|
||||||
|
.spawn()
|
||||||
|
.expect("Couldn't run test binary");
|
||||||
|
let is_tls = args
|
||||||
|
.into_iter()
|
||||||
|
.any(|x| x.as_ref().to_str().unwrap().contains("tls"));
|
||||||
|
|
||||||
|
wait_for_port(port);
|
||||||
|
TestServer::new(port, tmpdir, child, is_tls)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Same as `server()` but ignore stderr
|
||||||
|
#[fixture]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn server_no_stderr<I>(#[default(&[] as &[&str])] args: I) -> TestServer
|
||||||
|
where
|
||||||
|
I: IntoIterator + Clone,
|
||||||
|
I::Item: AsRef<std::ffi::OsStr>,
|
||||||
|
{
|
||||||
|
let port = port();
|
||||||
|
let tmpdir = tmpdir();
|
||||||
|
let child = Command::cargo_bin("duf")
|
||||||
|
.expect("Couldn't find test binary")
|
||||||
|
.env("RUST_LOG", "false")
|
||||||
|
.arg(tmpdir.path())
|
||||||
|
.arg("-p")
|
||||||
|
.arg(port.to_string())
|
||||||
|
.args(args.clone())
|
||||||
|
.stdout(Stdio::null())
|
||||||
|
.stderr(Stdio::null())
|
||||||
|
.spawn()
|
||||||
|
.expect("Couldn't run test binary");
|
||||||
|
let is_tls = args
|
||||||
|
.into_iter()
|
||||||
|
.any(|x| x.as_ref().to_str().unwrap().contains("tls"));
|
||||||
|
|
||||||
|
wait_for_port(port);
|
||||||
|
TestServer::new(port, tmpdir, child, is_tls)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait a max of 1s for the port to become available.
|
||||||
|
fn wait_for_port(port: u16) {
|
||||||
|
let start_wait = Instant::now();
|
||||||
|
|
||||||
|
while !port_check::is_port_reachable(format!("localhost:{}", port)) {
|
||||||
|
sleep(Duration::from_millis(100));
|
||||||
|
|
||||||
|
if start_wait.elapsed().as_secs() > 1 {
|
||||||
|
panic!("timeout waiting for port {}", port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub struct TestServer {
|
||||||
|
port: u16,
|
||||||
|
tmpdir: TempDir,
|
||||||
|
child: Child,
|
||||||
|
is_tls: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl TestServer {
|
||||||
|
pub fn new(port: u16, tmpdir: TempDir, child: Child, is_tls: bool) -> Self {
|
||||||
|
Self {
|
||||||
|
port,
|
||||||
|
tmpdir,
|
||||||
|
child,
|
||||||
|
is_tls,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn url(&self) -> Url {
|
||||||
|
let protocol = if self.is_tls { "https" } else { "http" };
|
||||||
|
Url::parse(&format!("{}://localhost:{}", protocol, self.port)).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn path(&self) -> &std::path::Path {
|
||||||
|
self.tmpdir.path()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn port(&self) -> u16 {
|
||||||
|
self.port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TestServer {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.child.kill().expect("Couldn't kill test server");
|
||||||
|
self.child.wait().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
184
tests/http.rs
Normal file
184
tests/http.rs
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use fixtures::{server, Error, TestServer};
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn get_dir(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
|
assert_index_resp!(resp);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn head_dir(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"HEAD", server.url()).send()?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("content-type").unwrap(),
|
||||||
|
"text/html; charset=utf-8"
|
||||||
|
);
|
||||||
|
assert_eq!(resp.text()?, "");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn get_dir_404(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}404/", server.url()))?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn head_dir_404(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"HEAD", format!("{}404/", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn get_dir_zip(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}?zip", server.url()))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("content-type").unwrap(),
|
||||||
|
"application/zip"
|
||||||
|
);
|
||||||
|
assert!(resp.headers().contains_key("content-disposition"));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn head_dir_zip(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"HEAD", format!("{}?zip", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("content-type").unwrap(),
|
||||||
|
"application/zip"
|
||||||
|
);
|
||||||
|
assert!(resp.headers().contains_key("content-disposition"));
|
||||||
|
assert_eq!(resp.text()?, "");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn get_dir_search(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}?q={}", server.url(), "test.html"))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
let paths = utils::retrive_index_paths(&resp.text()?);
|
||||||
|
assert!(!paths.is_empty());
|
||||||
|
for p in paths {
|
||||||
|
assert!(p.contains(&"test.html"));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn head_dir_search(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"HEAD", format!("{}?q={}", server.url(), "test.html")).send()?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("content-type").unwrap(),
|
||||||
|
"text/html; charset=utf-8"
|
||||||
|
);
|
||||||
|
assert_eq!(resp.text()?, "");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn get_file(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}index.html", server.url()))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(resp.headers().get("content-type").unwrap(), "text/html");
|
||||||
|
assert_eq!(resp.headers().get("accept-ranges").unwrap(), "bytes");
|
||||||
|
assert!(resp.headers().contains_key("etag"));
|
||||||
|
assert!(resp.headers().contains_key("last-modified"));
|
||||||
|
assert!(resp.headers().contains_key("content-length"));
|
||||||
|
assert_eq!(resp.text()?, "This is index.html");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn head_file(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"HEAD", format!("{}index.html", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(resp.headers().get("content-type").unwrap(), "text/html");
|
||||||
|
assert_eq!(resp.headers().get("accept-ranges").unwrap(), "bytes");
|
||||||
|
assert!(resp.headers().contains_key("etag"));
|
||||||
|
assert!(resp.headers().contains_key("last-modified"));
|
||||||
|
assert!(resp.headers().contains_key("content-length"));
|
||||||
|
assert_eq!(resp.text()?, "");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn get_file_404(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}404", server.url()))?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn head_file_404(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"HEAD", format!("{}404", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn options_dir(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"OPTIONS", format!("{}index.html", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get("allow").unwrap(),
|
||||||
|
"GET,HEAD,PUT,OPTIONS,DELETE,PROPFIND,COPY,MOVE"
|
||||||
|
);
|
||||||
|
assert_eq!(resp.headers().get("dav").unwrap(), "1");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn put_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}file1", server.url());
|
||||||
|
let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 201);
|
||||||
|
let resp = reqwest::blocking::get(url)?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn put_file_create_dir(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}xyz/file1", server.url());
|
||||||
|
let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 201);
|
||||||
|
let resp = reqwest::blocking::get(url)?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn put_file_conflict_dir(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}dira", server.url());
|
||||||
|
let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?;
|
||||||
|
assert_eq!(resp.status(), 403);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn delete_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let url = format!("{}test.html", server.url());
|
||||||
|
let resp = fetch!(b"DELETE", &url).send()?;
|
||||||
|
assert_eq!(resp.status(), 204);
|
||||||
|
let resp = reqwest::blocking::get(url)?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn delete_file_404(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"DELETE", format!("{}file1", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
30
tests/path_prefix.rs
Normal file
30
tests/path_prefix.rs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use fixtures::{server, Error, TestServer};
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn path_prefix_index(#[with(&["--path-prefix", "xyz"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}", server.url(), "xyz"))?;
|
||||||
|
assert_index_resp!(resp);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn path_prefix_file(#[with(&["--path-prefix", "xyz"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}/index.html", server.url(), "xyz"))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
assert_eq!(resp.text()?, "This is index.html");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn path_prefix_propfind(
|
||||||
|
#[with(&["--path-prefix", "xyz"])] server: TestServer,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"PROPFIND", format!("{}{}", server.url(), "xyz")).send()?;
|
||||||
|
let text = resp.text()?;
|
||||||
|
assert!(text.contains("<D:href>/xyz/</D:href>"));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
35
tests/render.rs
Normal file
35
tests/render.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
mod fixtures;
|
||||||
|
|
||||||
|
use fixtures::{server, Error, TestServer, DIR_NO_INDEX};
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn render_index(#[with(&["--render-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_index_404(#[with(&["--render-index"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}/{}", server.url(), DIR_NO_INDEX))?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn render_spa(#[with(&["--render-spa"])] 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_spa_no_404(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = reqwest::blocking::get(format!("{}/{}", server.url(), DIR_NO_INDEX))?;
|
||||||
|
let text = resp.text()?;
|
||||||
|
assert_eq!(text, "This is index.html");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
46
tests/symlink.rs
Normal file
46
tests/symlink.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use assert_fs::fixture::TempDir;
|
||||||
|
use fixtures::{server, tmpdir, Error, TestServer};
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
use std::os::unix::fs::symlink as symlink_dir;
|
||||||
|
#[cfg(windows)]
|
||||||
|
use std::os::windows::fs::symlink_dir;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn default_not_allow_symlink(server: TestServer, tmpdir: TempDir) -> Result<(), Error> {
|
||||||
|
// Create symlink directory "foo" to point outside the root
|
||||||
|
let dir = "foo";
|
||||||
|
symlink_dir(tmpdir.path(), server.path().join(dir)).expect("Couldn't create symlink");
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}", server.url(), dir))?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}/index.html", server.url(), dir))?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
|
let paths = utils::retrive_index_paths(&resp.text()?);
|
||||||
|
assert!(!paths.is_empty());
|
||||||
|
assert!(!paths.contains(&format!("{}/", dir)));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn allow_symlink(
|
||||||
|
#[with(&["--allow-symlink"])] server: TestServer,
|
||||||
|
tmpdir: TempDir,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
// Create symlink directory "foo" to point outside the root
|
||||||
|
let dir = "foo";
|
||||||
|
symlink_dir(tmpdir.path(), server.path().join(dir)).expect("Couldn't create symlink");
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}", server.url(), dir))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
let resp = reqwest::blocking::get(format!("{}{}/index.html", server.url(), dir))?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
let resp = reqwest::blocking::get(server.url())?;
|
||||||
|
let paths = utils::retrive_index_paths(&resp.text()?);
|
||||||
|
assert!(!paths.is_empty());
|
||||||
|
assert!(paths.contains(&format!("{}/", dir)));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
51
tests/tls.rs
Normal file
51
tests/tls.rs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use assert_cmd::Command;
|
||||||
|
use fixtures::{server, Error, TestServer};
|
||||||
|
use predicates::str::contains;
|
||||||
|
use reqwest::blocking::ClientBuilder;
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
/// Can start the server with TLS and receive encrypted responses.
|
||||||
|
#[rstest]
|
||||||
|
#[case(server(&[
|
||||||
|
"--tls-cert", "tests/data/cert.pem",
|
||||||
|
"--tls-key", "tests/data/key_pkcs8.pem",
|
||||||
|
]))]
|
||||||
|
#[case(server(&[
|
||||||
|
"--tls-cert", "tests/data/cert.pem",
|
||||||
|
"--tls-key", "tests/data/key_pkcs1.pem",
|
||||||
|
]))]
|
||||||
|
fn tls_works(#[case] server: TestServer) -> Result<(), Error> {
|
||||||
|
let client = ClientBuilder::new()
|
||||||
|
.danger_accept_invalid_certs(true)
|
||||||
|
.build()?;
|
||||||
|
let resp = client.get(server.url()).send()?.error_for_status()?;
|
||||||
|
assert_index_resp!(resp);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wrong path for cert throws error.
|
||||||
|
#[rstest]
|
||||||
|
fn wrong_path_cert() -> Result<(), Error> {
|
||||||
|
Command::cargo_bin("duf")?
|
||||||
|
.args(&["--tls-cert", "wrong", "--tls-key", "tests/data/key.pem"])
|
||||||
|
.assert()
|
||||||
|
.failure()
|
||||||
|
.stderr(contains("error: Failed to access `wrong`"));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wrong paths for key throws errors.
|
||||||
|
#[rstest]
|
||||||
|
fn wrong_path_key() -> Result<(), Error> {
|
||||||
|
Command::cargo_bin("duf")?
|
||||||
|
.args(&["--tls-cert", "tests/data/cert.pem", "--tls-key", "wrong"])
|
||||||
|
.assert()
|
||||||
|
.failure()
|
||||||
|
.stderr(contains("error: Failed to access `wrong`"));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
61
tests/utils.rs
Normal file
61
tests/utils.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
use serde_json::Value;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! assert_index_resp {
|
||||||
|
($resp:ident) => {
|
||||||
|
assert_index_resp!($resp, self::fixtures::FILES)
|
||||||
|
};
|
||||||
|
($resp:ident, $files:expr) => {
|
||||||
|
assert_eq!($resp.status(), 200);
|
||||||
|
let body = $resp.text()?;
|
||||||
|
let paths = self::utils::retrive_index_paths(&body);
|
||||||
|
assert!(!paths.is_empty());
|
||||||
|
for file in $files {
|
||||||
|
assert!(paths.contains(&file.to_string()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! fetch {
|
||||||
|
($method:literal, $url:expr) => {
|
||||||
|
reqwest::blocking::Client::new().request(hyper::Method::from_bytes($method)?, $url)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn retrive_index_paths(index: &str) -> HashSet<String> {
|
||||||
|
retrive_index_paths_impl(index).unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn encode_uri(v: &str) -> String {
|
||||||
|
let parts: Vec<_> = v.split('/').map(urlencoding::encode).collect();
|
||||||
|
parts.join("/")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn retrive_index_paths_impl(index: &str) -> Option<HashSet<String>> {
|
||||||
|
let lines: Vec<&str> = index.lines().collect();
|
||||||
|
let (i, _) = lines
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.find(|(_, v)| v.contains("const DATA"))?;
|
||||||
|
let line = lines.get(i + 1)?;
|
||||||
|
let value: Value = line.parse().ok()?;
|
||||||
|
let paths = value
|
||||||
|
.get("paths")?
|
||||||
|
.as_array()?
|
||||||
|
.iter()
|
||||||
|
.flat_map(|v| {
|
||||||
|
let name = v.get("name")?.as_str()?;
|
||||||
|
let path_type = v.get("path_type")?.as_str()?;
|
||||||
|
if path_type.ends_with("Dir") {
|
||||||
|
Some(format!("{}/", name))
|
||||||
|
} else {
|
||||||
|
Some(name.to_owned())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
Some(paths)
|
||||||
|
}
|
||||||
203
tests/webdav.rs
Normal file
203
tests/webdav.rs
Normal file
@@ -0,0 +1,203 @@
|
|||||||
|
mod fixtures;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use fixtures::{server, Error, TestServer, FILES};
|
||||||
|
use rstest::rstest;
|
||||||
|
use xml::escape::escape_str_pcdata;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn propfind_dir(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"PROPFIND", format!("{}dira", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 207);
|
||||||
|
let body = resp.text()?;
|
||||||
|
assert!(body.contains("<D:href>/dira</D:href>"));
|
||||||
|
assert!(body.contains("<D:displayname>dira</D:displayname>"));
|
||||||
|
for f in FILES {
|
||||||
|
assert!(body.contains(&format!("<D:href>/dira/{}</D:href>", utils::encode_uri(f))));
|
||||||
|
assert!(body.contains(&format!(
|
||||||
|
"<D:displayname>{}</D:displayname>",
|
||||||
|
escape_str_pcdata(f)
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn propfind_dir_depth0(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"PROPFIND", format!("{}dira", server.url()))
|
||||||
|
.header("depth", "0")
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 207);
|
||||||
|
let body = resp.text()?;
|
||||||
|
assert!(body.contains("<D:href>/dira</D:href>"));
|
||||||
|
assert!(body.contains("<D:displayname>dira</D:displayname>"));
|
||||||
|
assert_eq!(
|
||||||
|
body.lines()
|
||||||
|
.filter(|v| *v == "<D:status>HTTP/1.1 200 OK</D:status>")
|
||||||
|
.count(),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn propfind_404(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"PROPFIND", format!("{}404", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn propfind_file(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"PROPFIND", format!("{}test.html", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 207);
|
||||||
|
let body = resp.text()?;
|
||||||
|
assert!(body.contains("<D:href>/test.html</D:href>"));
|
||||||
|
assert!(body.contains("<D:displayname>test.html</D:displayname>"));
|
||||||
|
assert_eq!(
|
||||||
|
body.lines()
|
||||||
|
.filter(|v| *v == "<D:status>HTTP/1.1 200 OK</D:status>")
|
||||||
|
.count(),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn proppatch_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"PROPPATCH", format!("{}test.html", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 207);
|
||||||
|
let body = resp.text()?;
|
||||||
|
assert!(body.contains("<D:href>/test.html</D:href>"));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn proppatch_404(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"PROPPATCH", format!("{}404", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn mkcol_dir(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"MKCOL", format!("{}newdir", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 201);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn mkcol_not_allow_upload(server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"MKCOL", format!("{}newdir", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 403);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn copy_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let new_url = format!("{}test2.html", server.url());
|
||||||
|
let resp = fetch!(b"COPY", format!("{}test.html", server.url()))
|
||||||
|
.header("Destination", &new_url)
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 204);
|
||||||
|
let resp = reqwest::blocking::get(new_url)?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn copy_not_allow_upload(server: TestServer) -> Result<(), Error> {
|
||||||
|
let new_url = format!("{}test2.html", server.url());
|
||||||
|
let resp = fetch!(b"COPY", format!("{}test.html", server.url()))
|
||||||
|
.header("Destination", &new_url)
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 403);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn copy_file_404(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let new_url = format!("{}test2.html", server.url());
|
||||||
|
let resp = fetch!(b"COPY", format!("{}404", server.url()))
|
||||||
|
.header("Destination", &new_url)
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn move_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let origin_url = format!("{}test.html", server.url());
|
||||||
|
let new_url = format!("{}test2.html", server.url());
|
||||||
|
let resp = fetch!(b"MOVE", &origin_url)
|
||||||
|
.header("Destination", &new_url)
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 204);
|
||||||
|
let resp = reqwest::blocking::get(new_url)?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
let resp = reqwest::blocking::get(origin_url)?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn move_not_allow_upload(#[with(&["--allow-delete"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let origin_url = format!("{}test.html", server.url());
|
||||||
|
let new_url = format!("{}test2.html", server.url());
|
||||||
|
let resp = fetch!(b"MOVE", &origin_url)
|
||||||
|
.header("Destination", &new_url)
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 403);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn move_not_allow_delete(#[with(&["--allow-upload"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let origin_url = format!("{}test.html", server.url());
|
||||||
|
let new_url = format!("{}test2.html", server.url());
|
||||||
|
let resp = fetch!(b"MOVE", &origin_url)
|
||||||
|
.header("Destination", &new_url)
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 403);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn move_file_404(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let new_url = format!("{}test2.html", server.url());
|
||||||
|
let resp = fetch!(b"MOVE", format!("{}404", server.url()))
|
||||||
|
.header("Destination", &new_url)
|
||||||
|
.send()?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn lock_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"LOCK", format!("{}test.html", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
let body = resp.text()?;
|
||||||
|
assert!(body.contains("<D:href>/test.html</D:href>"));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn lock_file_404(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"LOCK", format!("{}404", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn unlock_file(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"LOCK", format!("{}test.html", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 200);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn unlock_file_404(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
|
||||||
|
let resp = fetch!(b"LOCK", format!("{}404", server.url())).send()?;
|
||||||
|
assert_eq!(resp.status(), 404);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user