From d790d13394c0642695aa4a9218c2384668003574 Mon Sep 17 00:00:00 2001 From: Serein <2075337935@qq.com> Date: Sun, 20 Oct 2024 12:45:37 +0800 Subject: [PATCH] fix: handle 3xx redirect response --- .../Network/HttpsAccessManager.cc | 5 ---- src/Infrastructure/Network/NetworkClient.cc | 27 ++++++++++++++++++- src/Infrastructure/Network/NetworkClient.h | 15 ++++------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Infrastructure/Network/HttpsAccessManager.cc b/src/Infrastructure/Network/HttpsAccessManager.cc index 13a748b3..940514d1 100644 --- a/src/Infrastructure/Network/HttpsAccessManager.cc +++ b/src/Infrastructure/Network/HttpsAccessManager.cc @@ -86,11 +86,6 @@ Task HttpsAccessManager::makeReply(std::string host, if (!ec || ec == net::error::eof || (ignoreSslError && ec == ssl::error::stream_truncated) || ec == beast::error::timeout) { // If we get here then the connection is closed gracefully - if (res.result() == http::status::found) { - auto location = res.base().at("Location"); - req.set(http::field::host, location); - co_return co_await makeReply(location, req); - } co_return Ok(res); } diff --git a/src/Infrastructure/Network/NetworkClient.cc b/src/Infrastructure/Network/NetworkClient.cc index e18d42cf..c9d0a8a9 100644 --- a/src/Infrastructure/Network/NetworkClient.cc +++ b/src/Infrastructure/Network/NetworkClient.cc @@ -937,7 +937,7 @@ urls::url NetworkClient::githubEndpoint(std::string_view endpoint, return r; } -JsonResult NetworkClient::handleResponse(http::response response) { +JsonResult NetworkClient::handleEventoResponse(http::response response) { if (response.result() != http::status::ok) { return Err(Error(response.result_int())); } @@ -978,6 +978,31 @@ JsonResult NetworkClient::handleResponse(http::response resp return Ok(data); } +Task NetworkClient::handleGithubResponse(http::response response) { + auto status = response.result(); + std::string data; + if (status == http::status::found || status == http::status::moved_permanently + || status == http::status::temporary_redirect) { + auto location = response.base().at("Location"); + spdlog::info("Redirecting to {}", location); + auto redirectUrl = urls::url_view(location); + co_return co_await this->request(http::verb::get, redirectUrl); + } else if (status != http::status::ok) { + co_return Err(Error(response.result_int())); + } + + data = beast::buffers_to_string(response.body().data()); + + nlohmann::basic_json<> res; + try { + res = nlohmann::json::parse(data); + debug(), res.dump(); + } catch (const nlohmann::json::parse_error& e) { + co_return Err(Error(Error::JsonDes, e.what())); + } + co_return Ok(res); +} + Task NetworkClient::saveToDisk(std::string const& data, std::filesystem::path const& path) { #if defined(PLATFORM_APPLE) diff --git a/src/Infrastructure/Network/NetworkClient.h b/src/Infrastructure/Network/NetworkClient.h index 14925934..f5beb172 100644 --- a/src/Infrastructure/Network/NetworkClient.h +++ b/src/Infrastructure/Network/NetworkClient.h @@ -159,7 +159,7 @@ class NetworkClient { if (reply.isErr()) co_return reply.unwrapErr(); - auto result = handleResponse(reply.unwrap()); + auto result = handleEventoResponse(reply.unwrap()); if (cacheTtl != 0s && result.isOk()) { // Update cache @@ -186,15 +186,9 @@ class NetworkClient { if (reply.isErr()) co_return reply.unwrapErr(); - nlohmann::basic_json<> res; - try { - res = nlohmann::json::parse(beast::buffers_to_string(reply.unwrap().body().data())); - debug(), res.dump(); - } catch (const nlohmann::json::parse_error& e) { - co_return Err(Error(Error::JsonDes, e.what())); - } + auto result = co_await handleGithubResponse(reply.unwrap()); - co_return res; + co_return result; } // url builder @@ -206,7 +200,8 @@ class NetworkClient { static urls::url githubEndpoint(std::string_view endpoint, std::initializer_list const& queryParams); //response handler for github api - static JsonResult handleResponse(http::response response); + static JsonResult handleEventoResponse(http::response response); + Task handleGithubResponse(http::response response); static Task saveToDisk(std::string const& data, std::filesystem::path const& path);