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:
David Legrand 2024-02-11 10:19:37 +01:00 committed by GitHub
parent 59a8690dbd
commit 5da88800ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 2 deletions

View File

@ -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')!

View File

@ -0,0 +1 @@
OK

View File

@ -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
}