diff --git a/Cargo.lock b/Cargo.lock index 8b9624b7..79976fb5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,27 +62,6 @@ dependencies = [ "serde_json", ] -[[package]] -name = "async-stream" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3670df70cbc01729f901f94c887814b3c68db038aad1329a418bae178bc5295c" -dependencies = [ - "async-stream-impl", - "futures-core", -] - -[[package]] -name = "async-stream-impl" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3548b8efc9f8e8a5a0a2808c5bd8451a9031b9e5b879a79590304ae928b0a70" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "atty" version = "0.2.14" @@ -204,12 +183,6 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" -[[package]] -name = "bytes" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1f8e949d755f9d79112b5bb46938e0ef9d3804a0b16dfab13aafcaa5f0fa72" - [[package]] name = "cc" version = "1.0.60" @@ -231,12 +204,6 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - [[package]] name = "chrono" version = "0.4.18" @@ -320,7 +287,7 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", ] [[package]] @@ -351,7 +318,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" dependencies = [ "autocfg", - "cfg-if 0.1.10", + "cfg-if", "crossbeam-utils", "lazy_static", "maybe-uninit", @@ -366,7 +333,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ "autocfg", - "cfg-if 0.1.10", + "cfg-if", "lazy_static", ] @@ -548,7 +515,7 @@ version = "0.8.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a51b8cf747471cb9499b6d59e59b0444f4c90eba8968c4e44874e92b5b64ace2" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", ] [[package]] @@ -585,7 +552,7 @@ version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ed85775dcc68644b5c950ac06a2b23768d3bc9390464151aaf27136998dcf9e" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "libc", "redox_syscall", "winapi 0.3.9", @@ -597,7 +564,7 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "766d0e77a2c1502169d4a93ff3b8c15a71fd946cd0126309752104e5f3c46d94" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "crc32fast", "libc", "miniz_oxide 0.4.2", @@ -728,7 +695,7 @@ dependencies = [ "futures-macro", "futures-task", "memchr", - "pin-project 0.4.24", + "pin-project", "pin-utils", "proc-macro-hack", "proc-macro-nested", @@ -760,7 +727,7 @@ version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "libc", "wasi 0.9.0+wasi-snapshot-preview1", ] @@ -819,31 +786,11 @@ dependencies = [ "http", "indexmap", "slab", - "tokio 0.2.22", - "tokio-util 0.3.1", + "tokio", + "tokio-util", "tracing", ] -[[package]] -name = "h2" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b67e66362108efccd8ac053abafc8b7a8d86a37e6e48fc4f6f7485eb5e9e6a5" -dependencies = [ - "bytes 1.0.0", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http", - "indexmap", - "slab", - "tokio 1.0.1", - "tokio-util 0.6.0", - "tracing", - "tracing-futures", -] - [[package]] name = "hashbrown" version = "0.8.2" @@ -913,16 +860,6 @@ dependencies = [ "http", ] -[[package]] -name = "http-body" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2861bd27ee074e5ee891e8b539837a9430012e249d7f0ca2d795650f579c1994" -dependencies = [ - "bytes 1.0.0", - "http", -] - [[package]] name = "httparse" version = "1.3.4" @@ -951,39 +888,15 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2 0.2.6", + "h2", "http", - "http-body 0.3.1", + "http-body", "httparse", "httpdate", "itoa", - "pin-project 0.4.24", + "pin-project", "socket2", - "tokio 0.2.22", - "tower-service", - "tracing", - "want", -] - -[[package]] -name = "hyper" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7b980c7bc75203b968f06374cbde00bf1818e02e156b8e5b6ccf440fb53b6d0" -dependencies = [ - "bytes 1.0.0", - "futures-channel", - "futures-core", - "futures-util", - "h2 0.3.0", - "http", - "http-body 0.4.0", - "httparse", - "httpdate", - "itoa", - "pin-project 1.0.2", - "socket2", - "tokio 1.0.1", + "tokio", "tower-service", "tracing", "want", @@ -997,14 +910,31 @@ checksum = "37743cc83e8ee85eacfce90f2f4102030d9ff0a95244098d781e9bee4a90abb6" dependencies = [ "bytes 0.5.6", "futures-util", - "hyper 0.13.8", + "hyper", "log", "rustls", - "tokio 0.2.22", + "tokio", "tokio-rustls", "webpki", ] +[[package]] +name = "hyper-staticfile" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f059991575d4be26e3946276a4f3ee24bcf17a232449407354045eaf064d3475" +dependencies = [ + "chrono", + "futures-util", + "http", + "hyper", + "mime_guess", + "percent-encoding", + "tokio", + "url", + "winapi 0.3.9", +] + [[package]] name = "idna" version = "0.2.0" @@ -1301,7 +1231,7 @@ version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", ] [[package]] @@ -1430,32 +1360,19 @@ version = "0.6.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "fuchsia-zircon", "fuchsia-zircon-sys", "iovec", "kernel32-sys", "libc", "log", - "miow 0.2.1", + "miow", "net2", "slab", "winapi 0.2.8", ] -[[package]] -name = "mio" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f33bc887064ef1fd66020c9adfc45bb9f33d75a42096c81e7c56c65b75dd1a8b" -dependencies = [ - "libc", - "log", - "miow 0.3.6", - "ntapi", - "winapi 0.3.9", -] - [[package]] name = "mio-extras" version = "2.0.6" @@ -1464,7 +1381,7 @@ checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" dependencies = [ "lazycell", "log", - "mio 0.6.22", + "mio", "slab", ] @@ -1480,16 +1397,6 @@ dependencies = [ "ws2_32-sys", ] -[[package]] -name = "miow" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897" -dependencies = [ - "socket2", - "winapi 0.3.9", -] - [[package]] name = "mockito" version = "0.27.0" @@ -1514,7 +1421,7 @@ version = "0.2.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ebc3ec692ed7c9a255596c67808dee269f64655d8baf7b4f0638e51ba1d6853" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "libc", "winapi 0.3.9", ] @@ -1533,7 +1440,7 @@ checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" dependencies = [ "bitflags", "cc", - "cfg-if 0.1.10", + "cfg-if", "libc", "void", ] @@ -1550,21 +1457,12 @@ dependencies = [ "fsevent-sys", "inotify", "libc", - "mio 0.6.22", + "mio", "mio-extras", "walkdir", "winapi 0.3.9", ] -[[package]] -name = "ntapi" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" -dependencies = [ - "winapi 0.3.9", -] - [[package]] name = "num-integer" version = "0.1.43" @@ -1767,16 +1665,7 @@ version = "0.4.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f48fad7cfbff853437be7cf54d7b993af21f53be7f0988cbfe4a51535aa77205" dependencies = [ - "pin-project-internal 0.4.24", -] - -[[package]] -name = "pin-project" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ccc2237c2c489783abd8c4c80e5450fc0e98644555b1364da68cc29aa151ca7" -dependencies = [ - "pin-project-internal 1.0.2", + "pin-project-internal", ] [[package]] @@ -1790,29 +1679,12 @@ dependencies = [ "syn", ] -[[package]] -name = "pin-project-internal" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "pin-project-lite" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71f349a4f0e70676ffb2dbafe16d0c992382d02f0a952e3ddf584fc289dac6b3" -[[package]] -name = "pin-project-lite" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c" - [[package]] name = "pin-utils" version = "0.1.0" @@ -2091,8 +1963,8 @@ dependencies = [ "futures-core", "futures-util", "http", - "http-body 0.3.1", - "hyper 0.13.8", + "http-body", + "hyper", "hyper-rustls", "ipnet", "js-sys", @@ -2101,11 +1973,11 @@ dependencies = [ "mime", "mime_guess", "percent-encoding", - "pin-project-lite 0.1.8", + "pin-project-lite", "rustls", "serde", "serde_urlencoded", - "tokio 0.2.22", + "tokio", "tokio-rustls", "url", "wasm-bindgen", @@ -2302,7 +2174,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2933378ddfeda7ea26f48c555bdad8bb446bf8a3d17832dc83e380d444cfb8c1" dependencies = [ "block-buffer 0.9.0", - "cfg-if 0.1.10", + "cfg-if", "cpuid-bool", "digest 0.9.0", "opaque-debug 0.3.0", @@ -2368,12 +2240,13 @@ checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" [[package]] name = "socket2" -version = "0.3.19" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" +checksum = "b1fa70dc5c8104ec096f4fe7ede7a221d35ae13dcd19ba1ad9a81d2cab9a1c44" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", + "redox_syscall", "winapi 0.3.9", ] @@ -2446,9 +2319,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.55" +version = "1.0.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a571a711dddd09019ccc628e1b17fe87c59b09d513c06c026877aa708334f37a" +checksum = "9c51d92969d209b54a98397e1b91c8ae82d8c87a7bb87df0b29aa2ad81454228" dependencies = [ "proc-macro2", "quote", @@ -2483,7 +2356,7 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "libc", "rand", "redox_syscall", @@ -2615,24 +2488,22 @@ dependencies = [ "iovec", "lazy_static", "memchr", - "mio 0.6.22", + "mio", "num_cpus", - "pin-project-lite 0.1.8", + "pin-project-lite", "slab", + "tokio-macros", ] [[package]] -name = "tokio" -version = "1.0.1" +name = "tokio-macros" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d258221f566b6c803c7b4714abadc080172b272090cdc5e244a6d4dd13c3a6bd" +checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" dependencies = [ - "autocfg", - "bytes 1.0.0", - "libc", - "memchr", - "mio 0.7.6", - "pin-project-lite 0.2.0", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -2643,22 +2514,10 @@ checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a" dependencies = [ "futures-core", "rustls", - "tokio 0.2.22", + "tokio", "webpki", ] -[[package]] -name = "tokio-stream" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f3be913b74b13210c8fe04b17ab833f5a124f45b93d0f99f59fff621f64392a" -dependencies = [ - "async-stream", - "futures-core", - "pin-project-lite 0.2.0", - "tokio 1.0.1", -] - [[package]] name = "tokio-util" version = "0.3.1" @@ -2669,23 +2528,8 @@ dependencies = [ "futures-core", "futures-sink", "log", - "pin-project-lite 0.1.8", - "tokio 0.2.22", -] - -[[package]] -name = "tokio-util" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36135b7e7da911f5f8b9331209f7fab4cc13498f3fff52f72a710c78187e3148" -dependencies = [ - "bytes 1.0.0", - "futures-core", - "futures-sink", - "log", - "pin-project-lite 0.2.0", - "tokio 1.0.1", - "tokio-stream", + "pin-project-lite", + "tokio", ] [[package]] @@ -2709,7 +2553,7 @@ version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d79ca061b032d6ce30c660fded31189ca0b9922bf483cd70759f13a2d86786c" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "log", "tracing-core", ] @@ -2723,16 +2567,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "tracing-futures" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab7bb6f14721aa00656086e9335d363c5c8747bae02ebe32ea2c7dece5689b4c" -dependencies = [ - "pin-project 0.4.24", - "tracing", -] - [[package]] name = "try-lock" version = "0.2.3" @@ -2949,7 +2783,7 @@ version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "serde", "serde_json", "wasm-bindgen-macro", @@ -2976,7 +2810,7 @@ version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "js-sys", "wasm-bindgen", "web-sys", @@ -3108,7 +2942,7 @@ dependencies = [ "bytes 0.4.12", "httparse", "log", - "mio 0.6.22", + "mio", "mio-extras", "rand", "sha-1", @@ -3170,14 +3004,15 @@ dependencies = [ "errors", "front_matter", "globset", - "hyper 0.14.1", + "hyper", + "hyper-staticfile", "lazy_static", "notify", "open", "relative-path", "site", "termcolor", - "tokio 1.0.1", + "tokio", "url", "utils", "ws", diff --git a/Cargo.toml b/Cargo.toml index 3a8e7016..2f475f63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,8 +27,9 @@ termcolor = "1.0.4" # Used in init to ensure the url given as base_url is a valid one url = "2" # Below is for the serve cmd -hyper = { version = "0.14.1", default-features = false, features = ["runtime", "server", "http2", "http1"] } -tokio = { version = "1.0.1", default-features = false, features = ["rt", "fs"] } +hyper = { version = "0.13", default-features = false, features = ["runtime"] } +hyper-staticfile = "0.5" +tokio = { version = "0.2", default-features = false, features = [] } notify = "4" ws = "0.9" ctrlc = "3" diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index d03e87b9..2f2f299a 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -22,16 +22,16 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. use std::fs::{read_dir, remove_dir_all}; -use std::net::{SocketAddrV4, TcpListener}; use std::path::{Path, PathBuf}; use std::sync::mpsc::channel; use std::thread; use std::time::{Duration, Instant}; +use std::net::{SocketAddrV4, TcpListener}; use hyper::header; -use hyper::server::Server; use hyper::service::{make_service_fn, service_fn}; -use hyper::{Body, Method, Request, Response, StatusCode}; +use hyper::{Body, Method, Request, Response, Server, StatusCode}; +use hyper_staticfile::ResolveResult; use chrono::prelude::*; use notify::{watcher, RecursiveMode, Watcher}; @@ -70,7 +70,7 @@ static NOT_FOUND_TEXT: &[u8] = b"Not Found"; // This is dist/livereload.min.js from the LiveReload.js v3.2.4 release const LIVE_RELOAD: &str = include_str!("livereload.js"); -async fn handle_request(req: Request, mut root: PathBuf) -> Result> { +async fn handle_request(req: Request, root: PathBuf) -> Result> { let mut path = RelativePathBuf::new(); for c in req.uri().path().split('/') { @@ -90,37 +90,18 @@ async fn handle_request(req: Request, mut root: PathBuf) -> Result {} - _ => return Ok(method_not_allowed()), - } - - // Handle only simple path requests - if req.uri().scheme_str().is_some() || req.uri().host().is_some() { - return Ok(not_found()); - } - - // Remove the trailing slash from the request path - // otherwise `PathBuf` will interpret it as an absolute path - root.push(&req.uri().path()[1..]); - let result = tokio::fs::read(root).await; - - let contents = match result { - Err(err) => match err.kind() { - std::io::ErrorKind::NotFound => return Ok(not_found()), - std::io::ErrorKind::PermissionDenied => { - return Ok(Response::builder() - .status(StatusCode::FORBIDDEN) - .body(Body::empty()) - .unwrap()) - } - _ => panic!("{}", err), - }, - Ok(contents) => contents, + let result = hyper_staticfile::resolve(&root, &req).await.unwrap(); + match result { + ResolveResult::MethodNotMatched => return Ok(method_not_allowed()), + ResolveResult::NotFound | ResolveResult::UriNotMatched => { + let not_found_path = RelativePath::new("404.html"); + let content_404 = SITE_CONTENT.read().unwrap().get(not_found_path).cloned(); + return Ok(not_found(content_404)); + } + _ => (), }; - Ok(Response::builder().status(StatusCode::OK).body(Body::from(contents)).unwrap()) + Ok(hyper_staticfile::ResponseBuilder::new().request(&req).build(result).unwrap()) } fn livereload_js() -> Response { @@ -147,10 +128,7 @@ fn method_not_allowed() -> Response { .expect("Could not build Method Not Allowed response") } -fn not_found() -> Response { - let not_found_path = RelativePath::new("404.html"); - let content = SITE_CONTENT.read().unwrap().get(not_found_path).cloned(); - +fn not_found(content: Option) -> Response { if let Some(body) = content { return Response::builder() .header(header::CONTENT_TYPE, "text/html") @@ -308,8 +286,9 @@ pub fn serve( thread::spawn(move || { let addr = address.parse().unwrap(); - let rt = tokio::runtime::Builder::new_current_thread() + let mut rt = tokio::runtime::Builder::new() .enable_all() + .basic_scheduler() .build() .expect("Could not build tokio runtime");