HttpClient: Improve url redirect process

This commit is contained in:
halx99 2021-07-19 00:17:50 +08:00
parent 4e3a2355ba
commit cc0bb3230f
1 changed files with 12 additions and 4 deletions

View File

@ -79,7 +79,7 @@ void HttpClient::destroyInstance() {
void HttpClient::enableCookies(const char* cookieFile) { void HttpClient::enableCookies(const char* cookieFile) {
std::lock_guard<std::recursive_mutex> lock(_cookieFileMutex); std::lock_guard<std::recursive_mutex> lock(_cookieFileMutex);
if (cookieFile) { if (cookieFile) {
_cookieFilename = std::string(cookieFile); _cookieFilename = cookieFile;
} else { } else {
_cookieFilename = (FileUtils::getInstance()->getNativeWritableAbsolutePath() + "cookieFile.txt"); _cookieFilename = (FileUtils::getInstance()->getNativeWritableAbsolutePath() + "cookieFile.txt");
} }
@ -92,8 +92,13 @@ void HttpClient::setSSLVerification(const std::string& caFile) {
} }
HttpClient::HttpClient() HttpClient::HttpClient()
: _isInited(false), _dispatchOnWorkThread(false), _timeoutForConnect(30), _timeoutForRead(60), _cookie(nullptr), : _isInited(false)
_clearResponsePredicate(nullptr) { , _dispatchOnWorkThread(false)
, _timeoutForConnect(30)
, _timeoutForRead(60)
, _cookie(nullptr)
, _clearResponsePredicate(nullptr)
{
CCLOG("In the constructor of HttpClient!"); CCLOG("In the constructor of HttpClient!");
_scheduler = Director::getInstance()->getScheduler(); _scheduler = Director::getInstance()->getScheduler();
@ -289,11 +294,14 @@ void HttpClient::handleNetworkEOF(HttpResponse* response, yasio::io_channel* cha
auto responseCode = response->getResponseCode(); auto responseCode = response->getResponseCode();
switch (responseCode) { switch (responseCode) {
case 301: case 301:
case 307:
case 302: case 302:
case 307:
if (response->increaseRedirectCount() < HttpClient::MAX_REDIRECT_COUNT) { if (response->increaseRedirectCount() < HttpClient::MAX_REDIRECT_COUNT) {
auto iter = response->_responseHeaders.find("LOCATION"); auto iter = response->_responseHeaders.find("LOCATION");
if (iter != response->_responseHeaders.end()) { if (iter != response->_responseHeaders.end()) {
if (responseCode == 302)
response->getHttpRequest()->setRequestType(HttpRequest::Type::GET);
CCLOG("Process url redirect (%d): %s", responseCode, iter->second.c_str());
_availChannelQueue.push_back(channel->index()); _availChannelQueue.push_back(channel->index());
processResponse(response, iter->second); processResponse(response, iter->second);
response->release(); response->release();