2014-12-31 10:02:14 +08:00
|
|
|
/****************************************************************************
|
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-07-19 17:58:13 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2014-12-31 10:02:14 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-05-25 23:57:40 +08:00
|
|
|
#include "network/HttpCookie.h"
|
2021-07-19 12:26:25 +08:00
|
|
|
#include "network/Uri.h"
|
2016-05-25 23:57:40 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2021-07-19 12:26:25 +08:00
|
|
|
#include "yasio/detail/utils.hpp"
|
2023-01-04 23:20:31 +08:00
|
|
|
#include "yasio/stl/string_view.hpp"
|
2023-01-01 23:31:24 +08:00
|
|
|
#include "xsbase/fast_split.hpp"
|
2021-07-19 12:26:25 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
#include <stdio.h>
|
2021-07-19 12:26:25 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <locale>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2014-12-31 10:02:14 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2021-07-19 12:26:25 +08:00
|
|
|
|
|
|
|
namespace network
|
|
|
|
{
|
2014-12-31 10:02:14 +08:00
|
|
|
void HttpCookie::readFile()
|
|
|
|
{
|
2021-07-19 17:58:13 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
DOMAIN_INDEX = 0,
|
|
|
|
TAILMATCH_INDEX,
|
|
|
|
PATH_INDEX,
|
|
|
|
SECURE_INDEX,
|
|
|
|
EXPIRES_INDEX,
|
|
|
|
NAME_INDEX,
|
2021-07-21 21:06:55 +08:00
|
|
|
VALUE_INDEX,
|
2021-07-19 17:58:13 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
std::string inString = ax::FileUtils::getInstance()->getStringFromFile(_cookieFileName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!inString.empty())
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2023-01-01 23:31:24 +08:00
|
|
|
xsbase::fast_split(inString, '\n', [this](char* s, char* e) {
|
|
|
|
if (*s == '#') // skip comment
|
|
|
|
return;
|
2021-07-19 17:58:13 +08:00
|
|
|
int count = 0;
|
|
|
|
CookieInfo cookieInfo;
|
|
|
|
using namespace cxx17;
|
2023-01-01 23:31:24 +08:00
|
|
|
xsbase::fast_split(s, e - s, '\t', [&, this](char* ss, char* ee) {
|
|
|
|
auto ch = *ee; // store
|
|
|
|
*ee = '\0';
|
2021-07-19 17:58:13 +08:00
|
|
|
switch (count)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case DOMAIN_INDEX:
|
2023-01-01 23:31:24 +08:00
|
|
|
cookieInfo.domain.assign(ss, ee - ss);
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
case PATH_INDEX:
|
2023-01-01 23:31:24 +08:00
|
|
|
cookieInfo.path.assign(ss, ee - ss);
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
case SECURE_INDEX:
|
2023-01-01 23:31:24 +08:00
|
|
|
cookieInfo.secure = cxx17::string_view{ss, (size_t)(ee - ss)} == "TRUE"_sv;
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
case EXPIRES_INDEX:
|
2023-01-01 23:31:24 +08:00
|
|
|
cookieInfo.expires = static_cast<time_t>(strtoll(ss, nullptr, 10));
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
case NAME_INDEX:
|
2023-01-01 23:31:24 +08:00
|
|
|
cookieInfo.name.assign(ss, ee - ss);
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
case VALUE_INDEX:
|
2023-01-01 23:31:24 +08:00
|
|
|
cookieInfo.value.assign(ss, ee - ss);
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2021-07-19 17:58:13 +08:00
|
|
|
}
|
2023-01-01 23:31:24 +08:00
|
|
|
*ee = ch; // restore
|
2021-07-19 17:58:13 +08:00
|
|
|
++count;
|
2023-01-01 23:31:24 +08:00
|
|
|
});
|
2021-07-19 17:58:13 +08:00
|
|
|
if (count >= 7)
|
2022-08-08 13:18:33 +08:00
|
|
|
_cookies.emplace_back(std::move(cookieInfo));
|
2023-01-01 23:31:24 +08:00
|
|
|
});
|
2014-12-31 10:02:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:58:13 +08:00
|
|
|
const std::vector<CookieInfo>* HttpCookie::getCookies() const
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
|
|
|
return &_cookies;
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:58:13 +08:00
|
|
|
const CookieInfo* HttpCookie::getMatchCookie(const Uri& uri) const
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& cookie : _cookies)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2021-07-20 15:03:19 +08:00
|
|
|
if (cxx20::ends_with(uri.getHost(), cookie.domain) && cxx20::starts_with(uri.getPath(), cookie.path))
|
2016-10-27 15:10:24 +08:00
|
|
|
return &cookie;
|
2014-12-31 10:02:14 +08:00
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:58:13 +08:00
|
|
|
void HttpCookie::updateOrAddCookie(CookieInfo* cookie)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& _cookie : _cookies)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (cookie->isSame(_cookie))
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2021-07-19 12:26:25 +08:00
|
|
|
_cookie.updateValue(*cookie);
|
2014-12-31 10:02:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-08-08 13:18:33 +08:00
|
|
|
_cookies.emplace_back(std::move(*cookie));
|
2021-07-19 12:26:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string HttpCookie::checkAndGetFormatedMatchCookies(const Uri& uri)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
for (auto iter = _cookies.begin(); iter != _cookies.end();)
|
|
|
|
{
|
|
|
|
auto& cookie = *iter;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (cxx20::ends_with(uri.getHost(), cookie.domain) && cxx20::starts_with(uri.getPath(), cookie.path))
|
2021-07-19 12:26:25 +08:00
|
|
|
{
|
|
|
|
if (yasio::time_now() >= cookie.expires)
|
|
|
|
{
|
|
|
|
iter = _cookies.erase(iter);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret.empty())
|
|
|
|
ret += "; ";
|
|
|
|
|
|
|
|
ret += cookie.name;
|
|
|
|
ret.push_back('=');
|
|
|
|
ret += cookie.value;
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool HttpCookie::updateOrAddCookie(std::string_view cookie, const Uri& uri)
|
2021-07-19 12:26:25 +08:00
|
|
|
{
|
|
|
|
unsigned int count = 0;
|
2021-07-19 17:58:13 +08:00
|
|
|
CookieInfo info;
|
2023-01-01 23:31:24 +08:00
|
|
|
xsbase::nzls::fast_split(cookie.data(), cookie.length(), ';', [&](const char* start, const char* end) {
|
2021-07-19 12:26:25 +08:00
|
|
|
unsigned int count_ = 0;
|
2023-01-01 23:31:24 +08:00
|
|
|
while (*start == ' ')
|
|
|
|
++start; // skip ws
|
2021-07-19 12:26:25 +08:00
|
|
|
if (++count > 1)
|
|
|
|
{
|
|
|
|
cxx17::string_view key;
|
|
|
|
cxx17::string_view value;
|
2023-01-01 23:31:24 +08:00
|
|
|
xsbase::fast_split(start, end - start, '=', [&](const char* s, const char* e) {
|
2021-07-19 12:26:25 +08:00
|
|
|
switch (++count_)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case 1:
|
2023-01-01 23:31:24 +08:00
|
|
|
key = cxx17::string_view(s, e - s);
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2023-01-01 23:31:24 +08:00
|
|
|
value = cxx17::string_view(s, e - s);
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2021-07-19 12:26:25 +08:00
|
|
|
}
|
2023-01-01 23:31:24 +08:00
|
|
|
});
|
2021-07-19 12:26:25 +08:00
|
|
|
|
|
|
|
using namespace cxx17;
|
|
|
|
if (cxx20::ic::iequals(key, "domain"_sv))
|
|
|
|
{
|
|
|
|
if (!value.empty())
|
|
|
|
info.domain.assign(value.data(), value.length());
|
|
|
|
}
|
|
|
|
else if (cxx20::ic::iequals(key, "path"_sv))
|
|
|
|
{
|
|
|
|
if (!value.empty())
|
|
|
|
info.path.assign(value.data(), value.length());
|
|
|
|
}
|
|
|
|
else if (cxx20::ic::iequals(key, "expires"_sv))
|
|
|
|
{
|
|
|
|
std::string expires_ctime(!value.empty() ? value.data() : "", value.length());
|
2021-07-20 15:03:19 +08:00
|
|
|
if (cxx20::ends_with(expires_ctime, " GMT"_sv))
|
2021-07-19 12:26:25 +08:00
|
|
|
expires_ctime.resize(expires_ctime.length() - sizeof(" GMT") + 1);
|
|
|
|
if (expires_ctime.empty())
|
2023-01-01 23:31:24 +08:00
|
|
|
return;
|
2021-07-19 12:26:25 +08:00
|
|
|
size_t off = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto p = expires_ctime.find_first_of(',');
|
|
|
|
if (p != std::string::npos)
|
|
|
|
{
|
|
|
|
p = expires_ctime.find_first_not_of(' ', p + 1); // skip ws
|
|
|
|
if (p != std::string::npos)
|
2021-07-19 12:26:25 +08:00
|
|
|
off = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tm dt = {0};
|
|
|
|
std::stringstream ss(&expires_ctime[off]);
|
|
|
|
ss >> std::get_time(&dt, "%d %b %Y %H:%M:%S");
|
|
|
|
if (!ss.fail())
|
|
|
|
info.expires = mktime(&dt);
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2021-07-19 12:26:25 +08:00
|
|
|
ss.str("");
|
|
|
|
ss.clear();
|
|
|
|
ss << (&expires_ctime[off]);
|
|
|
|
ss >> std::get_time(&dt, "%d-%b-%Y %H:%M:%S");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!ss.fail())
|
2021-07-19 12:26:25 +08:00
|
|
|
info.expires = mktime(&dt);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (cxx20::ic::iequals(key, "secure"_sv))
|
|
|
|
{
|
2021-07-19 12:26:25 +08:00
|
|
|
info.secure = true;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{ // first is cookie name
|
2023-01-01 23:31:24 +08:00
|
|
|
xsbase::fast_split(start, end - start, '=', [&](const char* s, const char* e) {
|
2021-07-19 12:26:25 +08:00
|
|
|
switch (++count_)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case 1:
|
2023-01-01 23:31:24 +08:00
|
|
|
info.name.assign(s, e - s);
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2023-01-01 23:31:24 +08:00
|
|
|
info.value.assign(s, e - s);
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2021-07-19 12:26:25 +08:00
|
|
|
}
|
2023-01-01 23:31:24 +08:00
|
|
|
});
|
2021-07-19 12:26:25 +08:00
|
|
|
}
|
2023-01-01 23:31:24 +08:00
|
|
|
});
|
2021-07-19 12:26:25 +08:00
|
|
|
if (info.path.empty())
|
|
|
|
info.path.push_back('/');
|
|
|
|
|
|
|
|
if (info.domain.empty())
|
|
|
|
info.domain += uri.getHost();
|
|
|
|
|
|
|
|
if (info.expires <= 0)
|
|
|
|
info.expires = (std::numeric_limits<time_t>::max)();
|
|
|
|
|
|
|
|
updateOrAddCookie(&info);
|
|
|
|
return true;
|
2014-12-31 10:02:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpCookie::writeFile()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
FILE* out;
|
2021-07-19 12:26:25 +08:00
|
|
|
out = fopen(_cookieFileName.c_str(), "wb");
|
2021-12-25 10:04:45 +08:00
|
|
|
fputs(
|
|
|
|
"# Netscape HTTP Cookie File\n"
|
|
|
|
"# http://curl.haxx.se/docs/http-cookies.html\n"
|
2022-07-09 01:23:11 +08:00
|
|
|
"# This file was generated by axis! Edit at your own risk.\n"
|
|
|
|
"# Test axis cookie write.\n\n",
|
2021-12-25 10:04:45 +08:00
|
|
|
out);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
std::string line;
|
2021-07-19 12:26:25 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
char expires[32] = {0}; // LONGLONG_STRING_SIZE=20
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& cookie : _cookies)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
|
|
|
line.clear();
|
2016-10-27 15:10:24 +08:00
|
|
|
line.append(cookie.domain);
|
2014-12-31 10:02:14 +08:00
|
|
|
line.append(1, '\t');
|
2016-10-27 15:10:24 +08:00
|
|
|
cookie.tailmatch ? line.append("TRUE") : line.append("FALSE");
|
2014-12-31 10:02:14 +08:00
|
|
|
line.append(1, '\t');
|
2016-10-27 15:10:24 +08:00
|
|
|
line.append(cookie.path);
|
2014-12-31 10:02:14 +08:00
|
|
|
line.append(1, '\t');
|
2016-10-27 15:10:24 +08:00
|
|
|
cookie.secure ? line.append("TRUE") : line.append("FALSE");
|
2014-12-31 10:02:14 +08:00
|
|
|
line.append(1, '\t');
|
2022-11-14 15:02:16 +08:00
|
|
|
snprintf(expires, sizeof(expires), "%lld", static_cast<long long>(cookie.expires));
|
2021-07-19 12:26:25 +08:00
|
|
|
line.append(expires);
|
2014-12-31 10:02:14 +08:00
|
|
|
line.append(1, '\t');
|
2016-10-27 15:10:24 +08:00
|
|
|
line.append(cookie.name);
|
2014-12-31 10:02:14 +08:00
|
|
|
line.append(1, '\t');
|
2016-10-27 15:10:24 +08:00
|
|
|
line.append(cookie.value);
|
2021-12-25 10:04:45 +08:00
|
|
|
// line.append(1, '\n');
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
fprintf(out, "%s\n", line.c_str());
|
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void HttpCookie::setCookieFileName(std::string_view filename)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
|
|
|
_cookieFileName = filename;
|
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2021-07-19 12:26:25 +08:00
|
|
|
} // namespace network
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|