lingering TSRM macros
[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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientResponse_getCookies, 0, 0, 0)
16 ZEND_ARG_INFO(0, flags)
17 ZEND_ARG_INFO(0, allowed_extras)
18 ZEND_END_ARG_INFO();
19 static PHP_METHOD(HttpClientResponse, getCookies)
20 {
21 zend_long flags = 0;
22 zval *allowed_extras_array = NULL;
23 int i = 0;
24 char **allowed_extras = NULL;
25 zval *header = NULL, *entry = NULL;
26 php_http_message_object_t *msg;
27
28 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|la!/", &flags, &allowed_extras_array)) {
29 return;
30 }
31
32 msg = PHP_HTTP_OBJ(NULL, getThis());
33 array_init(return_value);
34
35 if (allowed_extras_array) {
36 /* FIXME: use zend_string** instead of char** */
37 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
38 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(allowed_extras_array), entry)
39 {
40 zend_string *zs = zval_get_string(entry);
41 allowed_extras[i++] = estrndup(zs->val, zs->len);
42 zend_string_release(zs);
43 }
44 ZEND_HASH_FOREACH_END();
45 }
46
47 if ((header = php_http_message_header(msg->message, ZEND_STRL("Set-Cookie")))) {
48 php_http_cookie_list_t *list;
49
50 if (Z_TYPE_P(header) == IS_ARRAY) {
51 zval *single_header;
52
53 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(header), single_header)
54 {
55 zend_string *zs = zval_get_string(single_header);
56
57 if ((list = php_http_cookie_list_parse(NULL, zs->val, zs->len, flags, allowed_extras))) {
58 zval cookie;
59
60 ZVAL_OBJ(&cookie, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo);
61 add_next_index_zval(return_value, &cookie);
62 }
63 zend_string_release(zs);
64 }
65 ZEND_HASH_FOREACH_END();
66 } else {
67 zend_string *zs = zval_get_string(header);
68
69 if ((list = php_http_cookie_list_parse(NULL, zs->val, zs->len, flags, allowed_extras))) {
70 zval cookie;
71
72 ZVAL_OBJ(&cookie, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo);
73 add_next_index_zval(return_value, &cookie);
74 }
75 zend_string_release(zs);
76 }
77 }
78
79 if (allowed_extras) {
80 for (i = 0; allowed_extras[i]; ++i) {
81 efree(allowed_extras[i]);
82 }
83 efree(allowed_extras);
84 }
85 }
86
87 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientResponse_getTransferInfo, 0, 0, 0)
88 ZEND_ARG_INFO(0, element)
89 ZEND_END_ARG_INFO();
90 static PHP_METHOD(HttpClientResponse, getTransferInfo)
91 {
92 char *info_name = NULL;
93 size_t info_len = 0;
94 zval info_tmp, info_name_tmp, *info;
95
96 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &info_name, &info_len), invalid_arg, return);
97
98 info = zend_read_property(php_http_client_response_class_entry, getThis(), ZEND_STRL("transferInfo"), 0, &info_tmp);
99
100 /* request completed? */
101 if (Z_TYPE_P(info) != IS_OBJECT) {
102 php_http_throw(bad_method_call, "Incomplete state", NULL);
103 return;
104 }
105
106 if (info_len && info_name) {
107 info = zend_read_property(NULL, info, php_http_pretty_key(info_name, info_len, 0, 0), info_len, 0, &info_name_tmp);
108
109 if (!info) {
110 php_http_throw(unexpected_val, "Could not find transfer info with name '%s'", info_name);
111 return;
112 }
113 }
114
115 RETURN_ZVAL(info, 1, 0);
116 }
117
118 static zend_function_entry php_http_client_response_methods[] = {
119 PHP_ME(HttpClientResponse, getCookies, ai_HttpClientResponse_getCookies, ZEND_ACC_PUBLIC)
120 PHP_ME(HttpClientResponse, getTransferInfo, ai_HttpClientResponse_getTransferInfo, ZEND_ACC_PUBLIC)
121 EMPTY_FUNCTION_ENTRY
122 };
123
124 zend_class_entry *php_http_client_response_class_entry;
125
126 PHP_MINIT_FUNCTION(http_client_response)
127 {
128 zend_class_entry ce = {0};
129
130 INIT_NS_CLASS_ENTRY(ce, "http\\Client", "Response", php_http_client_response_methods);
131 php_http_client_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry);
132
133 zend_declare_property_null(php_http_client_response_class_entry, ZEND_STRL("transferInfo"), ZEND_ACC_PROTECTED);
134
135 return SUCCESS;
136 }
137
138 /*
139 * Local variables:
140 * tab-width: 4
141 * c-basic-offset: 4
142 * End:
143 * vim600: noet sw=4 ts=4 fdm=marker
144 * vim<600: noet sw=4 ts=4
145 */
146