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"
14 #include "php_http_client.h"
16 #include "ext/spl/spl_observer.h"
19 * array of name => php_http_client_driver_t*
21 static HashTable php_http_client_drivers
;
23 static void php_http_client_driver_hash_dtor(zval
*pData
)
25 pefree(Z_PTR_P(pData
), 1);
28 ZEND_RESULT_CODE
php_http_client_driver_add(php_http_client_driver_t
*driver
)
30 return zend_hash_add_mem(&php_http_client_drivers
, driver
->driver_name
, (void *) driver
, sizeof(php_http_client_driver_t
))
34 php_http_client_driver_t
*php_http_client_driver_get(zend_string
*name
)
37 php_http_client_driver_t
*tmp
;
39 if (name
&& (tmp
= zend_hash_find_ptr(&php_http_client_drivers
, name
))) {
42 if ((ztmp
= zend_hash_get_current_data(&php_http_client_drivers
))) {
48 static int apply_driver_list(zval
*p
, void *arg
)
50 php_http_client_driver_t
*d
= Z_PTR_P(p
);
53 ZVAL_STR_COPY(&zname
, d
->driver_name
);
55 zend_hash_next_index_insert(arg
, &zname
);
56 return ZEND_HASH_APPLY_KEEP
;
59 void php_http_client_driver_list(HashTable
*ht
)
61 zend_hash_apply_with_argument(&php_http_client_drivers
, apply_driver_list
, ht
);
64 static zend_class_entry
*php_http_client_class_entry
;
65 zend_class_entry
*php_http_client_get_class_entry(void)
67 return php_http_client_class_entry
;
70 void php_http_client_options_set_subr(zval
*instance
, char *key
, size_t len
, zval
*opts
, int overwrite
)
72 if (overwrite
|| (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
)))) {
73 zend_class_entry
*this_ce
= Z_OBJCE_P(instance
);
74 zval old_opts_tmp
, *old_opts
, new_opts
, *entry
= NULL
;
76 array_init(&new_opts
);
77 old_opts
= zend_read_property(this_ce
, instance
, ZEND_STRL("options"), 0, &old_opts_tmp
);
79 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
80 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL(new_opts
));
84 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
86 zend_symtable_str_update(Z_ARRVAL(new_opts
), key
, len
, opts
);
88 zend_symtable_str_del(Z_ARRVAL(new_opts
), key
, len
);
90 } else if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
91 if ((entry
= zend_symtable_str_find(Z_ARRVAL(new_opts
), key
, len
))) {
93 array_join(Z_ARRVAL_P(opts
), Z_ARRVAL_P(entry
), 0, 0);
96 zend_symtable_str_update(Z_ARRVAL(new_opts
), key
, len
, opts
);
100 zend_update_property(this_ce
, instance
, ZEND_STRL("options"), &new_opts
);
101 zval_ptr_dtor(&new_opts
);
105 void php_http_client_options_set(zval
*instance
, zval
*opts
)
107 php_http_arrkey_t key
;
109 zend_class_entry
*this_ce
= Z_OBJCE_P(instance
);
110 zend_bool is_client
= instanceof_function(this_ce
, php_http_client_class_entry
);
112 array_init(&new_opts
);
114 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
115 zend_update_property(this_ce
, instance
, ZEND_STRL("options"), &new_opts
);
116 zval_ptr_dtor(&new_opts
);
118 zval old_opts_tmp
, *old_opts
, add_opts
, *opt
;
120 array_init(&add_opts
);
121 /* some options need extra attention -- thus cannot use array_merge() directly */
122 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(opts
), key
.h
, key
.key
, opt
)
125 if (Z_TYPE_P(opt
) == IS_ARRAY
&& (zend_string_equals_literal(key
.key
, "ssl") || zend_string_equals_literal(key
.key
, "cookies"))) {
126 php_http_client_options_set_subr(instance
, key
.key
->val
, key
.key
->len
, opt
, 0);
127 } else if (is_client
&& (zend_string_equals_literal(key
.key
, "recordHistory") || zend_string_equals_literal(key
.key
, "responseMessageClass"))) {
128 zend_update_property(this_ce
, instance
, key
.key
->val
, key
.key
->len
, opt
);
129 } else if (Z_TYPE_P(opt
) == IS_NULL
) {
130 old_opts
= zend_read_property(this_ce
, instance
, ZEND_STRL("options"), 0, &old_opts_tmp
);
131 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
132 zend_symtable_del(Z_ARRVAL_P(old_opts
), key
.key
);
136 add_assoc_zval_ex(&add_opts
, key
.key
->val
, key
.key
->len
, opt
);
140 ZEND_HASH_FOREACH_END();
142 old_opts
= zend_read_property(this_ce
, instance
, ZEND_STRL("options"), 0, &old_opts_tmp
);
143 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
144 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL(new_opts
));
146 array_join(Z_ARRVAL(add_opts
), Z_ARRVAL(new_opts
), 0, 0);
147 zend_update_property(this_ce
, instance
, ZEND_STRL("options"), &new_opts
);
148 zval_ptr_dtor(&new_opts
);
149 zval_ptr_dtor(&add_opts
);
153 void php_http_client_options_get_subr(zval
*instance
, char *key
, size_t len
, zval
*return_value
)
155 zend_class_entry
*this_ce
= Z_OBJCE_P(instance
);
156 zval
*options
, opts_tmp
, *opts
= zend_read_property(this_ce
, instance
, ZEND_STRL("options"), 0, &opts_tmp
);
158 if ((Z_TYPE_P(opts
) == IS_ARRAY
) && (options
= zend_symtable_str_find(Z_ARRVAL_P(opts
), key
, len
))) {
159 RETVAL_ZVAL(options
, 1, 0);
163 static void queue_dtor(void *enqueued
)
165 php_http_client_enqueue_t
*e
= enqueued
;
172 php_http_client_t
*php_http_client_init(php_http_client_t
*h
, php_http_client_ops_t
*ops
, php_resource_factory_t
*rf
, void *init_arg
)
174 php_http_client_t
*free_h
= NULL
;
177 free_h
= h
= emalloc(sizeof(*h
));
179 memset(h
, 0, sizeof(*h
));
184 } else if (ops
->rsrc
) {
185 h
->rf
= php_resource_factory_init(NULL
, h
->ops
->rsrc
, h
, NULL
);
187 zend_llist_init(&h
->requests
, sizeof(php_http_client_enqueue_t
), queue_dtor
, 0);
188 zend_llist_init(&h
->responses
, sizeof(void *), NULL
, 0);
191 if (!(h
= h
->ops
->init(h
, init_arg
))) {
192 php_error_docref(NULL
, E_WARNING
, "Could not initialize client");
200 php_http_client_t
*php_http_client_copy(php_http_client_t
*from
, php_http_client_t
*to
)
202 if (from
->ops
->copy
) {
203 return from
->ops
->copy(from
, to
);
209 void php_http_client_dtor(php_http_client_t
*h
)
211 php_http_client_reset(h
);
216 h
->callback
.debug
.func
= NULL
;
217 h
->callback
.debug
.arg
= NULL
;
219 php_resource_factory_free(&h
->rf
);
222 void php_http_client_free(php_http_client_t
**h
) {
224 php_http_client_dtor(*h
);
230 ZEND_RESULT_CODE
php_http_client_enqueue(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue
)
232 if (h
->ops
->enqueue
) {
233 if (php_http_client_enqueued(h
, enqueue
->request
, NULL
)) {
234 php_error_docref(NULL
, E_WARNING
, "Failed to enqueue request; request already in queue");
237 return h
->ops
->enqueue(h
, enqueue
);
243 ZEND_RESULT_CODE
php_http_client_dequeue(php_http_client_t
*h
, php_http_message_t
*request
)
245 if (h
->ops
->dequeue
) {
246 php_http_client_enqueue_t
*enqueue
= php_http_client_enqueued(h
, request
, NULL
);
249 php_error_docref(NULL
, E_WARNING
, "Failed to dequeue request; request not in queue");
252 return h
->ops
->dequeue(h
, enqueue
);
257 ZEND_RESULT_CODE
php_http_client_requeue(php_http_client_t
*h
, php_http_message_t
*request
)
259 if (h
->ops
->dequeue
) {
260 php_http_client_enqueue_t
*enqueue
= php_http_client_enqueued(h
, request
, NULL
);
263 php_error_docref(NULL
, E_WARNING
, "Failed to requeue request; request not in queue");
266 return h
->ops
->requeue(h
, enqueue
);
271 php_http_client_enqueue_t
*php_http_client_enqueued(php_http_client_t
*h
, void *compare_arg
, php_http_client_enqueue_cmp_func_t compare_func
)
273 zend_llist_element
*el
= NULL
;
276 for (el
= h
->requests
.head
; el
; el
= el
->next
) {
277 if (compare_func((php_http_client_enqueue_t
*) el
->data
, compare_arg
)) {
282 for (el
= h
->requests
.head
; el
; el
= el
->next
) {
283 if (((php_http_client_enqueue_t
*) el
->data
)->request
== compare_arg
) {
288 return el
? (php_http_client_enqueue_t
*) el
->data
: NULL
;
291 ZEND_RESULT_CODE
php_http_client_wait(php_http_client_t
*h
, struct timeval
*custom_timeout
)
294 return h
->ops
->wait(h
, custom_timeout
);
300 int php_http_client_once(php_http_client_t
*h
)
303 return h
->ops
->once(h
);
309 ZEND_RESULT_CODE
php_http_client_exec(php_http_client_t
*h
)
312 return h
->ops
->exec(h
);
318 void php_http_client_reset(php_http_client_t
*h
)
324 zend_llist_clean(&h
->requests
);
325 zend_llist_clean(&h
->responses
);
328 ZEND_RESULT_CODE
php_http_client_setopt(php_http_client_t
*h
, php_http_client_setopt_opt_t opt
, void *arg
)
330 if (h
->ops
->setopt
) {
331 return h
->ops
->setopt(h
, opt
, arg
);
337 ZEND_RESULT_CODE
php_http_client_getopt(php_http_client_t
*h
, php_http_client_getopt_opt_t opt
, void *arg
, void *res_ptr
)
339 if (h
->ops
->getopt
) {
340 return h
->ops
->getopt(h
, opt
, arg
, res_ptr
);
345 static zend_object_handlers php_http_client_object_handlers
;
347 void php_http_client_object_free(zend_object
*object
)
349 php_http_client_object_t
*o
= PHP_HTTP_OBJ(object
, NULL
);
353 php_http_client_free(&o
->client
);
354 if (o
->debug
.fci
.size
> 0) {
355 zend_fcall_info_args_clear(&o
->debug
.fci
, 1);
356 zval_ptr_dtor(&o
->debug
.fci
.function_name
);
357 o
->debug
.fci
.size
= 0;
359 php_http_object_method_dtor(&o
->notify
);
360 php_http_object_method_free(&o
->update
);
361 zend_object_std_dtor(object
);
364 php_http_client_object_t
*php_http_client_object_new_ex(zend_class_entry
*ce
, php_http_client_t
*client
)
366 php_http_client_object_t
*o
;
368 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
369 zend_object_std_init(&o
->zo
, ce
);
370 object_properties_init(&o
->zo
, ce
);
374 o
->zo
.handlers
= &php_http_client_object_handlers
;
379 zend_object
*php_http_client_object_new(zend_class_entry
*ce
)
381 return &php_http_client_object_new_ex(ce
, NULL
)->zo
;
384 static HashTable
*php_http_client_object_get_gc(zval
*object
, zval
**table
, int *n
)
386 php_http_client_object_t
*obj
= PHP_HTTP_OBJ(NULL
, object
);
387 zend_llist_element
*el
= NULL
;
388 HashTable
*props
= Z_OBJPROP_P(object
);
389 uint32_t count
= zend_hash_num_elements(props
) + zend_llist_count(&obj
->client
->responses
) + zend_llist_count(&obj
->client
->requests
) + 2;
393 *table
= obj
->gc
= erealloc(obj
->gc
, sizeof(zval
) * count
);
395 #if PHP_HTTP_HAVE_LIBCURL
396 if (obj
->client
->ops
== php_http_client_curl_get_ops()) {
397 php_http_client_curl_t
*curl
= obj
->client
->ctx
;
399 if (curl
->ev_ops
== php_http_client_curl_user_ops_get()) {
400 php_http_client_curl_user_context_t
*ctx
= curl
->ev_ctx
;
402 ZVAL_COPY_VALUE(&obj
->gc
[(*n
)++], &ctx
->user
);
407 if (obj
->debug
.fci
.size
> 0) {
408 ZVAL_COPY_VALUE(&obj
->gc
[(*n
)++], &obj
->debug
.fci
.function_name
);
411 for (el
= obj
->client
->responses
.head
; el
; el
= el
->next
) {
412 php_http_message_object_t
*response_obj
= *(php_http_message_object_t
**) el
->data
;
413 ZVAL_OBJ(&obj
->gc
[(*n
)++], &response_obj
->zo
);
416 for (el
= obj
->client
->requests
.head
; el
; el
= el
->next
) {
417 php_http_client_enqueue_t
*q
= (php_http_client_enqueue_t
*) el
->data
;
418 if (q
->request_obj
) {
419 ZVAL_OBJ(&obj
->gc
[(*n
)++], &q
->request_obj
->zo
);
423 ZEND_HASH_FOREACH_VAL(props
, val
)
425 ZVAL_COPY_VALUE(&obj
->gc
[(*n
)++], val
);
427 ZEND_HASH_FOREACH_END();
432 static void handle_history(zval
*zclient
, php_http_message_t
*request
, php_http_message_t
*response
)
434 zval new_hist
, old_hist_tmp
, *old_hist
= zend_read_property(php_http_client_class_entry
, zclient
, ZEND_STRL("history"), 0, &old_hist_tmp
);
435 php_http_message_t
*req_copy
= php_http_message_copy(request
, NULL
);
436 php_http_message_t
*res_copy
= php_http_message_copy(response
, NULL
);
437 php_http_message_t
*zipped
= php_http_message_zip(res_copy
, req_copy
);
438 php_http_message_object_t
*obj
= php_http_message_object_new_ex(php_http_message_get_class_entry(), zipped
);
440 ZVAL_OBJ(&new_hist
, &obj
->zo
);
442 if (Z_TYPE_P(old_hist
) == IS_OBJECT
) {
443 php_http_message_object_prepend(&new_hist
, old_hist
, 1);
446 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("history"), &new_hist
);
447 zval_ptr_dtor(&new_hist
);
450 static ZEND_RESULT_CODE
handle_response(void *arg
, php_http_client_t
*client
, php_http_client_enqueue_t
*e
, php_http_message_t
**response
)
452 zend_bool dequeue
= 0;
454 php_http_message_t
*msg
;
455 php_http_client_progress_state_t
*progress
;
457 ZVAL_OBJ(&zclient
, &((php_http_client_object_t
*) arg
)->zo
);
459 if ((msg
= *response
)) {
460 php_http_message_object_t
*msg_obj
;
461 zval info
, zresponse
, zrequest
, rec_hist_tmp
;
464 /* ensure the message is of type response (could be uninitialized in case of early error, like DNS) */
465 php_http_message_set_type(msg
, PHP_HTTP_RESPONSE
);
467 if (zend_is_true(zend_read_property(php_http_client_class_entry
, &zclient
, ZEND_STRL("recordHistory"), 0, &rec_hist_tmp
))) {
468 handle_history(&zclient
, e
->request
, *response
);
471 /* hard detach, redirects etc. are in the history */
472 php_http_message_free(&msg
->parent
);
475 msg_obj
= php_http_message_object_new_ex(php_http_get_client_response_class_entry(), msg
);
476 ZVAL_OBJECT(&zresponse
, &msg_obj
->zo
, 1);
477 ZVAL_OBJECT(&zrequest
, &((php_http_message_object_t
*) e
->opaque
)->zo
, 1);
479 php_http_message_object_prepend(&zresponse
, &zrequest
, 1);
482 info_ht
= HASH_OF(&info
);
483 php_http_client_getopt(client
, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
, e
->request
, &info_ht
);
484 zend_update_property(php_http_get_client_response_class_entry(), &zresponse
, ZEND_STRL("transferInfo"), &info
);
485 zval_ptr_dtor(&info
);
487 zend_llist_add_element(&client
->responses
, &msg_obj
);
489 if (e
->closure
.fci
.size
) {
491 zend_error_handling zeh
;
494 zend_fcall_info_argn(&e
->closure
.fci
, 1, &zresponse
);
495 zend_replace_error_handling(EH_NORMAL
, NULL
, &zeh
);
496 ++client
->callback
.depth
;
497 zend_fcall_info_call(&e
->closure
.fci
, &e
->closure
.fcc
, &retval
, NULL
);
498 --client
->callback
.depth
;
499 zend_restore_error_handling(&zeh
);
500 zend_fcall_info_argn(&e
->closure
.fci
, 0);
502 if (Z_TYPE(retval
) == IS_TRUE
) {
505 zval_ptr_dtor(&retval
);
508 zval_ptr_dtor(&zresponse
);
509 zval_ptr_dtor(&zrequest
);
512 if (SUCCESS
== php_http_client_getopt(client
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, e
->request
, &progress
)) {
513 progress
->info
= "finished";
514 progress
->finished
= 1;
515 client
->callback
.progress
.func(client
->callback
.progress
.arg
, client
, e
, progress
);
519 php_http_client_dequeue(client
, e
->request
);
525 static void handle_progress(void *arg
, php_http_client_t
*client
, php_http_client_enqueue_t
*e
, php_http_client_progress_state_t
*progress
)
527 zval zclient
, args
[2];
528 php_http_client_object_t
*client_obj
= arg
;
529 zend_error_handling zeh
;
531 ZVAL_OBJECT(&zclient
, &client_obj
->zo
, 1);
532 ZVAL_OBJECT(&args
[0], &((php_http_message_object_t
*) e
->opaque
)->zo
, 1);
533 object_init(&args
[1]);
534 add_property_bool(&args
[1], "started", progress
->started
);
535 add_property_bool(&args
[1], "finished", progress
->finished
);
536 add_property_string(&args
[1], "info", STR_PTR(progress
->info
));
537 add_property_double(&args
[1], "dltotal", progress
->dl
.total
);
538 add_property_double(&args
[1], "dlnow", progress
->dl
.now
);
539 add_property_double(&args
[1], "ultotal", progress
->ul
.total
);
540 add_property_double(&args
[1], "ulnow", progress
->ul
.now
);
542 zend_replace_error_handling(EH_NORMAL
, NULL
, &zeh
);
543 ++client
->callback
.depth
;
544 php_http_object_method_call(&client_obj
->notify
, &zclient
, NULL
, 2, args
);
545 --client
->callback
.depth
;
546 zend_restore_error_handling(&zeh
);
548 zval_ptr_dtor(&zclient
);
549 zval_ptr_dtor(&args
[0]);
550 zval_ptr_dtor(&args
[1]);
553 static void handle_debug(void *arg
, php_http_client_t
*client
, php_http_client_enqueue_t
*e
, unsigned type
, const char *data
, size_t size
)
555 zval ztype
, zdata
, zreq
, zclient
;
556 php_http_client_object_t
*client_obj
= arg
;
557 zend_error_handling zeh
;
559 ZVAL_OBJECT(&zclient
, &client_obj
->zo
, 1);
560 ZVAL_OBJECT(&zreq
, &((php_http_message_object_t
*) e
->opaque
)->zo
, 1);
561 ZVAL_LONG(&ztype
, type
);
562 ZVAL_STRINGL(&zdata
, data
, size
);
564 zend_replace_error_handling(EH_NORMAL
, NULL
, &zeh
);
565 if (SUCCESS
== zend_fcall_info_argn(&client_obj
->debug
.fci
, 4, &zclient
, &zreq
, &ztype
, &zdata
)) {
566 ++client
->callback
.depth
;
567 zend_fcall_info_call(&client_obj
->debug
.fci
, &client_obj
->debug
.fcc
, NULL
, NULL
);
568 --client
->callback
.depth
;
569 zend_fcall_info_args_clear(&client_obj
->debug
.fci
, 0);
571 zend_restore_error_handling(&zeh
);
573 zval_ptr_dtor(&zclient
);
574 zval_ptr_dtor(&zreq
);
575 zval_ptr_dtor(&ztype
);
576 zval_ptr_dtor(&zdata
);
579 static void response_dtor(void *data
)
581 php_http_message_object_t
*msg_obj
= *(php_http_message_object_t
**) data
;
583 zend_object_release(&msg_obj
->zo
);
586 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_construct
, 0, 0, 0)
587 ZEND_ARG_INFO(0, driver
)
588 ZEND_ARG_INFO(0, persistent_handle_id
)
590 static PHP_METHOD(HttpClient
, __construct
)
592 zend_string
*driver_name
= NULL
, *persistent_handle_name
= NULL
;
593 php_http_client_driver_t
*driver
;
594 php_resource_factory_t
*rf
= NULL
;
595 php_http_client_object_t
*obj
;
598 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &driver_name
, &persistent_handle_name
), invalid_arg
, return);
600 if (!zend_hash_num_elements(&php_http_client_drivers
)) {
601 php_http_throw(unexpected_val
, "No http\\Client drivers available");
604 if (!(driver
= php_http_client_driver_get(driver_name
))) {
605 php_http_throw(unexpected_val
, "Failed to locate \"%s\" client request handler", driver_name
? driver_name
->val
: "default");
609 object_init_ex(&os
, spl_ce_SplObjectStorage
);
610 zend_update_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), &os
);
613 if (persistent_handle_name
) {
614 php_persistent_handle_factory_t
*pf
;
616 if ((pf
= php_persistent_handle_concede(NULL
, driver
->client_name
, persistent_handle_name
, NULL
, NULL
))) {
617 rf
= php_persistent_handle_resource_factory_init(NULL
, pf
);
621 obj
= PHP_HTTP_OBJ(NULL
, getThis());
623 php_http_expect(obj
->client
= php_http_client_init(NULL
, driver
->client_ops
, rf
, NULL
), runtime
, return);
625 php_http_object_method_init(&obj
->notify
, getThis(), ZEND_STRL("notify"));
627 obj
->client
->callback
.response
.func
= handle_response
;
628 obj
->client
->callback
.response
.arg
= obj
;
629 obj
->client
->callback
.progress
.func
= handle_progress
;
630 obj
->client
->callback
.progress
.arg
= obj
;
632 obj
->client
->responses
.dtor
= response_dtor
;
635 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_reset
, 0, 0, 0)
637 static PHP_METHOD(HttpClient
, reset
)
639 php_http_client_object_t
*obj
;
640 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
642 obj
= PHP_HTTP_OBJ(NULL
, getThis());
645 php_http_client_reset(obj
->client
);
647 RETVAL_ZVAL(getThis(), 1, 0);
650 static HashTable
*combined_options(HashTable
*options
, zval
*client
, zval
*request
)
652 unsigned num_options
= 0;
653 zval z_roptions
, z_options_tmp
, *z_coptions
= zend_read_property(php_http_client_class_entry
, client
, ZEND_STRL("options"), 0, &z_options_tmp
);
655 if (Z_TYPE_P(z_coptions
) == IS_ARRAY
) {
656 num_options
= zend_hash_num_elements(Z_ARRVAL_P(z_coptions
));
658 ZVAL_UNDEF(&z_roptions
);
659 zend_call_method_with_0_params(request
, NULL
, NULL
, "getOptions", &z_roptions
);
660 if (Z_TYPE(z_roptions
) == IS_ARRAY
) {
661 unsigned num
= zend_hash_num_elements(Z_ARRVAL(z_roptions
));
662 if (num
> num_options
) {
667 zend_hash_clean(options
);
669 ALLOC_HASHTABLE(options
);
670 ZEND_INIT_SYMTABLE_EX(options
, num_options
, 0);
672 if (Z_TYPE_P(z_coptions
) == IS_ARRAY
) {
673 array_copy(Z_ARRVAL_P(z_coptions
), options
);
675 if (Z_TYPE(z_roptions
) == IS_ARRAY
) {
676 array_join(Z_ARRVAL(z_roptions
), options
, 0, 0);
678 zval_ptr_dtor(&z_roptions
);
683 static void msg_queue_dtor(php_http_client_enqueue_t
*e
)
685 php_http_message_object_t
*msg_obj
= e
->opaque
;
687 zend_object_release(&msg_obj
->zo
);
688 zend_hash_destroy(e
->options
);
689 FREE_HASHTABLE(e
->options
);
691 if (e
->closure
.fci
.size
) {
692 zval_ptr_dtor(&e
->closure
.fci
.function_name
);
693 if (e
->closure
.fci
.object
) {
694 zend_object_release(e
->closure
.fci
.object
);
699 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enqueue
, 0, 0, 1)
700 ZEND_ARG_OBJ_INFO(0, request
, http
\\Client
\\Request
, 0)
701 ZEND_ARG_INFO(0, callable
)
703 static PHP_METHOD(HttpClient
, enqueue
)
706 zend_fcall_info fci
= empty_fcall_info
;
707 zend_fcall_info_cache fcc
= empty_fcall_info_cache
;
708 php_http_client_object_t
*obj
;
709 php_http_message_object_t
*msg_obj
;
710 php_http_client_enqueue_t q
= {0};
712 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O|f", &request
, php_http_get_client_request_class_entry(), &fci
, &fcc
), invalid_arg
, return);
714 obj
= PHP_HTTP_OBJ(NULL
, getThis());
715 msg_obj
= PHP_HTTP_OBJ(NULL
, request
);
717 if (php_http_client_enqueued(obj
->client
, msg_obj
->message
, NULL
)) {
718 php_http_throw(bad_method_call
, "Failed to enqueue request; request already in queue");
722 /* set early for progress callback */
725 if (obj
->client
->callback
.progress
.func
) {
726 php_http_client_progress_state_t progress
= {0};
728 progress
.info
= "prepare";
729 obj
->client
->callback
.progress
.func(obj
->client
->callback
.progress
.arg
, obj
->client
, &q
, &progress
);
733 q
.request
= msg_obj
->message
;
734 q
.options
= combined_options(NULL
, getThis(), request
);
735 q
.dtor
= msg_queue_dtor
;
741 Z_TRY_ADDREF(fci
.function_name
);
743 GC_ADDREF(fci
.object
);
747 php_http_expect(SUCCESS
== php_http_client_enqueue(obj
->client
, &q
), runtime
,
752 RETVAL_ZVAL(getThis(), 1, 0);
755 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_dequeue
, 0, 0, 1)
756 ZEND_ARG_OBJ_INFO(0, request
, http
\\Client
\\Request
, 0)
758 static PHP_METHOD(HttpClient
, dequeue
)
761 php_http_client_object_t
*obj
;
762 php_http_message_object_t
*msg_obj
;
764 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request
, php_http_get_client_request_class_entry()), invalid_arg
, return);
766 obj
= PHP_HTTP_OBJ(NULL
, getThis());
767 msg_obj
= PHP_HTTP_OBJ(NULL
, request
);
769 if (!php_http_client_enqueued(obj
->client
, msg_obj
->message
, NULL
)) {
770 php_http_throw(bad_method_call
, "Failed to dequeue request; request not in queue");
774 php_http_expect(SUCCESS
== php_http_client_dequeue(obj
->client
, msg_obj
->message
), runtime
, return);
776 RETVAL_ZVAL(getThis(), 1, 0);
779 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_requeue
, 0, 0, 1)
780 ZEND_ARG_OBJ_INFO(0, request
, http
\\Client
\\Request
, 0)
781 ZEND_ARG_INFO(0, callable
)
783 static PHP_METHOD(HttpClient
, requeue
)
786 zend_fcall_info fci
= empty_fcall_info
;
787 zend_fcall_info_cache fcc
= empty_fcall_info_cache
;
788 php_http_client_object_t
*obj
;
789 php_http_message_object_t
*msg_obj
;
790 php_http_client_enqueue_t q
, *e
;
792 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O|f", &request
, php_http_get_client_request_class_entry(), &fci
, &fcc
), invalid_arg
, return);
794 obj
= PHP_HTTP_OBJ(NULL
, getThis());
795 msg_obj
= PHP_HTTP_OBJ(NULL
, request
);
797 if ((e
= php_http_client_enqueued(obj
->client
, msg_obj
->message
, NULL
))) {
798 combined_options(e
->options
, getThis(), request
);
799 php_http_expect(SUCCESS
== php_http_client_requeue(obj
->client
, msg_obj
->message
), runtime
, return);
801 if (e
->closure
.fci
.size
) {
802 zval_ptr_dtor(&e
->closure
.fci
.function_name
);
803 if (e
->closure
.fci
.object
) {
804 zend_object_release(e
->closure
.fci
.object
);
807 Z_TRY_ADDREF(fci
.function_name
);
809 GC_ADDREF(fci
.object
);
812 RETURN_ZVAL(getThis(), 1, 0);
815 q
.request
= msg_obj
->message
;
816 q
.options
= combined_options(NULL
, getThis(), request
);
817 q
.dtor
= msg_queue_dtor
;
823 Z_TRY_ADDREF(fci
.function_name
);
825 GC_ADDREF(fci
.object
);
831 php_http_expect(SUCCESS
== php_http_client_enqueue(obj
->client
, &q
), runtime
,
836 RETVAL_ZVAL(getThis(), 1, 0);
839 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_count
, 0, 0, 0)
841 static PHP_METHOD(HttpClient
, count
)
843 zend_long count_mode
= -1;
845 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &count_mode
)) {
846 php_http_client_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
848 RETVAL_LONG(zend_llist_count(&obj
->client
->requests
));
852 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getResponse
, 0, 0, 0)
853 ZEND_ARG_OBJ_INFO(0, request
, http
\\Client
\\Request
, 1)
855 static PHP_METHOD(HttpClient
, getResponse
)
857 zval
*zrequest
= NULL
;
858 php_http_client_object_t
*obj
;
860 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|O", &zrequest
, php_http_get_client_request_class_entry()), invalid_arg
, return);
862 obj
= PHP_HTTP_OBJ(NULL
, getThis());
865 /* lookup the response with the request */
866 zend_llist_element
*el
= NULL
;
867 php_http_message_object_t
*req_obj
= PHP_HTTP_OBJ(NULL
, zrequest
);
869 for (el
= obj
->client
->responses
.head
; el
; el
= el
->next
) {
870 php_http_message_object_t
*response_obj
= *(php_http_message_object_t
**) el
->data
;
872 if (response_obj
->message
->parent
== req_obj
->message
) {
873 RETURN_OBJECT(&response_obj
->zo
, 1);
877 /* not found for the request! */
878 php_http_throw(unexpected_val
, "Could not find response for the request");
882 /* pop off the last response */
883 if (obj
->client
->responses
.tail
) {
884 php_http_message_object_t
*response_obj
= *(php_http_message_object_t
**) obj
->client
->responses
.tail
->data
;
888 RETVAL_OBJECT(&response_obj
->zo
, 1);
889 zend_llist_remove_tail(&obj
->client
->responses
);
894 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getHistory
, 0, 0, 0)
896 static PHP_METHOD(HttpClient
, getHistory
)
898 zval zhistory_tmp
, *zhistory
;
900 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
902 zhistory
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("history"), 0, &zhistory_tmp
);
903 RETVAL_ZVAL(zhistory
, 1, 0);
906 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_send
, 0, 0, 0)
908 static PHP_METHOD(HttpClient
, send
)
910 php_http_client_object_t
*obj
;
912 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
914 obj
= PHP_HTTP_OBJ(NULL
, getThis());
916 php_http_expect(SUCCESS
== php_http_client_exec(obj
->client
), runtime
, return);
918 RETVAL_ZVAL(getThis(), 1, 0);
921 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_once
, 0, 0, 0)
923 static PHP_METHOD(HttpClient
, once
)
925 if (SUCCESS
== zend_parse_parameters_none()) {
926 php_http_client_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
928 RETURN_BOOL(0 < php_http_client_once(obj
->client
));
932 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_wait
, 0, 0, 0)
933 ZEND_ARG_INFO(0, timeout
)
935 static PHP_METHOD(HttpClient
, wait
)
939 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|d", &timeout
)) {
940 struct timeval timeout_val
;
941 php_http_client_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
943 timeout_val
.tv_sec
= (time_t) timeout
;
944 timeout_val
.tv_usec
= PHP_HTTP_USEC(timeout
) % PHP_HTTP_MCROSEC
;
946 RETURN_BOOL(SUCCESS
== php_http_client_wait(obj
->client
, timeout
> 0 ? &timeout_val
: NULL
));
950 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_configure
, 0, 0, 1)
951 ZEND_ARG_ARRAY_INFO(0, settings
, 1)
953 static PHP_METHOD(HttpClient
, configure
)
955 HashTable
*settings
= NULL
;
956 php_http_client_object_t
*obj
;
958 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|H!", &settings
), invalid_arg
, return);
959 obj
= PHP_HTTP_OBJ(NULL
, getThis());
961 php_http_expect(SUCCESS
== php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_CONFIGURATION
, settings
), unexpected_val
, return);
963 RETVAL_ZVAL(getThis(), 1, 0);
966 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enablePipelining
, 0, 0, 0)
967 ZEND_ARG_INFO(0, enable
)
969 static PHP_METHOD(HttpClient
, enablePipelining
)
971 zend_bool enable
= 1;
972 php_http_client_object_t
*obj
;
974 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable
), invalid_arg
, return);
976 obj
= PHP_HTTP_OBJ(NULL
, getThis());
978 php_http_expect(SUCCESS
== php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING
, &enable
), unexpected_val
, return);
980 RETVAL_ZVAL(getThis(), 1, 0);
983 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enableEvents
, 0, 0, 0)
984 ZEND_ARG_INFO(0, enable
)
986 static PHP_METHOD(HttpClient
, enableEvents
)
988 zend_bool enable
= 1;
989 php_http_client_object_t
*obj
;
991 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable
), invalid_arg
, return);
993 obj
= PHP_HTTP_OBJ(NULL
, getThis());
995 php_http_expect(SUCCESS
== php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_USE_EVENTS
, &enable
), unexpected_val
, return);
997 RETVAL_ZVAL(getThis(), 1, 0);
1001 php_http_object_method_t
*cb
;
1006 static int notify(zend_object_iterator
*iter
, void *puser
)
1009 struct notify_arg
*arg
= puser
;
1011 if ((observer
= iter
->funcs
->get_current_data(iter
))) {
1012 if (SUCCESS
== php_http_object_method_call(arg
->cb
, observer
, NULL
, arg
->argc
, arg
->args
)) {
1013 return ZEND_HASH_APPLY_KEEP
;
1016 return ZEND_HASH_APPLY_STOP
;
1019 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_notify
, 0, 0, 0)
1020 ZEND_ARG_OBJ_INFO(0, request
, http
\\Client
\\Request
, 1)
1021 ZEND_ARG_INFO(0, progress
)
1022 ZEND_END_ARG_INFO();
1023 static PHP_METHOD(HttpClient
, notify
)
1025 zval
*request
= NULL
, *zprogress
= NULL
, observers_tmp
, *observers
;
1026 php_http_client_object_t
*client_obj
;
1027 struct notify_arg arg
= {NULL
};
1029 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|O!o!", &request
, php_http_get_client_request_class_entry(), &zprogress
), invalid_arg
, return);
1031 client_obj
= PHP_HTTP_OBJ(NULL
, getThis());
1032 observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0, &observers_tmp
);
1034 if (Z_TYPE_P(observers
) != IS_OBJECT
) {
1035 php_http_throw(unexpected_val
, "Observer storage is corrupted");
1039 if (client_obj
->update
) {
1040 arg
.cb
= client_obj
->update
;
1041 ZVAL_COPY(&arg
.args
[0], getThis());
1045 ZVAL_COPY(&arg
.args
[1], request
);
1049 ZVAL_COPY(&arg
.args
[2], zprogress
);
1053 spl_iterator_apply(observers
, notify
, &arg
);
1055 zval_ptr_dtor(getThis());
1057 zval_ptr_dtor(request
);
1060 zval_ptr_dtor(zprogress
);
1064 RETVAL_ZVAL(getThis(), 1, 0);
1067 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_attach
, 0, 0, 1)
1068 ZEND_ARG_OBJ_INFO(0, observer
, SplObserver
, 0)
1069 ZEND_END_ARG_INFO();
1070 static PHP_METHOD(HttpClient
, attach
)
1072 zval observers_tmp
, *observers
, *observer
, retval
;
1073 php_http_client_object_t
*client_obj
;
1075 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &observer
, spl_ce_SplObserver
), invalid_arg
, return);
1077 client_obj
= PHP_HTTP_OBJ(NULL
, getThis());
1078 observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0, &observers_tmp
);
1080 if (Z_TYPE_P(observers
) != IS_OBJECT
) {
1081 php_http_throw(unexpected_val
, "Observer storage is corrupted");
1085 if (!client_obj
->update
) {
1086 client_obj
->update
= php_http_object_method_init(NULL
, observer
, ZEND_STRL("update"));
1089 ZVAL_UNDEF(&retval
);
1090 zend_call_method_with_1_params(observers
, NULL
, NULL
, "attach", &retval
, observer
);
1091 zval_ptr_dtor(&retval
);
1093 RETVAL_ZVAL(getThis(), 1, 0);
1096 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_detach
, 0, 0, 1)
1097 ZEND_ARG_OBJ_INFO(0, observer
, SplObserver
, 0)
1098 ZEND_END_ARG_INFO();
1099 static PHP_METHOD(HttpClient
, detach
)
1101 zval observers_tmp
, *observers
, *observer
, retval
;
1103 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &observer
, spl_ce_SplObserver
), invalid_arg
, return);
1105 observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0, &observers_tmp
);
1107 if (Z_TYPE_P(observers
) != IS_OBJECT
) {
1108 php_http_throw(unexpected_val
, "Observer storage is corrupted");
1112 ZVAL_UNDEF(&retval
);
1113 zend_call_method_with_1_params(observers
, NULL
, NULL
, "detach", &retval
, observer
);
1114 zval_ptr_dtor(&retval
);
1116 RETVAL_ZVAL(getThis(), 1, 0);
1119 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getObservers
, 0, 0, 0)
1120 ZEND_END_ARG_INFO();
1121 static PHP_METHOD(HttpClient
, getObservers
)
1123 zval observers_tmp
, *observers
;
1125 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1127 observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0, &observers_tmp
);
1129 if (Z_TYPE_P(observers
) != IS_OBJECT
) {
1130 php_http_throw(unexpected_val
, "Observer storage is corrupted");
1134 RETVAL_ZVAL(observers
, 1, 0);
1137 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getProgressInfo
, 0, 0, 1)
1138 ZEND_ARG_OBJ_INFO(0, request
, http
\\Client
\\Request
, 0)
1139 ZEND_END_ARG_INFO();
1140 static PHP_METHOD(HttpClient
, getProgressInfo
)
1143 php_http_client_object_t
*obj
;
1144 php_http_message_object_t
*req_obj
;
1145 php_http_client_progress_state_t
*progress
;
1147 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request
, php_http_get_client_request_class_entry()), invalid_arg
, return);
1149 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1150 req_obj
= PHP_HTTP_OBJ(NULL
, request
);
1152 php_http_expect(SUCCESS
== php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, req_obj
->message
, &progress
), unexpected_val
, return);
1154 object_init(return_value
);
1155 add_property_bool(return_value
, "started", progress
->started
);
1156 add_property_bool(return_value
, "finished", progress
->finished
);
1157 add_property_string(return_value
, "info", STR_PTR(progress
->info
));
1158 add_property_double(return_value
, "dltotal", progress
->dl
.total
);
1159 add_property_double(return_value
, "dlnow", progress
->dl
.now
);
1160 add_property_double(return_value
, "ultotal", progress
->ul
.total
);
1161 add_property_double(return_value
, "ulnow", progress
->ul
.now
);
1164 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getTransferInfo
, 0, 0, 1)
1165 ZEND_ARG_OBJ_INFO(0, request
, http
\\Client
\\Request
, 0)
1166 ZEND_END_ARG_INFO();
1167 static PHP_METHOD(HttpClient
, getTransferInfo
)
1171 php_http_client_object_t
*obj
;
1172 php_http_message_object_t
*req_obj
;
1174 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request
, php_http_get_client_request_class_entry()), invalid_arg
, return);
1176 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1177 req_obj
= PHP_HTTP_OBJ(NULL
, request
);
1179 object_init(return_value
);
1180 info
= HASH_OF(return_value
);
1181 php_http_expect(SUCCESS
== php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
, req_obj
->message
, &info
), unexpected_val
, return);
1184 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setOptions
, 0, 0, 0)
1185 ZEND_ARG_ARRAY_INFO(0, options
, 1)
1186 ZEND_END_ARG_INFO();
1187 static PHP_METHOD(HttpClient
, setOptions
)
1191 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts
), invalid_arg
, return);
1193 php_http_client_options_set(getThis(), opts
);
1195 RETVAL_ZVAL(getThis(), 1, 0);
1198 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getOptions
, 0, 0, 0)
1199 ZEND_END_ARG_INFO();
1200 static PHP_METHOD(HttpClient
, getOptions
)
1202 if (SUCCESS
== zend_parse_parameters_none()) {
1203 zval options_tmp
, *options
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("options"), 0, &options_tmp
);
1204 RETVAL_ZVAL(options
, 1, 0);
1208 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setSslOptions
, 0, 0, 0)
1209 ZEND_ARG_ARRAY_INFO(0, ssl_option
, 1)
1210 ZEND_END_ARG_INFO();
1211 static PHP_METHOD(HttpClient
, setSslOptions
)
1215 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts
), invalid_arg
, return);
1217 php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts
, 1);
1219 RETVAL_ZVAL(getThis(), 1, 0);
1222 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addSslOptions
, 0, 0, 0)
1223 ZEND_ARG_ARRAY_INFO(0, ssl_options
, 1)
1224 ZEND_END_ARG_INFO();
1225 static PHP_METHOD(HttpClient
, addSslOptions
)
1229 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts
), invalid_arg
, return);
1231 php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts
, 0);
1233 RETVAL_ZVAL(getThis(), 1, 0);
1236 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getSslOptions
, 0, 0, 0)
1237 ZEND_END_ARG_INFO();
1238 static PHP_METHOD(HttpClient
, getSslOptions
)
1240 if (SUCCESS
== zend_parse_parameters_none()) {
1241 php_http_client_options_get_subr(getThis(), ZEND_STRL("ssl"), return_value
);
1245 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setCookies
, 0, 0, 0)
1246 ZEND_ARG_ARRAY_INFO(0, cookies
, 1)
1247 ZEND_END_ARG_INFO();
1248 static PHP_METHOD(HttpClient
, setCookies
)
1252 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts
), invalid_arg
, return);
1254 php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts
, 1);
1256 RETVAL_ZVAL(getThis(), 1, 0);
1259 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addCookies
, 0, 0, 0)
1260 ZEND_ARG_ARRAY_INFO(0, cookies
, 1)
1261 ZEND_END_ARG_INFO();
1262 static PHP_METHOD(HttpClient
, addCookies
)
1266 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts
), invalid_arg
, return);
1268 php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts
, 0);
1270 RETVAL_ZVAL(getThis(), 1, 0);
1273 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getCookies
, 0, 0, 0)
1274 ZEND_END_ARG_INFO();
1275 static PHP_METHOD(HttpClient
, getCookies
)
1277 if (SUCCESS
== zend_parse_parameters_none()) {
1278 php_http_client_options_get_subr(getThis(), ZEND_STRL("cookies"), return_value
);
1282 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableDrivers
, 0, 0, 0)
1283 ZEND_END_ARG_INFO();
1284 static PHP_METHOD(HttpClient
, getAvailableDrivers
)
1286 if (SUCCESS
== zend_parse_parameters_none()) {
1287 array_init(return_value
);
1288 php_http_client_driver_list(Z_ARRVAL_P(return_value
));
1292 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableOptions
, 0, 0, 0)
1293 ZEND_END_ARG_INFO();
1294 static PHP_METHOD(HttpClient
, getAvailableOptions
)
1296 if (SUCCESS
== zend_parse_parameters_none()) {
1297 php_http_client_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1299 array_init(return_value
);
1300 php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS
, NULL
, &Z_ARRVAL_P(return_value
));
1304 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableConfiguration
, 0, 0, 0)
1305 ZEND_END_ARG_INFO();
1306 static PHP_METHOD(HttpClient
, getAvailableConfiguration
)
1308 if (SUCCESS
== zend_parse_parameters_none()) {
1309 php_http_client_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1311 array_init(return_value
);
1312 php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION
, NULL
, &Z_ARRVAL_P(return_value
));
1316 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setDebug
, 0, 0, 1)
1317 /* using IS_CALLABLE type hint would create a forwards compatibility break */
1318 ZEND_ARG_INFO(0, callback
)
1319 ZEND_END_ARG_INFO();
1320 static PHP_METHOD(HttpClient
, setDebug
)
1322 zend_fcall_info fci
;
1323 zend_fcall_info_cache fcc
;
1324 php_http_client_object_t
*client_obj
;
1327 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|f", &fci
, &fcc
), invalid_arg
, return);
1329 client_obj
= PHP_HTTP_OBJ(NULL
, getThis());
1331 if (client_obj
->debug
.fci
.size
> 0) {
1332 zval_ptr_dtor(&client_obj
->debug
.fci
.function_name
);
1333 client_obj
->debug
.fci
.size
= 0;
1336 memcpy(&client_obj
->debug
.fci
, &fci
, sizeof(fci
));
1337 memcpy(&client_obj
->debug
.fcc
, &fcc
, sizeof(fcc
));
1338 Z_ADDREF_P(&fci
.function_name
);
1339 client_obj
->client
->callback
.debug
.func
= handle_debug
;
1340 client_obj
->client
->callback
.debug
.arg
= client_obj
;
1342 client_obj
->client
->callback
.debug
.func
= NULL
;
1343 client_obj
->client
->callback
.debug
.arg
= NULL
;
1346 RETVAL_ZVAL(getThis(), 1, 0);
1349 static zend_function_entry php_http_client_methods
[] = {
1350 PHP_ME(HttpClient
, __construct
, ai_HttpClient_construct
, ZEND_ACC_PUBLIC
)
1351 PHP_ME(HttpClient
, reset
, ai_HttpClient_reset
, ZEND_ACC_PUBLIC
)
1352 PHP_ME(HttpClient
, enqueue
, ai_HttpClient_enqueue
, ZEND_ACC_PUBLIC
)
1353 PHP_ME(HttpClient
, dequeue
, ai_HttpClient_dequeue
, ZEND_ACC_PUBLIC
)
1354 PHP_ME(HttpClient
, requeue
, ai_HttpClient_requeue
, ZEND_ACC_PUBLIC
)
1355 PHP_ME(HttpClient
, count
, ai_HttpClient_count
, ZEND_ACC_PUBLIC
)
1356 PHP_ME(HttpClient
, send
, ai_HttpClient_send
, ZEND_ACC_PUBLIC
)
1357 PHP_ME(HttpClient
, once
, ai_HttpClient_once
, ZEND_ACC_PUBLIC
)
1358 PHP_ME(HttpClient
, wait
, ai_HttpClient_wait
, ZEND_ACC_PUBLIC
)
1359 PHP_ME(HttpClient
, getResponse
, ai_HttpClient_getResponse
, ZEND_ACC_PUBLIC
)
1360 PHP_ME(HttpClient
, getHistory
, ai_HttpClient_getHistory
, ZEND_ACC_PUBLIC
)
1361 PHP_ME(HttpClient
, configure
, ai_HttpClient_configure
, ZEND_ACC_PUBLIC
)
1362 PHP_ME(HttpClient
, enablePipelining
, ai_HttpClient_enablePipelining
, ZEND_ACC_PUBLIC
|ZEND_ACC_DEPRECATED
)
1363 PHP_ME(HttpClient
, enableEvents
, ai_HttpClient_enableEvents
, ZEND_ACC_PUBLIC
|ZEND_ACC_DEPRECATED
)
1364 PHP_ME(HttpClient
, notify
, ai_HttpClient_notify
, ZEND_ACC_PUBLIC
)
1365 PHP_ME(HttpClient
, attach
, ai_HttpClient_attach
, ZEND_ACC_PUBLIC
)
1366 PHP_ME(HttpClient
, detach
, ai_HttpClient_detach
, ZEND_ACC_PUBLIC
)
1367 PHP_ME(HttpClient
, getObservers
, ai_HttpClient_getObservers
, ZEND_ACC_PUBLIC
)
1368 PHP_ME(HttpClient
, getProgressInfo
, ai_HttpClient_getProgressInfo
, ZEND_ACC_PUBLIC
)
1369 PHP_ME(HttpClient
, getTransferInfo
, ai_HttpClient_getTransferInfo
, ZEND_ACC_PUBLIC
)
1370 PHP_ME(HttpClient
, setOptions
, ai_HttpClient_setOptions
, ZEND_ACC_PUBLIC
)
1371 PHP_ME(HttpClient
, getOptions
, ai_HttpClient_getOptions
, ZEND_ACC_PUBLIC
)
1372 PHP_ME(HttpClient
, setSslOptions
, ai_HttpClient_setSslOptions
, ZEND_ACC_PUBLIC
)
1373 PHP_ME(HttpClient
, addSslOptions
, ai_HttpClient_addSslOptions
, ZEND_ACC_PUBLIC
)
1374 PHP_ME(HttpClient
, getSslOptions
, ai_HttpClient_getSslOptions
, ZEND_ACC_PUBLIC
)
1375 PHP_ME(HttpClient
, setCookies
, ai_HttpClient_setCookies
, ZEND_ACC_PUBLIC
)
1376 PHP_ME(HttpClient
, addCookies
, ai_HttpClient_addCookies
, ZEND_ACC_PUBLIC
)
1377 PHP_ME(HttpClient
, getCookies
, ai_HttpClient_getCookies
, ZEND_ACC_PUBLIC
)
1378 PHP_ME(HttpClient
, getAvailableDrivers
, ai_HttpClient_getAvailableDrivers
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
1379 PHP_ME(HttpClient
, getAvailableOptions
, ai_HttpClient_getAvailableOptions
, ZEND_ACC_PUBLIC
)
1380 PHP_ME(HttpClient
, getAvailableConfiguration
, ai_HttpClient_getAvailableConfiguration
, ZEND_ACC_PUBLIC
)
1381 PHP_ME(HttpClient
, setDebug
, ai_HttpClient_setDebug
, ZEND_ACC_PUBLIC
)
1382 EMPTY_FUNCTION_ENTRY
1385 PHP_MINIT_FUNCTION(http_client
)
1387 zend_class_entry ce
= {0};
1389 INIT_NS_CLASS_ENTRY(ce
, "http", "Client", php_http_client_methods
);
1390 php_http_client_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
1391 php_http_client_class_entry
->create_object
= php_http_client_object_new
;
1392 zend_class_implements(php_http_client_class_entry
, 2, spl_ce_SplSubject
, spl_ce_Countable
);
1393 memcpy(&php_http_client_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
1394 php_http_client_object_handlers
.offset
= XtOffsetOf(php_http_client_object_t
, zo
);
1395 php_http_client_object_handlers
.free_obj
= php_http_client_object_free
;
1396 php_http_client_object_handlers
.clone_obj
= NULL
;
1397 php_http_client_object_handlers
.get_gc
= php_http_client_object_get_gc
;
1398 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("observers"), ZEND_ACC_PRIVATE
);
1399 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("options"), ZEND_ACC_PROTECTED
);
1400 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("history"), ZEND_ACC_PROTECTED
);
1401 zend_declare_property_bool(php_http_client_class_entry
, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC
);
1403 zend_declare_class_constant_long(php_http_client_class_entry
, ZEND_STRL("DEBUG_INFO"), PHP_HTTP_CLIENT_DEBUG_INFO
);
1404 zend_declare_class_constant_long(php_http_client_class_entry
, ZEND_STRL("DEBUG_IN"), PHP_HTTP_CLIENT_DEBUG_IN
);
1405 zend_declare_class_constant_long(php_http_client_class_entry
, ZEND_STRL("DEBUG_OUT"), PHP_HTTP_CLIENT_DEBUG_OUT
);
1406 zend_declare_class_constant_long(php_http_client_class_entry
, ZEND_STRL("DEBUG_HEADER"), PHP_HTTP_CLIENT_DEBUG_HEADER
);
1407 zend_declare_class_constant_long(php_http_client_class_entry
, ZEND_STRL("DEBUG_BODY"), PHP_HTTP_CLIENT_DEBUG_BODY
);
1408 zend_declare_class_constant_long(php_http_client_class_entry
, ZEND_STRL("DEBUG_SSL"), PHP_HTTP_CLIENT_DEBUG_SSL
);
1410 zend_hash_init(&php_http_client_drivers
, 2, NULL
, php_http_client_driver_hash_dtor
, 1);
1415 PHP_MSHUTDOWN_FUNCTION(http_client
)
1417 zend_hash_destroy(&php_http_client_drivers
);
1426 * vim600: noet sw=4 ts=4 fdm=marker
1427 * vim<600: noet sw=4 ts=4