From 9788ae1f3e9c8715b47e6b168fe5957ff5dd1fed Mon Sep 17 00:00:00 2001 From: alexey semenyuk Date: Fri, 11 Oct 2024 10:50:32 +0500 Subject: [PATCH] veb,vweb: make the MIME type checks case insensitive (allow for serving static file.JPG) (fix #22425) (#22483) --- vlib/veb/static_handler.v | 2 +- vlib/veb/tests/static_handler_test.v | 7 +++++++ vlib/veb/tests/testdata/upper_case.TXT | 1 + vlib/veb/veb.v | 2 +- vlib/x/vweb/static_handler.v | 2 +- vlib/x/vweb/tests/static_handler_test.v | 7 +++++++ vlib/x/vweb/tests/testdata/upper_case.TXT | 1 + vlib/x/vweb/vweb.v | 2 +- 8 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 vlib/veb/tests/testdata/upper_case.TXT create mode 100644 vlib/x/vweb/tests/testdata/upper_case.TXT diff --git a/vlib/veb/static_handler.v b/vlib/veb/static_handler.v index 068f7f6f80ae35..6cbc59c6c61a60 100644 --- a/vlib/veb/static_handler.v +++ b/vlib/veb/static_handler.v @@ -104,7 +104,7 @@ pub fn (mut sh StaticHandler) serve_static(url string, file_path string) ! { // `url` is the access path on the site, `file_path` is the real path to the file // `host` is the host to serve the file from pub fn (mut sh StaticHandler) host_serve_static(host string, url string, file_path string) ! { - ext := os.file_ext(file_path) + ext := os.file_ext(file_path).to_lower() // Rudimentary guard against adding files not in mime_types. if ext !in sh.static_mime_types && ext !in mime_types { diff --git a/vlib/veb/tests/static_handler_test.v b/vlib/veb/tests/static_handler_test.v index 134bf1e374b1cd..28a0defa644aa2 100644 --- a/vlib/veb/tests/static_handler_test.v +++ b/vlib/veb/tests/static_handler_test.v @@ -119,3 +119,10 @@ fn test_custom_folder_mount() { assert x.status() == .ok assert x.body == 'root' } + +fn test_upper_case_mime_type() { + x := http.get('${localserver}/upper_case.TXT')! + + assert x.status() == .ok + assert x.body == 'body' +} diff --git a/vlib/veb/tests/testdata/upper_case.TXT b/vlib/veb/tests/testdata/upper_case.TXT new file mode 100644 index 00000000000000..fb4c35e739d539 --- /dev/null +++ b/vlib/veb/tests/testdata/upper_case.TXT @@ -0,0 +1 @@ +body \ No newline at end of file diff --git a/vlib/veb/veb.v b/vlib/veb/veb.v index 24e1d54d3a64e3..5302794fb14848 100644 --- a/vlib/veb/veb.v +++ b/vlib/veb/veb.v @@ -845,7 +845,7 @@ fn serve_if_static[A, X](app &A, mut user_context X, url urllib.URL, host string static_file := app.static_files[asked_path] or { return false } // StaticHandler ensures that the mime type exists on either the App or in veb - ext := os.file_ext(static_file) + ext := os.file_ext(static_file).to_lower() mut mime_type := app.static_mime_types[ext] or { mime_types[ext] } static_host := app.static_hosts[asked_path] or { '' } diff --git a/vlib/x/vweb/static_handler.v b/vlib/x/vweb/static_handler.v index 7fe7db32ac86b6..3921752c67b447 100644 --- a/vlib/x/vweb/static_handler.v +++ b/vlib/x/vweb/static_handler.v @@ -104,7 +104,7 @@ pub fn (mut sh StaticHandler) serve_static(url string, file_path string) ! { // `url` is the access path on the site, `file_path` is the real path to the file // `host` is the host to serve the file from pub fn (mut sh StaticHandler) host_serve_static(host string, url string, file_path string) ! { - ext := os.file_ext(file_path) + ext := os.file_ext(file_path).to_lower() // Rudimentary guard against adding files not in mime_types. if ext !in sh.static_mime_types && ext !in mime_types { diff --git a/vlib/x/vweb/tests/static_handler_test.v b/vlib/x/vweb/tests/static_handler_test.v index f51368e4231329..663aee2e9dc32e 100644 --- a/vlib/x/vweb/tests/static_handler_test.v +++ b/vlib/x/vweb/tests/static_handler_test.v @@ -119,3 +119,10 @@ fn test_custom_folder_mount() { assert x.status() == .ok assert x.body == 'root' } + +fn test_upper_case_mime_type() { + x := http.get('${localserver}/upper_case.TXT')! + + assert x.status() == .ok + assert x.body == 'body' +} diff --git a/vlib/x/vweb/tests/testdata/upper_case.TXT b/vlib/x/vweb/tests/testdata/upper_case.TXT new file mode 100644 index 00000000000000..fb4c35e739d539 --- /dev/null +++ b/vlib/x/vweb/tests/testdata/upper_case.TXT @@ -0,0 +1 @@ +body \ No newline at end of file diff --git a/vlib/x/vweb/vweb.v b/vlib/x/vweb/vweb.v index 2044652112ae7b..da016ac0fa565c 100644 --- a/vlib/x/vweb/vweb.v +++ b/vlib/x/vweb/vweb.v @@ -981,7 +981,7 @@ fn serve_if_static[A, X](app &A, mut user_context X, url urllib.URL, host string 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) + ext := os.file_ext(static_file).to_lower() mut mime_type := app.static_mime_types[ext] or { mime_types[ext] } static_host := app.static_hosts[asked_path] or { '' }