2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 static zend_class_entry
*php_http_client_response_class_entry
;
16 zend_class_entry
*php_http_get_client_response_class_entry(void)
18 return php_http_client_response_class_entry
;
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
)
25 static PHP_METHOD(HttpClientResponse
, getCookies
)
28 zval
*allowed_extras_array
= NULL
;
30 char **allowed_extras
= NULL
;
31 zval
*header
= NULL
, *entry
= NULL
;
32 php_http_message_object_t
*msg
;
34 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "|la!/", &flags
, &allowed_extras_array
)) {
38 msg
= PHP_HTTP_OBJ(NULL
, getThis());
39 array_init(return_value
);
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
)
46 zend_string
*zs
= zval_get_string(entry
);
47 allowed_extras
[i
++] = estrndup(zs
->val
, zs
->len
);
48 zend_string_release(zs
);
50 ZEND_HASH_FOREACH_END();
53 if ((header
= php_http_message_header(msg
->message
, ZEND_STRL("Set-Cookie")))) {
54 php_http_cookie_list_t
*list
;
56 if (Z_TYPE_P(header
) == IS_ARRAY
) {
59 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(header
), single_header
)
61 zend_string
*zs
= zval_get_string(single_header
);
63 if ((list
= php_http_cookie_list_parse(NULL
, zs
->val
, zs
->len
, flags
, allowed_extras
))) {
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
);
69 zend_string_release(zs
);
71 ZEND_HASH_FOREACH_END();
73 zend_string
*zs
= zval_get_string(header
);
75 if ((list
= php_http_cookie_list_parse(NULL
, zs
->val
, zs
->len
, flags
, allowed_extras
))) {
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
);
81 zend_string_release(zs
);
86 for (i
= 0; allowed_extras
[i
]; ++i
) {
87 efree(allowed_extras
[i
]);
89 efree(allowed_extras
);
93 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientResponse_getTransferInfo
, 0, 0, 0)
94 ZEND_ARG_INFO(0, element
)
96 static PHP_METHOD(HttpClientResponse
, getTransferInfo
)
98 char *info_name
= NULL
;
100 zval info_tmp
, info_name_tmp
, *info
;
102 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &info_name
, &info_len
), invalid_arg
, return);
104 info
= zend_read_property(php_http_client_response_class_entry
, getThis(), ZEND_STRL("transferInfo"), 0, &info_tmp
);
106 /* request completed? */
107 if (Z_TYPE_P(info
) != IS_OBJECT
) {
108 php_http_throw(bad_method_call
, "Incomplete state");
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
);
116 php_http_throw(unexpected_val
, "Could not find transfer info with name '%s'", info_name
);
121 RETURN_ZVAL(info
, 1, 0);
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
)
130 PHP_MINIT_FUNCTION(http_client_response
)
132 zend_class_entry ce
= {0};
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());
137 zend_declare_property_null(php_http_client_response_class_entry
, ZEND_STRL("transferInfo"), ZEND_ACC_PROTECTED
);
147 * vim600: noet sw=4 ts=4 fdm=marker
148 * vim<600: noet sw=4 ts=4