mirror of
https://github.com/vlang/v.git
synced 2025-09-25 22:03:15 -04:00
x.vweb: add full static host support, for urls ending with /folder/ , where the folder backing it, has index.html
inside (#20784)
This commit is contained in:
parent
59a8690dbd
commit
5da88800ae
@ -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')!
|
||||
|
||||
|
1
vlib/x/vweb/tests/testdata/sub_folder/index.htm
vendored
Normal file
1
vlib/x/vweb/tests/testdata/sub_folder/index.htm
vendored
Normal file
@ -0,0 +1 @@
|
||||
OK
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user