Skip to content

Commit

Permalink
fix: handle 3xx redirect response
Browse files Browse the repository at this point in the history
  • Loading branch information
Serein207 committed Oct 20, 2024
1 parent d3a2939 commit d790d13
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
5 changes: 0 additions & 5 deletions src/Infrastructure/Network/HttpsAccessManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ Task<ResponseResult> 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);
}

Expand Down
27 changes: 26 additions & 1 deletion src/Infrastructure/Network/NetworkClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ urls::url NetworkClient::githubEndpoint(std::string_view endpoint,
return r;
}

JsonResult NetworkClient::handleResponse(http::response<http::dynamic_body> response) {
JsonResult NetworkClient::handleEventoResponse(http::response<http::dynamic_body> response) {
if (response.result() != http::status::ok) {
return Err(Error(response.result_int()));
}
Expand Down Expand Up @@ -978,6 +978,31 @@ JsonResult NetworkClient::handleResponse(http::response<http::dynamic_body> resp
return Ok(data);
}

Task<JsonResult> NetworkClient::handleGithubResponse(http::response<http::dynamic_body> 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<api::Github>(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<bool> NetworkClient::saveToDisk(std::string const& data, std::filesystem::path const& path) {
#if defined(PLATFORM_APPLE)

Expand Down
15 changes: 5 additions & 10 deletions src/Infrastructure/Network/NetworkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -206,7 +200,8 @@ class NetworkClient {
static urls::url githubEndpoint(std::string_view endpoint,
std::initializer_list<urls::param> const& queryParams);
//response handler for github api
static JsonResult handleResponse(http::response<http::dynamic_body> response);
static JsonResult handleEventoResponse(http::response<http::dynamic_body> response);
Task<JsonResult> handleGithubResponse(http::response<http::dynamic_body> response);

static Task<bool> saveToDisk(std::string const& data, std::filesystem::path const& path);

Expand Down

0 comments on commit d790d13

Please sign in to comment.