summaryrefslogtreecommitdiffabout
path: root/include/kingate/cookies.h
blob: 83ef0c69b5fcab99ebb2c9cf6421bf98d1104c3f (plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
#ifndef __KINGATE_COOKIES_H
#define __KINGATE_COOKIES_H

#include <string>
#include <map>
#include <ostream>

/**
 * @file
 * @brief cookies-related classes.
 */

namespace kingate {
    using namespace std;

    /**
     * Class, holding the cookie with parameters.
     */
    class cookie : public map<string,string> {
	public:
	    /**
	     * Cookie name.
	     */
	    string name;
	    /**
	     * Cookie value.
	     */
	    string value;

	    cookie() { }
	    /**
	     * @param n cookie name.
	     * @param v cookie value.
	     */
	    cookie(const string& n,const string& v)
		: name(n), value(v) { }

	    /**
	     * set cookie parameter.
	     * @param p parameter name.
	     * @param v parameter value.
	     * @see _get_string()
	     */
	    void _set_string(const string& p,const string& v);
	   
	    /**
	     * @param n cookie name.
	     * @see get_name()
	     */
	    void set_name(const string& n) { name = n; }
	    /**
	     * @param v cookie value.
	     * @see set_value()
	     */
	    void set_value(const string& v) { value = v; }
	    /**
	     * @param c coomment.
	     * @see get_comment()
	     * @see has_comment()
	     * @see unset_comment()
	     */
	    void set_comment(const string& c);
	    /**
	     * @param d domain.
	     * @see get_domain()
	     * @see has_domain()
	     * @see unset_domain()
	     */
	    void set_domain(const string& d);
	    /**
	     * @param ma max-age.
	     * @see get_max_age()
	     * @see has_max_age()
	     * @see unset_max_age()
	     */
	    void set_max_age(const string& ma);
	    /**
	     * @param p path.
	     * @see get_path()
	     * @see has_path()
	     * @see unset_path()
	     */
	    void set_path(const string& p);
	    /**
	     * set cookie security.
	     * @param s true if secure.
	     * @see get_secure()
	     * @see is_secure()
	     */
	    void set_secure(bool s);

	    /**
	     * @param e expiration time.
	     * @see get_expires()
	     * @see has_expires()
	     * @see unset_expires()
	     */
	    void set_expires(const string& e);

	    /**
	     * get cookie parameter.
	     * @param p parameter name.
	     * @return parameter value.
	     * @see _set_string()
	     */
	    const string& _get_string(const string& p) const;

	    /**
	     * @return cookie name.
	     * @see set_name()
	     */
	    const string& get_name() const { return name; }
	    /**
	     * @return cookie value.
	     * @see set_value()
	     */
	    const string& get_value() const { return value; }
	    /**
	     * @return cookie comment.
	     * @see set_comment()
	     * @see has_comment()
	     * @see unset_comment()
	     */
	    const string& get_comment() const;
	    /**
	     * @return cookie domain.
	     * @see set_domain()
	     * @see has_domain()
	     * @see unset_domain()
	     */
	    const string& get_domain() const;
	    /**
	     * @return cookie max-age.
	     * @see set_max_age()
	     * @see has_max_age()
	     * @see unset_max_age()
	     */
	    const string& get_max_age() const;
	    /**
	     * @return cookie path.
	     * @see set_path()
	     * @see has_path()
	     * @see unset_path()
	     */
	    const string& get_path() const;
	    /**
	     * @return cookie security.
	     * @see is_secure()
	     * @see set_secure()
	     */
	    bool get_secure() const;
	    /**
	     * @return cookie security.
	     * @see get_secure()
	     * @see set_secure()
	     */
	    bool is_secure() const { return get_secure(); }

	    /**
	     * @return cookie expiration time.
	     * @see set_expires()
	     * @see has_expires()
	     * @see unset_expires()
	     */
	    const string& get_expires() const;

	    /**
	     * @return true if cookie has comment.
	     * @see set_comment()
	     * @see get_comment()
	     * @see unset_comment()
	     */
	    bool has_comment() const;
	    /**
	     * @return true if cookie has domain.
	     * @see set_domain()
	     * @see get_domain()
	     * @see unset_domain()
	     */
	    bool has_domain() const;
	    /**
	     * @return true if cookie has max-age.
	     * @see set_max_age()
	     * @see get_max_age()
	     * @see unset_max_age()
	     */
	    bool has_max_age() const;
	    /**
	     * @return true if cookie has path.
	     * @see set_path()
	     * @see get_path()
	     * @see unset_path()
	     */
	    bool has_path() const;

	    /**
	     * @return true if cookie has expiration time.
	     * @see set_expires()
	     * @see get_expires()
	     * @see unset_expires()
	     */
	    bool has_expires() const;

	    /**
	     * rid cookie of comment.
	     * @see set_comment()
	     * @see get_comment()
	     * @see has_comment()
	     */
	    void unset_comment();
	    /**
	     * rid cookie of domain.
	     * @see set_domain()
	     * @see get_domain()
	     * @see has_domain()
	     */
	    void unset_domain();
	    /**
	     * rid cookie of max-age.
	     * @see set_max_age()
	     * @see get_max_age()
	     * @see has_max_age()
	     */
	    void unset_max_age();
	    /**
	     * rid cookie of path.
	     * @see set_path()
	     * @see get_path()
	     * @see has_path()
	     */
	    void unset_path();

	    /**
	     * rid cookie of expiration time.
	     * @see set_expires()
	     * @see get_expires()
	     * @see has_expires()
	     */
	    void unset_expires();

	    /**
	     * render the 'Set-Cookie' HTTP header according to RFC2109.
	     * Absolutely useless, only works with lynx.
	     * @return the rendered header content.
	     */
	    string set_cookie_header_rfc2109() const;
	    /**
	     * render the 'Set-Cookie' header according to the early vague
	     * netscape specs and common practice.
	     * @return the rendered header content.
	     */
	    string set_cookie_header() const;
    };

    /**
     * Cookies container class.
     */
    class cookies_t : public map<string,cookie> {
	public:

	    cookies_t() { }
	    /**
	     * @param s 'Cookie:' HTTP header contents to parse.
	     */
	    cookies_t(const string& s) { parse_cookies(s); }

	    /**
	     * @param c cookie to set.
	     */
	    void set_cookie(const cookie& c) { (*this)[c.get_name()]=c; }
	    /**
	     * @param n cookie name to remove.
	     */
	    void unset_cookie(const key_type& n) { erase(n); }
	    /**
	     * @param n cookie name.
	     * @return true if exists.
	     */
	    bool has_cookie(const key_type& n) const;
	    /**
	     * Return the named cookie if one exists.
	     * @param n cookie name.
	     * @return const reference to cookie object.
	     */
	    const cookie& get_cookie(const key_type& n) const;
	    /**
	     * Return the named cookie if one exists.
	     * @param n cookie name.
	     * @return reference to cookie object.
	     */
	    cookie& get_cookie(const key_type& n);

	    /**
	     * @param s HTTP 'Cookie' header content.
	     */
	    void parse_cookies(const string& s);
    };
}

#endif /* __KINGATE_COOKIES_H */