release 1.6.5
[m6w6/ext-http] / http_cookie_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #include "php_http.h"
16 #include "php_http_api.h"
17 #include "php_http_date_api.h"
18 #include "php_http_cookie_api.h"
19
20 #include "ext/standard/url.h"
21
22 /* {{{ PHP_MINIT_FUNCTION(http_cookie) */
23 PHP_MINIT_FUNCTION(http_cookie)
24 {
25 HTTP_LONG_CONSTANT("HTTP_COOKIE_PARSE_RAW", HTTP_COOKIE_PARSE_RAW);
26 HTTP_LONG_CONSTANT("HTTP_COOKIE_SECURE", HTTP_COOKIE_SECURE);
27 HTTP_LONG_CONSTANT("HTTP_COOKIE_HTTPONLY", HTTP_COOKIE_HTTPONLY);
28
29 return SUCCESS;
30 }
31 /* }}} */
32
33 /* {{{ http_cookie_list *http_cookie_list_init(http_cookie_list *) */
34 PHP_HTTP_API http_cookie_list *_http_cookie_list_init(http_cookie_list *list ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
35 {
36 if (!list) {
37 list = emalloc_rel(sizeof(http_cookie_list));
38 }
39
40 zend_hash_init(&list->cookies, 0, NULL, ZVAL_PTR_DTOR, 0);
41 zend_hash_init(&list->extras, 0, NULL, ZVAL_PTR_DTOR, 0);
42
43 list->path = NULL;
44 list->domain = NULL;
45 list->expires = 0;
46 list->flags = 0;
47
48 return list;
49 }
50 /* }}} */
51
52 /* {{{ void http_cookie_list_dtor(http_cookie_list *) */
53 PHP_HTTP_API void _http_cookie_list_dtor(http_cookie_list *list TSRMLS_DC)
54 {
55 if (list) {
56 zend_hash_destroy(&list->cookies);
57 zend_hash_destroy(&list->extras);
58
59 STR_SET(list->path, NULL);
60 STR_SET(list->domain, NULL);
61 }
62 }
63 /* }}} */
64
65 /* {{{ void http_cookie_list_free(http_cookie_list **) */
66 PHP_HTTP_API void _http_cookie_list_free(http_cookie_list **list TSRMLS_DC)
67 {
68 if (list) {
69 http_cookie_list_dtor(*list);
70 efree(*list);
71 *list = NULL;
72 }
73 }
74 /* }}} */
75
76 /* {{{ const char *http_cookie_list_get_cookie(http_cookie_list *, const char*, size_t) */
77 PHP_HTTP_API const char *_http_cookie_list_get_cookie(http_cookie_list *list, const char *name, size_t name_len TSRMLS_DC)
78 {
79 zval **cookie = NULL;
80 if ((SUCCESS != zend_hash_find(&list->cookies, HTTP_ZAPI_CONST_CAST(char *) name, name_len + 1, (void *) &cookie)) || (Z_TYPE_PP(cookie) != IS_STRING)) {
81 return NULL;
82 }
83 return Z_STRVAL_PP(cookie);
84 }
85 /* }}} */
86
87 /* {{{ const char *http_cookie_list_get_extra(http_cookie_list *, const char *, size_t) */
88 PHP_HTTP_API const char *_http_cookie_list_get_extra(http_cookie_list *list, const char *name, size_t name_len TSRMLS_DC)
89 {
90 zval **extra = NULL;
91 if ((SUCCESS != zend_hash_find(&list->extras, HTTP_ZAPI_CONST_CAST(char *) name, name_len + 1, (void *) &extra)) || (Z_TYPE_PP(extra) != IS_STRING)) {
92 return NULL;
93 }
94 return Z_STRVAL_PP(extra);
95 }
96 /* }}} */
97
98 /* {{{ void http_cookie_list_add_cookie(http_cookie_list *, const char *, size_t, const char *, size_t) */
99 PHP_HTTP_API void _http_cookie_list_add_cookie(http_cookie_list *list, const char *name, size_t name_len, const char *value, size_t value_len TSRMLS_DC)
100 {
101 zval *cookie_value;
102 char *key = estrndup(name, name_len);
103 MAKE_STD_ZVAL(cookie_value);
104 ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0);
105 zend_hash_update(&list->cookies, key, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
106 efree(key);
107 }
108 /* }}} */
109
110 /* {{{ void http_cookie_list_add_extr(http_cookie_list *, const char *, size_t, const char *, size_t) */
111 PHP_HTTP_API void _http_cookie_list_add_extra(http_cookie_list *list, const char *name, size_t name_len, const char *value, size_t value_len TSRMLS_DC)
112 {
113 zval *cookie_value;
114 char *key = estrndup(name, name_len);
115 MAKE_STD_ZVAL(cookie_value);
116 ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0);
117 zend_hash_update(&list->extras, key, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
118 efree(key);
119 }
120 /* }}} */
121
122 typedef struct _http_parse_param_cb_arg_t {
123 http_cookie_list *list;
124 long flags;
125 char **allowed_extras;
126 } http_parse_param_cb_arg;
127
128 /* {{{ static void http_parse_cookie_callback */
129 static void http_parse_cookie_callback(void *ptr, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
130 {
131 http_parse_param_cb_arg *arg = (http_parse_param_cb_arg *) ptr;
132
133 #define _KEY_IS(s) (keylen == lenof(s) && !strncasecmp(key, (s), keylen))
134 if _KEY_IS("path") {
135 STR_SET(arg->list->path, estrndup(val, vallen));
136 } else if _KEY_IS("domain") {
137 STR_SET(arg->list->domain, estrndup(val, vallen));
138 } else if _KEY_IS("expires") {
139 char *date = estrndup(val, vallen);
140 arg->list->expires = http_parse_date(date);
141 efree(date);
142 } else if _KEY_IS("secure") {
143 arg->list->flags |= HTTP_COOKIE_SECURE;
144 } else if _KEY_IS("httpOnly") {
145 arg->list->flags |= HTTP_COOKIE_HTTPONLY;
146 } else {
147 /* check for extra */
148 if (arg->allowed_extras) {
149 char **ae = arg->allowed_extras;
150
151 for (; *ae; ++ae) {
152 if ((size_t) keylen == strlen(*ae) && !strncasecmp(key, *ae, keylen)) {
153 if (arg->flags & HTTP_COOKIE_PARSE_RAW) {
154 http_cookie_list_add_extra(arg->list, key, keylen, val, vallen);
155 } else {
156 char *dec = estrndup(val, vallen);
157 int declen = php_url_decode(dec, vallen);
158
159 http_cookie_list_add_extra(arg->list, key, keylen, dec, declen);
160 efree(dec);
161 }
162 return;
163 }
164 }
165 }
166 /* new cookie */
167 if (arg->flags & HTTP_COOKIE_PARSE_RAW) {
168 http_cookie_list_add_cookie(arg->list, key, keylen, val, vallen);
169 } else {
170 char *dec = estrndup(val, vallen);
171 int declen = php_url_decode(dec, vallen);
172
173 http_cookie_list_add_cookie(arg->list, key, keylen, dec, declen);
174 efree(dec);
175 }
176 }
177 }
178 /* }}} */
179
180 /* {{{ http_cookie_list *http_parse_cookie(char *, long) */
181 PHP_HTTP_API http_cookie_list *_http_parse_cookie_ex(http_cookie_list *list, const char *string, long flags, char **allowed_extras TSRMLS_DC)
182 {
183 int free_list = !list;
184 http_parse_param_cb_arg arg;
185
186 list = http_cookie_list_init(list);
187
188 arg.list = list;
189 arg.flags = flags;
190 arg.allowed_extras = allowed_extras;
191
192 if (SUCCESS != http_parse_params_ex(string, HTTP_PARAMS_RAISE_ERROR, http_parse_cookie_callback, &arg)) {
193 if (free_list) {
194 http_cookie_list_free(&list);
195 } else {
196 http_cookie_list_dtor(list);
197 }
198 list = NULL;
199 }
200
201 return list;
202 }
203 /* }}} */
204
205 /* {{{ void http_cookie_list_tostruct(http_cookie_list *, zval *) */
206 PHP_HTTP_API void _http_cookie_list_tostruct(http_cookie_list *list, zval *strct TSRMLS_DC)
207 {
208 zval array, *cookies, *extras;
209
210 INIT_ZARR(array, HASH_OF(strct));
211
212 MAKE_STD_ZVAL(cookies);
213 array_init(cookies);
214 zend_hash_copy(Z_ARRVAL_P(cookies), &list->cookies, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
215 add_assoc_zval(&array, "cookies", cookies);
216
217 MAKE_STD_ZVAL(extras);
218 array_init(extras);
219 zend_hash_copy(Z_ARRVAL_P(extras), &list->extras, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
220 add_assoc_zval(&array, "extras", extras);
221
222 add_assoc_long(&array, "flags", list->flags);
223 add_assoc_long(&array, "expires", (long) list->expires);
224 add_assoc_string(&array, "path", STR_PTR(list->path), 1);
225 add_assoc_string(&array, "domain", STR_PTR(list->domain), 1);
226 }
227 /* }}} */
228
229 /* {{{ http_cookie_list *http_cookie_list_fromstruct(http_cookie_list *, zval *strct) */
230 PHP_HTTP_API http_cookie_list *_http_cookie_list_fromstruct(http_cookie_list *list, zval *strct TSRMLS_DC)
231 {
232 zval **tmp, *cpy;
233 HashTable *ht = HASH_OF(strct);
234
235 list = http_cookie_list_init(list);
236
237 if (SUCCESS == zend_hash_find(ht, "cookies", sizeof("cookies"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_ARRAY) {
238 zend_hash_copy(&list->cookies, Z_ARRVAL_PP(tmp), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
239 }
240 if (SUCCESS == zend_hash_find(ht, "extras", sizeof("extras"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_ARRAY) {
241 zend_hash_copy(&list->extras, Z_ARRVAL_PP(tmp), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
242 }
243 if (SUCCESS == zend_hash_find(ht, "flags", sizeof("flags"), (void *) &tmp)) {
244 switch (Z_TYPE_PP(tmp)) {
245 case IS_LONG:
246 list->flags = Z_LVAL_PP(tmp);
247 break;
248 case IS_DOUBLE:
249 list->flags = (long) Z_DVAL_PP(tmp);
250 break;
251 case IS_STRING:
252 cpy = http_zsep(IS_LONG, *tmp);
253 list->flags = Z_LVAL_P(cpy);
254 zval_ptr_dtor(&cpy);
255 break;
256 default:
257 break;
258 }
259 }
260 if (SUCCESS == zend_hash_find(ht, "expires", sizeof("expires"), (void *) &tmp)) {
261 switch (Z_TYPE_PP(tmp)) {
262 case IS_LONG:
263 list->expires = Z_LVAL_PP(tmp);
264 break;
265 case IS_DOUBLE:
266 list->expires = (long) Z_DVAL_PP(tmp);
267 break;
268 case IS_STRING:
269 cpy = http_zsep(IS_LONG, *tmp);
270 if (Z_LVAL_P(cpy)) {
271 list->expires = Z_LVAL_P(cpy);
272 } else {
273 time_t expires = http_parse_date(Z_STRVAL_PP(tmp));
274 if (expires > 0) {
275 list->expires = expires;
276 }
277 }
278 zval_ptr_dtor(&cpy);
279 break;
280 default:
281 break;
282 }
283 }
284 if (SUCCESS == zend_hash_find(ht, "path", sizeof("path"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_STRING) {
285 list->path = estrndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
286 }
287 if (SUCCESS == zend_hash_find(ht, "domain", sizeof("domain"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_STRING) {
288 list->domain = estrndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
289 }
290
291 return list;
292 }
293 /* }}} */
294
295 /* {{{ inline append_encoded */
296 static inline void append_encoded(phpstr *buf, const char *key, size_t key_len, const char *val, size_t val_len)
297 {
298 char *enc_str[2];
299 int enc_len[2];
300
301 enc_str[0] = php_url_encode(key, key_len, &enc_len[0]);
302 enc_str[1] = php_url_encode(val, val_len, &enc_len[1]);
303
304 phpstr_append(buf, enc_str[0], enc_len[0]);
305 phpstr_appends(buf, "=");
306 phpstr_append(buf, enc_str[1], enc_len[1]);
307 phpstr_appends(buf, "; ");
308
309 efree(enc_str[0]);
310 efree(enc_str[1]);
311 }
312 /* }}} */
313
314 /* {{{ void http_cookie_list_tostring(http_cookie_list *, char **, size_t *) */
315 PHP_HTTP_API void _http_cookie_list_tostring(http_cookie_list *list, char **str, size_t *len TSRMLS_DC)
316 {
317 phpstr buf;
318 zval **val;
319 HashKey key = initHashKey(0);
320 HashPosition pos;
321
322 phpstr_init(&buf);
323
324 FOREACH_HASH_KEYVAL(pos, &list->cookies, key, val) {
325 if (key.type == HASH_KEY_IS_STRING && key.len) {
326 append_encoded(&buf, key.str, key.len-1, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
327 }
328 }
329
330 if (list->domain && *list->domain) {
331 phpstr_appendf(&buf, "domain=%s; ", list->domain);
332 }
333 if (list->path && *list->path) {
334 phpstr_appendf(&buf, "path=%s; ", list->path);
335 }
336 if (list->expires) {
337 char *date = http_date(list->expires);
338 phpstr_appendf(&buf, "expires=%s; ", date);
339 efree(date);
340 }
341
342 FOREACH_HASH_KEYVAL(pos, &list->extras, key, val) {
343 if (key.type == HASH_KEY_IS_STRING && key.len) {
344 append_encoded(&buf, key.str, key.len-1, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
345 }
346 }
347
348 if (list->flags & HTTP_COOKIE_SECURE) {
349 phpstr_appends(&buf, "secure; ");
350 }
351 if (list->flags & HTTP_COOKIE_HTTPONLY) {
352 phpstr_appends(&buf, "httpOnly; ");
353 }
354
355 phpstr_fix(&buf);
356 *str = PHPSTR_VAL(&buf);
357 *len = PHPSTR_LEN(&buf);
358 }
359 /* }}} */
360
361 /*
362 * Local variables:
363 * tab-width: 4
364 * c-basic-offset: 4
365 * End:
366 * vim600: noet sw=4 ts=4 fdm=marker
367 * vim<600: noet sw=4 ts=4
368 */