mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-02-22 20:17:16 +01:00
Common/HttpRequest: Add option to allow non 200 response codes
This commit is contained in:
parent
635fd8c22c
commit
eddcb70b84
@ -32,7 +32,7 @@ public:
|
|||||||
void UseIPv4();
|
void UseIPv4();
|
||||||
void FollowRedirects(long max);
|
void FollowRedirects(long max);
|
||||||
Response Fetch(const std::string& url, Method method, const Headers& headers, const u8* payload,
|
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,
|
static int CurlProgressCallback(Impl* impl, double dlnow, double dltotal, double ulnow,
|
||||||
double ultotal);
|
double ultotal);
|
||||||
@ -80,22 +80,23 @@ std::string HttpRequest::EscapeComponent(const std::string& string)
|
|||||||
return m_impl->EscapeComponent(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,
|
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,
|
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,
|
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,
|
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,
|
HttpRequest::Response HttpRequest::Impl::Fetch(const std::string& url, Method method,
|
||||||
const Headers& headers, const u8* payload,
|
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_POST, method == Method::POST);
|
||||||
curl_easy_setopt(m_curl.get(), CURLOPT_URL, url.c_str());
|
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 {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (codes == AllowedReturnCodes::All)
|
||||||
|
return buffer;
|
||||||
|
|
||||||
long response_code = 0;
|
long response_code = 0;
|
||||||
curl_easy_getinfo(m_curl.get(), CURLINFO_RESPONSE_CODE, &response_code);
|
curl_easy_getinfo(m_curl.get(), CURLINFO_RESPONSE_CODE, &response_code);
|
||||||
if (response_code != 200)
|
if (response_code != 200)
|
||||||
|
@ -19,6 +19,12 @@ namespace Common
|
|||||||
class HttpRequest final
|
class HttpRequest final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
enum class AllowedReturnCodes : u8
|
||||||
|
{
|
||||||
|
Ok_Only,
|
||||||
|
All
|
||||||
|
};
|
||||||
|
|
||||||
// Return false to abort the request
|
// Return false to abort the request
|
||||||
using ProgressCallback =
|
using ProgressCallback =
|
||||||
std::function<bool(double dlnow, double dltotal, double ulnow, double ultotal)>;
|
std::function<bool(double dlnow, double dltotal, double ulnow, double ultotal)>;
|
||||||
@ -35,10 +41,12 @@ public:
|
|||||||
void UseIPv4();
|
void UseIPv4();
|
||||||
void FollowRedirects(long max = 1);
|
void FollowRedirects(long max = 1);
|
||||||
std::string EscapeComponent(const std::string& string);
|
std::string EscapeComponent(const std::string& string);
|
||||||
Response Get(const std::string& url, const Headers& headers = {});
|
Response Get(const std::string& url, const Headers& headers = {},
|
||||||
Response Post(const std::string& url, const std::vector<u8>& payload,
|
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only);
|
||||||
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 = {});
|
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only);
|
||||||
|
Response Post(const std::string& url, const std::string& payload, const Headers& headers = {},
|
||||||
|
AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class Impl;
|
class Impl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user