From a3d40b8c2f7cc6961a3cb830d6d75bc513eddfd6 Mon Sep 17 00:00:00 2001 From: Jose Quintana <1700322+joseluisq@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:28:54 +0200 Subject: [PATCH] fix: issues when building SWS without default features (#480) * fix: issues when building without default features * fix: windows http1_cancel_recv build issues --- .github/workflows/devel.yml | 31 ++++++++++++++++++++++++++++ docs/content/building-from-source.md | 9 ++++++-- src/headers_ext/mod.rs | 2 ++ src/http_ext.rs | 1 + src/lib.rs | 7 ------- src/server.rs | 30 ++++++++++++++++++--------- src/static_files.rs | 5 ----- tests/compression.rs | 7 +++++++ tests/compression_static.rs | 5 ----- tests/handler.rs | 14 +++++++++++++ tests/static_files.rs | 2 -- 11 files changed, 82 insertions(+), 31 deletions(-) diff --git a/.github/workflows/devel.yml b/.github/workflows/devel.yml index 874c14be..d4992940 100644 --- a/.github/workflows/devel.yml +++ b/.github/workflows/devel.yml @@ -263,6 +263,37 @@ jobs: ;; esac + - name: Turn off default features + shell: bash + run: | + echo "CARGO_FEATURES=--no-default-features" >> $GITHUB_ENV + + - name: Run tests without default features + shell: bash + run: | + ${{ env.CARGO_BIN }} test --verbose ${{ env.CARGO_FEATURES }} ${{ env.TARGET_FLAGS }} ${{ env.SKIP_TESTS }} + + - name: Run build without default features + shell: bash + run: | + ${{ env.CARGO_BIN }} build --bin static-web-server -vv ${{ env.CARGO_FEATURES }} ${{ env.TARGET_FLAGS }} + + - name: Run executable without default features + shell: bash + run: | + case "${{ matrix.build }}" in + *-arm*|*bsd*|*illumos*|*ppc64*|*s390x*) + echo "arm,bsd,illumos,ppc64,s390x are unable to execute on CI for now!" + ;; + *) + if [[ "${{ matrix.os }}" == "windows-2022" ]]; then + target/${{ matrix.target }}/debug/static-web-server.exe -h + else + target/${{ matrix.target }}/debug/static-web-server -h + fi + ;; + esac + checks: name: checks runs-on: ubuntu-22.04 diff --git a/docs/content/building-from-source.md b/docs/content/building-from-source.md index c2b8ef3b..39710ddd 100644 --- a/docs/content/building-from-source.md +++ b/docs/content/building-from-source.md @@ -55,10 +55,10 @@ For example, if you want to run or build SWS without the default features like ` # run cargo run --no-default-features -- -h -# or build +# build cargo build --release --no-default-features -# or including all features (example) +# or build including all features (example) RUSTFLAGS="--cfg tokio_unstable" \ cargo build -vv --release --features all ``` @@ -87,8 +87,13 @@ Built binaries can be found under the corresponding toolchain directory inside ` ```sh # run tests for default features cargo test + +# run all tests without default features +cargo test --tests --no-default-features + # or run tests for all features including experimental ones RUSTFLAGS="--cfg tokio_unstable" cargo test --features all + # or run specific tests cargo test --test rewrites ``` diff --git a/src/headers_ext/mod.rs b/src/headers_ext/mod.rs index 3999c5af..0ce8e896 100644 --- a/src/headers_ext/mod.rs +++ b/src/headers_ext/mod.rs @@ -7,6 +7,8 @@ //! header. //! +#![allow(unused)] + mod accept_encoding; mod content_coding; mod quality_value; diff --git a/src/http_ext.rs b/src/http_ext.rs index fb1b6bb8..a6814148 100644 --- a/src/http_ext.rs +++ b/src/http_ext.rs @@ -15,6 +15,7 @@ pub trait MethodExt { /// If method is allowed. fn is_allowed(&self) -> bool; /// If method is `GET`. + #[allow(unused)] fn is_get(&self) -> bool; /// If method is `HEAD`. fn is_head(&self) -> bool; diff --git a/src/lib.rs b/src/lib.rs index 5c9a356c..a169e67e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,13 +162,6 @@ pub mod error_page; pub mod fallback_page; pub(crate) mod fs; pub mod handler; -#[cfg(any( - feature = "compression", - feature = "compression-gzip", - feature = "compression-brotli", - feature = "compression-zstd", - feature = "compression-deflate" -))] pub(crate) mod headers_ext; pub(crate) mod health; pub(crate) mod http_ext; diff --git a/src/server.rs b/src/server.rs index bf7e7f1a..43b5aa85 100644 --- a/src/server.rs +++ b/src/server.rs @@ -350,7 +350,8 @@ impl Server { #[cfg(windows)] let (sender, receiver) = tokio::sync::watch::channel(()); - // ctrl+c listening + + // Windows ctrl+c listening #[cfg(windows)] let ctrlc_task = tokio::spawn(async move { if !general.windows_service { @@ -603,18 +604,16 @@ impl Server { http1_cancel_recv, )); - #[cfg(windows)] - let http1_cancel_recv = Arc::new(Mutex::new(_cancel_recv)); - #[cfg(windows)] - let http1_ctrlc_recv = Arc::new(Mutex::new(Some(receiver))); - #[cfg(windows)] let http1_server = http1_server.with_graceful_shutdown(async move { - if general.windows_service { - signals::wait_for_ctrl_c(http1_cancel_recv, grace_period).await; + let http1_cancel_recv = if general.windows_service { + // http1_cancel_recv + Arc::new(Mutex::new(_cancel_recv)) } else { - signals::wait_for_ctrl_c(http1_ctrlc_recv, grace_period).await; - } + // http1_ctrlc_recv + Arc::new(Mutex::new(Some(receiver))) + }; + signals::wait_for_ctrl_c(http1_cancel_recv, grace_period).await; }); server_info!( @@ -625,8 +624,19 @@ impl Server { server_info!("press ctrl+c to shut down the server"); + #[cfg(unix)] http1_server.await?; + #[cfg(windows)] + let http1_server_task = tokio::spawn(async move { + if let Err(err) = http1_server.await { + tracing::error!("http1 server failed to start up: {:?}", err); + std::process::exit(1) + } + }); + #[cfg(windows)] + tokio::try_join!(ctrlc_task, http1_server_task)?; + #[cfg(windows)] _cancel_fn(); diff --git a/src/static_files.rs b/src/static_files.rs index 2a26fffb..5ace8112 100644 --- a/src/static_files.rs +++ b/src/static_files.rs @@ -27,7 +27,6 @@ use crate::Result; feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] @@ -278,7 +277,6 @@ fn get_composed_file_metadata<'a>( feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] @@ -326,7 +324,6 @@ fn get_composed_file_metadata<'a>( feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] @@ -356,7 +353,6 @@ fn get_composed_file_metadata<'a>( feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] @@ -381,7 +377,6 @@ fn get_composed_file_metadata<'a>( feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] diff --git a/tests/compression.rs b/tests/compression.rs index 8c8e7e46..2ca82293 100644 --- a/tests/compression.rs +++ b/tests/compression.rs @@ -3,6 +3,13 @@ #![deny(rust_2018_idioms)] #![deny(dead_code)] +#[cfg(any( + feature = "compression", + feature = "compression-gzip", + feature = "compression-brotli", + feature = "compression-zstd", + feature = "compression-deflate" +))] #[cfg(test)] pub mod tests { use headers::HeaderValue; diff --git a/tests/compression_static.rs b/tests/compression_static.rs index 6b4f6c17..c3951b89 100644 --- a/tests/compression_static.rs +++ b/tests/compression_static.rs @@ -7,7 +7,6 @@ feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] @@ -57,7 +56,6 @@ mod tests { feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] @@ -129,7 +127,6 @@ mod tests { feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] @@ -198,7 +195,6 @@ mod tests { feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] @@ -298,7 +294,6 @@ mod tests { feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] diff --git a/tests/handler.rs b/tests/handler.rs index d53c6585..a98b2407 100644 --- a/tests/handler.rs +++ b/tests/handler.rs @@ -30,6 +30,13 @@ pub mod tests { res.headers().get("content-type"), Some(&HeaderValue::from_static("text/html")) ); + #[cfg(any( + feature = "compression", + feature = "compression-deflate", + feature = "compression-gzip", + feature = "compression-brotli", + feature = "compression-zstd" + ))] assert_eq!( res.headers().get("vary"), Some(&HeaderValue::from_static("accept-encoding")) @@ -62,6 +69,13 @@ pub mod tests { res.headers().get("content-type"), Some(&HeaderValue::from_static("text/html")) ); + #[cfg(any( + feature = "compression", + feature = "compression-deflate", + feature = "compression-gzip", + feature = "compression-brotli", + feature = "compression-zstd" + ))] assert_eq!( res.headers().get("vary"), Some(&HeaderValue::from_static("accept-encoding")) diff --git a/tests/static_files.rs b/tests/static_files.rs index dee51ef2..6f6decf6 100644 --- a/tests/static_files.rs +++ b/tests/static_files.rs @@ -15,7 +15,6 @@ mod tests { feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))] @@ -698,7 +697,6 @@ mod tests { feature = "compression", feature = "compression-deflate", feature = "compression-gzip", - feature = "compression-deflate", feature = "compression-brotli", feature = "compression-zstd" ))]