index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-05-03 9:18:58.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-05-03 9:18:58.0 +00:00:00
commit
520e2204f70f7a7755ef35d2201d5c0e68c55e4a [patch]
tree
dff74a5121fa50d442accc723c562d70b1fda418
parent
58ff9b7b3f20d7631be3b16fee4839e9fe92d5e5
download
520e2204f70f7a7755ef35d2201d5c0e68c55e4a.tar.gz

refactor: compress response only when Accept-Encoding is available



Diff

 .gitignore         |  2 ++
 src/compression.rs | 17 +++++++++--------
 src/handler.rs     |  2 +-
 src/server.rs      |  2 +-
 4 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore
index 220a87a..2b30973 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,6 +15,8 @@
**/*.env
**/*.svg
**/*.data
**/*.zst
**/*.out*
release
.vscode
TODO
diff --git a/src/compression.rs b/src/compression.rs
index 293b147..f6c5c2d 100644
--- a/src/compression.rs
+++ b/src/compression.rs
@@ -51,16 +51,17 @@ pub fn auto(
        return Ok(resp);
    }

    // Skip compression for non-text-based MIME types
    if let Some(content_type) = resp.headers().typed_get::<ContentType>() {
        let content_type = content_type.to_string();
        if !TEXT_MIME_TYPES.iter().any(|h| *h == content_type) {
            return Ok(resp);
        }
    }

    // Compress response based on Accept-Encoding header
    if let Some(ref accept_encoding) = headers.typed_get::<AcceptEncoding>() {
        if let Some(encoding) = accept_encoding.prefered_encoding() {
            // Skip compression for non-text-based MIME types
            if let Some(content_type) = resp.headers().typed_get::<ContentType>() {
                let content_type = &content_type.to_string();
                if !TEXT_MIME_TYPES.iter().any(|h| *h == content_type) {
                    return Ok(resp);
                }
            }

            if encoding == ContentCoding::GZIP {
                let (head, body) = resp.into_parts();
                return Ok(gzip(head, body.into()));
diff --git a/src/handler.rs b/src/handler.rs
index 3e0c15c..7ab7c2d 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -5,7 +5,7 @@ use crate::{compression, static_files};
use crate::{error::Result, error_page};

/// Main server request handler.
pub async fn handle_request(base: &Path, req: Request<Body>) -> Result<Response<Body>> {
pub async fn handle_request(base: &Path, req: &Request<Body>) -> Result<Response<Body>> {
    let headers = req.headers();
    let method = req.method();

diff --git a/src/server.rs b/src/server.rs
index 0fa9876..a9c3fae 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -88,7 +88,7 @@ impl Server {
                async move {
                    Ok::<_, error::Error>(service_fn(move |req| {
                        let root_dir = root_dir.clone();
                        async move { handler::handle_request(root_dir.as_ref(), req).await }
                        async move { handler::handle_request(root_dir.as_ref(), &req).await }
                    }))
                }
            });