-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.c
218 lines (200 loc) · 5.8 KB
/
cookies.c
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
/* cookies.c
* Cookies
* (c) 2002 Mikulas Patocka
* This file is a part of the Links program, released under GPL
*/
#include "links.h"
#define ACCEPT_NONE 0
#define ACCEPT_ALL 1
static int accept_cookies = ACCEPT_ALL;
struct list_head all_cookies = { &all_cookies, &all_cookies };
struct list_head c_domains = { &c_domains, &c_domains };
struct c_server {
list_entry_1st
int accpt;
list_entry_last
unsigned char server[1];
};
static struct list_head c_servers = { &c_servers, &c_servers };
void free_cookie(struct cookie *c)
{
mem_free(c->name);
if (c->value) mem_free(c->value);
mem_free(c->server);
mem_free(c->path);
mem_free(c->domain);
mem_free(c);
}
static void accept_cookie(struct cookie *);
/* sezere 1 cookie z retezce str, na zacatku nesmi byt zadne whitechars
* na konci muze byt strednik nebo 0
* cookie musi byt ve tvaru nazev=hodnota, kolem rovnase nesmi byt zadne mezery
* (respektive mezery se budou pocitat do nazvu a do hodnoty)
*/
int set_cookie(struct terminal *term, unsigned char *url, unsigned char *str)
{
int noval = 0;
struct cookie *cookie;
struct c_server *cs;
struct list_head *lcs;
unsigned char *p, *q, *s, *server, *date, *dom;
if (accept_cookies == ACCEPT_NONE) return 0;
for (p = str; *p != ';' && *p; p++) { /*if (WHITECHAR(*p)) return 0;*/ }
for (q = str; *q != '='; q++) if (!*q || q >= p) {
noval = 1;
break;
}
if (str == q || q + 1 == p) return 0;
cookie = mem_alloc(sizeof(struct cookie));
server = get_host_name(url);
cookie->name = memacpy(str, q - str);
cookie->value = !noval ? memacpy(q + 1, p - q - 1) : NULL;
cookie->server = stracpy(server);
date = parse_header_param(str, cast_uchar "expires", 0);
if (date) {
cookie->expires = parse_http_date(date);
mem_free(date);
} else {
cookie->expires = 0;
}
if (!(cookie->path = parse_header_param(str, cast_uchar "path", 0))) {
cookie->path = stracpy(cast_uchar "/");
} else {
if (cookie->path[0] != '/') {
add_to_strn(&cookie->path, cast_uchar "x");
memmove(cookie->path + 1, cookie->path, strlen(cast_const_char cookie->path) - 1);
cookie->path[0] = '/';
}
}
dom = parse_header_param(str, cast_uchar "domain", 0);
if (!dom) {
cookie->domain = stracpy(server);
} else {
cookie->domain = idn_encode_host(dom, (int)strlen(cast_const_char dom), cast_uchar ".", 0);
if (!cookie->domain)
cookie->domain = stracpy(server);
mem_free(dom);
}
if (cookie->domain[0] == '.') memmove(cookie->domain, cookie->domain + 1, strlen(cast_const_char cookie->domain));
if ((s = parse_header_param(str, cast_uchar "secure", 0))) {
cookie->secure = 1;
mem_free(s);
} else cookie->secure = 0;
if (!allow_cookie_domain(server, cookie->domain)) {
mem_free(cookie->domain);
cookie->domain = stracpy(server);
}
foreach(struct c_server, cs, lcs, c_servers) if (!casestrcmp(cs->server, server)) {
if (cs->accpt) goto ok;
else {
free_cookie(cookie);
mem_free(server);
return 0;
}
}
if (accept_cookies != ACCEPT_ALL) {
free_cookie(cookie);
mem_free(server);
return 1;
}
ok:
accept_cookie(cookie);
mem_free(server);
return 0;
}
static void accept_cookie(struct cookie *c)
{
struct c_domain *cd;
struct list_head *lcd;
struct cookie *d;
struct list_head *ld;
size_t sl;
foreach(struct cookie, d, ld, all_cookies) if (!casestrcmp(d->name, c->name) && !casestrcmp(d->domain, c->domain)) {
ld = ld->prev;
del_from_list(d);
free_cookie(d);
}
if (c->value && !casestrcmp(c->value, cast_uchar "deleted")) {
free_cookie(c);
return;
}
add_to_list(all_cookies, c);
foreach(struct c_domain, cd, lcd, c_domains) if (!casestrcmp(cd->domain, c->domain)) return;
sl = strlen(cast_const_char c->domain);
if (sl > MAXINT - sizeof(struct c_domain)) overalloc();
cd = mem_alloc(sizeof(struct c_domain) + sl);
strcpy(cast_char cd->domain, cast_const_char c->domain);
add_to_list(c_domains, cd);
}
int is_in_domain(unsigned char *d, unsigned char *s)
{
int dl = (int)strlen(cast_const_char d);
int sl = (int)strlen(cast_const_char s);
if (dl > sl) return 0;
if (dl == sl) return !casestrcmp(d, s);
if (s[sl - dl - 1] != '.') return 0;
return !casecmp(d, s + sl - dl, dl);
}
int is_path_prefix(unsigned char *d, unsigned char *s)
{
int dl = (int)strlen(cast_const_char d);
int sl = (int)strlen(cast_const_char s);
if (!dl) return 1;
if (dl > sl) return 0;
if (memcmp(d, s, dl)) return 0;
return d[dl - 1] == '/' || !s[dl] || s[dl] == '/' || s[dl] == POST_CHAR || s[dl] == '?' || s[dl] == '&';
}
int cookie_expired(struct cookie *c)
{
time_t t;
errno = 0;
EINTRLOOPX(t, time(NULL), (time_t)-1);
return c->expires && c->expires < t;
}
void add_cookies(unsigned char **s, int *l, unsigned char *url)
{
int nc = 0;
struct c_domain *cd;
struct list_head *lcd;
struct cookie *c;
struct list_head *lc;
unsigned char *server = get_host_name(url);
unsigned char *data = get_url_data(url);
if (data > url) data--;
foreach(struct c_domain, cd, lcd, c_domains) if (is_in_domain(cd->domain, server)) goto ok;
mem_free(server);
return;
ok:
foreachback(struct cookie, c, lc, all_cookies) if (is_in_domain(c->domain, server)) if (is_path_prefix(c->path, data)) {
if (cookie_expired(c)) {
lc = lc->prev;
del_from_list(c);
free_cookie(c);
continue;
}
if (c->secure && casecmp(url, cast_uchar "https://", 8)) continue;
if (!nc) add_to_str(s, l, cast_uchar "Cookie: "), nc = 1;
else add_to_str(s, l, cast_uchar "; ");
add_to_str(s, l, c->name);
if (c->value) {
add_chr_to_str(s, l, '=');
add_to_str(s, l, c->value);
}
}
if (nc) add_to_str(s, l, cast_uchar "\r\n");
mem_free(server);
}
void free_cookies(void)
{
free_list(struct c_domain, c_domains);
/* !!! FIXME: save cookies */
while (!list_empty(all_cookies)) {
struct cookie *c = list_struct(all_cookies.next, struct cookie);
del_from_list(c);
free_cookie(c);
}
}
void init_cookies(void)
{
/* !!! FIXME: read cookies */
}