feat: gzip, deflate or brotli --compression (-x) flag
this flag enables or disables gzip, deflate or brotli on demand based
on Accept-Encoding header. feature enabled by default
Diff
README.md | 10 ++++++++--
src/config.rs | 10 ++++++++++
src/handler.rs | 9 ++++++---
src/server.rs | 19 +++++++++++++++++--
4 files changed, 41 insertions(+), 7 deletions(-)
@@ -54,6 +54,7 @@ Server can be configured either via environment variables or their equivalent co
### Command-line arguments
@@ -72,16 +73,21 @@ FLAGS:
-V, --version Prints version information
OPTIONS:
-x, --compression <compression>
Gzip, Deflate or Brotli compression on demand determined by the Accept-Encoding header and applied to text-
based web file types only [env: SERVER_COMPRESSION=] [default: true]
-c, --cors-allow-origins <cors-allow-origins>
Specify an optional CORS list of allowed origin hosts separated by comas. Host ports or protocols aren't
being checked. Use an asterisk (*) to allow any host [env: SERVER_CORS_ALLOW_ORIGINS=] [default: ]
-z, --directory-listing <directory-listing>
Enable directory listing for all requests ending with the slash character (‘/’) [env:
SERVER_DIRECTORY_LISTING=]
SERVER_DIRECTORY_LISTING=] [default: false]
-a, --host <host>
Host address (E.g 127.0.0.1 or ::1) [env: SERVER_HOST=] [default: ::]
-t, --http2 <http2> Enable HTTP/2 with TLS support [env: SERVER_HTTP2_TLS=]
-t, --http2 <http2>
Enable HTTP/2 with TLS support [env: SERVER_HTTP2_TLS=] [default: false]
--http2-tls-cert <http2-tls-cert>
Specify the file path to read the certificate [env: SERVER_HTTP2_TLS_CERT=] [default: ]
@@ -76,6 +76,16 @@ pub struct Config {
#[structopt(
long,
short = "x",
parse(try_from_str),
default_value = "true",
env = "SERVER_COMPRESSION"
)]
pub compression: bool,
#[structopt(
long,
short = "z",
parse(try_from_str),
default_value = "false",
@@ -7,6 +7,7 @@ use crate::{error::Result, error_page};
pub async fn handle_request(
base: &Path,
compression: bool,
dir_listing: bool,
req: &Request<Body>,
) -> Result<Response<Body>> {
@@ -14,9 +15,11 @@ pub async fn handle_request(
let method = req.method();
match static_files::handle_request(method, headers, base, req.uri().path(), dir_listing).await {
Ok(resp) => {
let mut resp = compression::auto(method, headers, resp)?;
Ok(mut resp) => {
if compression {
resp = compression::auto(method, headers, resp)?;
}
let ext = req.uri().path().to_lowercase();
@@ -78,6 +78,9 @@ impl Server {
let dir_listing = opts.directory_listing;
let compression = opts.compression;
let threads = self.threads;
@@ -94,7 +97,13 @@ impl Server {
Ok::<_, error::Error>(service_fn(move |req| {
let root_dir = root_dir.clone();
async move {
handler::handle_request(root_dir.as_ref(), dir_listing, &req).await
handler::handle_request(
root_dir.as_ref(),
compression,
dir_listing,
&req,
)
.await
}
}))
}
@@ -130,7 +139,13 @@ impl Server {
Ok::<_, error::Error>(service_fn(move |req| {
let root_dir = root_dir.clone();
async move {
handler::handle_request(root_dir.as_ref(), dir_listing, &req).await
handler::handle_request(
root_dir.as_ref(),
compression,
dir_listing,
&req,
)
.await
}
}))
}