fix broken web request stuff

This commit is contained in:
altalk23 2023-09-08 18:05:20 +03:00
parent 645eb8e03d
commit 92ca99b82c
2 changed files with 9 additions and 8 deletions

View file

@ -130,7 +130,7 @@ namespace geode::utils::web {
AsyncExpectCode m_expect = nullptr; AsyncExpectCode m_expect = nullptr;
AsyncProgress m_progress = nullptr; AsyncProgress m_progress = nullptr;
AsyncCancelled m_cancelled = nullptr; AsyncCancelled m_cancelled = nullptr;
AsyncWebRequestData* m_extra = nullptr; mutable AsyncWebRequestData* m_extra = nullptr;
std::variant<std::monostate, std::ostream*, ghc::filesystem::path> m_target; std::variant<std::monostate, std::ostream*, ghc::filesystem::path> m_target;
std::vector<std::string> m_httpHeaders; std::vector<std::string> m_httpHeaders;

View file

@ -160,6 +160,7 @@ private:
mutable std::mutex m_mutex; mutable std::mutex m_mutex;
AsyncWebRequestData m_extra; AsyncWebRequestData m_extra;
std::variant<std::monostate, std::ostream*, ghc::filesystem::path> m_target;
std::vector<std::string> m_httpHeaders; std::vector<std::string> m_httpHeaders;
@ -184,7 +185,7 @@ static std::unordered_map<std::string, SentAsyncWebRequestHandle> RUNNING_REQUES
static std::mutex RUNNING_REQUESTS_MUTEX; static std::mutex RUNNING_REQUESTS_MUTEX;
SentAsyncWebRequest::Impl::Impl(SentAsyncWebRequest* self, AsyncWebRequest const& req, std::string const& id) : SentAsyncWebRequest::Impl::Impl(SentAsyncWebRequest* self, AsyncWebRequest const& req, std::string const& id) :
m_id(id), m_url(req.m_url), m_extra(req.extra()), m_httpHeaders(req.m_httpHeaders) { m_id(id), m_url(req.m_url), m_target(req.m_target), m_extra(req.extra()), m_httpHeaders(req.m_httpHeaders) {
#define AWAIT_RESUME() \ #define AWAIT_RESUME() \
{\ {\
@ -218,16 +219,16 @@ SentAsyncWebRequest::Impl::Impl(SentAsyncWebRequest* self, AsyncWebRequest const
std::unique_ptr<std::ofstream> file = nullptr; std::unique_ptr<std::ofstream> file = nullptr;
// into file // into file
if (std::holds_alternative<ghc::filesystem::path>(m_extra.m_target)) { if (std::holds_alternative<ghc::filesystem::path>(m_target)) {
file = std::make_unique<std::ofstream>( file = std::make_unique<std::ofstream>(
std::get<ghc::filesystem::path>(m_extra.m_target), std::ios::out | std::ios::binary std::get<ghc::filesystem::path>(m_target), std::ios::out | std::ios::binary
); );
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file.get()); curl_easy_setopt(curl, CURLOPT_WRITEDATA, file.get());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, utils::fetch::writeBinaryData); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, utils::fetch::writeBinaryData);
} }
// into stream // into stream
else if (std::holds_alternative<std::ostream*>(m_extra.m_target)) { else if (std::holds_alternative<std::ostream*>(m_target)) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, std::get<std::ostream*>(m_extra.m_target)); curl_easy_setopt(curl, CURLOPT_WRITEDATA, std::get<std::ostream*>(m_target));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, utils::fetch::writeBinaryData); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, utils::fetch::writeBinaryData);
} }
// into memory // into memory
@ -334,8 +335,8 @@ void SentAsyncWebRequest::Impl::doCancel() {
m_cleanedUp = true; m_cleanedUp = true;
// remove file if downloaded to one // remove file if downloaded to one
if (std::holds_alternative<ghc::filesystem::path>(m_extra.m_target)) { if (std::holds_alternative<ghc::filesystem::path>(m_target)) {
auto path = std::get<ghc::filesystem::path>(m_extra.m_target); auto path = std::get<ghc::filesystem::path>(m_target);
if (ghc::filesystem::exists(path)) { if (ghc::filesystem::exists(path)) {
try { try {
ghc::filesystem::remove(path); ghc::filesystem::remove(path);