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.
|
2022-11-08 13:15:00 +08:00
|
|
|
Copyright (c) 2021-2022 Bytedance Inc.
|
2021-07-06 02:44:57 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-07-06 02:44:57 +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:
|
2021-07-06 02:44:57 +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.
|
2021-07-06 02:44:57 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "network/HttpClient.h"
|
2012-08-07 18:28:07 +08:00
|
|
|
#include <errno.h>
|
2021-06-24 12:33:07 +08:00
|
|
|
#include "base/ccUtils.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2021-06-24 12:33:07 +08:00
|
|
|
#include "yasio/yasio.hpp"
|
|
|
|
|
|
|
|
using namespace yasio;
|
2013-07-10 11:37:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2013-10-15 18:00:03 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace network
|
|
|
|
{
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static HttpClient* _httpClient = nullptr; // pointer to singleton
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2021-07-06 02:44:57 +08:00
|
|
|
template <typename _Cont, typename _Fty>
|
2021-12-25 10:04:45 +08:00
|
|
|
static void __clearQueueUnsafe(_Cont& queue, _Fty pred)
|
|
|
|
{
|
|
|
|
for (auto it = queue.unsafe_begin(); it != queue.unsafe_end();)
|
|
|
|
{
|
|
|
|
if (!pred || pred((*it)))
|
|
|
|
{
|
2020-10-05 02:40:38 +08:00
|
|
|
(*it)->release();
|
2021-06-24 12:33:07 +08:00
|
|
|
it = queue.unsafe_erase(it);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-05 02:40:38 +08:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:28:07 +08:00
|
|
|
// HttpClient implementation
|
2021-12-25 10:04:45 +08:00
|
|
|
HttpClient* HttpClient::getInstance()
|
|
|
|
{
|
|
|
|
if (_httpClient == nullptr)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_httpClient = new HttpClient();
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
2021-07-06 02:44:57 +08:00
|
|
|
|
2015-05-22 10:47:52 +08:00
|
|
|
return _httpClient;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void HttpClient::destroyInstance()
|
|
|
|
{
|
|
|
|
if (nullptr == _httpClient)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("HttpClient singleton is nullptr");
|
2017-03-16 14:09:51 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("HttpClient::destroyInstance begin");
|
2021-06-24 12:33:07 +08:00
|
|
|
delete _httpClient;
|
2017-03-16 14:09:51 +08:00
|
|
|
_httpClient = nullptr;
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("HttpClient::destroyInstance() finished!");
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void HttpClient::enableCookies(const char* cookieFile)
|
|
|
|
{
|
2020-10-08 00:00:14 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(_cookieFileMutex);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (cookieFile)
|
|
|
|
{
|
2021-07-19 00:17:50 +08:00
|
|
|
_cookieFilename = cookieFile;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-25 15:22:43 +08:00
|
|
|
_cookieFilename = (FileUtils::getInstance()->getNativeWritableAbsolutePath() + "cookieFile.txt");
|
2013-07-10 11:37:39 +08:00
|
|
|
}
|
2021-07-19 12:26:25 +08:00
|
|
|
|
|
|
|
if (!_cookie)
|
|
|
|
_cookie = new HttpCookie();
|
|
|
|
_cookie->setCookieFileName(_cookieFilename);
|
|
|
|
_cookie->readFile();
|
2013-07-10 11:37:39 +08:00
|
|
|
}
|
2021-07-06 02:44:57 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void HttpClient::setSSLVerification(std::string_view caFile)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-10-08 00:00:14 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(_sslCaFileMutex);
|
2015-05-22 10:47:52 +08:00
|
|
|
_sslCaFilename = caFile;
|
2021-06-24 12:33:07 +08:00
|
|
|
_service->set_option(yasio::YOPT_S_SSL_CACERT, _sslCaFilename.c_str());
|
2014-09-13 18:54:29 +08:00
|
|
|
}
|
2013-07-10 11:37:39 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
HttpClient::HttpClient()
|
2021-07-19 00:17:50 +08:00
|
|
|
: _isInited(false)
|
|
|
|
, _dispatchOnWorkThread(false)
|
|
|
|
, _timeoutForConnect(30)
|
|
|
|
, _timeoutForRead(60)
|
|
|
|
, _cookie(nullptr)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _clearResponsePredicate(nullptr)
|
2021-07-19 00:17:50 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("In the constructor of HttpClient!");
|
2017-03-16 14:09:51 +08:00
|
|
|
_scheduler = Director::getInstance()->getScheduler();
|
2021-06-24 12:33:07 +08:00
|
|
|
|
|
|
|
_service = new yasio::io_service(HttpClient::MAX_CHANNELS);
|
2023-04-05 13:32:13 +08:00
|
|
|
_service->set_option(yasio::YOPT_S_FORWARD_PACKET, 1); // forward packet immediately when got data from OS kernel
|
2021-07-31 14:39:42 +08:00
|
|
|
_service->set_option(yasio::YOPT_S_DNS_QUERIES_TIMEOUT, 3);
|
|
|
|
_service->set_option(yasio::YOPT_S_DNS_QUERIES_TRIES, 1);
|
2023-03-08 08:34:17 +08:00
|
|
|
_service->start([this](yasio::event_ptr&& e) { handleNetworkEvent(e.get()); });
|
2021-06-24 12:33:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < HttpClient::MAX_CHANNELS; ++i)
|
2022-08-08 13:18:33 +08:00
|
|
|
_availChannelQueue.unsafe_emplace_back(i);
|
2021-06-24 12:33:07 +08:00
|
|
|
|
2023-04-03 19:54:58 +08:00
|
|
|
setDispatchOnWorkThread(false);
|
2021-07-19 13:43:59 +08:00
|
|
|
|
2021-06-24 12:33:07 +08:00
|
|
|
_isInited = true;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
HttpClient::~HttpClient()
|
|
|
|
{
|
2021-07-19 13:43:59 +08:00
|
|
|
_scheduler->unscheduleAllForTarget(this);
|
2021-11-23 12:53:13 +08:00
|
|
|
delete _service;
|
2021-07-19 13:43:59 +08:00
|
|
|
|
|
|
|
clearPendingResponseQueue();
|
|
|
|
clearFinishedResponseQueue();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_cookie)
|
|
|
|
{
|
2021-07-19 12:26:25 +08:00
|
|
|
_cookie->writeFile();
|
|
|
|
delete _cookie;
|
|
|
|
}
|
2021-11-23 12:53:13 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("HttpClient destructor");
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 13:43:59 +08:00
|
|
|
void HttpClient::setDispatchOnWorkThread(bool bVal)
|
|
|
|
{
|
|
|
|
_scheduler->unscheduleAllForTarget(this);
|
|
|
|
_dispatchOnWorkThread = bVal;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!bVal)
|
2023-04-03 19:54:58 +08:00
|
|
|
_scheduler->schedule([this](float) { tickInput(); }, this, 0, false, "#");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Poll and notify main thread if responses exists in queue
|
|
|
|
void HttpClient::tickInput()
|
|
|
|
{
|
|
|
|
if (_finishedResponseQueue.unsafe_empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto lck = _finishedResponseQueue.get_lock();
|
|
|
|
if (!_finishedResponseQueue.unsafe_empty())
|
|
|
|
{
|
|
|
|
HttpResponse* response = _finishedResponseQueue.front();
|
|
|
|
_finishedResponseQueue.unsafe_pop_front();
|
|
|
|
lck.unlock();
|
|
|
|
invokeResposneCallbackAndRelease(response);
|
|
|
|
}
|
2021-07-19 13:43:59 +08:00
|
|
|
}
|
|
|
|
|
2021-07-31 14:39:42 +08:00
|
|
|
void HttpClient::handleNetworkStatusChanged()
|
|
|
|
{
|
|
|
|
_service->set_option(YOPT_S_DNS_DIRTY, 1);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void HttpClient::setNameServers(std::string_view servers)
|
2021-10-23 21:57:36 +08:00
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
_service->set_option(YOPT_S_DNS_LIST, servers.data());
|
2021-10-23 21:57:36 +08:00
|
|
|
}
|
|
|
|
|
2021-09-05 22:18:42 +08:00
|
|
|
yasio::io_service* HttpClient::getInternalService()
|
|
|
|
{
|
|
|
|
return _service;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool HttpClient::send(HttpRequest* request)
|
|
|
|
{
|
2012-08-07 18:28:07 +08:00
|
|
|
if (!request)
|
2021-06-24 12:33:07 +08:00
|
|
|
return false;
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2021-06-24 12:33:07 +08:00
|
|
|
auto response = new HttpResponse(request);
|
2023-04-03 19:54:58 +08:00
|
|
|
response->setLocation(request->getUrl(), false);
|
|
|
|
processResponse(response, -1);
|
2021-06-24 12:33:07 +08:00
|
|
|
response->release();
|
|
|
|
return true;
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int HttpClient::tryTakeAvailChannel()
|
|
|
|
{
|
2021-06-24 12:33:07 +08:00
|
|
|
auto lck = _availChannelQueue.get_lock();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_availChannelQueue.empty())
|
|
|
|
{
|
2021-06-24 12:33:07 +08:00
|
|
|
int channel = _availChannelQueue.front();
|
|
|
|
_availChannelQueue.pop_front();
|
|
|
|
return channel;
|
2014-05-21 10:44:07 +08:00
|
|
|
}
|
2021-06-24 12:33:07 +08:00
|
|
|
return -1;
|
2014-05-20 20:08:26 +08:00
|
|
|
}
|
|
|
|
|
2023-04-03 19:54:58 +08:00
|
|
|
void HttpClient::processResponse(HttpResponse* response, int channelIndex)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2021-06-24 12:33:07 +08:00
|
|
|
response->retain();
|
|
|
|
|
2023-04-03 19:54:58 +08:00
|
|
|
if (response->validateUri())
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2023-04-03 19:54:58 +08:00
|
|
|
if (channelIndex == -1)
|
|
|
|
channelIndex = tryTakeAvailChannel();
|
|
|
|
|
|
|
|
if (channelIndex != -1)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2023-04-03 19:54:58 +08:00
|
|
|
auto channelHandle = _service->channel_at(channelIndex);
|
|
|
|
auto& requestUri = response->getRequestUri();
|
2021-06-24 12:33:07 +08:00
|
|
|
channelHandle->ud_.ptr = response;
|
2021-12-26 23:26:34 +08:00
|
|
|
_service->set_option(YOPT_C_REMOTE_ENDPOINT, channelIndex, requestUri.getHost().data(),
|
2021-12-25 10:04:45 +08:00
|
|
|
(int)requestUri.getPort());
|
2021-06-24 12:33:07 +08:00
|
|
|
if (requestUri.isSecure())
|
|
|
|
_service->open(channelIndex, YCK_SSL_CLIENT);
|
|
|
|
else
|
|
|
|
_service->open(channelIndex, YCK_TCP_CLIENT);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
2023-04-03 19:54:58 +08:00
|
|
|
_pendingResponseQueue.emplace_back(response);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
2023-04-03 19:54:58 +08:00
|
|
|
finishResponse(response);
|
2012-08-07 18:28:07 +08:00
|
|
|
}
|
2021-07-06 02:44:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void HttpClient::handleNetworkEvent(yasio::io_event* event)
|
|
|
|
{
|
2021-07-06 02:44:57 +08:00
|
|
|
int channelIndex = event->cindex();
|
|
|
|
auto channel = _service->channel_at(event->cindex());
|
2021-12-25 10:04:45 +08:00
|
|
|
HttpResponse* response = (HttpResponse*)channel->ud_.ptr;
|
2023-04-03 19:54:58 +08:00
|
|
|
assert(response);
|
2012-08-07 18:28:07 +08:00
|
|
|
|
2021-06-24 12:33:07 +08:00
|
|
|
bool responseFinished = response->isFinished();
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (event->kind())
|
|
|
|
{
|
2021-06-24 12:33:07 +08:00
|
|
|
case YEK_ON_PACKET:
|
|
|
|
if (!responseFinished)
|
2023-01-01 23:11:32 +08:00
|
|
|
{
|
2023-04-03 22:32:33 +08:00
|
|
|
auto&& pkt = event->packet_view();
|
2023-01-01 23:11:32 +08:00
|
|
|
response->handleInput(pkt.data(), pkt.size());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (response->isFinished())
|
|
|
|
{
|
2021-06-25 18:29:16 +08:00
|
|
|
response->updateInternalCode(yasio::errc::eof);
|
2021-06-24 12:33:07 +08:00
|
|
|
_service->close(event->cindex());
|
2021-06-25 18:29:16 +08:00
|
|
|
}
|
2017-03-16 14:09:51 +08:00
|
|
|
break;
|
2021-06-24 12:33:07 +08:00
|
|
|
case YEK_ON_OPEN:
|
2021-12-25 10:04:45 +08:00
|
|
|
if (event->status() == 0)
|
|
|
|
{
|
2021-06-24 12:33:07 +08:00
|
|
|
obstream obs;
|
2021-06-25 08:18:32 +08:00
|
|
|
bool usePostData = false;
|
2021-07-06 02:44:57 +08:00
|
|
|
auto request = response->getHttpRequest();
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (request->getRequestType())
|
|
|
|
{
|
2021-06-24 12:33:07 +08:00
|
|
|
case HttpRequest::Type::GET:
|
|
|
|
obs.write_bytes("GET");
|
|
|
|
break;
|
|
|
|
case HttpRequest::Type::POST:
|
|
|
|
obs.write_bytes("POST");
|
2021-06-25 07:07:59 +08:00
|
|
|
usePostData = true;
|
2021-06-24 12:33:07 +08:00
|
|
|
break;
|
|
|
|
case HttpRequest::Type::DELETE:
|
|
|
|
obs.write_bytes("DELETE");
|
|
|
|
break;
|
|
|
|
case HttpRequest::Type::PUT:
|
|
|
|
obs.write_bytes("PUT");
|
2021-06-25 07:07:59 +08:00
|
|
|
usePostData = true;
|
2021-06-24 12:33:07 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
obs.write_bytes("GET");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
obs.write_bytes(" ");
|
2021-06-25 07:07:59 +08:00
|
|
|
|
|
|
|
auto& uri = response->getRequestUri();
|
2022-11-08 13:15:00 +08:00
|
|
|
obs.write_bytes(uri.getPathEtc());
|
|
|
|
|
2021-06-24 12:33:07 +08:00
|
|
|
obs.write_bytes(" HTTP/1.1\r\n");
|
|
|
|
|
|
|
|
obs.write_bytes("Host: ");
|
|
|
|
obs.write_bytes(uri.getHost());
|
|
|
|
obs.write_bytes("\r\n");
|
|
|
|
|
2021-08-05 14:56:40 +08:00
|
|
|
// process custom headers
|
2021-12-25 10:04:45 +08:00
|
|
|
struct HeaderFlag
|
|
|
|
{
|
|
|
|
enum
|
|
|
|
{
|
2021-08-05 14:56:40 +08:00
|
|
|
UESR_AGENT = 1,
|
|
|
|
CONTENT_TYPE = 1 << 1,
|
|
|
|
ACCEPT = 1 << 2,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
int headerFlags = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& headers = request->getHeaders();
|
|
|
|
if (!headers.empty())
|
|
|
|
{
|
|
|
|
using namespace cxx17; // for string_view literal
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& header : headers)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2021-06-24 12:33:07 +08:00
|
|
|
obs.write_bytes(header);
|
|
|
|
obs.write_bytes("\r\n");
|
2021-07-05 20:42:22 +08:00
|
|
|
|
|
|
|
if (cxx20::ic::starts_with(cxx17::string_view{header}, "User-Agent:"_sv))
|
2021-08-05 14:56:40 +08:00
|
|
|
headerFlags |= HeaderFlag::UESR_AGENT;
|
2021-07-05 20:42:22 +08:00
|
|
|
else if (cxx20::ic::starts_with(cxx17::string_view{header}, "Content-Type:"_sv))
|
2021-08-05 14:56:40 +08:00
|
|
|
headerFlags |= HeaderFlag::CONTENT_TYPE;
|
|
|
|
else if (cxx20::ic::starts_with(cxx17::string_view{header}, "Accept:"_sv))
|
|
|
|
headerFlags |= HeaderFlag::ACCEPT;
|
2021-06-24 12:33:07 +08:00
|
|
|
}
|
2021-07-06 02:44:57 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_cookie)
|
|
|
|
{
|
2021-07-19 12:26:25 +08:00
|
|
|
auto cookies = _cookie->checkAndGetFormatedMatchCookies(uri);
|
|
|
|
if (!cookies.empty())
|
|
|
|
{
|
|
|
|
obs.write_bytes("Cookie: ");
|
|
|
|
obs.write_bytes(cookies);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 14:56:40 +08:00
|
|
|
if (!(headerFlags & HeaderFlag::UESR_AGENT))
|
|
|
|
obs.write_bytes("User-Agent: yasio-http\r\n");
|
|
|
|
|
|
|
|
if (!(headerFlags & HeaderFlag::ACCEPT))
|
|
|
|
obs.write_bytes("Accept: */*;q=0.8\r\n");
|
2021-06-25 07:07:59 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (usePostData)
|
|
|
|
{
|
2021-08-05 14:56:40 +08:00
|
|
|
if (!(headerFlags & HeaderFlag::CONTENT_TYPE))
|
2021-07-05 14:24:04 +08:00
|
|
|
obs.write_bytes("Content-Type: application/x-www-form-urlencoded;charset=UTF-8\r\n");
|
2021-06-25 07:07:59 +08:00
|
|
|
|
|
|
|
char strContentLength[128] = {0};
|
|
|
|
auto requestData = request->getRequestData();
|
|
|
|
auto requestDataSize = request->getRequestDataSize();
|
2023-04-03 19:54:58 +08:00
|
|
|
snprintf(strContentLength, sizeof(strContentLength), "Content-Length: %d\r\n\r\n",
|
|
|
|
static_cast<int>(requestDataSize));
|
2021-06-25 07:07:59 +08:00
|
|
|
obs.write_bytes(strContentLength);
|
2017-03-16 14:09:51 +08:00
|
|
|
|
2021-06-25 07:07:59 +08:00
|
|
|
if (requestData && requestDataSize > 0)
|
|
|
|
obs.write_bytes(cxx17::string_view{requestData, static_cast<size_t>(requestDataSize)});
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-25 07:07:59 +08:00
|
|
|
obs.write_bytes("\r\n");
|
2021-06-24 12:33:07 +08:00
|
|
|
}
|
2017-03-16 14:09:51 +08:00
|
|
|
|
2021-06-24 12:33:07 +08:00
|
|
|
_service->write(event->transport(), std::move(obs.buffer()));
|
|
|
|
|
|
|
|
auto& timerForRead = channel->get_user_timer();
|
2023-04-22 20:52:23 +08:00
|
|
|
timerForRead.cancel();
|
2021-06-24 12:33:07 +08:00
|
|
|
timerForRead.expires_from_now(std::chrono::seconds(this->_timeoutForRead));
|
2023-04-22 20:52:23 +08:00
|
|
|
timerForRead.async_wait([=](io_service& s) {
|
2021-06-25 18:29:16 +08:00
|
|
|
response->updateInternalCode(yasio::errc::read_timeout);
|
2021-12-25 10:04:45 +08:00
|
|
|
s.close(channelIndex); // timeout
|
2021-06-24 12:33:07 +08:00
|
|
|
return true;
|
2021-07-06 02:44:57 +08:00
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-25 18:29:16 +08:00
|
|
|
handleNetworkEOF(response, channel, event->status());
|
2021-06-24 12:33:07 +08:00
|
|
|
}
|
2017-03-16 14:09:51 +08:00
|
|
|
break;
|
2021-06-24 12:33:07 +08:00
|
|
|
case YEK_ON_CLOSE:
|
2021-06-25 18:29:16 +08:00
|
|
|
handleNetworkEOF(response, channel, event->status());
|
2017-03-16 14:09:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-06-24 12:33:07 +08:00
|
|
|
}
|
2017-03-16 14:09:51 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void HttpClient::handleNetworkEOF(HttpResponse* response, yasio::io_channel* channel, int internalErrorCode)
|
|
|
|
{
|
2023-04-03 19:54:58 +08:00
|
|
|
channel->ud_.ptr = nullptr;
|
|
|
|
|
2023-04-22 20:52:23 +08:00
|
|
|
channel->get_user_timer().cancel();
|
2021-06-25 18:29:16 +08:00
|
|
|
response->updateInternalCode(internalErrorCode);
|
2021-06-24 12:33:07 +08:00
|
|
|
auto responseCode = response->getResponseCode();
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (responseCode)
|
|
|
|
{
|
2021-06-24 12:33:07 +08:00
|
|
|
case 301:
|
|
|
|
case 302:
|
2021-07-19 00:17:50 +08:00
|
|
|
case 307:
|
2023-04-03 19:54:58 +08:00
|
|
|
if (response->tryRedirect())
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2023-04-03 19:54:58 +08:00
|
|
|
processResponse(response, channel->index());
|
|
|
|
response->release();
|
|
|
|
break;
|
2021-06-24 12:33:07 +08:00
|
|
|
}
|
2023-04-03 19:54:58 +08:00
|
|
|
default:
|
|
|
|
finishResponse(response);
|
2015-05-22 10:47:52 +08:00
|
|
|
|
2023-04-03 19:54:58 +08:00
|
|
|
// try process pending response
|
|
|
|
auto lck = _pendingResponseQueue.get_lock();
|
|
|
|
if (!_pendingResponseQueue.unsafe_empty())
|
|
|
|
{
|
|
|
|
auto pendingResponse = _pendingResponseQueue.unsafe_front();
|
|
|
|
_pendingResponseQueue.unsafe_pop_front();
|
|
|
|
lck.unlock();
|
2021-07-19 13:43:59 +08:00
|
|
|
|
2023-04-03 19:54:58 +08:00
|
|
|
processResponse(pendingResponse, channel->index());
|
|
|
|
pendingResponse->release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // recycle channel
|
|
|
|
_availChannelQueue.push_front(channel->index());
|
|
|
|
}
|
2021-07-19 13:43:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void HttpClient::finishResponse(HttpResponse* response)
|
|
|
|
{
|
2021-07-06 02:57:13 +08:00
|
|
|
auto request = response->getHttpRequest();
|
|
|
|
auto syncState = request->getSyncState();
|
2021-07-19 12:26:25 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_cookie)
|
|
|
|
{
|
2021-07-19 12:26:25 +08:00
|
|
|
auto cookieRange = response->getResponseHeaders().equal_range("set-cookie");
|
|
|
|
for (auto cookieIt = cookieRange.first; cookieIt != cookieRange.second; ++cookieIt)
|
|
|
|
_cookie->updateOrAddCookie(cookieIt->second, response->_requestUri);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!syncState)
|
|
|
|
{
|
2022-10-12 07:04:36 +08:00
|
|
|
if (_dispatchOnWorkThread || std::this_thread::get_id() == Director::getInstance()->getAxmolThreadId())
|
2021-07-19 13:43:59 +08:00
|
|
|
invokeResposneCallbackAndRelease(response);
|
2021-07-06 02:57:13 +08:00
|
|
|
else
|
2022-08-08 13:18:33 +08:00
|
|
|
_finishedResponseQueue.emplace_back(response);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-06 02:57:13 +08:00
|
|
|
syncState->set_value(response);
|
|
|
|
}
|
2021-06-24 12:33:07 +08:00
|
|
|
}
|
2021-07-06 02:57:13 +08:00
|
|
|
|
2021-07-19 13:43:59 +08:00
|
|
|
void HttpClient::invokeResposneCallbackAndRelease(HttpResponse* response)
|
|
|
|
{
|
|
|
|
HttpRequest* request = response->getHttpRequest();
|
|
|
|
const ccHttpRequestCallback& callback = request->getCallback();
|
|
|
|
|
|
|
|
if (callback != nullptr)
|
|
|
|
callback(this, response);
|
|
|
|
|
|
|
|
response->release();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void HttpClient::clearResponseQueue()
|
|
|
|
{
|
2021-07-19 13:43:59 +08:00
|
|
|
clearPendingResponseQueue();
|
|
|
|
clearFinishedResponseQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpClient::clearPendingResponseQueue()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
auto AX_UNUSED lck = _pendingResponseQueue.get_lock();
|
2021-07-19 13:43:59 +08:00
|
|
|
__clearQueueUnsafe(_pendingResponseQueue, ClearResponsePredicate{});
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void HttpClient::clearFinishedResponseQueue()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
auto AX_UNUSED lck = _finishedResponseQueue.get_lock();
|
2021-07-19 13:43:59 +08:00
|
|
|
__clearQueueUnsafe(_finishedResponseQueue, ClearResponsePredicate{});
|
2015-05-22 10:47:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void HttpClient::setTimeoutForConnect(int value)
|
|
|
|
{
|
2020-10-08 00:00:14 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(_timeoutForConnectMutex);
|
2015-05-22 10:47:52 +08:00
|
|
|
_timeoutForConnect = value;
|
2021-06-24 12:33:07 +08:00
|
|
|
_service->set_option(YOPT_S_CONNECT_TIMEOUT, value);
|
2015-05-22 10:47:52 +08:00
|
|
|
}
|
2021-07-06 02:44:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int HttpClient::getTimeoutForConnect()
|
|
|
|
{
|
2020-10-08 00:00:14 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(_timeoutForConnectMutex);
|
2015-05-22 10:47:52 +08:00
|
|
|
return _timeoutForConnect;
|
|
|
|
}
|
2021-07-06 02:44:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void HttpClient::setTimeoutForRead(int value)
|
|
|
|
{
|
2020-10-08 00:00:14 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(_timeoutForReadMutex);
|
2015-05-22 10:47:52 +08:00
|
|
|
_timeoutForRead = value;
|
|
|
|
}
|
2021-07-06 02:44:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int HttpClient::getTimeoutForRead()
|
|
|
|
{
|
2020-10-08 00:00:14 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(_timeoutForReadMutex);
|
2015-05-22 10:47:52 +08:00
|
|
|
return _timeoutForRead;
|
|
|
|
}
|
2021-07-06 02:44:57 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view HttpClient::getCookieFilename()
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-10-08 00:00:14 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(_cookieFileMutex);
|
2015-05-22 10:47:52 +08:00
|
|
|
return _cookieFilename;
|
|
|
|
}
|
2021-07-06 02:44:57 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view HttpClient::getSSLVerification()
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-10-08 00:00:14 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(_sslCaFileMutex);
|
2015-05-22 10:47:52 +08:00
|
|
|
return _sslCaFilename;
|
|
|
|
}
|
|
|
|
|
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
|