Skip to content

Commit

Permalink
veb,vweb: make the MIME type checks case insensitive (allow for servi…
Browse files Browse the repository at this point in the history
…ng static file.JPG) (fix #22425) (#22483)
  • Loading branch information
alex-semenyuk authored Oct 11, 2024
1 parent 2ca3fdf commit 9788ae1
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion vlib/veb/static_handler.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions vlib/veb/tests/static_handler_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
1 change: 1 addition & 0 deletions vlib/veb/tests/testdata/upper_case.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body
2 changes: 1 addition & 1 deletion vlib/veb/veb.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 { '' }
Expand Down
2 changes: 1 addition & 1 deletion vlib/x/vweb/static_handler.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions vlib/x/vweb/tests/static_handler_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
1 change: 1 addition & 0 deletions vlib/x/vweb/tests/testdata/upper_case.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body
2 changes: 1 addition & 1 deletion vlib/x/vweb/vweb.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 { '' }
Expand Down

0 comments on commit 9788ae1

Please sign in to comment.