index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-01-30 22:22:16.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-01-30 22:22:16.0 +00:00:00
commit
e853410e4d3a9d4faa65c80d134fdddfc5be737b [patch]
tree
d151a502ebdcf778abbfd1ec9f5f75cc90e49d0c
parent
99b8b7e03c98960cbfa288eb9958e0a468e02b93
download
e853410e4d3a9d4faa65c80d134fdddfc5be737b.tar.gz

refactor: drop support for deflate in favor of gzip

since gzip is deflate with extra headers and checksum

Diff

 README.md     |  6 +++---
 src/config.rs |  2 +-
 src/server.rs | 54 +-----------------------------------------------------
 3 files changed, 5 insertions(+), 57 deletions(-)

diff --git a/README.md b/README.md
index 819205c..dd61bd2 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@ For stable `v1` and contributions please refer to [1.x](https://github.com/josel
- Memory safety and very reduced CPU and RAM overhead.
- Blazing fast static files-serving and asynchronous powered by [Warp]https://github.com/seanmonstar/warp/ `v0.3` ([Hyper]https://github.com/hyperium/hyper/ `v0.14`), [Tokio]https://github.com/tokio-rs/tokio `v1` and a set of [awesome crates]./Cargo.toml.
- Suitable for lightweight [GNU/Linux Docker containers]https://hub.docker.com/r/joseluisq/static-web-server/tags. It's a fully __5MB__ static binary thanks to [Rust and Musl libc]https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html.
- Opt-in GZip, Deflate or Brotli compression for text-based web files only.
- Opt-in GZip or Brotli compression for text-based web files only.
- Compression on demand via [Accept-Encoding]https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding header.
- [Partial Content Delivery]https://en.wikipedia.org/wiki/Byte_serving support for byte-serving of large files.
- [Cache Control]https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control headers for assets.
@@ -49,7 +49,7 @@ Server can be configured either via environment variables or their equivalent co
| `SERVER_LOG_LEVEL`          | Specify a logging level in lower case. (Values `error`, `warn`, `info`, `debug`, `trace`).                                                                                                                                                                                                                                                                                                         | Default `error`                                                                                                                 |
| `SERVER_ERROR_PAGE_404`     | HTML file path for 404 errors.                                                                                                                                                                                                                                                                                                                                                                     | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/404.html`. |
| `SERVER_ERROR_PAGE_50X`     | HTML file path for 50x errors.                                                                                                                                                                                                                                                                                                                                                                     | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/50x.html`  |
| `SERVER_COMPRESSION`        | Compression body support for web text-based file types. Values: `gzip`, `deflate` or `brotli`.                                                                                                                                                                                                                                                                                                     | Default: `gzip`                                                                                                                 |
| `SERVER_COMPRESSION`        | Compression body support for web text-based file types. Values: `gzip` or `brotli`.                                                                                                                                                                                                                                                                                                     | Default: `gzip`                                                                                                                 |
| `SERVER_THREADS_MULTIPLIER` | Number of worker threads multiplier that'll be multiplied by the number of system CPUs using the formula: `worker threads = number of CPUs * n` where `n` is the value that changes here. When multiplier value is 0 or 1 then the `number of CPUs` is used.  Number of worker threads result should be a number between 1 and 32,768 though it is advised to keep this value on the smaller side. | Default `8`                                                                                                                     |
| `SERVER_HTTP2_TLS`          | Enable HTTP/2 with TLS support. Make sure also to adjust current server port.                                                                                                                                                                                                                                                                                                                      | Default `false`                                                                                                                 |
| `SERVER_HTTP2_TLS_CERT`     | Specify the file path to read the certificate.                                                                                                                                                                                                                                                                                                                                                     | Default empty                                                                                                                   |
@@ -73,7 +73,7 @@ FLAGS:

OPTIONS:
    -x, --compression <compression>
            Compression body support for web text-based file types. Values: "gzip", "deflate" or "brotli". Use an empty
            Compression body support for web text-based file types. Values: "gzip" or "brotli". Use an empty
            value to skip compression [env: SERVER_COMPRESSION=]  [default: gzip]
    -c, --cors-allow-origins <cors-allow-origins>
            Specify a optional CORS list of allowed origin hosts separated by comas. Host ports or protocols aren't
diff --git a/src/config.rs b/src/config.rs
index 4589e6d..c317acb 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -44,7 +44,7 @@ pub struct Config {
    pub page404: String,

    #[structopt(long, short = "x", default_value = "gzip", env = "SERVER_COMPRESSION")]
    /// Compression body support for web text-based file types. Values: "gzip", "deflate" or "brotli".
    /// Compression body support for web text-based file types. Values: "gzip" or "brotli".
    /// Use an empty value to skip compression.
    pub compression: String,

diff --git a/src/server.rs b/src/server.rs
index ced7479..ef8cdbf 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -80,7 +80,7 @@ impl Server {
        let http2_tls_cert_path = opts.http2_tls_cert;
        let http2_tls_key_path = opts.http2_tls_key;

        // Public GET/HEAD endpoints with compression (deflate, gzip, brotli, none)
        // Public GET/HEAD endpoints with compression (gzip, brotli or none)
        match opts.compression.as_ref() {
            "brotli" => tokio::task::spawn(async move {
                let with_dir = warp::fs::dir(root_dir)
@@ -134,58 +134,6 @@ impl Server {
                    }
                }
            }),
            "deflate" => tokio::task::spawn(async move {
                let with_dir = warp::fs::dir(root_dir)
                    .map(cache::control_headers)
                    .with(warp::trace::request())
                    .with(warp::compression::deflate(true))
                    .recover(move |rej| {
                        let page404 = page404.clone();
                        let page50x = page50x.clone();
                        async move { rejection::handle_rejection(page404, page50x, rej).await }
                    });

                if let Some(cors_filter) = cors_filter {
                    tracing::info!(
                        cors_enabled = ?true,
                        allowed_origins = ?cors_allowed_origins
                    );
                    let server = warp::serve(
                        public_head.with(cors_filter.clone()).or(warp::get()
                            .and(filters::has_accept_encoding("deflate"))
                            .and(with_dir)
                            .with(cors_filter.clone())
                            .or(public_get_default.with(cors_filter))),
                    );
                    if http2 {
                        server
                            .tls()
                            .cert_path(http2_tls_cert_path)
                            .key_path(http2_tls_key_path)
                            .run(addr)
                            .await;
                    } else {
                        server.run(addr).await
                    }
                } else {
                    let server = warp::serve(
                        public_head.or(warp::get()
                            .and(filters::has_accept_encoding("deflate"))
                            .and(with_dir)
                            .or(public_get_default)),
                    );
                    if http2 {
                        server
                            .tls()
                            .cert_path(http2_tls_cert_path)
                            .key_path(http2_tls_key_path)
                            .run(addr)
                            .await;
                    } else {
                        server.run(addr).await
                    }
                }
            }),
            "gzip" => tokio::task::spawn(async move {
                let with_dir = warp::fs::dir(root_dir)
                    .map(cache::control_headers)