diff --git a/src/misc.cpp b/src/misc.cpp index 4967042..32e6103 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -711,10 +711,11 @@ std::string fileGet(const std::string &path, bool scope_limit) std::fseek(fp, 0, SEEK_END); long tot = std::ftell(fp); char *data = new char[tot + 1]; + data[tot] = '\0'; std::rewind(fp); std::fread(&data[0], 1, tot, fp); std::fclose(fp); - content.append(data, 0, tot); + content.assign(data, tot); delete[] data; } diff --git a/src/misc.h b/src/misc.h index 5388bb0..c8838d7 100644 --- a/src/misc.h +++ b/src/misc.h @@ -47,7 +47,7 @@ std::string getFormData(const std::string &raw_data); void sleep(int interval); bool regValid(const std::string &target); -bool regFind(const std::string &src, const std::string &target); +bool regFind(const std::string &src, const std::string &match); std::string regReplace(const std::string &src, const std::string &match, const std::string &rep); bool regMatch(const std::string &src, const std::string &match); std::string speedCalc(double speed); diff --git a/src/webget.cpp b/src/webget.cpp index b978c7d..7de3c8b 100644 --- a/src/webget.cpp +++ b/src/webget.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -17,6 +18,9 @@ extern bool print_debug_info; +typedef std::lock_guard guarded_mutex; +std::mutex cache_rw_lock; + //std::string user_agent_str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"; std::string user_agent_str = "subconverter/" + std::string(VERSION) + " cURL/" + std::string(LIBCURL_VERSION); @@ -124,6 +128,7 @@ std::string webGet(std::string url, std::string proxy, std::string &response_hea if(difftime(now, mtime) <= cache_ttl) // within TTL { writeLog(0, "CACHE HIT: '" + url + "', using local cache."); + guarded_mutex guard(cache_rw_lock); response_headers = fileGet(path_header, true); return fileGet(path, true); } @@ -134,6 +139,7 @@ std::string webGet(std::string url, std::string proxy, std::string &response_hea content = curlGet(url, proxy, response_headers, return_code); // try to fetch data if(return_code == CURLE_OK) // success, save new cache { + guarded_mutex guard(cache_rw_lock); fileWrite(path, content, true); fileWrite(path_header, response_headers, true); } @@ -142,6 +148,7 @@ std::string webGet(std::string url, std::string proxy, std::string &response_hea if(fileExist(path)) // failed, check if cache exist { writeLog(0, "Fetch failed. Serving cached content."); // cache exist, serving cache + guarded_mutex guard(cache_rw_lock); content = fileGet(path, true); response_headers = fileGet(path_header, true); }