* standard return values
[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-2013, 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 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 HashPosition pos;
27 php_http_message_object_t *msg;
28
29 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la!", &flags, &allowed_extras_array)) {
30 return;
31 }
32
33 msg = zend_object_store_get_object(getThis() TSRMLS_CC);
34 array_init(return_value);
35
36 if (allowed_extras_array) {
37 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
38 FOREACH_VAL(pos, allowed_extras_array, entry) {
39 zval *data = php_http_ztyp(IS_STRING, *entry);
40 allowed_extras[i++] = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
41 zval_ptr_dtor(&data);
42 }
43 }
44
45 if ((header = php_http_message_header(msg->message, ZEND_STRL("Set-Cookie"), 0))) {
46 php_http_cookie_list_t *list;
47
48 if (Z_TYPE_P(header) == IS_ARRAY) {
49 zval **single_header;
50
51 FOREACH_VAL(pos, header, single_header) {
52 zval *data = php_http_ztyp(IS_STRING, *single_header);
53
54 if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) {
55 zval *cookie;
56
57 MAKE_STD_ZVAL(cookie);
58 ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0);
59 add_next_index_zval(return_value, cookie);
60 }
61 zval_ptr_dtor(&data);
62 }
63 } else {
64 zval *data = php_http_ztyp(IS_STRING, header);
65 if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) {
66 zval *cookie;
67
68 MAKE_STD_ZVAL(cookie);
69 ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0);
70 add_next_index_zval(return_value, cookie);
71 }
72 zval_ptr_dtor(&data);
73 }
74 zval_ptr_dtor(&header);
75 }
76
77 if (allowed_extras) {
78 for (i = 0; allowed_extras[i]; ++i) {
79 efree(allowed_extras[i]);
80 }
81 efree(allowed_extras);
82 }
83 }
84
85 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientResponse_getTransferInfo, 0, 0, 0)
86 ZEND_ARG_INFO(0, element)
87 ZEND_END_ARG_INFO();
88 static PHP_METHOD(HttpClientResponse, getTransferInfo)
89 {
90 char *info_name = NULL;
91 int info_len = 0;
92 zval *infop, *info;
93
94 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
95 return;
96 }
97
98 info = zend_read_property(php_http_client_response_class_entry, getThis(), ZEND_STRL("transferInfo"), 0 TSRMLS_CC);
99
100 /* request completed? */
101 if (Z_TYPE_P(info) == IS_OBJECT) {
102 if (info_len && info_name) {
103 infop = zend_read_property(NULL, info, php_http_pretty_key(info_name, info_len, 0, 0), info_len, 0 TSRMLS_CC);
104
105 if (infop) {
106 RETURN_ZVAL(infop, 1, 0);
107 } else {
108 php_http_error(HE_NOTICE, PHP_HTTP_E_INVALID_PARAM, "Could not find transfer info named %s", info_name);
109 }
110 } else {
111 RETURN_ZVAL(info, 1, 0);
112 }
113 }
114 RETURN_FALSE;
115 }
116
117 static zend_function_entry php_http_client_response_methods[] = {
118 PHP_ME(HttpClientResponse, getCookies, ai_HttpClientResponse_getCookies, ZEND_ACC_PUBLIC)
119 PHP_ME(HttpClientResponse, getTransferInfo, ai_HttpClientResponse_getTransferInfo, ZEND_ACC_PUBLIC)
120 EMPTY_FUNCTION_ENTRY
121 };
122
123 zend_class_entry *php_http_client_response_class_entry;
124
125 PHP_MINIT_FUNCTION(http_client_response)
126 {
127 zend_class_entry ce = {0};
128
129 INIT_NS_CLASS_ENTRY(ce, "http\\Client", "Response", php_http_client_response_methods);
130 php_http_client_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
131
132 return SUCCESS;
133 }
134
135 /*
136 * Local variables:
137 * tab-width: 4
138 * c-basic-offset: 4
139 * End:
140 * vim600: noet sw=4 ts=4 fdm=marker
141 * vim<600: noet sw=4 ts=4
142 */
143