2019-11-23 20:27:39 +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.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-19 12:26:25 +08:00
|
|
|
https://adxe.org
|
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_COOKIE_H
|
|
|
|
#define HTTP_COOKIE_H
|
|
|
|
/// @cond DO_NOT_SHOW
|
|
|
|
|
2021-07-19 12:40:22 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2021-07-19 12:26:25 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-19 12:26:25 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace network
|
|
|
|
{
|
|
|
|
|
|
|
|
class Uri;
|
2021-07-19 17:58:13 +08:00
|
|
|
struct CookieInfo
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CookieInfo() = default;
|
2021-07-19 17:58:13 +08:00
|
|
|
CookieInfo(const CookieInfo&) = default;
|
|
|
|
CookieInfo(CookieInfo&& rhs)
|
2021-07-19 12:26:25 +08:00
|
|
|
: domain(std::move(rhs.domain))
|
|
|
|
, tailmatch(rhs.tailmatch)
|
|
|
|
, path(std::move(rhs.path))
|
|
|
|
, secure(rhs.secure)
|
|
|
|
, name(std::move(rhs.name))
|
|
|
|
, value(std::move(rhs.value))
|
|
|
|
, expires(rhs.expires)
|
|
|
|
{}
|
|
|
|
|
2021-07-19 17:58:13 +08:00
|
|
|
CookieInfo& operator=(CookieInfo&& rhs)
|
2021-07-19 12:26:25 +08:00
|
|
|
{
|
|
|
|
domain = std::move(rhs.domain);
|
|
|
|
tailmatch = rhs.tailmatch;
|
|
|
|
path = std::move(rhs.path);
|
|
|
|
secure = rhs.secure;
|
|
|
|
name = std::move(rhs.name);
|
|
|
|
value = std::move(rhs.value);
|
|
|
|
expires = rhs.expires;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-07-19 17:58:13 +08:00
|
|
|
bool isSame(const CookieInfo& rhs) { return name == rhs.name && domain == rhs.domain; }
|
2021-07-19 12:26:25 +08:00
|
|
|
|
2021-07-19 17:58:13 +08:00
|
|
|
void updateValue(const CookieInfo& rhs)
|
2021-07-19 12:26:25 +08:00
|
|
|
{
|
|
|
|
value = rhs.value;
|
|
|
|
expires = rhs.expires;
|
|
|
|
path = rhs.path;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string domain;
|
2021-07-19 12:26:25 +08:00
|
|
|
bool tailmatch = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string path;
|
2021-07-19 12:26:25 +08:00
|
|
|
bool secure = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string name;
|
|
|
|
std::string value;
|
2021-07-19 12:26:25 +08:00
|
|
|
time_t expires = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class HttpCookie
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void readFile();
|
|
|
|
|
|
|
|
void writeFile();
|
2021-12-26 23:26:34 +08:00
|
|
|
void setCookieFileName(std::string_view fileName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-19 17:58:13 +08:00
|
|
|
const std::vector<CookieInfo>* getCookies() const;
|
|
|
|
const CookieInfo* getMatchCookie(const Uri& uri) const;
|
|
|
|
void updateOrAddCookie(CookieInfo* cookie);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-07-19 12:26:25 +08:00
|
|
|
// Check match cookies for http request
|
|
|
|
std::string checkAndGetFormatedMatchCookies(const Uri& uri);
|
2021-12-26 23:26:34 +08:00
|
|
|
bool updateOrAddCookie(std::string_view cookie, const Uri& uri);
|
2021-07-19 12:26:25 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
|
|
|
std::string _cookieFileName;
|
2021-07-19 17:58:13 +08:00
|
|
|
std::vector<CookieInfo> _cookies;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
2021-07-19 12:26:25 +08:00
|
|
|
} // namespace network
|
|
|
|
|
|
|
|
NS_CC_END
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/// @endcond
|
|
|
|
#endif /* HTTP_COOKIE_H */
|