-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpCookie.cpp
More file actions
162 lines (134 loc) · 4.08 KB
/
HttpCookie.cpp
File metadata and controls
162 lines (134 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "HttpCookie.h"
#include <stdio.h>
#include <string.h>
#include <sstream>
static std::string getStringFromFile(const char* filename) {
std::string tmp;
do {
FILE* cpfile = fopen(filename,"rb");
size_t filelen = 0;
char* buf = NULL;
if(cpfile == NULL)
break;
fseek(cpfile,0,SEEK_END);
filelen = ftell(cpfile);
buf = new char[filelen + 1];
fseek(cpfile,0,SEEK_SET);
buf[filelen] = 0;
fread(buf,1,filelen,cpfile);
tmp = buf;
delete[] buf;
fclose(cpfile);
}while(0);
return tmp;
}
void HttpCookie::readFile()
{
std::string inString = getStringFromFile(_cookieFileName.c_str());
if(inString.length() != 0)
{
std::vector<std::string> cookiesVec;
cookiesVec.clear();
std::stringstream stream(inString);
std::string item;
while(std::getline(stream, item, '\n'))
{
cookiesVec.push_back(item);
}
if(cookiesVec.empty())
return;
_cookies.clear();
for(std::vector<std::string>::iterator iter = cookiesVec.begin();iter != cookiesVec.end(); iter++)
{
std::string cookie = *iter;
if(cookie.length() == 0)
continue;
if(cookie.find("#HttpOnly_") != std::string::npos)
{
cookie = cookie.substr(10);
}
if(cookie.at(0) == '#')
continue;
CookiesInfo co;
std::stringstream streamInfo(cookie);
std::vector<std::string> elems;
std::string elemsItem;
while (std::getline(streamInfo, elemsItem, '\t'))
{
elems.push_back(elemsItem);
}
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);
}
}
}
const std::vector<CookiesInfo>* HttpCookie::getCookies() const
{
return &_cookies;
}
const CookiesInfo* HttpCookie::getMatchCookie(const std::string& url) const
{
for(std::vector<CookiesInfo>::const_iterator iter = _cookies.begin(); iter != _cookies.end(); iter++)
{
if(url.find(iter->domain) != std::string::npos)
return &(*iter);
}
return NULL;
}
void HttpCookie::updateOrAddCookie(CookiesInfo* cookie)
{
for(std::vector<CookiesInfo>::iterator iter = _cookies.begin(); iter != _cookies.end(); iter++)
{
if(cookie->domain == iter->domain)
{
*iter = *cookie;
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);
std::string line;
for(std::vector<CookiesInfo>::iterator iter = _cookies.begin(); iter != _cookies.end(); iter++)
{
line.clear();
line.append(iter->domain);
line.append(1, '\t');
iter->tailmatch ? line.append("TRUE") : line.append("FALSE");
line.append(1, '\t');
line.append(iter->path);
line.append(1, '\t');
iter->secure ? line.append("TRUE") : line.append("FALSE");
line.append(1, '\t');
line.append(iter->expires);
line.append(1, '\t');
line.append(iter->name);
line.append(1, '\t');
line.append(iter->value);
//line.append(1, '\n');
fprintf(out, "%s\n", line.c_str());
}
fclose(out);
}
void HttpCookie::setCookieFileName(std::string filename)
{
_cookieFileName = filename;
}