2012-08-07 18:28:07 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2012 greathqy
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-06-24 12:33:07 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2021-07-05 14:24:04 +08:00
|
|
|
https://adxe.org
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
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:
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-27 18:26:14 +08:00
|
|
|
#ifndef __CCHTTPCLIENT_H__
|
|
|
|
#define __CCHTTPCLIENT_H__
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
#include <thread>
|
|
|
|
#include <condition_variable>
|
2020-10-05 08:32:48 +08:00
|
|
|
#include <deque>
|
2015-05-22 10:47:52 +08:00
|
|
|
#include "base/CCScheduler.h"
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "network/HttpRequest.h"
|
|
|
|
#include "network/HttpResponse.h"
|
2015-05-22 10:47:52 +08:00
|
|
|
#include "network/HttpCookie.h"
|
2021-06-24 12:33:07 +08:00
|
|
|
#include "network/Uri.h"
|
|
|
|
#include "yasio/yasio_fwd.hpp"
|
|
|
|
#include "base/ConcurrentDeque.h"
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
/**
|
2015-05-28 15:46:36 +08:00
|
|
|
* @addtogroup network
|
2012-08-07 18:28:07 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace network {
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2016-01-11 18:15:34 +08:00
|
|
|
/** Singleton that handles asynchronous http requests.
|
2015-03-19 16:45:53 +08:00
|
|
|
*
|
|
|
|
* Once the request completed, a callback will issued in main thread when it provided during make request.
|
|
|
|
*
|
|
|
|
* @lua NA
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2014-08-22 13:42:46 +08:00
|
|
|
class CC_DLL HttpClient
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
|
|
|
public:
|
2016-05-18 12:54:16 +08:00
|
|
|
/**
|
2021-06-24 12:33:07 +08:00
|
|
|
* How many requests could be perform concurrency.
|
2016-05-18 12:54:16 +08:00
|
|
|
*/
|
2021-06-24 12:33:07 +08:00
|
|
|
static const int MAX_CHANNELS = 21;
|
|
|
|
static const int MAX_REDIRECT_COUNT = 3;
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
|
|
|
* Get instance of HttpClient.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return the instance of HttpClient.
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
static HttpClient *getInstance();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the instance of HttpClient.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2012-08-07 18:28:07 +08:00
|
|
|
static void destroyInstance();
|
2013-07-10 11:37:39 +08:00
|
|
|
|
2016-05-18 12:54:16 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Enable cookie support.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2021-07-19 12:26:25 +08:00
|
|
|
* @param [nullable] cookieFile the filepath of cookie file.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2013-07-10 15:46:01 +08:00
|
|
|
void enableCookies(const char* cookieFile);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
/**
|
|
|
|
* Get the cookie filename
|
2016-05-18 12:54:16 +08:00
|
|
|
*
|
2015-05-22 10:47:52 +08:00
|
|
|
* @return the cookie filename
|
|
|
|
*/
|
|
|
|
const std::string& getCookieFilename();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-09-13 23:28:11 +08:00
|
|
|
/**
|
|
|
|
* Set root certificate path for SSL verification.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @param caFile a full path of root certificate.if it is empty, SSL verification is disabled.
|
2014-09-13 23:28:11 +08:00
|
|
|
*/
|
|
|
|
void setSSLVerification(const std::string& caFile);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
/**
|
2016-01-11 18:15:34 +08:00
|
|
|
* Get the ssl CA filename
|
2016-05-18 12:54:16 +08:00
|
|
|
*
|
2015-05-22 10:47:52 +08:00
|
|
|
* @return the ssl CA filename
|
|
|
|
*/
|
|
|
|
const std::string& getSSLVerification();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
/**
|
2021-07-06 02:44:57 +08:00
|
|
|
* Send http request concurrently, non-blocking
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2013-06-20 14:15:53 +08:00
|
|
|
* @param request a HttpRequest object, which includes url, response callback etc.
|
2012-09-10 23:17:16 +08:00
|
|
|
please make sure request->_requestData is clear before calling "send" here.
|
2021-07-05 14:24:04 +08:00
|
|
|
* @notes for post data
|
|
|
|
* a. By default will fill Content-Type: application/x-www-form-urlencoded;charset=UTF-8
|
|
|
|
* b. You can specific content-type at custom header, such as:
|
|
|
|
* std::vector<std::string> headers = {"Content-Type: application/json;charset=UTF-8"};
|
|
|
|
* request->setHeaders(headers);
|
|
|
|
* c. other content type, please see:
|
|
|
|
* https://stackoverflow.com/questions/23714383/what-are-all-the-possible-values-for-http-content-type-header
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2021-06-24 12:33:07 +08:00
|
|
|
bool send(HttpRequest* request);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2021-07-06 02:57:13 +08:00
|
|
|
/**
|
|
|
|
* Send http request sync, will block caller thread until request finished.
|
|
|
|
* @remark You need call release manually after you don't want use the returned response
|
|
|
|
*/
|
|
|
|
HttpResponse* sendSync(HttpRequest* request);
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Set the timeout value for connecting.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @param value the timeout value for connecting.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2015-05-22 10:47:52 +08:00
|
|
|
void setTimeoutForConnect(int value);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Get the timeout value for connecting.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return int the timeout value for connecting.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2015-05-22 10:47:52 +08:00
|
|
|
int getTimeoutForConnect();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Set the timeout value for reading.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @param value the timeout value for reading.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2015-05-22 10:47:52 +08:00
|
|
|
void setTimeoutForRead(int value);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Get the timeout value for reading.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return int the timeout value for reading.
|
2012-08-07 18:28:07 +08:00
|
|
|
*/
|
2015-05-22 10:47:52 +08:00
|
|
|
int getTimeoutForRead();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
HttpCookie* getCookie() const {return _cookie; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex& getCookieFileMutex() {return _cookieFileMutex;}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex& getSSLCaFileMutex() {return _sslCaFileMutex;}
|
2019-04-11 09:33:55 +08:00
|
|
|
|
|
|
|
typedef std::function<bool(HttpResponse*)> ClearResponsePredicate;
|
|
|
|
|
|
|
|
/**
|
2021-06-24 12:33:07 +08:00
|
|
|
* Clears the pending http response
|
2019-04-11 09:33:55 +08:00
|
|
|
*/
|
2021-06-24 12:33:07 +08:00
|
|
|
void clearResponseQueue();
|
2019-04-11 09:33:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Sets a predicate function that is going to be called to determine if we proceed
|
|
|
|
* each of the pending requests
|
|
|
|
*
|
|
|
|
* @param cb predicate function that will be called
|
|
|
|
*/
|
|
|
|
void setClearResponsePredicate(ClearResponsePredicate predicate) { _clearResponsePredicate = predicate; }
|
|
|
|
|
2020-10-08 00:00:14 +08:00
|
|
|
void setDispatchOnWorkThread(bool bVal) { _dispatchOnWorkThread = bVal; }
|
|
|
|
bool isDispatchOnWorkThread() const { return _dispatchOnWorkThread; }
|
2019-04-11 09:33:55 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
private:
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpClient();
|
|
|
|
virtual ~HttpClient();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2021-06-24 12:33:07 +08:00
|
|
|
void processResponse(HttpResponse* response, const std::string& url);
|
|
|
|
|
|
|
|
int tryTakeAvailChannel();
|
|
|
|
|
|
|
|
void handleNetworkEvent(yasio::io_event* event);
|
|
|
|
|
2021-06-25 18:29:16 +08:00
|
|
|
void handleNetworkEOF(HttpResponse* response, yasio::io_channel* channel, int internalErrorCode);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2021-06-24 12:33:07 +08:00
|
|
|
void finishResponse(HttpResponse* response);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
private:
|
2015-05-22 10:47:52 +08:00
|
|
|
bool _isInited;
|
2021-06-24 12:33:07 +08:00
|
|
|
|
|
|
|
yasio::io_service* _service;
|
2020-10-08 00:00:14 +08:00
|
|
|
|
|
|
|
bool _dispatchOnWorkThread;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
int _timeoutForConnect;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _timeoutForConnectMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
int _timeoutForRead;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _timeoutForReadMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
Scheduler* _scheduler;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _schedulerMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2021-06-24 12:33:07 +08:00
|
|
|
ConcurrentDeque<HttpResponse*> _responseQueue;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2021-06-24 12:33:07 +08:00
|
|
|
ConcurrentDeque<int> _availChannelQueue;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
std::string _cookieFilename;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _cookieFileMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
std::string _sslCaFilename;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _sslCaFileMutex;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
HttpCookie* _cookie;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2019-04-11 09:33:55 +08:00
|
|
|
ClearResponsePredicate _clearResponsePredicate;
|
2012-08-07 18:28:07 +08:00
|
|
|
};
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
} // namespace network
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2014-01-02 16:25:35 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
#endif //__CCHTTPCLIENT_H__
|
2016-05-18 12:54:16 +08:00
|
|
|
|