-
Notifications
You must be signed in to change notification settings - Fork 951
Expand file tree
/
Copy pathHttpCookie.cpp
More file actions
288 lines (245 loc) · 6.06 KB
/
Copy pathHttpCookie.cpp
File metadata and controls
288 lines (245 loc) · 6.06 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/stdlib/dbuf_pool.hpp"
#include "acl_cpp/stdlib/snprintf.hpp"
#include "acl_cpp/http/http_header.hpp"
#include "acl_cpp/http/HttpCookie.hpp"
#endif
namespace acl
{
HttpCookie::HttpCookie(const char* name, const char* value,
dbuf_guard* dbuf /* = NULL */)
{
if (dbuf != NULL) {
dbuf_ = dbuf;
dbuf_internal_ = NULL;
} else {
dbuf_internal_ = NEW dbuf_guard;
dbuf_ = dbuf_internal_;
}
acl_assert(name && *name && value);
name_ = dbuf_->dbuf_strdup(name);
value_ = dbuf_->dbuf_strdup(value);
dummy_[0] = 0;
}
HttpCookie::HttpCookie(dbuf_guard* dbuf /* = NULL */)
{
if (dbuf != NULL) {
dbuf_ = dbuf;
dbuf_internal_ = NULL;
} else {
dbuf_internal_ = NEW dbuf_guard;
dbuf_ = dbuf_internal_;
}
name_ = NULL;
value_ = NULL;
dummy_[0] = 0;
}
HttpCookie::HttpCookie(const HttpCookie* cookie, dbuf_guard* dbuf /* = NULL */)
{
if (dbuf != NULL) {
dbuf_ = dbuf;
dbuf_internal_ = NULL;
} else {
dbuf_internal_ = NEW dbuf_guard;
dbuf_ = dbuf_internal_;
}
dummy_[0] = 0;
acl_assert(cookie);
if (cookie->name_) {
name_ = dbuf_->dbuf_strdup(cookie->name_);
} else {
name_ = NULL;
}
if (cookie->value_) {
value_ = dbuf_->dbuf_strdup(cookie->value_);
} else {
value_ = NULL;
}
std::list<HTTP_PARAM*>::const_iterator cit = cookie->params_.begin();
for (; cit != cookie->params_.end(); ++cit) {
HTTP_PARAM* param = (HTTP_PARAM*)
dbuf_->dbuf_alloc(sizeof(HTTP_PARAM));
param->name = dbuf_->dbuf_strdup((*cit)->name);
param->value = dbuf_->dbuf_strdup((*cit)->value);
params_.push_back(param);
}
}
HttpCookie::~HttpCookie(void)
{
delete dbuf_internal_;
}
void HttpCookie::destroy(void)
{
delete this;
}
bool HttpCookie::splitNameValue(char* data, HTTP_PARAM* param)
{
#define SKIP_SPECIAL(x) { while (*(x) == ' ' || *(x) == '\t' || *(x) == '=') (x)++; }
#define SKIP_WHILE(cond, x) { while(*(x) && (cond)) (x)++; }
// 开始解析过程
param->name = data;
// 去掉开头无用的特殊字符
SKIP_SPECIAL(param->name);
if (*(param->name) == 0) {
return false;
}
// 找到 '='
param->value = param->name;
SKIP_WHILE(*(param->value) != '=', param->value);
if (*(param->value) != '=') {
return false;
}
// 去掉 '=' 前面的空格
char* ptr = param->value - 1;
*param->value++ = 0;
while (ptr > param->name && (*ptr == ' ' || *ptr == '\t')) {
*ptr-- = 0;
}
// 去掉 value 开始的无效字符
SKIP_SPECIAL(param->value);
// 找到 value 值的结束位置
// 允许 value = "\0"
ptr = param->value + strlen(param->value) - 1;
while (ptr >= param->value && (*ptr == ' ' || *ptr == '\t')) {
*ptr-- = 0;
}
return true;
}
// value 格式:xxx=xxx; domain=xxx; expires=xxx; path=xxx
bool HttpCookie::setCookie(const char* value)
{
if (value == NULL || *value == 0) {
return false;
}
ACL_ARGV* tokens = acl_argv_split(value, ";");
acl_assert(tokens->argc > 0);
HTTP_PARAM param;
// 从第一个 name=value 字段中取得 cookie 名及 cookie 值
if (splitNameValue(tokens->argv[0], ¶m) == false) {
acl_argv_free(tokens);
return false;
}
// name 肯定非 "\0",而 value 可以为 "\0"
name_ = dbuf_->dbuf_strdup(param.name);
value_ = dbuf_->dbuf_strdup(param.value);
for (int i = 1; i < tokens->argc; i++) {
if (splitNameValue(tokens->argv[i], ¶m) == false) {
continue;
}
if (*(param.value) == 0) {
continue;
}
if (strcasecmp(param.name, "domain") == 0) {
setDomain(param.value);
} else if (strcasecmp(param.name, "expires") == 0) {
setExpires(param.value);
} else if (strcasecmp(param.name, "path") == 0) {
setPath(param.value);
} else {
add(param.name, param.value);
}
}
acl_argv_free(tokens);
return true;
}
HttpCookie& HttpCookie::setDomain(const char* domain)
{
add("Domain", domain);
return *this;
}
HttpCookie& HttpCookie::setPath(const char* path)
{
add("Path", path);
return *this;
}
HttpCookie& HttpCookie::setExpires(time_t timeout)
{
if (timeout > 0) {
time_t n = time(NULL);
n += timeout;
char buf[64];
http_header::date_format(buf, sizeof(buf), n);
add("Expires", buf);
}
return *this;
}
HttpCookie& HttpCookie::setExpires(const char* expires)
{
if (expires && *expires) {
add("Expires", expires);
}
return *this;
}
HttpCookie& HttpCookie::setMaxAge(int max_age)
{
char tmp[20];
safe_snprintf(tmp, sizeof(tmp), "%d", max_age);
add("Max-Age", tmp);
return *this;
}
HttpCookie& HttpCookie::add(const char* name, const char* value)
{
if (name == NULL || *name == 0 || value == NULL) {
return *this;
}
HTTP_PARAM* param = (HTTP_PARAM*) dbuf_->dbuf_alloc(sizeof(HTTP_PARAM));
param->name = dbuf_->dbuf_strdup(name);
param->value = dbuf_->dbuf_strdup(value);
params_.push_back(param);
return *this;
}
const char* HttpCookie::getName(void) const
{
if (name_ == NULL) {
return dummy_;
}
return name_;
}
const char* HttpCookie::getValue(void) const
{
if (value_ == NULL) {
return dummy_;
}
return value_;
}
const char* HttpCookie::getExpires(void) const
{
return getParam("Expires");
}
const char* HttpCookie::getDomain(void) const
{
return getParam("Domain");
}
const char* HttpCookie::getPath(void) const
{
return getParam("Path");
}
int HttpCookie::getMaxAge(void) const
{
const char* ptr = getParam("Max-Age");
if (ptr == NULL || *ptr == 0) {
return -1;
}
return atoi(ptr);
}
const char* HttpCookie::getParam(const char* name,
bool case_insensitive /* = true */) const
{
std::list<HTTP_PARAM*>::const_iterator cit = params_.begin();
for (; cit != params_.end(); ++cit) {
if (case_insensitive) {
if (strcasecmp((*cit)->name, name) == 0) {
return (*cit)->value;
}
} else if (strcasecmp((*cit)->name, name) == 0) {
return (*cit)->value;
}
}
return dummy_;
}
const std::list<HTTP_PARAM*>& HttpCookie::getParams(void) const
{
return params_;
}
} // namespace acl end