axmol/cocos/network/HttpClient.cpp

643 lines
18 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2012 greathqy
Copyright (c) 2012 cocos2d-x.org
2016-08-05 09:42:15 +08:00
Copyright (c) 2013-2016 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "network/HttpClient.h"
#include <queue>
#include <errno.h>
#include <curl/curl.h>
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
2014-01-02 16:25:35 +08:00
NS_CC_BEGIN
namespace network {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
typedef int int32_t;
#endif
static HttpClient* _httpClient = nullptr; // pointer to singleton
typedef size_t (*write_callback)(void *ptr, size_t size, size_t nmemb, void *stream);
// Callback function used by libcurl for collect response data
static size_t writeData(void *ptr, size_t size, size_t nmemb, void *stream)
{
std::vector<char> *recvBuffer = (std::vector<char>*)stream;
size_t sizes = size * nmemb;
// add data to the end of recvBuffer
// write data maybe called more than once in a single request
recvBuffer->insert(recvBuffer->end(), (char*)ptr, (char*)ptr+sizes);
return sizes;
}
// Callback function used by libcurl for collect header data
static size_t writeHeaderData(void *ptr, size_t size, size_t nmemb, void *stream)
{
std::vector<char> *recvBuffer = (std::vector<char>*)stream;
size_t sizes = size * nmemb;
// add data to the end of recvBuffer
// write data maybe called more than once in a single request
recvBuffer->insert(recvBuffer->end(), (char*)ptr, (char*)ptr+sizes);
return sizes;
}
static int processGetTask(HttpClient* client, HttpRequest* request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char* errorBuffer);
static int processPostTask(HttpClient* client, HttpRequest* request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char* errorBuffer);
static int processPutTask(HttpClient* client, HttpRequest* request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char* errorBuffer);
static int processDeleteTask(HttpClient* client, HttpRequest* request, write_callback callback, void *stream, long *errorCode, write_callback headerCallback, void *headerStream, char* errorBuffer);
// int processDownloadTask(HttpRequest *task, write_callback callback, void *stream, int32_t *errorCode);
// Worker thread
void HttpClient::networkThread()
{
increaseThreadCount();
while (true)
{
HttpRequest *request;
// step 1: send http request if the requestQueue isn't empty
{
std::lock_guard<std::mutex> lock(_requestQueueMutex);
while (_requestQueue.empty())
{
_sleepCondition.wait(_requestQueueMutex);
}
request = _requestQueue.at(0);
_requestQueue.erase(0);
}
if (request == _requestSentinel) {
break;
}
// step 2: libcurl sync access
// Create a HttpResponse object, the default setting is http access failed
HttpResponse *response = new (std::nothrow) HttpResponse(request);
processResponse(response, _responseMessage);
// add response packet into queue
_responseQueueMutex.lock();
_responseQueue.pushBack(response);
_responseQueueMutex.unlock();
_schedulerMutex.lock();
if (nullptr != _scheduler)
{
_scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this));
}
_schedulerMutex.unlock();
}
// cleanup: if worker thread received quit signal, clean up un-completed request queue
_requestQueueMutex.lock();
_requestQueue.clear();
_requestQueueMutex.unlock();
_responseQueueMutex.lock();
_responseQueue.clear();
_responseQueueMutex.unlock();
decreaseThreadCountAndMayDeleteThis();
}
2014-05-20 20:08:26 +08:00
// Worker thread
void HttpClient::networkThreadAlone(HttpRequest* request, HttpResponse* response)
{
increaseThreadCount();
char responseMessage[RESPONSE_BUFFER_SIZE] = { 0 };
processResponse(response, responseMessage);
_schedulerMutex.lock();
if (nullptr != _scheduler)
{
_scheduler->performFunctionInCocosThread([this, response, request]{
const ccHttpRequestCallback& callback = request->getCallback();
Ref* pTarget = request->getTarget();
SEL_HttpResponse pSelector = request->getSelector();
if (callback != nullptr)
{
callback(this, response);
}
else if (pTarget && pSelector)
{
(pTarget->*pSelector)(this, response);
}
response->release();
// do not release in other thread
request->release();
});
}
_schedulerMutex.unlock();
decreaseThreadCountAndMayDeleteThis();
2014-05-20 20:08:26 +08:00
}
//Configure curl's timeout property
static bool configureCURL(HttpClient* client, CURL* handle, char* errorBuffer)
{
if (!handle) {
return false;
}
int32_t code;
code = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuffer);
if (code != CURLE_OK) {
return false;
}
code = curl_easy_setopt(handle, CURLOPT_TIMEOUT, HttpClient::getInstance()->getTimeoutForRead());
if (code != CURLE_OK) {
return false;
}
code = curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, HttpClient::getInstance()->getTimeoutForConnect());
if (code != CURLE_OK) {
return false;
}
std::string sslCaFilename = client->getSSLVerification();
if (sslCaFilename.empty()) {
2014-09-13 18:54:29 +08:00
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
} else {
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(handle, CURLOPT_CAINFO, sslCaFilename.c_str());
2014-09-13 18:54:29 +08:00
}
// FIXED #3224: The subthread of CCHttpClient interrupts main thread if timeout comes.
// Document is here: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTNOSIGNAL
curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
2015-06-05 11:45:05 +08:00
curl_easy_setopt(handle, CURLOPT_ACCEPT_ENCODING, "");
2015-06-02 10:23:30 +08:00
return true;
}
class CURLRaii
{
/// Instance of CURL
CURL *_curl;
/// Keeps custom header data
curl_slist *_headers;
public:
CURLRaii()
: _curl(curl_easy_init())
, _headers(nullptr)
{
}
~CURLRaii()
{
if (_curl)
curl_easy_cleanup(_curl);
/* free the linked list for header data */
if (_headers)
curl_slist_free_all(_headers);
}
template <class T>
bool setOption(CURLoption option, T data)
{
return CURLE_OK == curl_easy_setopt(_curl, option, data);
}
/**
* @brief Inits CURL instance for common usage
* @param request Null not allowed
* @param callback Response write callback
* @param stream Response write stream
*/
bool init(HttpClient* client, HttpRequest* request, write_callback callback, void* stream, write_callback headerCallback, void* headerStream, char* errorBuffer)
{
if (!_curl)
return false;
if (!configureCURL(client, _curl, errorBuffer))
return false;
/* get custom header data (if set) */
2014-05-21 10:44:07 +08:00
std::vector<std::string> headers=request->getHeaders();
if(!headers.empty())
{
/* append custom headers one by one */
for (std::vector<std::string>::iterator it = headers.begin(); it != headers.end(); ++it)
_headers = curl_slist_append(_headers,it->c_str());
/* set custom headers for curl */
if (!setOption(CURLOPT_HTTPHEADER, _headers))
return false;
}
std::string cookieFilename = client->getCookieFilename();
if (!cookieFilename.empty()) {
if (!setOption(CURLOPT_COOKIEFILE, cookieFilename.c_str())) {
return false;
}
if (!setOption(CURLOPT_COOKIEJAR, cookieFilename.c_str())) {
return false;
}
}
return setOption(CURLOPT_URL, request->getUrl())
&& setOption(CURLOPT_WRITEFUNCTION, callback)
&& setOption(CURLOPT_WRITEDATA, stream)
&& setOption(CURLOPT_HEADERFUNCTION, headerCallback)
&& setOption(CURLOPT_HEADERDATA, headerStream);
}
/// @param responseCode Null not allowed
bool perform(long *responseCode)
{
if (CURLE_OK != curl_easy_perform(_curl))
return false;
CURLcode code = curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, responseCode);
if (code != CURLE_OK || !(*responseCode >= 200 && *responseCode < 300)) {
CCLOGERROR("Curl curl_easy_getinfo failed: %s", curl_easy_strerror(code));
return false;
}
// Get some mor data.
return true;
}
};
//Process Get Request
static int processGetTask(HttpClient* client, HttpRequest* request, write_callback callback, void* stream, long* responseCode, write_callback headerCallback, void* headerStream, char* errorBuffer)
{
CURLRaii curl;
bool ok = curl.init(client, request, callback, stream, headerCallback, headerStream, errorBuffer)
&& curl.setOption(CURLOPT_FOLLOWLOCATION, true)
&& curl.perform(responseCode);
return ok ? 0 : 1;
}
//Process POST Request
static int processPostTask(HttpClient* client, HttpRequest* request, write_callback callback, void* stream, long* responseCode, write_callback headerCallback, void* headerStream, char* errorBuffer)
{
CURLRaii curl;
bool ok = curl.init(client, request, callback, stream, headerCallback, headerStream, errorBuffer)
&& curl.setOption(CURLOPT_POST, 1)
&& curl.setOption(CURLOPT_POSTFIELDS, request->getRequestData())
&& curl.setOption(CURLOPT_POSTFIELDSIZE, request->getRequestDataSize())
&& curl.perform(responseCode);
return ok ? 0 : 1;
}
//Process PUT Request
static int processPutTask(HttpClient* client, HttpRequest* request, write_callback callback, void* stream, long* responseCode, write_callback headerCallback, void* headerStream, char* errorBuffer)
{
CURLRaii curl;
bool ok = curl.init(client, request, callback, stream, headerCallback, headerStream, errorBuffer)
&& curl.setOption(CURLOPT_CUSTOMREQUEST, "PUT")
&& curl.setOption(CURLOPT_POSTFIELDS, request->getRequestData())
&& curl.setOption(CURLOPT_POSTFIELDSIZE, request->getRequestDataSize())
&& curl.perform(responseCode);
return ok ? 0 : 1;
}
//Process DELETE Request
static int processDeleteTask(HttpClient* client, HttpRequest* request, write_callback callback, void* stream, long* responseCode, write_callback headerCallback, void* headerStream, char* errorBuffer)
{
CURLRaii curl;
bool ok = curl.init(client, request, callback, stream, headerCallback, headerStream, errorBuffer)
&& curl.setOption(CURLOPT_CUSTOMREQUEST, "DELETE")
&& curl.setOption(CURLOPT_FOLLOWLOCATION, true)
&& curl.perform(responseCode);
return ok ? 0 : 1;
}
// HttpClient implementation
HttpClient* HttpClient::getInstance()
{
if (_httpClient == nullptr)
{
_httpClient = new (std::nothrow) HttpClient();
}
return _httpClient;
}
void HttpClient::destroyInstance()
{
if (nullptr == _httpClient)
{
CCLOG("HttpClient singleton is nullptr");
return;
}
CCLOG("HttpClient::destroyInstance begin");
auto thiz = _httpClient;
_httpClient = nullptr;
thiz->_scheduler->unscheduleAllForTarget(thiz);
thiz->_schedulerMutex.lock();
thiz->_scheduler = nullptr;
thiz->_schedulerMutex.unlock();
thiz->_requestQueueMutex.lock();
thiz->_requestQueue.pushBack(thiz->_requestSentinel);
thiz->_requestQueueMutex.unlock();
thiz->_sleepCondition.notify_one();
thiz->decreaseThreadCountAndMayDeleteThis();
CCLOG("HttpClient::destroyInstance() finished!");
}
void HttpClient::enableCookies(const char* cookieFile)
{
std::lock_guard<std::mutex> lock(_cookieFileMutex);
if (cookieFile)
{
_cookieFilename = std::string(cookieFile);
}
else
{
_cookieFilename = (FileUtils::getInstance()->getWritablePath() + "cookieFile.txt");
}
}
2014-09-13 18:54:29 +08:00
2014-09-13 23:28:11 +08:00
void HttpClient::setSSLVerification(const std::string& caFile)
2014-09-13 18:54:29 +08:00
{
std::lock_guard<std::mutex> lock(_sslCaFileMutex);
_sslCaFilename = caFile;
2014-09-13 18:54:29 +08:00
}
HttpClient::HttpClient()
: _timeoutForConnect(30)
, _timeoutForRead(60)
, _isInited(false)
, _threadCount(0)
, _requestSentinel(new HttpRequest())
, _cookie(nullptr)
{
CCLOG("In the constructor of HttpClient!");
memset(_responseMessage, 0, RESPONSE_BUFFER_SIZE * sizeof(char));
_scheduler = Director::getInstance()->getScheduler();
increaseThreadCount();
}
HttpClient::~HttpClient()
{
CC_SAFE_RELEASE(_requestSentinel);
CCLOG("HttpClient destructor");
}
//Lazy create semaphore & mutex & thread
bool HttpClient::lazyInitThreadSemphore()
{
if (_isInited)
{
return true;
}
else
{
auto t = std::thread(CC_CALLBACK_0(HttpClient::networkThread, this));
t.detach();
_isInited = true;
}
return true;
}
//Add a get task to queue
void HttpClient::send(HttpRequest* request)
{
if (false == lazyInitThreadSemphore())
{
return;
}
if (!request)
{
return;
}
request->retain();
_requestQueueMutex.lock();
_requestQueue.pushBack(request);
_requestQueueMutex.unlock();
// Notify thread start to work
_sleepCondition.notify_one();
}
2014-06-22 11:39:39 +08:00
void HttpClient::sendImmediate(HttpRequest* request)
2014-05-20 20:08:26 +08:00
{
2014-05-21 10:44:07 +08:00
if(!request)
{
return;
}
request->retain();
// Create a HttpResponse object, the default setting is http access failed
HttpResponse *response = new (std::nothrow) HttpResponse(request);
auto t = std::thread(&HttpClient::networkThreadAlone, this, request, response);
2014-05-21 10:44:07 +08:00
t.detach();
2014-05-20 20:08:26 +08:00
}
// Poll and notify main thread if responses exists in queue
void HttpClient::dispatchResponseCallbacks()
{
// log("CCHttpClient::dispatchResponseCallbacks is running");
//occurs when cocos thread fires but the network thread has already quited
HttpResponse* response = nullptr;
_responseQueueMutex.lock();
if (!_responseQueue.empty())
{
response = _responseQueue.at(0);
_responseQueue.erase(0);
}
_responseQueueMutex.unlock();
if (response)
{
HttpRequest *request = response->getHttpRequest();
const ccHttpRequestCallback& callback = request->getCallback();
Ref* pTarget = request->getTarget();
SEL_HttpResponse pSelector = request->getSelector();
if (callback != nullptr)
{
callback(this, response);
}
else if (pTarget && pSelector)
{
(pTarget->*pSelector)(this, response);
}
response->release();
2014-06-22 12:37:23 +08:00
// do not release in other thread
request->release();
}
}
// Process Response
void HttpClient::processResponse(HttpResponse* response, char* responseMessage)
{
auto request = response->getHttpRequest();
long responseCode = -1;
int retValue = 0;
// Process the request -> get response packet
switch (request->getRequestType())
{
case HttpRequest::Type::GET: // HTTP GET
retValue = processGetTask(this, request,
writeData,
response->getResponseData(),
&responseCode,
writeHeaderData,
response->getResponseHeader(),
responseMessage);
break;
case HttpRequest::Type::POST: // HTTP POST
retValue = processPostTask(this, request,
writeData,
response->getResponseData(),
&responseCode,
writeHeaderData,
response->getResponseHeader(),
responseMessage);
break;
case HttpRequest::Type::PUT:
retValue = processPutTask(this, request,
writeData,
response->getResponseData(),
&responseCode,
writeHeaderData,
response->getResponseHeader(),
responseMessage);
break;
case HttpRequest::Type::DELETE:
retValue = processDeleteTask(this, request,
writeData,
response->getResponseData(),
&responseCode,
writeHeaderData,
response->getResponseHeader(),
responseMessage);
break;
default:
CCASSERT(true, "CCHttpClient: unknown request type, only GET and POSt are supported");
break;
}
// write data to HttpResponse
response->setResponseCode(responseCode);
if (retValue != 0)
{
response->setSucceed(false);
response->setErrorBuffer(responseMessage);
}
else
{
response->setSucceed(true);
}
}
void HttpClient::increaseThreadCount()
{
_threadCountMutex.lock();
++_threadCount;
_threadCountMutex.unlock();
}
void HttpClient::decreaseThreadCountAndMayDeleteThis()
{
bool needDeleteThis = false;
_threadCountMutex.lock();
--_threadCount;
if (0 == _threadCount)
{
needDeleteThis = true;
}
_threadCountMutex.unlock();
if (needDeleteThis)
{
delete this;
}
}
void HttpClient::setTimeoutForConnect(int value)
{
std::lock_guard<std::mutex> lock(_timeoutForConnectMutex);
_timeoutForConnect = value;
}
int HttpClient::getTimeoutForConnect()
{
std::lock_guard<std::mutex> lock(_timeoutForConnectMutex);
return _timeoutForConnect;
}
void HttpClient::setTimeoutForRead(int value)
{
std::lock_guard<std::mutex> lock(_timeoutForReadMutex);
_timeoutForRead = value;
}
int HttpClient::getTimeoutForRead()
{
std::lock_guard<std::mutex> lock(_timeoutForReadMutex);
return _timeoutForRead;
}
const std::string& HttpClient::getCookieFilename()
{
std::lock_guard<std::mutex> lock(_cookieFileMutex);
return _cookieFilename;
}
const std::string& HttpClient::getSSLVerification()
{
std::lock_guard<std::mutex> lock(_sslCaFileMutex);
return _sslCaFilename;
}
}
2014-01-02 16:25:35 +08:00
NS_CC_END