#ifndef CURL_HTTPS_HANDLER_H #define CURL_HTTPS_HANDLER_H #include #include #include #include #include #include #include class CurlHttpsHandler : public wxWebViewHandler { public: CurlHttpsHandler() : wxWebViewHandler("phttps") {} wxFSFile *GetFile(const wxString &url) override { wxString uri = url.SubString(1, url.length() - 1); std::cout << "CurlHttpsHandler::GetFile called with uri: " << uri << std::endl; // Check cache first if (cache.find(uri) != cache.end()) { return new wxFSFile( new wxMemoryInputStream(cache[uri].data(), cache[uri].size()), uri, "text/html", wxEmptyString, wxDateTime::Now()); new wxMemoryInputStream(cache[uri].data(), cache[uri].size()), uri, "text/html"; } // Fetch content using curl std::pair response = FetchURL(uri.ToStdString()); if (response.second.empty()) { wxLogError("Failed to load %s", response.first); return nullptr; } auto content = response.second; std::string finalUrl = response.first; uri = wxString::FromUTF8(finalUrl.c_str()); // Cache the response std::vector buffer(content.begin(), content.end()); cache[uri] = buffer; return new wxFSFile( new wxMemoryInputStream(cache[uri].data(), cache[uri].size()), uri, "text/html", wxEmptyString, wxDateTime::Now()); } private: static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *output) { size_t totalSize = size * nmemb; output->append((char *)contents, totalSize); return totalSize; } std::pair FetchURL(const std::string &url) { CURL *curl = curl_easy_init(); std::string response; std::string finalUrl; if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); if (curl_easy_perform(curl) == CURLE_OK) { char *effectiveUrl = nullptr; curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effectiveUrl); if (effectiveUrl) { finalUrl = effectiveUrl; } } curl_easy_cleanup(curl); } return {finalUrl, response}; } std::map> cache; }; #endif // CURL_HTTPS_HANDLER_H