2012-08-07 18:28:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-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
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __HTTP_REQUEST_H__
|
|
|
|
#define __HTTP_REQUEST_H__
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-07-06 02:57:13 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <future>
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2021-12-10 19:15:48 +08:00
|
|
|
#include "yasio/detail/byte_buffer.hpp"
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
/**
|
2015-05-28 15:46:36 +08:00
|
|
|
* @addtogroup network
|
2015-03-26 22:07:44 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2014-01-02 16:25:35 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace network
|
|
|
|
{
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
class HttpClient;
|
|
|
|
class HttpResponse;
|
2014-02-20 10:53:49 +08:00
|
|
|
|
2014-04-13 05:06:04 +08:00
|
|
|
typedef std::function<void(HttpClient* client, HttpResponse* response)> ccHttpRequestCallback;
|
2013-04-26 11:42:50 +08:00
|
|
|
|
2016-05-18 12:54:16 +08:00
|
|
|
/**
|
2015-03-19 16:59:21 +08:00
|
|
|
* Defines the object which users must packed for HttpClient::send(HttpRequest*) method.
|
|
|
|
* Please refer to tests/test-cpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample
|
|
|
|
* @since v2.0.2
|
|
|
|
*
|
|
|
|
* @lua NA
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2013-04-26 11:42:50 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL HttpRequest : public Ref
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2021-07-06 02:57:13 +08:00
|
|
|
friend class HttpClient;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
public:
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
|
|
|
* The HttpRequest type enum used in the HttpRequest::setRequestType.
|
|
|
|
*/
|
2013-07-27 00:55:41 +08:00
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
GET,
|
|
|
|
POST,
|
|
|
|
PUT,
|
|
|
|
DELETE,
|
|
|
|
UNKNOWN,
|
|
|
|
};
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:59:21 +08:00
|
|
|
* Constructor.
|
2015-10-23 15:37:33 +08:00
|
|
|
* Because HttpRequest object will be used between UI thread and network thread,
|
2015-03-19 16:59:21 +08:00
|
|
|
requestObj->autorelease() is forbidden to avoid crashes in AutoreleasePool
|
|
|
|
new/retain/release still works, which means you need to release it manually
|
|
|
|
Please refer to HttpRequestTest.cpp to find its usage.
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
HttpRequest() : _requestType(Type::UNKNOWN), _pCallback(nullptr), _pUserData(nullptr) {}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-03-19 16:45:53 +08:00
|
|
|
/** Destructor. */
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual ~HttpRequest() {}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Override autorelease method to avoid developers to call it.
|
|
|
|
* If this function was called, it would trigger assert in debug mode
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
|
|
|
* @return Ref* always return nullptr.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2016-05-18 12:54:16 +08:00
|
|
|
Ref* autorelease()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false,
|
2021-12-25 10:04:45 +08:00
|
|
|
"HttpResponse is used between network thread and ui thread \
|
2012-08-07 18:28:07 +08:00
|
|
|
therefore, autorelease is forbidden here");
|
2016-05-18 12:54:16 +08:00
|
|
|
return nullptr;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
// setter/getters for properties
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Set request type of HttpRequest object before being sent,now it support the enum value of HttpRequest::Type.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
|
|
|
* @param type the request type.
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setRequestType(Type type) { _requestType = type; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Get the request type of HttpRequest object.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
|
|
|
* @return HttpRequest::Type.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
Type getRequestType() const { return _requestType; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Set the url address of HttpRequest object.
|
|
|
|
* The url value could be like these: "http://httpbin.org/ip" or "https://httpbin.org/get"
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2016-01-26 04:10:47 +08:00
|
|
|
* @param url the string object.
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setUrl(std::string_view url) { _url = url; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Get the url address of HttpRequest object.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
|
|
|
* @return const char* the pointer of _url.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view getUrl() const { return _url; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Set the request data of HttpRequest object.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
|
|
|
* @param buffer the buffer of request data, it support binary data.
|
|
|
|
* @param len the size of request data.
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setRequestData(const char* buffer, size_t len) { _requestData.assign(buffer, buffer + len); }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:59:21 +08:00
|
|
|
* Get the request data pointer of HttpRequest object.
|
|
|
|
*
|
|
|
|
* @return char* the request data pointer.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2016-09-12 09:44:21 +08:00
|
|
|
char* getRequestData()
|
2012-08-07 18:28:07 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_requestData.empty())
|
2016-05-18 12:54:16 +08:00
|
|
|
return _requestData.data();
|
2013-10-29 17:54:35 +08:00
|
|
|
|
|
|
|
return nullptr;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Get the size of request data
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return ssize_t the size of request data
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t getRequestDataSize() const { return _requestData.size(); }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Set a string tag to identify your request.
|
2015-03-19 16:59:21 +08:00
|
|
|
* This tag can be found in HttpResponse->getHttpRequest->getTag().
|
|
|
|
*
|
2016-01-26 04:10:47 +08:00
|
|
|
* @param tag the string object.
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
void setTag(std::string_view tag) { _tag = tag; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Get the string tag to identify the request.
|
2015-03-19 16:59:21 +08:00
|
|
|
* The best practice is to use it in your MyClass::onMyHttpRequestCompleted(sender, HttpResponse*) callback.
|
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return const char* the pointer of _tag
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* getTag() const { return _tag.c_str(); }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
|
|
|
* Set user-customed data of HttpRequest object.
|
|
|
|
* You can attach a customed data in each request, and get it back in response callback.
|
2015-10-23 15:37:33 +08:00
|
|
|
* But you need to new/delete the data pointer manually.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @param pUserData the string pointer
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setUserData(void* pUserData) { _pUserData = pUserData; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:45:53 +08:00
|
|
|
* Get the user-customed data pointer which were pre-setted.
|
|
|
|
* Don't forget to delete it. HttpClient/HttpResponse/HttpRequest will do nothing with this pointer.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
2015-03-19 16:45:53 +08:00
|
|
|
* @return void* the pointer of user-customed data.
|
2012-08-08 12:05:02 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void* getUserData() const { return _pUserData; }
|
|
|
|
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
|
|
|
* Set response callback function of HttpRequest object.
|
|
|
|
* When response come back, we would call _pCallback to process response data.
|
2015-03-19 16:59:21 +08:00
|
|
|
*
|
|
|
|
* @param callback the ccHttpRequestCallback function.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setResponseCallback(const ccHttpRequestCallback& callback) { _pCallback = callback; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2015-03-19 16:45:53 +08:00
|
|
|
/**
|
2015-03-19 16:59:21 +08:00
|
|
|
* Get ccHttpRequestCallback callback function.
|
|
|
|
*
|
|
|
|
* @return const ccHttpRequestCallback& ccHttpRequestCallback callback function.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const ccHttpRequestCallback& getCallback() const { return _pCallback; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:59:21 +08:00
|
|
|
* Set custom-defined headers.
|
|
|
|
*
|
2016-07-03 23:42:10 +08:00
|
|
|
* @param headers The string vector of custom-defined headers.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setHeaders(const std::vector<std::string>& headers) { _headers = headers; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 16:59:21 +08:00
|
|
|
* Get custom headers.
|
|
|
|
*
|
|
|
|
* @return std::vector<std::string> the string vector of custom-defined headers.
|
2015-03-19 16:45:53 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
const std::vector<std::string>& getHeaders() const { return _headers; }
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2020-10-04 23:44:52 +08:00
|
|
|
void setHosts(std::vector<std::string> hosts) { _hosts = std::move(hosts); }
|
|
|
|
const std::vector<std::string>& getHosts() const { return _hosts; }
|
|
|
|
|
2015-11-18 17:52:24 +08:00
|
|
|
private:
|
2021-12-25 10:04:45 +08:00
|
|
|
void setSync(bool sync)
|
|
|
|
{
|
2021-07-06 02:57:13 +08:00
|
|
|
if (sync)
|
|
|
|
_syncState = std::make_shared<std::promise<HttpResponse*>>();
|
|
|
|
else
|
|
|
|
_syncState.reset();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::shared_ptr<std::promise<HttpResponse*>> getSyncState() { return _syncState; }
|
2021-07-06 02:57:13 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
HttpResponse* wait()
|
|
|
|
{
|
2021-07-06 02:57:13 +08:00
|
|
|
if (_syncState)
|
|
|
|
return _syncState->get_future().get();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
protected:
|
|
|
|
// properties
|
2021-12-25 10:04:45 +08:00
|
|
|
Type _requestType; /// kHttpRequestGet, kHttpRequestPost or other enums
|
|
|
|
std::string _url; /// target url that this request is sent to
|
|
|
|
yasio::sbyte_buffer _requestData; /// used for POST
|
|
|
|
std::string _tag; /// user defined tag, to identify different requests in response callback
|
|
|
|
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;
|
2021-07-06 02:57:13 +08:00
|
|
|
|
|
|
|
std::shared_ptr<std::promise<HttpResponse*>> _syncState;
|
2012-08-07 18:28:07 +08:00
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace network
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2014-01-02 16:25:35 +08:00
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif //__HTTP_REQUEST_H__
|