From ead4b0594b46c8cd82c437cc41a9a03be0f2099f Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 1 Jul 2022 22:13:45 +0200 Subject: [PATCH] Refactored FileCache so it actually works with this setup. --- modules/web/file_cache.cpp | 72 +++++++++++++++++++++++++++++------ modules/web/file_cache.h | 21 ++++++++-- modules/web/http/web_root.cpp | 13 +++++-- 3 files changed, 86 insertions(+), 20 deletions(-) diff --git a/modules/web/file_cache.cpp b/modules/web/file_cache.cpp index 89954e81a..a12485e18 100644 --- a/modules/web/file_cache.cpp +++ b/modules/web/file_cache.cpp @@ -2,12 +2,18 @@ #include "core/os/dir_access.h" #include "core/os/file_access.h" +#include "core/os/os.h" +#include "core/print_string.h" String FileCache::get_wwwroot() { - return wwwroot; + return _wwwroot_orig; } void FileCache::set_wwwroot(const String &val) { - wwwroot = val; + _wwwroot_orig = val; +} + +String FileCache::get_wwwroot_abs() { + return _wwwroot_orig; } int FileCache::get_cache_invalidation_time() { @@ -18,25 +24,67 @@ void FileCache::set_cache_invalidation_time(const int &val) { } void FileCache::wwwroot_register_file(const String &file_path) { - registered_files.insert(file_path); + RegisteredFileEntry e; + e.orig_path = file_path; + e.lowercase_path = file_path.to_lower(); + + _registered_files.push_back(e); } void FileCache::wwwroot_deregister_file(const String &file_path) { - registered_files.erase(file_path); + for (int i = 0; i < _registered_files.size(); ++i) { + const RegisteredFileEntry &e = _registered_files[i]; + + if (file_path == e.orig_path) { + _registered_files.remove(i); + return; + } + } } bool FileCache::wwwroot_has_file(const String &file_path) { - return registered_files.has(file_path); + //return registered_files.has(file_path); + + for (int i = 0; i < _registered_files.size(); ++i) { + const RegisteredFileEntry &e = _registered_files[i]; + + if (file_path == e.lowercase_path) { + return true; + } + } + + return false; +} + +int FileCache::wwwroot_get_file_index(const String &file_path) { + for (int i = 0; i < _registered_files.size(); ++i) { + const RegisteredFileEntry &e = _registered_files[i]; + + if (file_path == e.lowercase_path) { + return i; + } + } + + return -1; +} + +String FileCache::wwwroot_get_file_orig_path(const int index) { + ERR_FAIL_INDEX_V(index, _registered_files.size(), ""); + + return _registered_files[index].orig_path; } void FileCache::wwwroot_refresh_cache() { _lock.write_lock(); - registered_files.clear(); + _registered_files.clear(); - wwwroot.path_clean_end_slash(); + if (_wwwroot_orig != "") { + _wwwroot = DirAccess::get_full_path(_wwwroot_orig, DirAccess::ACCESS_FILESYSTEM); - wwwroot_evaluate_dir(wwwroot); + _wwwroot = _wwwroot.path_clean_end_slash(); + wwwroot_evaluate_dir(_wwwroot); + } _lock.write_unlock(); } @@ -57,8 +105,8 @@ void FileCache::wwwroot_evaluate_dir(const String &path, const bool should_exist if (!da->current_is_dir()) { String np = path + "/" + f; - np = np.substr(wwwroot.size(), np.size() - wwwroot.size()); - registered_files.insert(np); + np = np.substr(_wwwroot.size() - 1, np.size() - _wwwroot.size()); + wwwroot_register_file(np); } else { wwwroot_evaluate_dir(path + "/" + f); } @@ -162,7 +210,7 @@ void FileCache::set_cached_body(const String &path, const String &body) { void FileCache::clear() { _lock.write_lock(); - registered_files.clear(); + _registered_files.clear(); for (Map::Element *E = cache_map.front(); E; E++) { CacheEntry *ce = E->get(); @@ -182,7 +230,7 @@ FileCache::FileCache() { } FileCache::~FileCache() { - registered_files.clear(); + _registered_files.clear(); } void FileCache::_bind_methods() { diff --git a/modules/web/file_cache.h b/modules/web/file_cache.h index 0269ed02c..e9c3f6ec1 100644 --- a/modules/web/file_cache.h +++ b/modules/web/file_cache.h @@ -1,11 +1,12 @@ #ifndef FILE_CACHE_H #define FILE_CACHE_H +#include "core/hash_map.h" #include "core/map.h" #include "core/os/os.h" #include "core/os/rw_lock.h" -#include "core/set.h" #include "core/ustring.h" +#include "core/vector.h" #include "core/reference.h" @@ -16,6 +17,8 @@ public: String get_wwwroot(); void set_wwwroot(const String &val); + String get_wwwroot_abs(); + int get_cache_invalidation_time(); void set_cache_invalidation_time(const int &val); @@ -24,6 +27,9 @@ public: void wwwroot_register_file(const String &file_path); void wwwroot_deregister_file(const String &file_path); bool wwwroot_has_file(const String &file_path); + //return -1 if does not exists + int wwwroot_get_file_index(const String &file_path); + String wwwroot_get_file_orig_path(const int index); void wwwroot_refresh_cache(); void wwwroot_evaluate_dir(const String &path, const bool should_exist = true); @@ -37,11 +43,8 @@ public: FileCache(); ~FileCache(); - String wwwroot; uint64_t cache_invalidation_time; - Set registered_files; - protected: static void _bind_methods(); @@ -56,6 +59,16 @@ protected: RWLock _lock; Map cache_map; + + String _wwwroot_orig; + String _wwwroot; + + struct RegisteredFileEntry { + String orig_path; + String lowercase_path; + }; + + Vector _registered_files; }; #endif diff --git a/modules/web/http/web_root.cpp b/modules/web/http/web_root.cpp index d8fb53703..0429ece49 100644 --- a/modules/web/http/web_root.cpp +++ b/modules/web/http/web_root.cpp @@ -15,9 +15,11 @@ void WebRoot::set_www_root_path(const String &val) { _www_root_path = val; if (val == "") { + _www_root_file_cache->set_wwwroot(val); _www_root_file_cache->clear(); } else { - _www_root_file_cache->wwwroot_evaluate_dir(val); + _www_root_file_cache->set_wwwroot(val); + _www_root_file_cache->wwwroot_refresh_cache(); } } @@ -134,9 +136,12 @@ bool WebRoot::process_middlewares(Ref request) { bool WebRoot::try_send_wwwroot_file(Ref request) { String path = request->get_path_full(); + path = path.to_lower(); - if (_www_root_file_cache->wwwroot_has_file(path)) { - send_file(path, request); + int file_indx = _www_root_file_cache->wwwroot_get_file_index(path); + + if (file_indx != -1) { + send_file(_www_root_file_cache->wwwroot_get_file_orig_path(file_indx), request); return true; } @@ -145,7 +150,7 @@ bool WebRoot::try_send_wwwroot_file(Ref request) { } void WebRoot::send_file(const String &path, Ref request) { - String fp = _www_root_file_cache->wwwroot + path; + String fp = _www_root_file_cache->get_wwwroot_abs() + path; request->send_file(fp); }