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.
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
http://www.cocos2d-x.org
|
2016-05-18 12:54:16 +08:00
|
|
|
|
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:
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +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-05-25 23:57:40 +08:00
|
|
|
#include "network/HttpCookie.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2014-12-31 10:02:14 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void HttpCookie::readFile()
|
|
|
|
{
|
|
|
|
std::string inString = cocos2d::FileUtils::getInstance()->getStringFromFile(_cookieFileName);
|
2016-05-18 12:54:16 +08:00
|
|
|
if(!inString.empty())
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
|
|
|
std::vector<std::string> cookiesVec;
|
|
|
|
cookiesVec.clear();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
std::stringstream stream(inString);
|
|
|
|
std::string item;
|
|
|
|
while(std::getline(stream, item, '\n'))
|
|
|
|
{
|
|
|
|
cookiesVec.push_back(item);
|
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
if(cookiesVec.empty())
|
|
|
|
return;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
_cookies.clear();
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2016-10-27 15:10:24 +08:00
|
|
|
for(auto& cookie : cookiesVec)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2020-08-18 14:29:09 +08:00
|
|
|
if(cookie.empty())
|
2014-12-31 10:02:14 +08:00
|
|
|
continue;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
if(cookie.find("#HttpOnly_") != std::string::npos)
|
|
|
|
{
|
|
|
|
cookie = cookie.substr(10);
|
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
if(cookie.at(0) == '#')
|
|
|
|
continue;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
CookiesInfo co;
|
|
|
|
std::stringstream streamInfo(cookie);
|
|
|
|
std::vector<std::string> elems;
|
|
|
|
std::string elemsItem;
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
while (std::getline(streamInfo, elemsItem, '\t'))
|
|
|
|
{
|
|
|
|
elems.push_back(elemsItem);
|
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
co.domain = elems[0];
|
|
|
|
if (co.domain.at(0) == '.')
|
|
|
|
{
|
|
|
|
co.domain = co.domain.substr(1);
|
|
|
|
}
|
|
|
|
co.tailmatch = strcmp("TRUE", elems[1].c_str())?true: false;
|
|
|
|
co.path = elems[2];
|
|
|
|
co.secure = strcmp("TRUE", elems[3].c_str())?true: false;
|
|
|
|
co.expires = elems[4];
|
|
|
|
co.name = elems[5];
|
|
|
|
co.value = elems[6];
|
|
|
|
_cookies.push_back(co);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 11:32:35 +08:00
|
|
|
const std::vector<CookiesInfo>* HttpCookie::getCookies() const
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
|
|
|
return &_cookies;
|
|
|
|
}
|
|
|
|
|
2014-12-31 11:32:35 +08:00
|
|
|
const CookiesInfo* HttpCookie::getMatchCookie(const std::string& url) const
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for(auto& cookie : _cookies)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
if(url.find(cookie.domain) != std::string::npos)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpCookie::updateOrAddCookie(CookiesInfo* cookie)
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for(auto& _cookie : _cookies)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
if(cookie->domain == _cookie.domain)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
_cookie = *cookie;
|
2014-12-31 10:02:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_cookies.push_back(*cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpCookie::writeFile()
|
|
|
|
{
|
|
|
|
FILE *out;
|
|
|
|
out = fopen(_cookieFileName.c_str(), "w");
|
|
|
|
fputs("# Netscape HTTP Cookie File\n"
|
|
|
|
"# http://curl.haxx.se/docs/http-cookies.html\n"
|
|
|
|
"# This file was generated by cocos2d-x! Edit at your own risk.\n"
|
|
|
|
"# Test cocos2d-x cookie write.\n\n",
|
|
|
|
out);
|
2016-05-18 12:54:16 +08:00
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
std::string line;
|
2016-10-27 15:10:24 +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');
|
2016-10-27 15:10:24 +08:00
|
|
|
line.append(cookie.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);
|
2014-12-31 10:02:14 +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);
|
|
|
|
}
|
|
|
|
|
2016-05-18 12:54:16 +08:00
|
|
|
void HttpCookie::setCookieFileName(const std::string& filename)
|
2014-12-31 10:02:14 +08:00
|
|
|
{
|
|
|
|
_cookieFileName = filename;
|
|
|
|
}
|
2016-05-18 12:54:16 +08:00
|
|
|
|