flush WIP
[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 zval_ptr_dtor(header);
78 }
79
80 if (allowed_extras) {
81 for (i = 0; allowed_extras[i]; ++i) {
82 efree(allowed_extras[i]);
83 }
84 efree(allowed_extras);
85 }
86 }
87
88 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientResponse_getTransferInfo, 0, 0, 0)
89 ZEND_ARG_INFO(0, element)
90 ZEND_END_ARG_INFO();
91 static PHP_METHOD(HttpClientResponse, getTransferInfo)
92 {
93 char *info_name = NULL;
94 size_t info_len = 0;
95 zval *info;
96
97 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &info_name, &info_len), invalid_arg, return);
98
99 info = zend_read_property(php_http_client_response_class_entry, getThis(), ZEND_STRL("transferInfo"), 0);
100
101 /* request completed? */
102 if (Z_TYPE_P(info) != IS_OBJECT) {
103 php_http_throw(bad_method_call, "Incomplete state", NULL);
104 return;
105 }
106
107 if (info_len && info_name) {
108 info = zend_read_property(NULL, info, php_http_pretty_key(info_name, info_len, 0, 0), info_len, 0);
109
110 if (!info) {
111 php_http_throw(unexpected_val, "Could not find transfer info with name '%s'", info_name);
112 return;
113 }
114 }
115
116 RETURN_ZVAL_FAST(info);
117 }
118
119 static zend_function_entry php_http_client_response_methods[] = {
120 PHP_ME(HttpClientResponse, getCookies, ai_HttpClientResponse_getCookies, ZEND_ACC_PUBLIC)
121 PHP_ME(HttpClientResponse, getTransferInfo, ai_HttpClientResponse_getTransferInfo, ZEND_ACC_PUBLIC)
122 EMPTY_FUNCTION_ENTRY
123 };
124
125 zend_class_entry *php_http_client_response_class_entry;
126
127 PHP_MINIT_FUNCTION(http_client_response)
128 {
129 zend_class_entry ce = {0};
130
131 INIT_NS_CLASS_ENTRY(ce, "http\\Client", "Response", php_http_client_response_methods);
132 php_http_client_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry);
133
134 zend_declare_property_null(php_http_client_response_class_entry, ZEND_STRL("transferInfo"), ZEND_ACC_PROTECTED);
135
136 return SUCCESS;
137 }
138
139 /*
140 * Local variables:
141 * tab-width: 4
142 * c-basic-offset: 4
143 * End:
144 * vim600: noet sw=4 ts=4 fdm=marker
145 * vim<600: noet sw=4 ts=4
146 */
147