Skip to content

Commit

Permalink
x.vweb: add full static host support, for urls ending with /folder/ ,…
Browse files Browse the repository at this point in the history
… where the folder backing it, has `index.html` inside (#20784)
  • Loading branch information
davlgd committed Feb 11, 2024
1 parent 59a8690 commit 5da8880
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
7 changes: 7 additions & 0 deletions vlib/x/vweb/tests/static_handler_test.v
Expand Up @@ -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')!

Expand Down
1 change: 1 addition & 0 deletions vlib/x/vweb/tests/testdata/sub_folder/index.htm
@@ -0,0 +1 @@
OK
16 changes: 14 additions & 2 deletions vlib/x/vweb/vweb.v
Expand Up @@ -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
}
Expand Down

0 comments on commit 5da8880

Please sign in to comment.