diff --git a/cocos/network/HttpClient.cpp b/cocos/network/HttpClient.cpp index e180099423..334dee7654 100644 --- a/cocos/network/HttpClient.cpp +++ b/cocos/network/HttpClient.cpp @@ -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 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) diff --git a/cocos/network/HttpRequest.h b/cocos/network/HttpRequest.h index bba00aae1f..a6d2da3bd8 100644 --- a/cocos/network/HttpRequest.h +++ b/cocos/network/HttpRequest.h @@ -315,11 +315,14 @@ public: * * @return std::vector the string vector of custom-defined headers. */ - std::vector getHeaders() const + const std::vector& getHeaders() const { return _headers; } + void setHosts(std::vector hosts) { _hosts = std::move(hosts); } + const std::vector& 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 _headers; /// custom http headers + std::vector _hosts; }; }