index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-07-02 12:54:16.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-07-02 12:54:16.0 +00:00:00
commit
a1b78366eed0bf97516b745964d2183be02a13b9 [patch]
tree
5e9eb3471e3b60f71adc61cc1dd8bd860e19600a
parent
b025536692c81565fd4358ec5ee84cb4132475d8
download
a1b78366eed0bf97516b745964d2183be02a13b9.tar.gz

fix: missing content-type for directory listing index & error pages



Diff

 src/error_page.rs   |  7 ++++++-
 src/handler.rs      | 10 +++++-----
 src/static_files.rs |  5 +++++
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/error_page.rs b/src/error_page.rs
index b4bc64e..b3853ec 100644
--- a/src/error_page.rs
+++ b/src/error_page.rs
@@ -1,4 +1,5 @@
use headers::{AcceptRanges, ContentLength, ContentType, HeaderMapExt};
use headers::{AcceptRanges, ContentLength, ContentType, HeaderMapExt, HeaderValue};
use http::header::CONTENT_TYPE;
use hyper::{Body, Method, Response, StatusCode};
use once_cell::sync::OnceCell;

@@ -89,6 +90,10 @@ pub fn error_response(method: &Method, status_code: &StatusCode) -> Result<Respo

    let mut resp = Response::new(body);
    *resp.status_mut() = *status_code;
    resp.headers_mut().insert(
        CONTENT_TYPE,
        HeaderValue::from_static("text/html; charset=utf-8"),
    );
    resp.headers_mut().typed_insert(ContentLength(len));
    resp.headers_mut().typed_insert(ContentType::html());
    resp.headers_mut().typed_insert(AcceptRanges::bytes());
diff --git a/src/handler.rs b/src/handler.rs
index d7a80fe..7909b3c 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -50,11 +50,6 @@ impl RequestHandler {
            // Static files
            match static_files::handle(method, headers, root_dir, uri_path, dir_listing).await {
                Ok(mut resp) => {
                    // Append security headers
                    if self.opts.security_headers {
                        security_headers::append_headers(&mut resp);
                    }

                    // Auto compression based on the `Accept-Encoding` header
                    if self.opts.compression {
                        resp = compression::auto(method, headers, resp)?;
@@ -64,6 +59,11 @@ impl RequestHandler {
                    let ext = uri_path.to_lowercase();
                    control_headers::append_headers(&ext, &mut resp);

                    // Append security headers
                    if self.opts.security_headers {
                        security_headers::append_headers(&mut resp);
                    }

                    Ok(resp)
                }
                Err(status) => error_page::error_response(method, &status),
diff --git a/src/static_files.rs b/src/static_files.rs
index 7c12831..b2ab824 100644
--- a/src/static_files.rs
+++ b/src/static_files.rs
@@ -8,6 +8,7 @@ use headers::{
    AcceptRanges, ContentLength, ContentRange, ContentType, HeaderMap, HeaderMapExt, HeaderValue,
    IfModifiedSince, IfRange, IfUnmodifiedSince, LastModified, Range,
};
use http::header::CONTENT_TYPE;
use humansize::{file_size_opts, FileSize};
use hyper::{Body, Method, Response, StatusCode};
use percent_encoding::percent_decode_str;
@@ -239,6 +240,10 @@ async fn read_directory_entries(
    );

    let mut resp = Response::new(Body::empty());
    resp.headers_mut().insert(
        CONTENT_TYPE,
        HeaderValue::from_static("text/html; charset=utf-8"),
    );
    resp.headers_mut()
        .typed_insert(ContentLength(page_str.len() as u64));