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
|
#include "cookies.h"
#include "tools.h"
#include <stdlib.h>
#include <string.h>
typedef enum dt { dt_plain = 0, dt_version, dt_path, dt_domain } dt;
typedef struct automata {
magi_cookies **list;
magi_cookie cookie;
char *buf;
int buf_len;
int buf_size;
int is_first;
int is_advanced;
int is_quoted;
dt datatype;
} automata;
typedef void *(*state)(automata *a, char c);
static void nulify_cookie(automata *a)
{
a->cookie.name = 0;
a->cookie.data = 0;
a->cookie.path = 0;
a->cookie.domain = 0;
a->cookie.max_age = 0;
}
static void buf_new(automata *a)
{
a->buf = 0;
a->buf_len = 0;
a->buf_size = 1;
}
static dt what_is_name(const automata *a)
{
dt datatype = dt_plain;
if (a->is_first && !strcmp(a->buf, "$Version")) {
datatype = dt_version;
} else if (a->is_advanced) {
if (!strcmp(a->buf, "$Path")) {
datatype = dt_path;
} else if (!strcmp(a->buf, "$Domain")) {
datatype = dt_domain;
}
}
return datatype;
}
static void end_name(automata *a)
{
a->datatype = what_is_name(a);
if (a->datatype == dt_plain) {
if (a->cookie.name) {
magi_cookies_add(a->list, &a->cookie);
a->list = &(*a->list)->next;
}
nulify_cookie(a);
a->cookie.name = a->buf;
} else {
free(a->buf);
}
buf_new(a);
}
static int end_data(automata *a)
{
switch (a->datatype) {
case dt_plain: a->cookie.data = a->buf; break;
case dt_path: a->cookie.path = a->buf; break;
case dt_domain: a->cookie.domain = a->buf; break;
case dt_version: if (strcmp(a->buf, "1")) { return 0; }
}
buf_new(a);
return 1;
}
static void *state_name(automata *a, char c);
static void *state_pre_name(automata *a, char c)
{
if (c == ' ' || c == '\t'){
return state_pre_name;
} else if (32 <= c && c <= 126 && !strchr("()<>@,;:\\\"/[]?={}", c)) {
magi_str_add(&a->buf, &a->buf_len, &a->buf_size, c);
return state_name;
}
return 0;
}
static void *state_pre_data(automata *a, char c);
static void *state_post_name(automata *a, char c);
static void *state_name(automata *a, char c)
{
if (c == '=') {
end_name(a);
return state_pre_data;
} else if (c == ' ' || c == '\t') {
end_name(a);
return state_post_name;
} else if (32 <= c && c <= 126 && !strchr("()<>@,;:\\\"/[]?={}", c)) {
magi_str_add(&a->buf, &a->buf_len, &a->buf_size, c);
return state_name;
}
return 0;
}
static void *state_post_name(automata *a, char c)
{
if (c == '=') {
return state_pre_data;
} else if (c == ' ' || c == '\t') {
return state_post_name;
}
return 0;
}
static void *state_data(automata *a, char c);
static void *state_data_quoted(automata *a, char c);
static void *state_pre_data(automata *a, char c)
{
if (c == '"') {
return state_data_quoted;
} else if (c == ' ' || c == '\t') {
return state_pre_data;
} else if (32 <= c && c <= 126 && !strchr("()<>@,;:\\\"/[]?={}", c)) {
magi_str_add(&a->buf, &a->buf_len, &a->buf_size, c);
return state_data;
}
return 0;
}
static void *state_post_data(automata *a, char c);
static void *state_data(automata *a, char c)
{
if (c == ';' || (c == ',' && a->is_first)) {
a->is_first = 0;
return end_data(a) ? state_pre_name : 0;
} else if (c == ' ' || c == '\t') {
return end_data(a) ? state_post_data : 0;
} else if (32 <= c && c <= 126 && !strchr("()<>@,;:\\\"/[]?={}", c)) {
magi_str_add(&a->buf, &a->buf_len, &a->buf_size, c);
return state_data;
}
return 0;
}
static void *state_data_quoted(automata *a, char c)
{
if (c == '"') {
return end_data(a) ? state_post_data : 0;
}
return state_data_quoted;
}
static void *state_post_data(automata *a, char c)
{
if (c == ';' || (c == ',' && a->is_first)) {
a->is_first = 0;
return state_pre_name;
} else if (c == ' ' || c == '\t') {
return state_post_data;
}
return 0;
}
static void parse_end(magi_error *e, automata *a, state s)
{
if (s == state_data_quoted) {
*e = magi_error_cookies;
} else if (s == state_data) {
if (!a->cookie.name) {
*e = magi_error_cookies;
} else if (end_data(a)) {
magi_cookies_add(a->list, &a->cookie);
nulify_cookie(a);
} else {
*e = magi_error_cookies;
}
} else if (s != state_post_data) {
*e = magi_error_cookies;
}
}
void magi_parse_cookies(magi_request *request, const char *data)
{
state s;
automata a = { 0, { 0, 0, 0, 0, 0 }, 0, 0, 0, 1, 0, 0, 0 };
a.list = &request->cookies;
request->cookies = 0;
for (s = state_pre_name; s && *data; ++data) {
s = s(&a, *data);
}
parse_end(&request->error, &a, s);
free(a.cookie.name);
free(a.cookie.data);
free(a.cookie.path);
free(a.cookie.domain);
free(a.buf);
}
|