Skip to content

Conversation

joseluisq
Copy link
Collaborator

Description

This PR checks auto-index (index.html auto-appended) for directory requests when the compression-static is enabled. Since previously the code was missing that check and was going directly to look up the pre-compressed variant.

Additionally, It prevents trying to serve a pre-compressed file variant if the file found was a directory (a folder named as a file).

Related Issue

Fixes #178

Motivation and Context

How Has This Been Tested?

Server Setup

$ static-web-server -w config.toml

config.toml

[general]
host = "::"
port = 8787
root = "docker/public/"
log-level = "trace"
compression = false
compression-static = true

Client Request

$ curl -I -H "accept-encoding: gzip, deflate, br" http://localhost:8787/assets/

Server Logs

Before:

2023-03-05T21:28:48.625650Z DEBUG hyper::proto::h1::conn: incoming body is empty
2023-03-05T21:28:48.625756Z  INFO static_web_server::handler: incoming request: method=HEAD uri=/assets
2023-03-05T21:28:48.625882Z TRACE static_web_server::static_files: dir: base="docker/public/", route="assets"
2023-03-05T21:28:48.625907Z TRACE static_web_server::compression_static: preparing pre-compressed file path variant of docker/public/assets
2023-03-05T21:28:48.626201Z TRACE static_web_server::compression_static: getting metadata for pre-compressed file variant docker/public/assets.gz
2023-03-05T21:28:48.626247Z DEBUG static_web_server::static_files: file not found: "docker/public/assets.gz" Os { code: 2, kind: NotFound, message: "No such file or directory" }
2023-03-05T21:28:48.626272Z TRACE static_web_server::static_files: getting metadata for file docker/public/assets
2023-03-05T21:28:48.626300Z TRACE static_web_server::static_files: file found: "docker/public/assets"
2023-03-05T21:28:48.626314Z DEBUG static_web_server::static_files: dir: appending an index.html to the directory path
2023-03-05T21:28:48.626339Z TRACE static_web_server::static_files: file found: "docker/public/assets/index.html"
2023-03-05T21:28:48.626412Z TRACE static_web_server::static_files: uri doesn't end with a slash so redirecting permanently
2023-03-05T21:28:48.626504Z TRACE encode_headers: hyper::proto::h1::role: Server::encode status=308, body=None, req_method=Some(HEAD)
2023-03-05T21:28:48.626618Z TRACE encode_headers: hyper::proto::h1::role: server body forced to 0; method=Some(HEAD), status=308
2023-03-05T21:28:48.626649Z TRACE encode_headers: hyper::proto::h1::role: close time.busy=144µs time.idle=6.46µs
2023-03-05T21:28:48.626702Z DEBUG hyper::proto::h1::io: flushed 130 bytes

After:

2023-03-05T21:27:09.946550Z DEBUG hyper::proto::h1::conn: incoming body is empty
2023-03-05T21:27:09.946674Z  INFO static_web_server::handler: incoming request: method=HEAD uri=/assets
2023-03-05T21:27:09.946799Z TRACE static_web_server::static_files: dir: base="docker/public/", route="assets"
2023-03-05T21:27:09.946821Z TRACE static_web_server::static_files: getting metadata for file docker/public/assets
2023-03-05T21:27:09.946867Z TRACE static_web_server::static_files: file found: "docker/public/assets"
2023-03-05T21:27:09.946888Z DEBUG static_web_server::static_files: dir: appending an index.html to the directory path
2023-03-05T21:27:09.946902Z TRACE static_web_server::compression_static: preparing pre-compressed file path variant of docker/public/assets/index.html
2023-03-05T21:27:09.947179Z TRACE static_web_server::compression_static: getting metadata for pre-compressed file variant docker/public/assets/index.html.gz
2023-03-05T21:27:09.948409Z TRACE static_web_server::static_files: file found: "docker/public/assets/index.html.gz"
2023-03-05T21:27:09.948430Z TRACE static_web_server::compression_static: pre-compressed file variant found, serving it directly
2023-03-05T21:27:09.948873Z TRACE encode_headers: hyper::proto::h1::role: Server::encode status=200, body=Some(Unknown), req_method=Some(HEAD)
2023-03-05T21:27:09.949017Z TRACE encode_headers: hyper::proto::h1::role: server body forced to 0; method=Some(HEAD), status=200
2023-03-05T21:27:09.949053Z TRACE encode_headers: hyper::proto::h1::role: close time.busy=179µs time.idle=7.92µs
2023-03-05T21:27:09.949088Z TRACE hyper::proto::h1::dispatch: no more write body allowed, user body is_end_stream = false
2023-03-05T21:27:09.949142Z DEBUG hyper::proto::h1::io: flushed 211 bytes

Screenshots (if appropriate):

No

it also prevents serving a pre-compressed file variant if file found
was a directory (folder named as a file)

fixes #178
@joseluisq joseluisq self-assigned this Mar 5, 2023
@joseluisq joseluisq added v2 v2 release bugfix This is PR fixes a bug labels Mar 5, 2023
@joseluisq joseluisq merged commit 06cba46 into master Mar 5, 2023
@joseluisq joseluisq deleted the bugfix/compression-static-missing-autoindex branch March 5, 2023 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix This is PR fixes a bug v2 v2 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

static compressed index.html is missed when accessing a directory

1 participant