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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #include <ext/spl/spl_observer.h>
17 PHP_HTTP_API php_http_client_t
*php_http_client_init(php_http_client_t
*h
, php_http_client_ops_t
*ops
, php_http_resource_factory_t
*rf
, void *init_arg TSRMLS_DC
)
19 php_http_client_t
*free_h
= NULL
;
22 free_h
= h
= emalloc(sizeof(*h
));
24 memset(h
, 0, sizeof(*h
));
29 } else if (ops
->rsrc
) {
30 h
->rf
= php_http_resource_factory_init(NULL
, h
->ops
->rsrc
, h
, NULL
);
32 h
->request
.buffer
= php_http_buffer_init(NULL
);
33 h
->request
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
34 h
->request
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
36 h
->response
.buffer
= php_http_buffer_init(NULL
);
37 h
->response
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
38 h
->response
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
40 TSRMLS_SET_CTX(h
->ts
);
43 if (!(h
= h
->ops
->init(h
, init_arg
))) {
44 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Could not initialize request");
47 php_http_client_free(&free_h
);
55 PHP_HTTP_API
void php_http_client_dtor(php_http_client_t
*h
)
61 php_http_resource_factory_free(&h
->rf
);
63 php_http_message_parser_free(&h
->request
.parser
);
64 php_http_message_free(&h
->request
.message
);
65 php_http_buffer_free(&h
->request
.buffer
);
67 php_http_message_parser_free(&h
->response
.parser
);
68 php_http_message_free(&h
->response
.message
);
69 php_http_buffer_free(&h
->response
.buffer
);
72 PHP_HTTP_API
void php_http_client_free(php_http_client_t
**h
)
75 php_http_client_dtor(*h
);
81 PHP_HTTP_API php_http_client_t
*php_http_client_copy(php_http_client_t
*from
, php_http_client_t
*to
)
83 if (!from
->ops
->copy
) {
86 TSRMLS_FETCH_FROM_CTX(from
->ts
);
89 to
= ecalloc(1, sizeof(*to
));
94 php_http_resource_factory_addref(from
->rf
);
96 } else if (to
->ops
->rsrc
){
97 to
->rf
= php_http_resource_factory_init(NULL
, to
->ops
->rsrc
, to
, NULL
);
100 to
->request
.buffer
= php_http_buffer_init(NULL
);
101 to
->request
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
102 to
->request
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
104 to
->response
.buffer
= php_http_buffer_init(NULL
);
105 to
->response
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
106 to
->response
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
108 TSRMLS_SET_CTX(to
->ts
);
110 return to
->ops
->copy(from
, to
);
114 PHP_HTTP_API STATUS
php_http_client_exec(php_http_client_t
*h
, php_http_message_t
*msg
)
117 return h
->ops
->exec(h
, msg
);
122 PHP_HTTP_API STATUS
php_http_client_reset(php_http_client_t
*h
)
125 return h
->ops
->reset(h
);
130 PHP_HTTP_API STATUS
php_http_client_setopt(php_http_client_t
*h
, php_http_client_setopt_opt_t opt
, void *arg
)
132 if (h
->ops
->setopt
) {
133 return h
->ops
->setopt(h
, opt
, arg
);
138 PHP_HTTP_API STATUS
php_http_client_getopt(php_http_client_t
*h
, php_http_client_getopt_opt_t opt
, void *arg
)
140 if (h
->ops
->getopt
) {
141 return h
->ops
->getopt(h
, opt
, arg
);
146 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClient, method, 0, req_args)
147 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClient, method, 0)
148 #define PHP_HTTP_CLIENT_ME(method, visibility) PHP_ME(HttpClient, method, PHP_HTTP_ARGS(HttpClient, method), visibility)
149 #define PHP_HTTP_CLIENT_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClient, method))
150 #define PHP_HTTP_CLIENT_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClient_##al), PHP_HTTP_ARGS(HttpClient, al), vis)
152 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
153 PHP_HTTP_ARG_ARR(options
, 1, 0)
156 PHP_HTTP_EMPTY_ARGS(getOptions
);
157 PHP_HTTP_BEGIN_ARGS(setOptions
, 0)
158 PHP_HTTP_ARG_ARR(options
, 1, 0)
161 PHP_HTTP_EMPTY_ARGS(getSslOptions
);
162 PHP_HTTP_BEGIN_ARGS(setSslOptions
, 0)
163 PHP_HTTP_ARG_ARR(ssl_options
, 1, 0)
166 PHP_HTTP_BEGIN_ARGS(addSslOptions
, 0)
167 PHP_HTTP_ARG_ARR(ssl_options
, 1, 0)
170 PHP_HTTP_EMPTY_ARGS(getCookies
);
171 PHP_HTTP_BEGIN_ARGS(setCookies
, 0)
172 PHP_HTTP_ARG_VAL(cookies
, 0)
175 PHP_HTTP_BEGIN_ARGS(addCookies
, 1)
176 PHP_HTTP_ARG_VAL(cookies
, 0)
179 PHP_HTTP_EMPTY_ARGS(enableCookies
);
180 PHP_HTTP_BEGIN_ARGS(resetCookies
, 0)
181 PHP_HTTP_ARG_VAL(session_only
, 0)
183 PHP_HTTP_EMPTY_ARGS(flushCookies
);
185 PHP_HTTP_EMPTY_ARGS(getResponseMessageClass
);
186 PHP_HTTP_BEGIN_ARGS(setResponseMessageClass
, 1)
187 PHP_HTTP_ARG_VAL(message_class_name
, 0)
190 PHP_HTTP_EMPTY_ARGS(getResponseMessage
);
191 PHP_HTTP_EMPTY_ARGS(getRequestMessage
);
192 PHP_HTTP_EMPTY_ARGS(getHistory
);
193 PHP_HTTP_EMPTY_ARGS(clearHistory
);
195 PHP_HTTP_BEGIN_ARGS(setRequest
, 1)
196 PHP_HTTP_ARG_OBJ(http
\\Client
\\Request
, request
, 1)
198 PHP_HTTP_EMPTY_ARGS(getRequest
);
200 PHP_HTTP_EMPTY_ARGS(getObservers
);
201 PHP_HTTP_BEGIN_ARGS(attach
, 1)
202 PHP_HTTP_ARG_OBJ(SplObserver
, observer
, 0)
204 PHP_HTTP_BEGIN_ARGS(detach
, 1)
205 PHP_HTTP_ARG_OBJ(SplObserver
, observer
, 0)
207 PHP_HTTP_EMPTY_ARGS(notify
);
208 PHP_HTTP_EMPTY_ARGS(getProgress
);
209 PHP_HTTP_BEGIN_ARGS(getTransferInfo
, 0)
210 PHP_HTTP_ARG_VAL(name
, 0)
213 PHP_HTTP_BEGIN_ARGS(request
, 2)
214 PHP_HTTP_ARG_VAL(method
, 0)
215 PHP_HTTP_ARG_VAL(url
, 0)
216 PHP_HTTP_ARG_ARR(headers
, 1, 0)
217 PHP_HTTP_ARG_VAL(body
, 0)
218 PHP_HTTP_ARG_ARR(options
, 1, 0)
221 static zend_class_entry
*php_http_client_class_entry
;
223 zend_class_entry
*php_http_client_get_class_entry(void)
225 return php_http_client_class_entry
;
228 static zend_function_entry php_http_client_method_entry
[] = {
229 PHP_HTTP_CLIENT_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
230 PHP_HTTP_CLIENT_ME(getObservers
, ZEND_ACC_PUBLIC
)
231 PHP_HTTP_CLIENT_ME(notify
, ZEND_ACC_PUBLIC
)
232 PHP_HTTP_CLIENT_ME(attach
, ZEND_ACC_PUBLIC
)
233 PHP_HTTP_CLIENT_ME(detach
, ZEND_ACC_PUBLIC
)
234 PHP_HTTP_CLIENT_ME(getProgress
, ZEND_ACC_PUBLIC
)
235 PHP_HTTP_CLIENT_ME(getTransferInfo
, ZEND_ACC_PUBLIC
)
237 PHP_HTTP_CLIENT_ME(setOptions
, ZEND_ACC_PUBLIC
)
238 PHP_HTTP_CLIENT_ME(getOptions
, ZEND_ACC_PUBLIC
)
239 PHP_HTTP_CLIENT_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
240 PHP_HTTP_CLIENT_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
241 PHP_HTTP_CLIENT_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
243 PHP_HTTP_CLIENT_ME(addCookies
, ZEND_ACC_PUBLIC
)
244 PHP_HTTP_CLIENT_ME(getCookies
, ZEND_ACC_PUBLIC
)
245 PHP_HTTP_CLIENT_ME(setCookies
, ZEND_ACC_PUBLIC
)
247 PHP_HTTP_CLIENT_ME(enableCookies
, ZEND_ACC_PUBLIC
)
248 PHP_HTTP_CLIENT_ME(resetCookies
, ZEND_ACC_PUBLIC
)
249 PHP_HTTP_CLIENT_ME(flushCookies
, ZEND_ACC_PUBLIC
)
251 PHP_HTTP_CLIENT_ME(setRequest
, ZEND_ACC_PUBLIC
)
252 PHP_HTTP_CLIENT_ME(getRequest
, ZEND_ACC_PUBLIC
)
254 PHP_HTTP_CLIENT_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
255 PHP_HTTP_CLIENT_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
256 PHP_HTTP_CLIENT_ME(getHistory
, ZEND_ACC_PUBLIC
)
257 PHP_HTTP_CLIENT_ME(clearHistory
, ZEND_ACC_PUBLIC
)
259 PHP_HTTP_CLIENT_ME(getResponseMessageClass
, ZEND_ACC_PUBLIC
)
260 PHP_HTTP_CLIENT_ME(setResponseMessageClass
, ZEND_ACC_PUBLIC
)
262 PHP_HTTP_CLIENT_ME(request
, ZEND_ACC_PUBLIC
)
267 static zend_object_handlers php_http_client_object_handlers
;
269 zend_object_handlers
*php_http_client_get_object_handlers(void)
271 return &php_http_client_object_handlers
;
274 static php_http_client_ops_t php_http_client_user_ops
= {
283 (php_http_new_t
) php_http_client_object_new_ex
,
284 php_http_client_get_class_entry
287 zend_object_value
php_http_client_object_new(zend_class_entry
*ce TSRMLS_DC
)
289 return php_http_client_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
292 zend_object_value
php_http_client_object_new_ex(zend_class_entry
*ce
, php_http_client_t
*r
, php_http_client_object_t
**ptr TSRMLS_DC
)
294 zend_object_value ov
;
295 php_http_client_object_t
*o
;
297 o
= ecalloc(1, sizeof(*o
));
298 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
299 object_properties_init((zend_object
*) o
, ce
);
301 ov
.handle
= zend_objects_store_put(o
, NULL
, php_http_client_object_free
, NULL TSRMLS_CC
);
302 ov
.handlers
= &php_http_client_object_handlers
;
304 if (!(o
->client
= r
)) {
305 o
->client
= php_http_client_init(NULL
, &php_http_client_user_ops
, NULL
, &ov TSRMLS_CC
);
315 zend_object_value
php_http_client_object_clone(zval
*this_ptr TSRMLS_DC
)
317 zend_object_value new_ov
;
318 php_http_client_object_t
*new_obj
, *old_obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
320 new_ov
= php_http_client_object_new_ex(old_obj
->zo
.ce
, php_http_client_copy(old_obj
->client
, NULL
), &new_obj TSRMLS_CC
);
321 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
326 void php_http_client_object_free(void *object TSRMLS_DC
)
328 php_http_client_object_t
*o
= (php_http_client_object_t
*) object
;
330 php_http_client_free(&o
->client
);
331 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
335 static inline zend_object_value
php_http_client_object_message(zval
*this_ptr
, php_http_message_t
*msg TSRMLS_DC
)
337 zend_object_value ov
;
338 zval
*zcn
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("responseMessageClass"), 0 TSRMLS_CC
);
339 zend_class_entry
*class_entry
;
342 && (class_entry
= zend_fetch_class(Z_STRVAL_P(zcn
), Z_STRLEN_P(zcn
), 0 TSRMLS_CC
))
343 && (SUCCESS
== php_http_new(&ov
, class_entry
, (php_http_new_t
) php_http_message_object_new_ex
, php_http_client_response_get_class_entry(), msg
, NULL TSRMLS_CC
))) {
346 return php_http_message_object_new_ex(php_http_client_response_get_class_entry(), msg
, NULL TSRMLS_CC
);
350 STATUS
php_http_client_object_handle_request(zval
*zclient
, zval
**zreq TSRMLS_DC
)
352 php_http_client_object_t
*obj
= zend_object_store_get_object(zclient TSRMLS_CC
);
353 php_http_client_progress_t
*progress
;
356 /* do we have a valid request? */
358 /* remember the request */
359 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("request"), *zreq TSRMLS_CC
);
361 /* maybe a request is already set */
362 *zreq
= zend_read_property(php_http_client_class_entry
, zclient
, ZEND_STRL("request"), 0 TSRMLS_CC
);
364 if (Z_TYPE_PP(zreq
) != IS_OBJECT
|| !instanceof_function(Z_OBJCE_PP(zreq
), php_http_client_request_get_class_entry() TSRMLS_CC
)) {
365 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "The client does not have a valid request set");
370 /* reset request handle */
371 php_http_client_reset(obj
->client
);
373 /* reset transfer info */
374 zend_update_property_null(php_http_client_class_entry
, zclient
, ZEND_STRL("transferInfo") TSRMLS_CC
);
376 /* set client options */
377 zoptions
= zend_read_property(php_http_client_class_entry
, zclient
, ZEND_STRL("options"), 0 TSRMLS_CC
);
378 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_SETTINGS
, Z_ARRVAL_P(zoptions
));
379 /* set request options */
380 zoptions
= zend_read_property(php_http_client_request_get_class_entry(), *zreq
, ZEND_STRL("options"), 0 TSRMLS_CC
);
381 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_SETTINGS
, Z_ARRVAL_P(zoptions
));
383 /* set progress callback */
384 if (SUCCESS
== php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, &progress
)) {
385 if (!progress
->callback
) {
386 php_http_client_progress_callback_t
*callback
= emalloc(sizeof(*callback
));
388 callback
->type
= PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER
;
389 callback
->pass_state
= 0;
390 MAKE_STD_ZVAL(callback
->func
.user
);
391 array_init(callback
->func
.user
);
393 add_next_index_zval(callback
->func
.user
, zclient
);
394 add_next_index_stringl(callback
->func
.user
, ZEND_STRL("notify"), 1);
396 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK
, callback
);
398 progress
->state
.info
= "start";
399 php_http_client_progress_notify(progress TSRMLS_CC
);
400 progress
->state
.started
= 1;
407 STATUS
php_http_client_object_handle_response(zval
*zclient TSRMLS_DC
)
409 php_http_client_object_t
*obj
= zend_object_store_get_object(zclient TSRMLS_CC
);
410 php_http_client_progress_t
*progress
;
411 php_http_message_t
*msg
;
414 /* always fetch info */
417 php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
, Z_ARRVAL_P(info
));
418 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("transferInfo"), info TSRMLS_CC
);
419 zval_ptr_dtor(&info
);
421 if ((msg
= obj
->client
->response
.message
)) {
422 if (i_zend_is_true(zend_read_property(php_http_client_class_entry
, zclient
, ZEND_STRL("recordHistory"), 0 TSRMLS_CC
))) {
423 zval
*new_hist
, *old_hist
= zend_read_property(php_http_client_class_entry
, zclient
, ZEND_STRL("history"), 0 TSRMLS_CC
);
424 php_http_message_t
*zipped
= php_http_message_zip(obj
->client
->response
.message
, obj
->client
->request
.message
);
425 zend_object_value ov
= php_http_client_object_message(zclient
, zipped TSRMLS_CC
);
427 MAKE_STD_ZVAL(new_hist
);
428 ZVAL_OBJVAL(new_hist
, ov
, 0);
430 if (Z_TYPE_P(old_hist
) == IS_OBJECT
) {
431 php_http_message_object_prepend(new_hist
, old_hist
, 1 TSRMLS_CC
);
434 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("history"), new_hist TSRMLS_CC
);
435 zval_ptr_dtor(&new_hist
);
438 /* update response info */
439 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, msg
)) {
442 MAKE_STD_ZVAL(message
);
443 ZVAL_OBJVAL(message
, php_http_client_object_message(zclient
, msg TSRMLS_CC
), 0);
444 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("responseMessage"), message TSRMLS_CC
);
445 zval_ptr_dtor(&message
);
447 obj
->client
->response
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
449 zend_update_property_null(php_http_client_class_entry
, zclient
, ZEND_STRL("responseMessage") TSRMLS_CC
);
452 zend_update_property_null(php_http_client_class_entry
, zclient
, ZEND_STRL("responseMessage") TSRMLS_CC
);
455 if ((msg
= obj
->client
->request
.message
)) {
456 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, msg
)) {
459 /* update the actual request message */
460 MAKE_STD_ZVAL(message
);
461 ZVAL_OBJVAL(message
, php_http_message_object_new_ex(php_http_message_get_class_entry(), msg
, NULL TSRMLS_CC
), 0);
462 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("requestMessage"), message TSRMLS_CC
);
463 zval_ptr_dtor(&message
);
464 obj
->client
->request
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
468 if (SUCCESS
== php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, &progress
)) {
469 progress
->state
.info
= "finished";
470 progress
->state
.finished
= 1;
471 php_http_client_progress_notify(progress TSRMLS_CC
);
473 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK
, NULL
);
478 void php_http_client_options_set(zval
*this_ptr
, zval
*opts TSRMLS_DC
)
480 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
483 zend_class_entry
*this_ce
= Z_OBJCE_P(getThis());
484 zend_bool is_client
= instanceof_function(this_ce
, php_http_client_class_entry TSRMLS_CC
);
486 MAKE_STD_ZVAL(new_opts
);
487 array_init(new_opts
);
489 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
490 zend_update_property(this_ce
, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC
);
491 zval_ptr_dtor(&new_opts
);
493 zval
*old_opts
, *add_opts
, **opt
;
495 MAKE_STD_ZVAL(add_opts
);
496 array_init(add_opts
);
497 /* some options need extra attention -- thus cannot use array_merge() directly */
498 FOREACH_KEYVAL(pos
, opts
, key
, opt
) {
499 if (key
.type
== HASH_KEY_IS_STRING
) {
500 #define KEYMATCH(k, s) ((sizeof(s)==k.len) && !strcasecmp(k.str, s))
501 if (Z_TYPE_PP(opt
) == IS_ARRAY
&& (KEYMATCH(key
, "ssl") || KEYMATCH(key
, "cookies"))) {
502 php_http_client_options_set_subr(getThis(), key
.str
, key
.len
, *opt
, 0 TSRMLS_CC
);
503 } else if (is_client
&& (KEYMATCH(key
, "recordHistory") || KEYMATCH(key
, "responseMessageClass"))) {
504 zend_update_property(this_ce
, getThis(), key
.str
, key
.len
-1, *opt TSRMLS_CC
);
505 } else if (Z_TYPE_PP(opt
) == IS_NULL
) {
506 old_opts
= zend_read_property(this_ce
, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC
);
507 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
508 zend_symtable_del(Z_ARRVAL_P(old_opts
), key
.str
, key
.len
);
512 add_assoc_zval_ex(add_opts
, key
.str
, key
.len
, *opt
);
517 old_opts
= zend_read_property(this_ce
, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC
);
518 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
519 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL_P(new_opts
));
521 array_join(Z_ARRVAL_P(add_opts
), Z_ARRVAL_P(new_opts
), 0, 0);
522 zend_update_property(this_ce
, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC
);
523 zval_ptr_dtor(&new_opts
);
524 zval_ptr_dtor(&add_opts
);
528 void php_http_client_options_set_subr(zval
*this_ptr
, char *key
, size_t len
, zval
*opts
, int overwrite TSRMLS_DC
)
530 if (overwrite
|| (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
)))) {
531 zend_class_entry
*this_ce
= Z_OBJCE_P(getThis());
532 zval
*old_opts
, *new_opts
, **entry
= NULL
;
534 MAKE_STD_ZVAL(new_opts
);
535 array_init(new_opts
);
536 old_opts
= zend_read_property(this_ce
, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC
);
537 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
538 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL_P(new_opts
));
542 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
544 zend_symtable_update(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &opts
, sizeof(zval
*), NULL
);
546 zend_symtable_del(Z_ARRVAL_P(new_opts
), key
, len
);
548 } else if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
549 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &entry
)) {
550 array_join(Z_ARRVAL_P(opts
), Z_ARRVAL_PP(entry
), 0, 0);
553 zend_symtable_update(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &opts
, sizeof(zval
*), NULL
);
557 zend_update_property(this_ce
, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC
);
558 zval_ptr_dtor(&new_opts
);
562 void php_http_client_options_get_subr(zval
*this_ptr
, char *key
, size_t len
, zval
*return_value TSRMLS_DC
)
564 zend_class_entry
*this_ce
= Z_OBJCE_P(getThis());
565 zval
**options
, *opts
= zend_read_property(this_ce
, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC
);
567 if ((Z_TYPE_P(opts
) == IS_ARRAY
) && (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(opts
), key
, len
, (void *) &options
))) {
568 RETVAL_ZVAL(*options
, 1, 0);
572 PHP_METHOD(HttpClient
, __construct
)
574 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
575 zval
*os
, *opts
= NULL
;
578 object_init_ex(os
, spl_ce_SplObjectStorage
);
579 zend_update_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), os TSRMLS_CC
);
582 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
583 php_http_client_options_set(getThis(), opts TSRMLS_CC
);
586 } end_error_handling();
589 PHP_METHOD(HttpClient
, getObservers
)
591 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
592 if (SUCCESS
== zend_parse_parameters_none()) {
593 RETVAL_PROP(php_http_client_class_entry
, "observers");
595 } end_error_handling();
598 static int notify(zend_object_iterator
*iter
, void *puser TSRMLS_DC
)
600 zval
**observer
= NULL
;
602 iter
->funcs
->get_current_data(iter
, &observer TSRMLS_CC
);
606 zend_call_method_with_1_params(observer
, NULL
, NULL
, "update", &retval
, puser
);
607 zval_ptr_dtor(&retval
);
613 PHP_METHOD(HttpClient
, notify
)
615 if (SUCCESS
== zend_parse_parameters_none()) {
616 zval
*observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC
);
618 if (Z_TYPE_P(observers
) == IS_OBJECT
) {
619 Z_ADDREF_P(getThis());
620 spl_iterator_apply(observers
, notify
, getThis() TSRMLS_CC
);
621 zval_ptr_dtor(&getThis());
625 RETVAL_ZVAL(getThis(), 1, 0);
628 PHP_METHOD(HttpClient
, attach
)
632 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &observer
, spl_ce_SplObserver
)) {
633 zval
*retval
, *observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC
);
634 zend_call_method_with_1_params(&observers
, NULL
, NULL
, "attach", &retval
, observer
);
635 zval_ptr_dtor(&retval
);
638 RETVAL_ZVAL(getThis(), 1, 0);
641 PHP_METHOD(HttpClient
, detach
)
645 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &observer
, spl_ce_SplObserver
)) {
646 zval
*retval
, *observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC
);
647 zend_call_method_with_1_params(&observers
, NULL
, NULL
, "detach", &retval
, observer
);
648 zval_ptr_dtor(&retval
);
651 RETVAL_ZVAL(getThis(), 1, 0);
654 PHP_METHOD(HttpClient
, getProgress
)
656 if (SUCCESS
== zend_parse_parameters_none()) {
657 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
658 php_http_client_progress_t
*progress
= NULL
;
660 if (SUCCESS
== php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, &progress
)) {
661 object_init(return_value
);
662 add_property_bool(return_value
, "started", progress
->state
.started
);
663 add_property_bool(return_value
, "finished", progress
->state
.finished
);
664 add_property_string(return_value
, "info", STR_PTR(progress
->state
.info
), 1);
665 add_property_double(return_value
, "dltotal", progress
->state
.dl
.total
);
666 add_property_double(return_value
, "dlnow", progress
->state
.dl
.now
);
667 add_property_double(return_value
, "ultotal", progress
->state
.ul
.total
);
668 add_property_double(return_value
, "ulnow", progress
->state
.ul
.now
);
673 PHP_METHOD(HttpClient
, getTransferInfo
)
675 char *info_name
= NULL
;
678 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
679 zval
**infop
, *temp
= NULL
, *info
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("transferInfo"), 0 TSRMLS_CC
);
681 /* request completed? */
682 if (Z_TYPE_P(info
) != IS_ARRAY
) {
683 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
687 php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
, Z_ARRVAL_P(temp
));
691 if (info_len
&& info_name
) {
692 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(info
), php_http_pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void *) &infop
)) {
693 RETVAL_ZVAL(*infop
, 1, 0);
695 php_http_error(HE_NOTICE
, PHP_HTTP_E_INVALID_PARAM
, "Could not find transfer info named %s", info_name
);
699 RETVAL_ZVAL(info
, 1, 0);
703 zval_ptr_dtor(&temp
);
710 PHP_METHOD(HttpClient
, setOptions
)
714 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
715 php_http_client_options_set(getThis(), opts TSRMLS_CC
);
717 RETVAL_ZVAL(getThis(), 1, 0);
721 PHP_METHOD(HttpClient
, getOptions
)
723 if (SUCCESS
== zend_parse_parameters_none()) {
724 RETURN_PROP(php_http_client_class_entry
, "options");
729 PHP_METHOD(HttpClient
, setSslOptions
)
733 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
734 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts
, 1 TSRMLS_CC
);
736 RETVAL_ZVAL(getThis(), 1, 0);
740 PHP_METHOD(HttpClient
, addSslOptions
)
744 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
745 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts
, 0 TSRMLS_CC
);
747 RETVAL_ZVAL(getThis(), 1, 0);
751 PHP_METHOD(HttpClient
, getSslOptions
)
753 if (SUCCESS
== zend_parse_parameters_none()) {
754 php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC
);
758 PHP_METHOD(HttpClient
, setCookies
)
762 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
763 php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts
, 1 TSRMLS_CC
);
765 RETVAL_ZVAL(getThis(), 1, 0);
769 PHP_METHOD(HttpClient
, addCookies
)
773 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
774 php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts
, 0 TSRMLS_CC
);
776 RETVAL_ZVAL(getThis(), 1, 0);
780 PHP_METHOD(HttpClient
, getCookies
)
782 if (SUCCESS
== zend_parse_parameters_none()) {
783 php_http_client_options_get_subr(getThis(), ZEND_STRS("cookies"), return_value TSRMLS_CC
);
787 PHP_METHOD(HttpClient
, enableCookies
)
789 if (SUCCESS
== zend_parse_parameters_none()){
790 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
792 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_COOKIES_ENABLE
, NULL
);
794 RETVAL_ZVAL(getThis(), 1, 0);
797 PHP_METHOD(HttpClient
, resetCookies
)
799 zend_bool session_only
= 0;
801 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &session_only
)) {
802 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
804 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_COOKIES_RESET_SESSION
, NULL
);
806 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_COOKIES_RESET
, NULL
);
809 RETVAL_ZVAL(getThis(), 1, 0);
812 PHP_METHOD(HttpClient
, flushCookies
)
814 if (SUCCESS
== zend_parse_parameters_none()) {
815 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
817 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_COOKIES_FLUSH
, NULL
);
819 RETVAL_ZVAL(getThis(), 1, 0);
822 PHP_METHOD(HttpClient
, getResponseMessage
)
824 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
825 if (SUCCESS
== zend_parse_parameters_none()) {
826 zval
*message
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC
);
828 if (Z_TYPE_P(message
) == IS_OBJECT
) {
829 RETVAL_OBJECT(message
, 1);
831 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpClient does not contain a response message");
834 } end_error_handling();
837 PHP_METHOD(HttpClient
, getRequestMessage
)
839 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
840 if (SUCCESS
== zend_parse_parameters_none()) {
841 zval
*message
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("requestMessage"), 0 TSRMLS_CC
);
843 if (Z_TYPE_P(message
) == IS_OBJECT
) {
844 RETVAL_OBJECT(message
, 1);
846 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpClient does not contain a request message");
849 } end_error_handling();
852 PHP_METHOD(HttpClient
, getHistory
)
854 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
855 if (SUCCESS
== zend_parse_parameters_none()) {
856 zval
*hist
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC
);
858 if (Z_TYPE_P(hist
) == IS_OBJECT
) {
859 RETVAL_OBJECT(hist
, 1);
861 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "The history is empty");
864 } end_error_handling();
867 PHP_METHOD(HttpClient
, clearHistory
)
869 if (SUCCESS
== zend_parse_parameters_none()) {
870 zend_update_property_null(php_http_client_class_entry
, getThis(), ZEND_STRL("history") TSRMLS_CC
);
872 RETVAL_ZVAL(getThis(), 1, 0);
875 PHP_METHOD(HttpClient
, getResponseMessageClass
)
877 if (SUCCESS
== zend_parse_parameters_none()) {
878 RETURN_PROP(php_http_client_class_entry
, "responseMessageClass");
883 PHP_METHOD(HttpClient
, setResponseMessageClass
)
888 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &cn
, &cl
)) {
889 zend_update_property_stringl(php_http_client_class_entry
, getThis(), ZEND_STRL("responseMessageClass"), cn
, cl TSRMLS_CC
);
891 RETVAL_ZVAL(getThis(), 1, 0);
894 PHP_METHOD(HttpClient
, setRequest
)
898 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zreq
, php_http_client_request_get_class_entry())) {
899 zend_update_property(php_http_client_class_entry
, getThis(), ZEND_STRL("request"), zreq TSRMLS_CC
);
901 RETURN_ZVAL(getThis(), 1, 0);
904 PHP_METHOD(HttpClient
, getRequest
)
906 if (SUCCESS
== zend_parse_parameters_none()) {
907 RETURN_PROP(php_http_client_class_entry
, "request");
911 PHP_METHOD(HttpClient
, request
)
913 char *meth_str
, *url_str
;
914 int meth_len
, url_len
;
915 zval
*zheader
, *zbody
, *zoptions
;
917 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
918 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a!z!a!/", &meth_str
, &meth_len
, &url_str
, &url_len
, &zheader
, &zbody
, &zoptions
)) {
919 php_http_message_object_t
*msg_obj
;
920 zend_object_value ov
;
921 zval
*req
, *res
= NULL
;
923 php_http_new(&ov
, php_http_client_request_get_class_entry(), (php_http_new_t
) php_http_message_object_new_ex
, NULL
, NULL
, (void *) &msg_obj TSRMLS_CC
);
925 ZVAL_OBJVAL(req
, ov
, 0);
927 msg_obj
->message
= php_http_message_init(NULL
, PHP_HTTP_REQUEST TSRMLS_CC
);
928 PHP_HTTP_INFO(msg_obj
->message
).request
.url
= estrndup(url_str
, url_len
);
929 PHP_HTTP_INFO(msg_obj
->message
).request
.method
= estrndup(meth_str
, meth_len
);
932 array_copy(Z_ARRVAL_P(zheader
), &msg_obj
->message
->hdrs
);
936 php_http_message_object_set_body(msg_obj
, zbody TSRMLS_CC
);
940 php_http_client_options_set(req
, zoptions TSRMLS_CC
);
943 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "send", &res
, req
);
944 RETVAL_ZVAL(res
, 0, 1);
947 } end_error_handling();
950 PHP_METHOD(HttpClient
, send
)
956 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
957 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|O!", &zreq
, php_http_client_request_get_class_entry())) {
958 if (SUCCESS
== php_http_client_object_handle_request(getThis(), &zreq TSRMLS_CC
)) {
959 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
960 php_http_message_object_t
*req
= zend_object_store_get_object(zreq TSRMLS_CC
);
962 php_http_client_exec(obj
->client
, req
->message
);
964 if (SUCCESS
== php_http_client_object_handle_response(getThis() TSRMLS_CC
)) {
965 RETVAL_PROP(php_http_client_class_entry
, "responseMessage");
967 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Failed to handle response");
970 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Failed to handle request");
973 } end_error_handling();
976 PHP_MINIT_FUNCTION(http_client
)
978 PHP_HTTP_REGISTER_CLASS(http
\\Client
, AbstractClient
, http_client
, php_http_object_get_class_entry(), ZEND_ACC_ABSTRACT
);
979 php_http_client_class_entry
->create_object
= php_http_client_object_new
;
980 memcpy(&php_http_client_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
981 php_http_client_object_handlers
.clone_obj
= php_http_client_object_clone
;
983 zend_class_implements(php_http_client_class_entry TSRMLS_CC
, 2, spl_ce_SplSubject
, php_http_client_interface_get_class_entry());
985 zend_declare_property_string(php_http_client_class_entry
, ZEND_STRL("responseMessageClass"), "", ZEND_ACC_PRIVATE TSRMLS_CC
);
986 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("observers"), ZEND_ACC_PRIVATE TSRMLS_CC
);
987 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("transferInfo"), ZEND_ACC_PRIVATE TSRMLS_CC
);
988 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("responseMessage"), ZEND_ACC_PRIVATE TSRMLS_CC
);
989 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("requestMessage"), ZEND_ACC_PRIVATE TSRMLS_CC
);
990 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("history"), ZEND_ACC_PRIVATE TSRMLS_CC
);
992 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC
);
993 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("request"), ZEND_ACC_PROTECTED TSRMLS_CC
);
995 zend_declare_property_bool(php_http_client_class_entry
, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
1005 * vim600: noet sw=4 ts=4 fdm=marker
1006 * vim<600: noet sw=4 ts=4