mirror of https://github.com/axmolengine/axmol.git
Add custom hosts support for HttpReuqest
refer to: https://curl.haxx.se/libcurl/c/CURLOPT_RESOLVE.html
This commit is contained in:
parent
0e987a7a0d
commit
79e04aad3c
|
@ -214,10 +214,12 @@ class CURLRaii
|
|||
CURL *_curl;
|
||||
/// Keeps custom header data
|
||||
curl_slist *_headers;
|
||||
curl_slist *_hosts;
|
||||
public:
|
||||
CURLRaii()
|
||||
: _curl(curl_easy_init())
|
||||
, _headers(nullptr)
|
||||
, _hosts(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -250,7 +252,7 @@ public:
|
|||
return false;
|
||||
|
||||
/* get custom header data (if set) */
|
||||
std::vector<std::string> headers=request->getHeaders();
|
||||
auto& headers = request->getHeaders();
|
||||
if(!headers.empty())
|
||||
{
|
||||
/* append custom headers one by one */
|
||||
|
@ -260,6 +262,16 @@ public:
|
|||
if (!setOption(CURLOPT_HTTPHEADER, _headers))
|
||||
return false;
|
||||
}
|
||||
/* get custom host data (if set) */
|
||||
auto& hosts = request->getHosts();
|
||||
if (!hosts.empty()) {
|
||||
/* append hosts headers one by one */
|
||||
for (auto& host : hosts)
|
||||
_hosts = curl_slist_append(_hosts, host.c_str());
|
||||
/* set custom hosts for curl */
|
||||
if (!setOption(CURLOPT_RESOLVE, _hosts))
|
||||
return false;
|
||||
}
|
||||
std::string cookieFilename = client->getCookieFilename();
|
||||
if (!cookieFilename.empty()) {
|
||||
if (!setOption(CURLOPT_COOKIEFILE, cookieFilename.c_str())) {
|
||||
|
@ -269,7 +281,6 @@ public:
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return setOption(CURLOPT_URL, request->getUrl())
|
||||
&& setOption(CURLOPT_WRITEFUNCTION, callback)
|
||||
&& setOption(CURLOPT_WRITEDATA, stream)
|
||||
|
|
|
@ -315,11 +315,14 @@ public:
|
|||
*
|
||||
* @return std::vector<std::string> the string vector of custom-defined headers.
|
||||
*/
|
||||
std::vector<std::string> getHeaders() const
|
||||
const std::vector<std::string>& getHeaders() const
|
||||
{
|
||||
return _headers;
|
||||
}
|
||||
|
||||
void setHosts(std::vector<std::string> hosts) { _hosts = std::move(hosts); }
|
||||
const std::vector<std::string>& getHosts() const { return _hosts; }
|
||||
|
||||
private:
|
||||
void doSetResponseCallback(Ref* pTarget, SEL_HttpResponse pSelector)
|
||||
{
|
||||
|
@ -347,6 +350,7 @@ protected:
|
|||
ccHttpRequestCallback _pCallback; /// C++11 style callbacks
|
||||
void* _pUserData; /// You can add your customed data here
|
||||
std::vector<std::string> _headers; /// custom http headers
|
||||
std::vector<std::string> _hosts;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue