mirror of
https://github.com/vlang/v.git
synced 2025-09-26 22:31:03 -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'
|
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() {
|
fn test_custom_mime_types() {
|
||||||
x := http.get('${localserver}/unkown_mime.what')!
|
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]
|
@[manualfree]
|
||||||
fn serve_if_static[A, X](app &A, mut user_context X, url urllib.URL, host string) bool {
|
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
|
// 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
|
// 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)
|
||||||
mut mime_type := app.static_mime_types[ext] or { vweb.mime_types[ext] }
|
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 == '' {
|
if static_file == '' || mime_type == '' {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user