Merge pull request #7991 from spycrab/npi_retcode

UICommon/NetPlayIndex: Handle non 200 HTTP return codes
This commit is contained in:
spycrab 2019-04-13 15:24:10 +02:00 committed by GitHub
commit 241166a1a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 26 deletions

View File

@ -32,7 +32,7 @@ public:
void UseIPv4();
void FollowRedirects(long max);
Response Fetch(const std::string& url, Method method, const Headers& headers, const u8* payload,
size_t size);
size_t size, AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only);
static int CurlProgressCallback(Impl* impl, double dlnow, double dltotal, double ulnow,
double ultotal);
@ -80,22 +80,23 @@ std::string HttpRequest::EscapeComponent(const std::string& string)
return m_impl->EscapeComponent(string);
}
HttpRequest::Response HttpRequest::Get(const std::string& url, const Headers& headers)
HttpRequest::Response HttpRequest::Get(const std::string& url, const Headers& headers,
AllowedReturnCodes codes)
{
return m_impl->Fetch(url, Impl::Method::GET, headers, nullptr, 0);
return m_impl->Fetch(url, Impl::Method::GET, headers, nullptr, 0, codes);
}
HttpRequest::Response HttpRequest::Post(const std::string& url, const std::vector<u8>& payload,
const Headers& headers)
const Headers& headers, AllowedReturnCodes codes)
{
return m_impl->Fetch(url, Impl::Method::POST, headers, payload.data(), payload.size());
return m_impl->Fetch(url, Impl::Method::POST, headers, payload.data(), payload.size(), codes);
}
HttpRequest::Response HttpRequest::Post(const std::string& url, const std::string& payload,
const Headers& headers)
const Headers& headers, AllowedReturnCodes codes)
{
return m_impl->Fetch(url, Impl::Method::POST, headers,
reinterpret_cast<const u8*>(payload.data()), payload.size());
reinterpret_cast<const u8*>(payload.data()), payload.size(), codes);
}
int HttpRequest::Impl::CurlProgressCallback(Impl* impl, double dlnow, double dltotal, double ulnow,
@ -184,7 +185,7 @@ static size_t CurlWriteCallback(char* data, size_t size, size_t nmemb, void* use
HttpRequest::Response HttpRequest::Impl::Fetch(const std::string& url, Method method,
const Headers& headers, const u8* payload,
size_t size)
size_t size, AllowedReturnCodes codes)
{
curl_easy_setopt(m_curl.get(), CURLOPT_POST, method == Method::POST);
curl_easy_setopt(m_curl.get(), CURLOPT_URL, url.c_str());
@ -219,6 +220,9 @@ HttpRequest::Response HttpRequest::Impl::Fetch(const std::string& url, Method me
return {};
}
if (codes == AllowedReturnCodes::All)
return buffer;
long response_code = 0;
curl_easy_getinfo(m_curl.get(), CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200)

View File

@ -19,6 +19,12 @@ namespace Common
class HttpRequest final
{
public:
enum class AllowedReturnCodes : u8
{
Ok_Only,
All
};
// Return false to abort the request
using ProgressCallback =
std::function<bool(double dlnow, double dltotal, double ulnow, double ultotal)>;
@ -35,10 +41,12 @@ public:
void UseIPv4();
void FollowRedirects(long max = 1);
std::string EscapeComponent(const std::string& string);
Response Get(const std::string& url, const Headers& headers = {});
Response Post(const std::string& url, const std::vector<u8>& payload,
const Headers& headers = {});
Response Post(const std::string& url, const std::string& payload, const Headers& headers = {});
Response Get(const std::string& url, const Headers& headers = {},
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only);
Response Post(const std::string& url, const std::vector<u8>& payload, const Headers& headers = {},
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only);
Response Post(const std::string& url, const std::string& payload, const Headers& headers = {},
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only);
private:
class Impl;

View File

@ -56,7 +56,8 @@ NetPlayIndex::List(const std::map<std::string, std::string>& filters)
list_url.pop_back();
}
auto response = request.Get(list_url, {{"X-Is-Dolphin", "1"}});
auto response =
request.Get(list_url, {{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All);
if (!response)
{
m_last_error = "NO_RESPONSE";
@ -132,7 +133,7 @@ void NetPlayIndex::NotificationLoop()
Config::Get(Config::NETPLAY_INDEX_URL) + "/v0/session/active?secret=" + m_secret +
"&player_count=" + std::to_string(m_player_count) +
"&game=" + request.EscapeComponent(m_game) + "&in_game=" + std::to_string(m_in_game),
{{"X-Is-Dolphin", "1"}});
{{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All);
if (!response)
continue;
@ -162,17 +163,16 @@ void NetPlayIndex::NotificationLoop()
bool NetPlayIndex::Add(NetPlaySession session)
{
Common::HttpRequest request;
auto response = request.Get(Config::Get(Config::NETPLAY_INDEX_URL) +
"/v0/session/add?name=" + request.EscapeComponent(session.name) +
"&region=" + request.EscapeComponent(session.region) +
"&game=" + request.EscapeComponent(session.game_id) +
"&password=" + std::to_string(session.has_password) +
"&method=" + session.method + "&server_id=" + session.server_id +
"&in_game=" + std::to_string(session.in_game) +
"&port=" + std::to_string(session.port) +
"&player_count=" + std::to_string(session.player_count) +
"&version=" + Common::scm_desc_str,
{{"X-Is-Dolphin", "1"}});
auto response = request.Get(
Config::Get(Config::NETPLAY_INDEX_URL) +
"/v0/session/add?name=" + request.EscapeComponent(session.name) +
"&region=" + request.EscapeComponent(session.region) +
"&game=" + request.EscapeComponent(session.game_id) +
"&password=" + std::to_string(session.has_password) + "&method=" + session.method +
"&server_id=" + session.server_id + "&in_game=" + std::to_string(session.in_game) +
"&port=" + std::to_string(session.port) + "&player_count=" +
std::to_string(session.player_count) + "&version=" + Common::scm_desc_str,
{{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All);
if (!response.has_value())
{
@ -239,7 +239,7 @@ void NetPlayIndex::Remove()
// We don't really care whether this fails or not
Common::HttpRequest request;
request.Get(Config::Get(Config::NETPLAY_INDEX_URL) + "/v0/session/remove?secret=" + m_secret,
{{"X-Is-Dolphin", "1"}});
{{"X-Is-Dolphin", "1"}}, Common::HttpRequest::AllowedReturnCodes::All);
m_secret.clear();
}