2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2023-12-08 00:13:39 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
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 __HTTP_RESPONSE__
|
|
|
|
#define __HTTP_RESPONSE__
|
2021-06-24 17:04:04 +08:00
|
|
|
#include <ctype.h>
|
2021-07-18 23:20:22 +08:00
|
|
|
#include <map>
|
2021-06-24 17:04:04 +08:00
|
|
|
#include <unordered_map>
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "network/HttpRequest.h"
|
2021-06-24 17:04:04 +08:00
|
|
|
#include "network/Uri.h"
|
2021-07-18 23:20:22 +08:00
|
|
|
#include "llhttp.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup network
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace network
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-06-24 17:04:04 +08:00
|
|
|
class HttpClient;
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @brief defines the object which users will receive at onHttpCompleted(sender, HttpResponse) callback.
|
|
|
|
* Please refer to samples/TestCpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample.
|
|
|
|
* @since v2.0.2.
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2022-08-08 18:02:17 +08:00
|
|
|
class AX_DLL HttpResponse : public ax::Ref
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
friend class HttpClient;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
public:
|
2021-07-18 23:20:22 +08:00
|
|
|
using ResponseHeaderMap = std::multimap<std::string, std::string>;
|
2021-06-24 17:04:04 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* Constructor, it's used by HttpClient internal, users don't need to create HttpResponse manually.
|
|
|
|
* @param request the corresponding HttpRequest which leads to this response.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
HttpResponse(HttpRequest* request) : _pHttpRequest(request)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_pHttpRequest)
|
|
|
|
{
|
|
|
|
_pHttpRequest->retain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor, it will be called in HttpClient internal.
|
|
|
|
* Users don't need to destruct HttpResponse object manually.
|
|
|
|
*/
|
|
|
|
virtual ~HttpResponse()
|
|
|
|
{
|
|
|
|
if (_pHttpRequest)
|
|
|
|
{
|
|
|
|
_pHttpRequest->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override autorelease method to prevent developers from calling it.
|
2022-07-16 10:43:05 +08:00
|
|
|
* If this method is called , it would trigger AXASSERT.
|
2022-08-08 18:02:17 +08:00
|
|
|
* @return ax::Ref* always return nullptr.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::Ref* autorelease()
|
2019-11-23 20:27:39 +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 \
|
2019-11-23 20:27:39 +08:00
|
|
|
therefore, autorelease is forbidden here");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// getters, will be called by users
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the corresponding HttpRequest object which leads to this response.
|
|
|
|
* There's no paired setter for it, because it's already set in class constructor
|
|
|
|
* @return HttpRequest* the corresponding HttpRequest object which leads to this response.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
HttpRequest* getHttpRequest() const { return _pHttpRequest; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the http response data.
|
2021-12-10 19:15:48 +08:00
|
|
|
* @return yasio::sbyte_buffer* the pointer that point to the _responseData.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
yasio::sbyte_buffer* getResponseData() { return &_responseData; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isSucceed() const { return _responseCode == 200; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the http response code to judge whether response is successful or not.
|
|
|
|
* I know that you want to see the _responseCode is 200.
|
|
|
|
* If _responseCode is not 200, you should check the meaning for _responseCode by the net.
|
2021-09-05 20:45:32 +08:00
|
|
|
* @return int32_t the value of _responseCode
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
int getResponseCode() const { return _responseCode; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-06-25 18:29:16 +08:00
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* The yasio error code, see yasio::errc
|
|
|
|
*/
|
2020-10-05 02:40:38 +08:00
|
|
|
int getInternalCode() const { return _internalCode; }
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int getRedirectCount() const { return _redirectCount; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const ResponseHeaderMap& getResponseHeaders() const { return _responseHeaders; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-06-24 17:04:04 +08:00
|
|
|
private:
|
2023-09-01 16:31:14 +08:00
|
|
|
void setResponseCode(int value) { _responseCode = value; }
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void updateInternalCode(int value)
|
|
|
|
{
|
2021-06-25 18:29:16 +08:00
|
|
|
if (_internalCode == 0)
|
|
|
|
_internalCode = value;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2021-06-24 17:04:04 +08:00
|
|
|
* To see if the http request is finished.
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isFinished() const { return _finished; }
|
2021-06-24 15:54:02 +08:00
|
|
|
|
2023-01-01 23:11:32 +08:00
|
|
|
void handleInput(const char* d, size_t n)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2023-01-01 23:11:32 +08:00
|
|
|
enum llhttp_errno err = llhttp_execute(&_context, d, n);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (err != HPE_OK)
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
_finished = true;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2023-04-03 19:54:58 +08:00
|
|
|
bool tryRedirect()
|
|
|
|
{
|
|
|
|
if ((_redirectCount < HttpRequest::MAX_REDIRECT_COUNT))
|
|
|
|
{
|
|
|
|
auto iter = _responseHeaders.find("location");
|
|
|
|
if (iter != _responseHeaders.end())
|
|
|
|
{
|
|
|
|
auto redirectUrl = iter->second;
|
|
|
|
if (_responseCode == 302)
|
|
|
|
getHttpRequest()->setRequestType(HttpRequest::Type::GET);
|
|
|
|
AXLOG("Process url redirect (%d): %s", _responseCode, redirectUrl.c_str());
|
|
|
|
return setLocation(redirectUrl, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2023-04-03 19:54:58 +08:00
|
|
|
* Set new request location with url
|
|
|
|
* @param url the actually url to request
|
|
|
|
* @param redirect wither redirect
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2023-04-03 19:54:58 +08:00
|
|
|
bool setLocation(std::string_view url, bool redirect)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-04-03 19:54:58 +08:00
|
|
|
if (redirect)
|
|
|
|
{
|
|
|
|
++_redirectCount;
|
|
|
|
_requestUri.invalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_requestUri.isValid())
|
|
|
|
{
|
|
|
|
Uri uri = Uri::parse(url);
|
|
|
|
if (!uri.isValid())
|
|
|
|
return false;
|
|
|
|
_requestUri = std::move(uri);
|
|
|
|
|
|
|
|
/* Resets response status */
|
|
|
|
_responseHeaders.clear();
|
|
|
|
_finished = false;
|
|
|
|
_responseData.clear();
|
|
|
|
_currentHeader.clear();
|
|
|
|
_responseCode = -1;
|
|
|
|
_internalCode = 0;
|
|
|
|
|
|
|
|
/* Initialize user callbacks and settings */
|
|
|
|
llhttp_settings_init(&_contextSettings);
|
|
|
|
|
|
|
|
/* Initialize the parser in HTTP_BOTH mode, meaning that it will select between
|
|
|
|
* HTTP_REQUEST and HTTP_RESPONSE parsing automatically while reading the first
|
|
|
|
* input.
|
|
|
|
*/
|
|
|
|
llhttp_init(&_context, HTTP_RESPONSE, &_contextSettings);
|
|
|
|
|
|
|
|
_context.data = this;
|
|
|
|
|
|
|
|
/* Set user callbacks */
|
|
|
|
_contextSettings.on_header_field = on_header_field;
|
|
|
|
_contextSettings.on_header_field_complete = on_header_field_complete;
|
|
|
|
_contextSettings.on_header_value = on_header_value;
|
|
|
|
_contextSettings.on_header_value_complete = on_header_value_complete;
|
|
|
|
_contextSettings.on_body = on_body;
|
|
|
|
_contextSettings.on_message_complete = on_complete;
|
|
|
|
}
|
2021-06-24 17:04:04 +08:00
|
|
|
|
|
|
|
return true;
|
2021-06-24 12:33:07 +08:00
|
|
|
}
|
|
|
|
|
2023-04-03 19:54:58 +08:00
|
|
|
bool validateUri() const { return _requestUri.isValid(); }
|
2021-06-24 17:04:04 +08:00
|
|
|
|
2023-04-03 19:54:58 +08:00
|
|
|
const Uri& getRequestUri() const { return _requestUri; }
|
2021-06-24 17:04:04 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static int on_header_field(llhttp_t* context, const char* at, size_t length)
|
|
|
|
{
|
|
|
|
auto thiz = (HttpResponse*)context->data;
|
2021-07-18 23:20:22 +08:00
|
|
|
thiz->_currentHeader.insert(thiz->_currentHeader.end(), at, at + length);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
static int on_header_field_complete(llhttp_t* context)
|
|
|
|
{
|
2021-07-18 23:20:22 +08:00
|
|
|
auto thiz = (HttpResponse*)context->data;
|
2021-12-25 10:04:45 +08:00
|
|
|
std::transform(thiz->_currentHeader.begin(), thiz->_currentHeader.end(), thiz->_currentHeader.begin(),
|
|
|
|
::tolower);
|
2021-06-24 17:04:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
static int on_header_value(llhttp_t* context, const char* at, size_t length)
|
|
|
|
{
|
|
|
|
auto thiz = (HttpResponse*)context->data;
|
2021-07-18 23:20:22 +08:00
|
|
|
thiz->_currentHeaderValue.insert(thiz->_currentHeaderValue.end(), at, at + length);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
static int on_header_value_complete(llhttp_t* context)
|
|
|
|
{
|
2021-07-18 23:20:22 +08:00
|
|
|
auto thiz = (HttpResponse*)context->data;
|
|
|
|
thiz->_responseHeaders.emplace(std::move(thiz->_currentHeader), std::move(thiz->_currentHeaderValue));
|
2021-06-24 17:04:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
static int on_body(llhttp_t* context, const char* at, size_t length)
|
|
|
|
{
|
|
|
|
auto thiz = (HttpResponse*)context->data;
|
2021-06-24 17:04:04 +08:00
|
|
|
thiz->_responseData.insert(thiz->_responseData.end(), at, at + length);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
static int on_complete(llhttp_t* context)
|
|
|
|
{
|
|
|
|
auto thiz = (HttpResponse*)context->data;
|
2021-06-24 17:04:04 +08:00
|
|
|
thiz->_responseCode = context->status_code;
|
2021-12-25 10:04:45 +08:00
|
|
|
thiz->_finished = true;
|
2021-06-24 17:04:04 +08:00
|
|
|
return 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// properties
|
2021-12-25 10:04:45 +08:00
|
|
|
HttpRequest* _pHttpRequest; /// the corresponding HttpRequest pointer who leads to this response
|
|
|
|
int _redirectCount = 0;
|
2021-06-24 15:54:02 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Uri _requestUri;
|
|
|
|
bool _finished = false; /// to indicate if the http request is successful simply
|
2021-12-10 19:15:48 +08:00
|
|
|
yasio::sbyte_buffer _responseData; /// the returned raw data. You can also dump it as a string
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string _currentHeader;
|
|
|
|
std::string _currentHeaderValue;
|
|
|
|
ResponseHeaderMap _responseHeaders; /// the returned raw header data. You can also dump it as a string
|
2023-01-12 09:54:47 +08:00
|
|
|
int _responseCode = -1; /// the status code returned from server, e.g. 200, 404
|
2021-12-25 10:04:45 +08:00
|
|
|
int _internalCode = 0; /// the ret code of perform
|
|
|
|
llhttp_t _context;
|
|
|
|
llhttp_settings_t _contextSettings;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace network
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif //__HTTP_RESPONSE_H__
|