From 5da88800ae158e0ccdb6d9f4c2bbeec6f51bdc3a Mon Sep 17 00:00:00 2001 From: David Legrand <1110600+davlgd@users.noreply.github.com> Date: Sun, 11 Feb 2024 10:19:37 +0100 Subject: [PATCH] x.vweb: add full static host support, for urls ending with /folder/ , where the folder backing it, has `index.html` inside (#20784) --- vlib/x/vweb/tests/static_handler_test.v | 7 +++++++ vlib/x/vweb/tests/testdata/sub_folder/index.htm | 1 + vlib/x/vweb/vweb.v | 16 ++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 vlib/x/vweb/tests/testdata/sub_folder/index.htm diff --git a/vlib/x/vweb/tests/static_handler_test.v b/vlib/x/vweb/tests/static_handler_test.v index 31ae20ba53..c50f80501c 100644 --- a/vlib/x/vweb/tests/static_handler_test.v +++ b/vlib/x/vweb/tests/static_handler_test.v @@ -94,6 +94,13 @@ fn test_scans_subdirs() { assert x.body == 'sub' } +fn test_index_subdirs() { + x := http.get('${localserver}/sub_folder/')! + + assert x.status() == .ok + assert x.body.trim_space() == 'OK' +} + fn test_custom_mime_types() { x := http.get('${localserver}/unkown_mime.what')! diff --git a/vlib/x/vweb/tests/testdata/sub_folder/index.htm b/vlib/x/vweb/tests/testdata/sub_folder/index.htm new file mode 100644 index 0000000000..d86bac9de5 --- /dev/null +++ b/vlib/x/vweb/tests/testdata/sub_folder/index.htm @@ -0,0 +1 @@ +OK diff --git a/vlib/x/vweb/vweb.v b/vlib/x/vweb/vweb.v index 50fe843dba..cd684afae2 100644 --- a/vlib/x/vweb/vweb.v +++ b/vlib/x/vweb/vweb.v @@ -933,13 +933,25 @@ fn route_matches(url_words []string, route_words []string) ?[]string { @[manualfree] fn serve_if_static[A, X](app &A, mut user_context X, url urllib.URL, host string) bool { // TODO: handle url parameters properly - for now, ignore them - static_file := app.static_files[url.path] or { return false } + mut asked_path := url.path + if !asked_path.contains('.') && !asked_path.ends_with('/') { + asked_path += '/' + } + + if asked_path.ends_with('/') { + if app.static_files[asked_path + 'index.html'] != '' { + asked_path += 'index.html' + } else if app.static_files[asked_path + 'index.htm'] != '' { + asked_path += 'index.htm' + } + } + static_file := app.static_files[asked_path] or { return false } // StaticHandler ensures that the mime type exists on either the App or in vweb ext := os.file_ext(static_file) mut mime_type := app.static_mime_types[ext] or { vweb.mime_types[ext] } - static_host := app.static_hosts[url.path] or { '' } + static_host := app.static_hosts[asked_path] or { '' } if static_file == '' || mime_type == '' { return false }