simplify getCookies()
[m6w6/ext-http] / php_http_client_response.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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientResponse, method, 0, req_args)
16 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientResponse, method, 0)
17 #define PHP_HTTP_CLIENT_RESPONSE_ME(method, visibility) PHP_ME(HttpClientResponse, method, PHP_HTTP_ARGS(HttpClientResponse, method), visibility)
18 #define PHP_HTTP_CLIENT_RESPONSE_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientResponse, method))
19 #define PHP_HTTP_CLIENT_RESPONSE_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClientResponse_##al), PHP_HTTP_ARGS(HttpClientResponse, al), vis)
20
21 PHP_HTTP_BEGIN_ARGS(getCookies, 0)
22 PHP_HTTP_ARG_VAL(flags, 0)
23 PHP_HTTP_ARG_VAL(allowed_extras, 0)
24 PHP_HTTP_END_ARGS;
25
26 PHP_METHOD(HttpClientResponse, getCookies)
27 {
28 long flags = 0;
29 zval *allowed_extras_array = NULL;
30
31 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la!", &flags, &allowed_extras_array)) {
32 int i = 0;
33 char **allowed_extras = NULL;
34 zval *header = NULL, **entry = NULL;
35 HashPosition pos;
36 php_http_message_object_t *msg = zend_object_store_get_object(getThis() TSRMLS_CC);
37
38
39 array_init(return_value);
40
41 if (allowed_extras_array) {
42 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
43 FOREACH_VAL(pos, allowed_extras_array, entry) {
44 zval *data = php_http_ztyp(IS_STRING, *entry);
45 allowed_extras[i++] = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
46 zval_ptr_dtor(&data);
47 }
48 }
49
50 if ((header = php_http_message_header(msg->message, ZEND_STRL("Set-Cookie"), 0))) {
51 php_http_cookie_list_t *list;
52
53 if (Z_TYPE_P(header) == IS_ARRAY) {
54 zval **single_header;
55
56 FOREACH_VAL(pos, header, single_header) {
57 zval *data = php_http_ztyp(IS_STRING, *single_header);
58
59 if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) {
60 zval *cookie;
61
62 MAKE_STD_ZVAL(cookie);
63 ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_get_class_entry(), list, NULL TSRMLS_CC), 0);
64 add_next_index_zval(return_value, cookie);
65 }
66 zval_ptr_dtor(&data);
67 }
68 } else {
69 zval *data = php_http_ztyp(IS_STRING, header);
70 if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) {
71 zval *cookie;
72
73 MAKE_STD_ZVAL(cookie);
74 ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_get_class_entry(), list, NULL TSRMLS_CC), 0);
75 add_next_index_zval(return_value, cookie);
76 }
77 zval_ptr_dtor(&data);
78 }
79 zval_ptr_dtor(&header);
80 }
81
82 if (allowed_extras) {
83 for (i = 0; allowed_extras[i]; ++i) {
84 efree(allowed_extras[i]);
85 }
86 efree(allowed_extras);
87 }
88
89 return;
90 }
91 RETURN_FALSE;
92 }
93
94
95 static zend_class_entry *php_http_client_response_class_entry;
96
97 zend_class_entry *php_http_client_response_get_class_entry(void)
98 {
99 return php_http_client_response_class_entry;
100 }
101
102 static zend_function_entry php_http_client_response_method_entry[] = {
103 PHP_HTTP_CLIENT_RESPONSE_ME(getCookies, ZEND_ACC_PUBLIC)
104 EMPTY_FUNCTION_ENTRY
105 };
106
107 PHP_MINIT_FUNCTION(http_client_response)
108 {
109 PHP_HTTP_REGISTER_CLASS(http\\Client, Response, http_client_response, php_http_message_get_class_entry(), 0);
110
111 return SUCCESS;
112 }
113
114 /*
115 * Local variables:
116 * tab-width: 4
117 * c-basic-offset: 4
118 * End:
119 * vim600: noet sw=4 ts=4 fdm=marker
120 * vim<600: noet sw=4 ts=4
121 */
122