2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
16 #include "php_http_api.h"
17 #include "php_http_date_api.h"
18 #include "php_http_cookie_api.h"
20 PHP_MINIT_FUNCTION(http_cookie
)
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
);
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
)
32 list
= emalloc_rel(sizeof(http_cookie_list
));
35 zend_hash_init(&list
->cookies
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
36 zend_hash_init(&list
->extras
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
46 PHP_HTTP_API
void _http_cookie_list_dtor(http_cookie_list
*list TSRMLS_DC
)
49 zend_hash_destroy(&list
->cookies
);
50 zend_hash_destroy(&list
->extras
);
52 STR_SET(list
->path
, NULL
);
53 STR_SET(list
->domain
, NULL
);
57 PHP_HTTP_API
void _http_cookie_list_free(http_cookie_list
**list TSRMLS_DC
)
60 http_cookie_list_dtor(*list
);
66 PHP_HTTP_API
const char *_http_cookie_list_get_cookie(http_cookie_list
*list
, const char *name
, size_t name_len TSRMLS_DC
)
69 if ((SUCCESS
!= zend_hash_find(&list
->cookies
, (char *) name
, name_len
+ 1, (void *) &cookie
)) || (Z_TYPE_PP(cookie
) != IS_STRING
)) {
72 return Z_STRVAL_PP(cookie
);
75 PHP_HTTP_API
const char *_http_cookie_list_get_extra(http_cookie_list
*list
, const char *name
, size_t name_len TSRMLS_DC
)
78 if ((SUCCESS
!= zend_hash_find(&list
->extras
, (char *) name
, name_len
+ 1, (void *) &extra
)) || (Z_TYPE_PP(extra
) != IS_STRING
)) {
81 return Z_STRVAL_PP(extra
);
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
)
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
);
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
)
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
);
104 typedef struct _http_parse_param_cb_arg_t
{
105 http_cookie_list
*list
;
107 char **allowed_extras
;
108 } http_parse_param_cb_arg
;
110 static void http_parse_cookie_callback(void *ptr
, const char *key
, int keylen
, const char *val
, int vallen TSRMLS_DC
)
112 http_parse_param_cb_arg
*arg
= (http_parse_param_cb_arg
*) ptr
;
114 #define _KEY_IS(s) (keylen == lenof(s) && !strncasecmp(key, (s), keylen))
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
);
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
;
128 /* check for extra */
129 if (arg
->allowed_extras
) {
130 char **ae
= arg
->allowed_extras
;
133 if ((size_t) keylen
== strlen(*ae
) && !strncasecmp(key
, *ae
, keylen
)) {
134 http_cookie_list_add_extra(arg
->list
, key
, keylen
, val
, vallen
);
140 http_cookie_list_add_cookie(arg
->list
, key
, keylen
, val
, vallen
);
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
)
147 int free_list
= !list
;
148 http_parse_param_cb_arg arg
;
150 list
= http_cookie_list_init(list
);
154 arg
.allowed_extras
= allowed_extras
;
156 if (SUCCESS
!= http_parse_params_ex(string
, 0, http_parse_cookie_callback
, &arg
)) {
158 http_cookie_list_free(&list
);
160 http_cookie_list_dtor(list
);
169 PHP_HTTP_API
void _http_cookie_list_tostruct(http_cookie_list
*list
, zval
*strct TSRMLS_DC
)
171 zval array
, *cookies
, *extras
;
173 INIT_ZARR(array
, HASH_OF(strct
));
175 MAKE_STD_ZVAL(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
);
180 MAKE_STD_ZVAL(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
);
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);
196 * vim600: noet sw=4 ts=4 fdm=marker
197 * vim<600: noet sw=4 ts=4