2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 greathqy
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-06-24 17:04:04 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-05 14:24:04 +08:00
|
|
|
https://adxe.org
|
2019-11-23 20:27:39 +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:
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCHTTPCLIENT_H__
|
|
|
|
#define __CCHTTPCLIENT_H__
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <condition_variable>
|
2020-10-05 08:32:48 +08:00
|
|
|
#include <deque>
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "base/CCScheduler.h"
|
|
|
|
#include "network/HttpRequest.h"
|
|
|
|
#include "network/HttpResponse.h"
|
|
|
|
#include "network/HttpCookie.h"
|
2021-06-24 17:04:04 +08:00
|
|
|
#include "network/Uri.h"
|
|
|
|
#include "yasio/yasio_fwd.hpp"
|
|
|
|
#include "base/ConcurrentDeque.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup network
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace network
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Singleton that handles asynchronous http requests.
|
|
|
|
*
|
|
|
|
* Once the request completed, a callback will issued in main thread when it provided during make request.
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
class CC_DLL HttpClient
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* How many requests could be perform concurrency.
|
|
|
|
*/
|
|
|
|
static const int MAX_CHANNELS = 21;
|
2021-06-24 17:04:04 +08:00
|
|
|
static const int MAX_REDIRECT_COUNT = 3;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get instance of HttpClient.
|
|
|
|
*
|
|
|
|
* @return the instance of HttpClient.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
static HttpClient* getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the instance of HttpClient.
|
|
|
|
*/
|
|
|
|
static void destroyInstance();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable cookie support.
|
|
|
|
*
|
2021-07-19 12:26:25 +08:00
|
|
|
* @param [nullable] cookieFile the filepath of cookie file.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
|
|
|
void enableCookies(const char* cookieFile);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cookie filename
|
|
|
|
*
|
|
|
|
* @return the cookie filename
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view getCookieFilename();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set root certificate path for SSL verification.
|
|
|
|
*
|
|
|
|
* @param caFile a full path of root certificate.if it is empty, SSL verification is disabled.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setSSLVerification(std::string_view caFile);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ssl CA filename
|
|
|
|
*
|
|
|
|
* @return the ssl CA filename
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view getSSLVerification();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
2021-07-06 02:44:57 +08:00
|
|
|
* Send http request concurrently, non-blocking
|
2019-11-23 20:27:39 +08:00
|
|
|
*
|
|
|
|
* @param request a HttpRequest object, which includes url, response callback etc.
|
|
|
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
* c. other content type, please see:
|
2021-07-05 14:24:04 +08:00
|
|
|
* https://stackoverflow.com/questions/23714383/what-are-all-the-possible-values-for-http-content-type-header
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-06-24 17:04:04 +08:00
|
|
|
bool send(HttpRequest* request);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-06 02:57:13 +08:00
|
|
|
/**
|
|
|
|
* Send http request sync, will block caller thread until request finished.
|
2021-07-19 14:17:51 +08:00
|
|
|
* @remark Caller must call release manually when the response never been used.
|
2021-07-06 02:57:13 +08:00
|
|
|
*/
|
|
|
|
HttpResponse* sendSync(HttpRequest* request);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Set the timeout value for connecting.
|
|
|
|
*
|
|
|
|
* @param value the timeout value for connecting.
|
|
|
|
*/
|
|
|
|
void setTimeoutForConnect(int value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the timeout value for connecting.
|
|
|
|
*
|
|
|
|
* @return int the timeout value for connecting.
|
|
|
|
*/
|
|
|
|
int getTimeoutForConnect();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the timeout value for reading.
|
|
|
|
*
|
|
|
|
* @param value the timeout value for reading.
|
|
|
|
*/
|
|
|
|
void setTimeoutForRead(int value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the timeout value for reading.
|
|
|
|
*
|
|
|
|
* @return int the timeout value for reading.
|
|
|
|
*/
|
|
|
|
int getTimeoutForRead();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
HttpCookie* getCookie() const { return _cookie; }
|
|
|
|
|
|
|
|
std::recursive_mutex& getCookieFileMutex() { return _cookieFileMutex; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::recursive_mutex& getSSLCaFileMutex() { return _sslCaFileMutex; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
typedef std::function<bool(HttpResponse*)> ClearResponsePredicate;
|
|
|
|
|
|
|
|
/**
|
2021-07-19 13:43:59 +08:00
|
|
|
* Clears the pending & finished http response
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void clearResponseQueue();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-19 13:43:59 +08:00
|
|
|
/**
|
|
|
|
* Clears the pending http response
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void clearPendingResponseQueue();
|
2021-07-19 13:43:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the finished http response
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void clearFinishedResponseQueue();
|
2021-07-19 13:43:59 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
Sets a predicate function that is going to be called to determine if we proceed
|
|
|
|
* each of the pending requests
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param cb predicate function that will be called
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
|
|
|
void setClearResponsePredicate(ClearResponsePredicate predicate) { _clearResponsePredicate = predicate; }
|
|
|
|
|
2021-07-19 13:43:59 +08:00
|
|
|
void setDispatchOnWorkThread(bool bVal);
|
2020-10-08 00:00:14 +08:00
|
|
|
bool isDispatchOnWorkThread() const { return _dispatchOnWorkThread; }
|
2021-07-31 14:39:42 +08:00
|
|
|
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* When the device network status chagned, you should invoke this function
|
|
|
|
*/
|
2021-07-31 14:39:42 +08:00
|
|
|
void handleNetworkStatusChanged();
|
2021-09-05 22:18:42 +08:00
|
|
|
|
2021-10-23 21:57:36 +08:00
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* Sets custom dns server list:
|
|
|
|
* format: "xxx.xxx.xxx.xxx[:port],xxx.xxx.xxx.xxx[:port]
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setNameServers(std::string_view servers);
|
2021-10-23 21:57:36 +08:00
|
|
|
|
2021-09-05 22:18:42 +08:00
|
|
|
yasio::io_service* getInternalService();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
|
|
|
HttpClient();
|
|
|
|
virtual ~HttpClient();
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void processResponse(HttpResponse* response, std::string_view url);
|
2021-06-24 17:04:04 +08:00
|
|
|
|
|
|
|
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);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-19 13:43:59 +08:00
|
|
|
void dispatchResponseCallbacks();
|
|
|
|
|
2021-06-24 17:04:04 +08:00
|
|
|
void finishResponse(HttpResponse* response);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-19 13:43:59 +08:00
|
|
|
void invokeResposneCallbackAndRelease(HttpResponse* response);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
|
|
|
bool _isInited;
|
2021-06-24 17:04:04 +08:00
|
|
|
|
|
|
|
yasio::io_service* _service;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-08 00:00:14 +08:00
|
|
|
bool _dispatchOnWorkThread;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
int _timeoutForConnect;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _timeoutForConnectMutex;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
int _timeoutForRead;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _timeoutForReadMutex;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Scheduler* _scheduler;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _schedulerMutex;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-19 13:43:59 +08:00
|
|
|
ConcurrentDeque<HttpResponse*> _pendingResponseQueue;
|
|
|
|
ConcurrentDeque<HttpResponse*> _finishedResponseQueue;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-06-24 17:04:04 +08:00
|
|
|
ConcurrentDeque<int> _availChannelQueue;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::string _cookieFilename;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _cookieFileMutex;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::string _sslCaFilename;
|
2020-10-08 00:00:14 +08:00
|
|
|
std::recursive_mutex _sslCaFileMutex;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
HttpCookie* _cookie;
|
|
|
|
|
|
|
|
ClearResponsePredicate _clearResponsePredicate;
|
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace network
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif //__CCHTTPCLIENT_H__
|