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