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