- fix behaviour of http_build_url() when second parameter is NULL
[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-2006, 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 PHP_MINIT_FUNCTION(http_cookie)
21 {
22 HTTP_LONG_CONSTANT("HTTP_COOKIE_PARSE_RAW", HTTP_COOKIE_PARSE_RAW);
23 HTTP_LONG_CONSTANT("HTTP_COOKIE_SECURE", HTTP_COOKIE_SECURE);
24 HTTP_LONG_CONSTANT("HTTP_COOKIE_HTTPONLY", HTTP_COOKIE_HTTPONLY);
25
26 return SUCCESS;
27 }
28
29 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)
30 {
31 if (!list) {
32 list = emalloc_rel(sizeof(http_cookie_list));
33 }
34
35 zend_hash_init(&list->cookies, 0, NULL, ZVAL_PTR_DTOR, 0);
36 zend_hash_init(&list->extras, 0, NULL, ZVAL_PTR_DTOR, 0);
37
38 list->path = NULL;
39 list->domain = NULL;
40 list->expires = 0;
41 list->flags = 0;
42
43 return list;
44 }
45
46 PHP_HTTP_API void _http_cookie_list_dtor(http_cookie_list *list TSRMLS_DC)
47 {
48 if (list) {
49 zend_hash_destroy(&list->cookies);
50 zend_hash_destroy(&list->extras);
51
52 STR_SET(list->path, NULL);
53 STR_SET(list->domain, NULL);
54 }
55 }
56
57 PHP_HTTP_API void _http_cookie_list_free(http_cookie_list **list TSRMLS_DC)
58 {
59 if (list) {
60 http_cookie_list_dtor(*list);
61 efree(*list);
62 *list = NULL;
63 }
64 }
65
66 PHP_HTTP_API const char *_http_cookie_list_get_cookie(http_cookie_list *list, const char *name, size_t name_len TSRMLS_DC)
67 {
68 zval **cookie = NULL;
69 if ((SUCCESS != zend_hash_find(&list->cookies, (char *) name, name_len + 1, (void *) &cookie)) || (Z_TYPE_PP(cookie) != IS_STRING)) {
70 return NULL;
71 }
72 return Z_STRVAL_PP(cookie);
73 }
74
75 PHP_HTTP_API const char *_http_cookie_list_get_extra(http_cookie_list *list, const char *name, size_t name_len TSRMLS_DC)
76 {
77 zval **extra = NULL;
78 if ((SUCCESS != zend_hash_find(&list->extras, (char *) name, name_len + 1, (void *) &extra)) || (Z_TYPE_PP(extra) != IS_STRING)) {
79 return NULL;
80 }
81 return Z_STRVAL_PP(extra);
82 }
83
84 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)
85 {
86 zval *cookie_value;
87 char *key = estrndup(name, name_len);
88 MAKE_STD_ZVAL(cookie_value);
89 ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0);
90 zend_hash_update(&list->cookies, key, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
91 efree(key);
92 }
93
94 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)
95 {
96 zval *cookie_value;
97 char *key = estrndup(name, name_len);
98 MAKE_STD_ZVAL(cookie_value);
99 ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0);
100 zend_hash_update(&list->extras, key, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
101 efree(key);
102 }
103
104 typedef struct _http_parse_param_cb_arg_t {
105 http_cookie_list *list;
106 long flags;
107 char **allowed_extras;
108 } http_parse_param_cb_arg;
109
110 static void http_parse_cookie_callback(void *ptr, const char *key, int keylen, const char *val, int vallen TSRMLS_DC)
111 {
112 http_parse_param_cb_arg *arg = (http_parse_param_cb_arg *) ptr;
113
114 #define _KEY_IS(s) (keylen == lenof(s) && !strncasecmp(key, (s), keylen))
115 if _KEY_IS("path") {
116 STR_SET(arg->list->path, estrndup(val, vallen));
117 } else if _KEY_IS("domain") {
118 STR_SET(arg->list->domain, estrndup(val, vallen));
119 } else if _KEY_IS("expires") {
120 char *date = estrndup(val, vallen);
121 arg->list->expires = http_parse_date(date);
122 efree(date);
123 } else if _KEY_IS("secure") {
124 arg->list->flags |= HTTP_COOKIE_SECURE;
125 } else if _KEY_IS("httpOnly") {
126 arg->list->flags |= HTTP_COOKIE_HTTPONLY;
127 } else {
128 /* check for extra */
129 if (arg->allowed_extras) {
130 char **ae = arg->allowed_extras;
131
132 for (; *ae; ++ae) {
133 if ((size_t) keylen == strlen(*ae) && !strncasecmp(key, *ae, keylen)) {
134 http_cookie_list_add_extra(arg->list, key, keylen, val, vallen);
135 return;
136 }
137 }
138 }
139 /* new cookie */
140 http_cookie_list_add_cookie(arg->list, key, keylen, val, vallen);
141 }
142 }
143
144 /* {{{ http_cookie_list *http_parse_cookie(char *, long) */
145 PHP_HTTP_API http_cookie_list *_http_parse_cookie_ex(http_cookie_list *list, const char *string, long flags, char **allowed_extras TSRMLS_DC)
146 {
147 int free_list = !list;
148 http_parse_param_cb_arg arg;
149
150 list = http_cookie_list_init(list);
151
152 arg.list = list;
153 arg.flags = flags;
154 arg.allowed_extras = allowed_extras;
155
156 if (SUCCESS != http_parse_params_ex(string, 0, http_parse_cookie_callback, &arg)) {
157 if (free_list) {
158 http_cookie_list_free(&list);
159 } else {
160 http_cookie_list_dtor(list);
161 }
162 list = NULL;
163 }
164
165 return list;
166 }
167 /* }}} */
168
169 PHP_HTTP_API void _http_cookie_list_tostruct(http_cookie_list *list, zval *strct TSRMLS_DC)
170 {
171 zval array, *cookies, *extras;
172
173 INIT_ZARR(array, HASH_OF(strct));
174
175 MAKE_STD_ZVAL(cookies);
176 array_init(cookies);
177 zend_hash_copy(Z_ARRVAL_P(cookies), &list->cookies, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
178 add_assoc_zval(&array, "cookies", cookies);
179
180 MAKE_STD_ZVAL(extras);
181 array_init(extras);
182 zend_hash_copy(Z_ARRVAL_P(extras), &list->extras, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
183 add_assoc_zval(&array, "extras", extras);
184
185 add_assoc_long(&array, "flags", list->flags);
186 add_assoc_long(&array, "expires", (long) list->expires);
187 add_assoc_string(&array, "path", list->path?list->path:"", 1);
188 add_assoc_string(&array, "domain", list->domain?list->domain:"", 1);
189 }
190
191 /*
192 * Local variables:
193 * tab-width: 4
194 * c-basic-offset: 4
195 * End:
196 * vim600: noet sw=4 ts=4 fdm=marker
197 * vim<600: noet sw=4 ts=4
198 */