Merge branch 'v2.6.x'
[m6w6/ext-http] / src / php_http_client.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 #include "php_http_client.h"
15
16 #include "ext/spl/spl_observer.h"
17
18 /*
19 * array of name => php_http_client_driver_t*
20 */
21 static HashTable php_http_client_drivers;
22
23 static void php_http_client_driver_hash_dtor(zval *pData)
24 {
25 pefree(Z_PTR_P(pData), 1);
26 }
27
28 ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver)
29 {
30 return zend_hash_add_mem(&php_http_client_drivers, driver->driver_name, (void *) driver, sizeof(php_http_client_driver_t))
31 ? SUCCESS : FAILURE;
32 }
33
34 php_http_client_driver_t *php_http_client_driver_get(zend_string *name)
35 {
36 zval *ztmp;
37 php_http_client_driver_t *tmp;
38
39 if (name && (tmp = zend_hash_find_ptr(&php_http_client_drivers, name))) {
40 return tmp;
41 }
42 if ((ztmp = zend_hash_get_current_data(&php_http_client_drivers))) {
43 return Z_PTR_P(ztmp);
44 }
45 return NULL;
46 }
47
48 static int apply_driver_list(zval *p, void *arg)
49 {
50 php_http_client_driver_t *d = Z_PTR_P(p);
51 zval zname;
52
53 ZVAL_STR_COPY(&zname, d->driver_name);
54
55 zend_hash_next_index_insert(arg, &zname);
56 return ZEND_HASH_APPLY_KEEP;
57 }
58
59 void php_http_client_driver_list(HashTable *ht)
60 {
61 zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht);
62 }
63
64 static zend_class_entry *php_http_client_class_entry;
65 zend_class_entry *php_http_client_get_class_entry(void)
66 {
67 return php_http_client_class_entry;
68 }
69
70 void php_http_client_options_set_subr(zval *instance, char *key, size_t len, zval *opts, int overwrite)
71 {
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;
75
76 array_init(&new_opts);
77 old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp);
78 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
79 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL(new_opts));
80 }
81
82 if (overwrite) {
83 if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
84 Z_ADDREF_P(opts);
85 zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
86 } else {
87 zend_symtable_str_del(Z_ARRVAL(new_opts), key, len);
88 }
89 } else if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
90 if ((entry = zend_symtable_str_find(Z_ARRVAL(new_opts), key, len))) {
91 array_join(Z_ARRVAL_P(opts), Z_ARRVAL_P(entry), 0, 0);
92 } else {
93 Z_ADDREF_P(opts);
94 zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
95 }
96 }
97
98 zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
99 zval_ptr_dtor(&new_opts);
100 }
101 }
102
103 void php_http_client_options_set(zval *instance, zval *opts)
104 {
105 php_http_arrkey_t key;
106 zval new_opts;
107 zend_class_entry *this_ce = Z_OBJCE_P(instance);
108 zend_bool is_client = instanceof_function(this_ce, php_http_client_class_entry);
109
110 array_init(&new_opts);
111
112 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
113 zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
114 zval_ptr_dtor(&new_opts);
115 } else {
116 zval old_opts_tmp, *old_opts, add_opts, *opt;
117
118 array_init(&add_opts);
119 /* some options need extra attention -- thus cannot use array_merge() directly */
120 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(opts), key.h, key.key, opt)
121 {
122 if (key.key) {
123 if (Z_TYPE_P(opt) == IS_ARRAY && (zend_string_equals_literal(key.key, "ssl") || zend_string_equals_literal(key.key, "cookies"))) {
124 php_http_client_options_set_subr(instance, key.key->val, key.key->len, opt, 0);
125 } else if (is_client && (zend_string_equals_literal(key.key, "recordHistory") || zend_string_equals_literal(key.key, "responseMessageClass"))) {
126 zend_update_property(this_ce, instance, key.key->val, key.key->len, opt);
127 } else if (Z_TYPE_P(opt) == IS_NULL) {
128 old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp);
129 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
130 zend_symtable_del(Z_ARRVAL_P(old_opts), key.key);
131 }
132 } else {
133 Z_TRY_ADDREF_P(opt);
134 add_assoc_zval_ex(&add_opts, key.key->val, key.key->len, opt);
135 }
136 }
137 }
138 ZEND_HASH_FOREACH_END();
139
140 old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp);
141 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
142 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL(new_opts));
143 }
144 array_join(Z_ARRVAL(add_opts), Z_ARRVAL(new_opts), 0, 0);
145 zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
146 zval_ptr_dtor(&new_opts);
147 zval_ptr_dtor(&add_opts);
148 }
149 }
150
151 void php_http_client_options_get_subr(zval *instance, char *key, size_t len, zval *return_value)
152 {
153 zend_class_entry *this_ce = Z_OBJCE_P(instance);
154 zval *options, opts_tmp, *opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &opts_tmp);
155
156 if ((Z_TYPE_P(opts) == IS_ARRAY) && (options = zend_symtable_str_find(Z_ARRVAL_P(opts), key, len))) {
157 RETVAL_ZVAL(options, 1, 0);
158 }
159 }
160
161 static void queue_dtor(void *enqueued)
162 {
163 php_http_client_enqueue_t *e = enqueued;
164
165 if (e->dtor) {
166 e->dtor(e);
167 }
168 }
169
170 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)
171 {
172 php_http_client_t *free_h = NULL;
173
174 if (!h) {
175 free_h = h = emalloc(sizeof(*h));
176 }
177 memset(h, 0, sizeof(*h));
178
179 h->ops = ops;
180 if (rf) {
181 h->rf = rf;
182 } else if (ops->rsrc) {
183 h->rf = php_resource_factory_init(NULL, h->ops->rsrc, h, NULL);
184 }
185 zend_llist_init(&h->requests, sizeof(php_http_client_enqueue_t), queue_dtor, 0);
186 zend_llist_init(&h->responses, sizeof(void *), NULL, 0);
187
188 if (h->ops->init) {
189 if (!(h = h->ops->init(h, init_arg))) {
190 php_error_docref(NULL, E_WARNING, "Could not initialize client");
191 PTR_FREE(free_h);
192 }
193 }
194
195 return h;
196 }
197
198 php_http_client_t *php_http_client_copy(php_http_client_t *from, php_http_client_t *to)
199 {
200 if (from->ops->copy) {
201 return from->ops->copy(from, to);
202 }
203
204 return NULL;
205 }
206
207 void php_http_client_dtor(php_http_client_t *h)
208 {
209 php_http_client_reset(h);
210
211 if (h->ops->dtor) {
212 h->ops->dtor(h);
213 }
214 h->callback.debug.func = NULL;
215 h->callback.debug.arg = NULL;
216
217 php_resource_factory_free(&h->rf);
218 }
219
220 void php_http_client_free(php_http_client_t **h) {
221 if (*h) {
222 php_http_client_dtor(*h);
223 efree(*h);
224 *h = NULL;
225 }
226 }
227
228 ZEND_RESULT_CODE php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
229 {
230 if (h->ops->enqueue) {
231 if (php_http_client_enqueued(h, enqueue->request, NULL)) {
232 php_error_docref(NULL, E_WARNING, "Failed to enqueue request; request already in queue");
233 return FAILURE;
234 }
235 return h->ops->enqueue(h, enqueue);
236 }
237
238 return FAILURE;
239 }
240
241 ZEND_RESULT_CODE php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request)
242 {
243 if (h->ops->dequeue) {
244 php_http_client_enqueue_t *enqueue = php_http_client_enqueued(h, request, NULL);
245
246 if (!enqueue) {
247 php_error_docref(NULL, E_WARNING, "Failed to dequeue request; request not in queue");
248 return FAILURE;
249 }
250 return h->ops->dequeue(h, enqueue);
251 }
252 return FAILURE;
253 }
254
255 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)
256 {
257 zend_llist_element *el = NULL;
258
259 if (compare_func) {
260 for (el = h->requests.head; el; el = el->next) {
261 if (compare_func((php_http_client_enqueue_t *) el->data, compare_arg)) {
262 break;
263 }
264 }
265 } else {
266 for (el = h->requests.head; el; el = el->next) {
267 if (((php_http_client_enqueue_t *) el->data)->request == compare_arg) {
268 break;
269 }
270 }
271 }
272 return el ? (php_http_client_enqueue_t *) el->data : NULL;
273 }
274
275 ZEND_RESULT_CODE php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout)
276 {
277 if (h->ops->wait) {
278 return h->ops->wait(h, custom_timeout);
279 }
280
281 return FAILURE;
282 }
283
284 int php_http_client_once(php_http_client_t *h)
285 {
286 if (h->ops->once) {
287 return h->ops->once(h);
288 }
289
290 return FAILURE;
291 }
292
293 ZEND_RESULT_CODE php_http_client_exec(php_http_client_t *h)
294 {
295 if (h->ops->exec) {
296 return h->ops->exec(h);
297 }
298
299 return FAILURE;
300 }
301
302 void php_http_client_reset(php_http_client_t *h)
303 {
304 if (h->ops->reset) {
305 h->ops->reset(h);
306 }
307
308 zend_llist_clean(&h->requests);
309 zend_llist_clean(&h->responses);
310 }
311
312 ZEND_RESULT_CODE php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
313 {
314 if (h->ops->setopt) {
315 return h->ops->setopt(h, opt, arg);
316 }
317
318 return FAILURE;
319 }
320
321 ZEND_RESULT_CODE php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr)
322 {
323 if (h->ops->getopt) {
324 return h->ops->getopt(h, opt, arg, res_ptr);
325 }
326 return FAILURE;
327 }
328
329 static zend_object_handlers php_http_client_object_handlers;
330
331 void php_http_client_object_free(zend_object *object)
332 {
333 php_http_client_object_t *o = PHP_HTTP_OBJ(object, NULL);
334
335 PTR_FREE(o->gc);
336
337 php_http_client_free(&o->client);
338 if (o->debug.fci.size > 0) {
339 zend_fcall_info_args_clear(&o->debug.fci, 1);
340 zval_ptr_dtor(&o->debug.fci.function_name);
341 o->debug.fci.size = 0;
342 }
343 php_http_object_method_dtor(&o->notify);
344 php_http_object_method_free(&o->update);
345 zend_object_std_dtor(object);
346 }
347
348 php_http_client_object_t *php_http_client_object_new_ex(zend_class_entry *ce, php_http_client_t *client)
349 {
350 php_http_client_object_t *o;
351
352 o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
353 zend_object_std_init(&o->zo, ce);
354 object_properties_init(&o->zo, ce);
355
356 o->client = client;
357
358 o->zo.handlers = &php_http_client_object_handlers;
359
360 return o;
361 }
362
363 zend_object *php_http_client_object_new(zend_class_entry *ce)
364 {
365 return &php_http_client_object_new_ex(ce, NULL)->zo;
366 }
367
368 static HashTable *php_http_client_object_get_gc(zval *object, zval **table, int *n)
369 {
370 php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, object);
371 zend_llist_element *el = NULL;
372 HashTable *props = Z_OBJPROP_P(object);
373 uint32_t count = zend_hash_num_elements(props) + zend_llist_count(&obj->client->responses) + zend_llist_count(&obj->client->requests) + 2;
374 zval *val;
375
376 *n = 0;
377 *table = obj->gc = erealloc(obj->gc, sizeof(zval) * count);
378
379 #if PHP_HTTP_HAVE_CURL
380 if (obj->client->ops == php_http_client_curl_get_ops()) {
381 php_http_client_curl_t *curl = obj->client->ctx;
382
383 if (curl->ev_ops == php_http_client_curl_user_ops_get()) {
384 php_http_client_curl_user_context_t *ctx = curl->ev_ctx;
385
386 ZVAL_COPY_VALUE(&obj->gc[(*n)++], &ctx->user);
387 }
388 }
389 #endif
390
391 if (obj->debug.fci.size > 0) {
392 ZVAL_COPY_VALUE(&obj->gc[(*n)++], &obj->debug.fci.function_name);
393 }
394
395 for (el = obj->client->responses.head; el; el = el->next) {
396 php_http_message_object_t *response_obj = *(php_http_message_object_t **) el->data;
397 ZVAL_OBJ(&obj->gc[(*n)++], &response_obj->zo);
398 }
399
400 for (el = obj->client->requests.head; el; el = el->next) {
401 php_http_client_enqueue_t *q = (php_http_client_enqueue_t *) el->data;
402 php_http_message_object_t *request_obj = q->opaque; /* FIXME */
403 ZVAL_OBJ(&obj->gc[(*n)++], &request_obj->zo);
404 }
405
406 ZEND_HASH_FOREACH_VAL(props, val)
407 {
408 ZVAL_COPY_VALUE(&obj->gc[(*n)++], val);
409 }
410 ZEND_HASH_FOREACH_END();
411
412 return NULL;
413 }
414
415 static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response)
416 {
417 zval new_hist, old_hist_tmp, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0, &old_hist_tmp);
418 php_http_message_t *req_copy = php_http_message_copy(request, NULL);
419 php_http_message_t *res_copy = php_http_message_copy(response, NULL);
420 php_http_message_t *zipped = php_http_message_zip(res_copy, req_copy);
421 php_http_message_object_t *obj = php_http_message_object_new_ex(php_http_message_get_class_entry(), zipped);
422
423 ZVAL_OBJ(&new_hist, &obj->zo);
424
425 if (Z_TYPE_P(old_hist) == IS_OBJECT) {
426 php_http_message_object_prepend(&new_hist, old_hist, 1);
427 }
428
429 zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), &new_hist);
430 zval_ptr_dtor(&new_hist);
431 }
432
433 static ZEND_RESULT_CODE handle_response(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_message_t **response)
434 {
435 zend_bool dequeue = 0;
436 zval zclient;
437 php_http_message_t *msg;
438 php_http_client_progress_state_t *progress;
439
440 ZVAL_OBJ(&zclient, &((php_http_client_object_t*) arg)->zo);
441
442 if ((msg = *response)) {
443 php_http_message_object_t *msg_obj;
444 zval info, zresponse, zrequest, rec_hist_tmp;
445 HashTable *info_ht;
446
447 /* ensure the message is of type response (could be uninitialized in case of early error, like DNS) */
448 php_http_message_set_type(msg, PHP_HTTP_RESPONSE);
449
450 if (zend_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0, &rec_hist_tmp))) {
451 handle_history(&zclient, e->request, *response);
452 }
453
454 /* hard detach, redirects etc. are in the history */
455 php_http_message_free(&msg->parent);
456 *response = NULL;
457
458 msg_obj = php_http_message_object_new_ex(php_http_get_client_response_class_entry(), msg);
459 ZVAL_OBJECT(&zresponse, &msg_obj->zo, 1);
460 ZVAL_OBJECT(&zrequest, &((php_http_message_object_t *) e->opaque)->zo, 1);
461
462 php_http_message_object_prepend(&zresponse, &zrequest, 1);
463
464 object_init(&info);
465 info_ht = HASH_OF(&info);
466 php_http_client_getopt(client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, e->request, &info_ht);
467 zend_update_property(php_http_get_client_response_class_entry(), &zresponse, ZEND_STRL("transferInfo"), &info);
468 zval_ptr_dtor(&info);
469
470 zend_llist_add_element(&client->responses, &msg_obj);
471
472 if (e->closure.fci.size) {
473 zval retval;
474 zend_error_handling zeh;
475
476 ZVAL_UNDEF(&retval);
477 zend_fcall_info_argn(&e->closure.fci, 1, &zresponse);
478 zend_replace_error_handling(EH_NORMAL, NULL, &zeh);
479 ++client->callback.depth;
480 zend_fcall_info_call(&e->closure.fci, &e->closure.fcc, &retval, NULL);
481 --client->callback.depth;
482 zend_restore_error_handling(&zeh);
483 zend_fcall_info_argn(&e->closure.fci, 0);
484
485 if (Z_TYPE(retval) == IS_TRUE) {
486 dequeue = 1;
487 }
488 zval_ptr_dtor(&retval);
489 }
490
491 zval_ptr_dtor(&zresponse);
492 zval_ptr_dtor(&zrequest);
493 }
494
495 if (SUCCESS == php_http_client_getopt(client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, e->request, &progress)) {
496 progress->info = "finished";
497 progress->finished = 1;
498 client->callback.progress.func(client->callback.progress.arg, client, e, progress);
499 }
500
501 if (dequeue) {
502 php_http_client_dequeue(client, e->request);
503 }
504
505 return SUCCESS;
506 }
507
508 static void handle_progress(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_client_progress_state_t *progress)
509 {
510 zval zclient, args[2];
511 php_http_client_object_t *client_obj = arg;
512 zend_error_handling zeh;
513
514 ZVAL_OBJECT(&zclient, &client_obj->zo, 1);
515 ZVAL_OBJECT(&args[0], &((php_http_message_object_t *) e->opaque)->zo, 1);
516 object_init(&args[1]);
517 add_property_bool(&args[1], "started", progress->started);
518 add_property_bool(&args[1], "finished", progress->finished);
519 add_property_string(&args[1], "info", STR_PTR(progress->info));
520 add_property_double(&args[1], "dltotal", progress->dl.total);
521 add_property_double(&args[1], "dlnow", progress->dl.now);
522 add_property_double(&args[1], "ultotal", progress->ul.total);
523 add_property_double(&args[1], "ulnow", progress->ul.now);
524
525 zend_replace_error_handling(EH_NORMAL, NULL, &zeh);
526 ++client->callback.depth;
527 php_http_object_method_call(&client_obj->notify, &zclient, NULL, 2, args);
528 --client->callback.depth;
529 zend_restore_error_handling(&zeh);
530
531 zval_ptr_dtor(&zclient);
532 zval_ptr_dtor(&args[0]);
533 zval_ptr_dtor(&args[1]);
534 }
535
536 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)
537 {
538 zval ztype, zdata, zreq, zclient;
539 php_http_client_object_t *client_obj = arg;
540 zend_error_handling zeh;
541
542 ZVAL_OBJECT(&zclient, &client_obj->zo, 1);
543 ZVAL_OBJECT(&zreq, &((php_http_message_object_t *) e->opaque)->zo, 1);
544 ZVAL_LONG(&ztype, type);
545 ZVAL_STRINGL(&zdata, data, size);
546
547 zend_replace_error_handling(EH_NORMAL, NULL, &zeh);
548 if (SUCCESS == zend_fcall_info_argn(&client_obj->debug.fci, 4, &zclient, &zreq, &ztype, &zdata)) {
549 ++client->callback.depth;
550 zend_fcall_info_call(&client_obj->debug.fci, &client_obj->debug.fcc, NULL, NULL);
551 --client->callback.depth;
552 zend_fcall_info_args_clear(&client_obj->debug.fci, 0);
553 }
554 zend_restore_error_handling(&zeh);
555
556 zval_ptr_dtor(&zclient);
557 zval_ptr_dtor(&zreq);
558 zval_ptr_dtor(&ztype);
559 zval_ptr_dtor(&zdata);
560 }
561
562 static void response_dtor(void *data)
563 {
564 php_http_message_object_t *msg_obj = *(php_http_message_object_t **) data;
565
566 zend_object_release(&msg_obj->zo);
567 }
568
569 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_construct, 0, 0, 0)
570 ZEND_ARG_INFO(0, driver)
571 ZEND_ARG_INFO(0, persistent_handle_id)
572 ZEND_END_ARG_INFO();
573 static PHP_METHOD(HttpClient, __construct)
574 {
575 zend_string *driver_name = NULL, *persistent_handle_name = NULL;
576 php_http_client_driver_t *driver;
577 php_resource_factory_t *rf = NULL;
578 php_http_client_object_t *obj;
579 zval os;
580
581 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &driver_name, &persistent_handle_name), invalid_arg, return);
582
583 if (!zend_hash_num_elements(&php_http_client_drivers)) {
584 php_http_throw(unexpected_val, "No http\\Client drivers available", NULL);
585 return;
586 }
587 if (!(driver = php_http_client_driver_get(driver_name))) {
588 php_http_throw(unexpected_val, "Failed to locate \"%s\" client request handler", driver_name ? driver_name->val : "default");
589 return;
590 }
591
592 object_init_ex(&os, spl_ce_SplObjectStorage);
593 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), &os);
594 zval_ptr_dtor(&os);
595
596 if (persistent_handle_name) {
597 php_persistent_handle_factory_t *pf;
598
599 if ((pf = php_persistent_handle_concede(NULL, driver->client_name, persistent_handle_name, NULL, NULL))) {
600 rf = php_persistent_handle_resource_factory_init(NULL, pf);
601 }
602 }
603
604 obj = PHP_HTTP_OBJ(NULL, getThis());
605
606 php_http_expect(obj->client = php_http_client_init(NULL, driver->client_ops, rf, NULL), runtime, return);
607
608 php_http_object_method_init(&obj->notify, getThis(), ZEND_STRL("notify"));
609
610 obj->client->callback.response.func = handle_response;
611 obj->client->callback.response.arg = obj;
612 obj->client->callback.progress.func = handle_progress;
613 obj->client->callback.progress.arg = obj;
614
615 obj->client->responses.dtor = response_dtor;
616 }
617
618 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_reset, 0, 0, 0)
619 ZEND_END_ARG_INFO();
620 static PHP_METHOD(HttpClient, reset)
621 {
622 php_http_client_object_t *obj;
623 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
624
625 obj = PHP_HTTP_OBJ(NULL, getThis());
626
627 obj->iterator = 0;
628 php_http_client_reset(obj->client);
629
630 RETVAL_ZVAL(getThis(), 1, 0);
631 }
632
633 static HashTable *combined_options(zval *client, zval *request)
634 {
635 HashTable *options;
636 unsigned num_options = 0;
637 zval z_roptions, z_options_tmp, *z_coptions = zend_read_property(php_http_client_class_entry, client, ZEND_STRL("options"), 0, &z_options_tmp);
638
639 if (Z_TYPE_P(z_coptions) == IS_ARRAY) {
640 num_options = zend_hash_num_elements(Z_ARRVAL_P(z_coptions));
641 }
642 ZVAL_UNDEF(&z_roptions);
643 zend_call_method_with_0_params(request, NULL, NULL, "getOptions", &z_roptions);
644 if (Z_TYPE(z_roptions) == IS_ARRAY) {
645 unsigned num = zend_hash_num_elements(Z_ARRVAL(z_roptions));
646 if (num > num_options) {
647 num_options = num;
648 }
649 }
650 ALLOC_HASHTABLE(options);
651 ZEND_INIT_SYMTABLE_EX(options, num_options, 0);
652 if (Z_TYPE_P(z_coptions) == IS_ARRAY) {
653 array_copy(Z_ARRVAL_P(z_coptions), options);
654 }
655 if (Z_TYPE(z_roptions) == IS_ARRAY) {
656 array_join(Z_ARRVAL(z_roptions), options, 0, 0);
657 }
658 zval_ptr_dtor(&z_roptions);
659
660 return options;
661 }
662
663 static void msg_queue_dtor(php_http_client_enqueue_t *e)
664 {
665 php_http_message_object_t *msg_obj = e->opaque;
666
667 zend_object_release(&msg_obj->zo);
668 zend_hash_destroy(e->options);
669 FREE_HASHTABLE(e->options);
670
671 if (e->closure.fci.size) {
672 zval_ptr_dtor(&e->closure.fci.function_name);
673 if (e->closure.fci.object) {
674 zend_object_release(e->closure.fci.object);
675 }
676 }
677 }
678
679 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enqueue, 0, 0, 1)
680 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
681 ZEND_ARG_INFO(0, callable)
682 ZEND_END_ARG_INFO();
683 static PHP_METHOD(HttpClient, enqueue)
684 {
685 zval *request;
686 zend_fcall_info fci = empty_fcall_info;
687 zend_fcall_info_cache fcc = empty_fcall_info_cache;
688 php_http_client_object_t *obj;
689 php_http_message_object_t *msg_obj;
690 php_http_client_enqueue_t q;
691
692 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);
693
694 obj = PHP_HTTP_OBJ(NULL, getThis());
695 msg_obj = PHP_HTTP_OBJ(NULL, request);
696
697 if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
698 php_http_throw(bad_method_call, "Failed to enqueue request; request already in queue", NULL);
699 return;
700 }
701
702 q.request = msg_obj->message;
703 q.options = combined_options(getThis(), request);
704 q.dtor = msg_queue_dtor;
705 q.opaque = msg_obj;
706 q.closure.fci = fci;
707 q.closure.fcc = fcc;
708
709 if (fci.size) {
710 Z_TRY_ADDREF(fci.function_name);
711 if (fci.object) {
712 ++GC_REFCOUNT(fci.object);
713 }
714 }
715
716 Z_ADDREF_P(request);
717
718 php_http_expect(SUCCESS == php_http_client_enqueue(obj->client, &q), runtime,
719 msg_queue_dtor(&q);
720 return;
721 );
722
723 RETVAL_ZVAL(getThis(), 1, 0);
724 }
725
726 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_dequeue, 0, 0, 1)
727 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
728 ZEND_END_ARG_INFO();
729 static PHP_METHOD(HttpClient, dequeue)
730 {
731 zval *request;
732 php_http_client_object_t *obj;
733 php_http_message_object_t *msg_obj;
734
735 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_get_client_request_class_entry()), invalid_arg, return);
736
737 obj = PHP_HTTP_OBJ(NULL, getThis());
738 msg_obj = PHP_HTTP_OBJ(NULL, request);
739
740 if (!php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
741 php_http_throw(bad_method_call, "Failed to dequeue request; request not in queue", NULL);
742 return;
743 }
744
745 php_http_expect(SUCCESS == php_http_client_dequeue(obj->client, msg_obj->message), runtime, return);
746
747 RETVAL_ZVAL(getThis(), 1, 0);
748 }
749
750 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_requeue, 0, 0, 1)
751 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
752 ZEND_ARG_INFO(0, callable)
753 ZEND_END_ARG_INFO();
754 static PHP_METHOD(HttpClient, requeue)
755 {
756 zval *request;
757 zend_fcall_info fci = empty_fcall_info;
758 zend_fcall_info_cache fcc = empty_fcall_info_cache;
759 php_http_client_object_t *obj;
760 php_http_message_object_t *msg_obj;
761 php_http_client_enqueue_t q;
762
763 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);
764
765 obj = PHP_HTTP_OBJ(NULL, getThis());
766 msg_obj = PHP_HTTP_OBJ(NULL, request);
767
768 if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
769 php_http_expect(SUCCESS == php_http_client_dequeue(obj->client, msg_obj->message), runtime, return);
770 }
771
772 q.request = msg_obj->message;
773 q.options = combined_options(getThis(), request);
774 q.dtor = msg_queue_dtor;
775 q.opaque = msg_obj;
776 q.closure.fci = fci;
777 q.closure.fcc = fcc;
778
779 if (fci.size) {
780 Z_TRY_ADDREF(fci.function_name);
781 if (fci.object) {
782 ++GC_REFCOUNT(fci.object);
783 }
784 }
785
786 Z_ADDREF_P(request);
787
788 php_http_expect(SUCCESS == php_http_client_enqueue(obj->client, &q), runtime,
789 msg_queue_dtor(&q);
790 return;
791 );
792
793 RETVAL_ZVAL(getThis(), 1, 0);
794 }
795
796 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_count, 0, 0, 0)
797 ZEND_END_ARG_INFO();
798 static PHP_METHOD(HttpClient, count)
799 {
800 zend_long count_mode = -1;
801
802 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &count_mode)) {
803 php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
804
805 RETVAL_LONG(zend_llist_count(&obj->client->requests));
806 }
807 }
808
809 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getResponse, 0, 0, 0)
810 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 1)
811 ZEND_END_ARG_INFO();
812 static PHP_METHOD(HttpClient, getResponse)
813 {
814 zval *zrequest = NULL;
815 php_http_client_object_t *obj;
816
817 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O", &zrequest, php_http_get_client_request_class_entry()), invalid_arg, return);
818
819 obj = PHP_HTTP_OBJ(NULL, getThis());
820
821 if (zrequest) {
822 /* lookup the response with the request */
823 zend_llist_element *el = NULL;
824 php_http_message_object_t *req_obj = PHP_HTTP_OBJ(NULL, zrequest);
825
826 for (el = obj->client->responses.head; el; el = el->next) {
827 php_http_message_object_t *response_obj = *(php_http_message_object_t **) el->data;
828
829 if (response_obj->message->parent == req_obj->message) {
830 RETURN_OBJECT(&response_obj->zo, 1);
831 }
832 }
833
834 /* not found for the request! */
835 php_http_throw(unexpected_val, "Could not find response for the request", NULL);
836 return;
837 }
838
839 /* pop off the last response */
840 if (obj->client->responses.tail) {
841 php_http_message_object_t *response_obj = *(php_http_message_object_t **) obj->client->responses.tail->data;
842
843 /* pop off and go */
844 if (response_obj) {
845 RETVAL_OBJECT(&response_obj->zo, 1);
846 zend_llist_remove_tail(&obj->client->responses);
847 }
848 }
849 }
850
851 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getHistory, 0, 0, 0)
852 ZEND_END_ARG_INFO();
853 static PHP_METHOD(HttpClient, getHistory)
854 {
855 zval zhistory_tmp, *zhistory;
856
857 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
858
859 zhistory = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0, &zhistory_tmp);
860 RETVAL_ZVAL(zhistory, 1, 0);
861 }
862
863 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_send, 0, 0, 0)
864 ZEND_END_ARG_INFO();
865 static PHP_METHOD(HttpClient, send)
866 {
867 php_http_client_object_t *obj;
868
869 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
870
871 obj = PHP_HTTP_OBJ(NULL, getThis());
872
873 php_http_expect(SUCCESS == php_http_client_exec(obj->client), runtime, return);
874
875 RETVAL_ZVAL(getThis(), 1, 0);
876 }
877
878 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_once, 0, 0, 0)
879 ZEND_END_ARG_INFO();
880 static PHP_METHOD(HttpClient, once)
881 {
882 if (SUCCESS == zend_parse_parameters_none()) {
883 php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
884
885 RETURN_BOOL(0 < php_http_client_once(obj->client));
886 }
887 }
888
889 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_wait, 0, 0, 0)
890 ZEND_ARG_INFO(0, timeout)
891 ZEND_END_ARG_INFO();
892 static PHP_METHOD(HttpClient, wait)
893 {
894 double timeout = 0;
895
896 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|d", &timeout)) {
897 struct timeval timeout_val;
898 php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
899
900 timeout_val.tv_sec = (time_t) timeout;
901 timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC;
902
903 RETURN_BOOL(SUCCESS == php_http_client_wait(obj->client, timeout > 0 ? &timeout_val : NULL));
904 }
905 }
906
907 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_configure, 0, 0, 1)
908 ZEND_ARG_ARRAY_INFO(0, settings, 1)
909 ZEND_END_ARG_INFO();
910 static PHP_METHOD(HttpClient, configure)
911 {
912 HashTable *settings = NULL;
913 php_http_client_object_t *obj;
914
915 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|H!", &settings), invalid_arg, return);
916 obj = PHP_HTTP_OBJ(NULL, getThis());
917
918 php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_CONFIGURATION, settings), unexpected_val, return);
919
920 RETVAL_ZVAL(getThis(), 1, 0);
921 }
922
923 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enablePipelining, 0, 0, 0)
924 ZEND_ARG_INFO(0, enable)
925 ZEND_END_ARG_INFO();
926 static PHP_METHOD(HttpClient, enablePipelining)
927 {
928 zend_bool enable = 1;
929 php_http_client_object_t *obj;
930
931 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable), invalid_arg, return);
932
933 obj = PHP_HTTP_OBJ(NULL, getThis());
934
935 php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING, &enable), unexpected_val, return);
936
937 RETVAL_ZVAL(getThis(), 1, 0);
938 }
939
940 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enableEvents, 0, 0, 0)
941 ZEND_ARG_INFO(0, enable)
942 ZEND_END_ARG_INFO();
943 static PHP_METHOD(HttpClient, enableEvents)
944 {
945 zend_bool enable = 1;
946 php_http_client_object_t *obj;
947
948 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable), invalid_arg, return);
949
950 obj = PHP_HTTP_OBJ(NULL, getThis());
951
952 php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_USE_EVENTS, &enable), unexpected_val, return);
953
954 RETVAL_ZVAL(getThis(), 1, 0);
955 }
956
957 struct notify_arg {
958 php_http_object_method_t *cb;
959 zval args[3];
960 int argc;
961 };
962
963 static int notify(zend_object_iterator *iter, void *puser)
964 {
965 zval *observer;
966 struct notify_arg *arg = puser;
967
968 if ((observer = iter->funcs->get_current_data(iter))) {
969 if (SUCCESS == php_http_object_method_call(arg->cb, observer, NULL, arg->argc, arg->args)) {
970 return ZEND_HASH_APPLY_KEEP;
971 }
972 }
973 return ZEND_HASH_APPLY_STOP;
974 }
975
976 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_notify, 0, 0, 0)
977 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 1)
978 ZEND_END_ARG_INFO();
979 static PHP_METHOD(HttpClient, notify)
980 {
981 zval *request = NULL, *zprogress = NULL, observers_tmp, *observers;
982 php_http_client_object_t *client_obj;
983 struct notify_arg arg = {NULL};
984
985 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O!o!", &request, php_http_get_client_request_class_entry(), &zprogress), invalid_arg, return);
986
987 client_obj = PHP_HTTP_OBJ(NULL, getThis());
988 observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp);
989
990 if (Z_TYPE_P(observers) != IS_OBJECT) {
991 php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
992 return;
993 }
994
995 if (client_obj->update) {
996 arg.cb = client_obj->update;
997 ZVAL_COPY(&arg.args[0], getThis());
998 arg.argc = 1;
999
1000 if (request) {
1001 ZVAL_COPY(&arg.args[1], request);
1002 arg.argc += 1;
1003 }
1004 if (zprogress) {
1005 ZVAL_COPY(&arg.args[2], zprogress);
1006 arg.argc += 1;
1007 }
1008
1009 spl_iterator_apply(observers, notify, &arg);
1010
1011 zval_ptr_dtor(getThis());
1012 if (request) {
1013 zval_ptr_dtor(request);
1014 }
1015 if (zprogress) {
1016 zval_ptr_dtor(zprogress);
1017 }
1018 }
1019
1020 RETVAL_ZVAL(getThis(), 1, 0);
1021 }
1022
1023 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_attach, 0, 0, 1)
1024 ZEND_ARG_OBJ_INFO(0, observer, SplObserver, 0)
1025 ZEND_END_ARG_INFO();
1026 static PHP_METHOD(HttpClient, attach)
1027 {
1028 zval observers_tmp, *observers, *observer, retval;
1029 php_http_client_object_t *client_obj;
1030
1031 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &observer, spl_ce_SplObserver), invalid_arg, return);
1032
1033 client_obj = PHP_HTTP_OBJ(NULL, getThis());
1034 observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp);
1035
1036 if (Z_TYPE_P(observers) != IS_OBJECT) {
1037 php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
1038 return;
1039 }
1040
1041 if (!client_obj->update) {
1042 client_obj->update = php_http_object_method_init(NULL, observer, ZEND_STRL("update"));
1043 }
1044
1045 ZVAL_UNDEF(&retval);
1046 zend_call_method_with_1_params(observers, NULL, NULL, "attach", &retval, observer);
1047 zval_ptr_dtor(&retval);
1048
1049 RETVAL_ZVAL(getThis(), 1, 0);
1050 }
1051
1052 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_detach, 0, 0, 1)
1053 ZEND_ARG_OBJ_INFO(0, observer, SplObserver, 0)
1054 ZEND_END_ARG_INFO();
1055 static PHP_METHOD(HttpClient, detach)
1056 {
1057 zval observers_tmp, *observers, *observer, retval;
1058
1059 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &observer, spl_ce_SplObserver), invalid_arg, return);
1060
1061 observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp);
1062
1063 if (Z_TYPE_P(observers) != IS_OBJECT) {
1064 php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
1065 return;
1066 }
1067
1068 ZVAL_UNDEF(&retval);
1069 zend_call_method_with_1_params(observers, NULL, NULL, "detach", &retval, observer);
1070 zval_ptr_dtor(&retval);
1071
1072 RETVAL_ZVAL(getThis(), 1, 0);
1073 }
1074
1075 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getObservers, 0, 0, 0)
1076 ZEND_END_ARG_INFO();
1077 static PHP_METHOD(HttpClient, getObservers)
1078 {
1079 zval observers_tmp, *observers;
1080
1081 php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
1082
1083 observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp);
1084
1085 if (Z_TYPE_P(observers) != IS_OBJECT) {
1086 php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
1087 return;
1088 }
1089
1090 RETVAL_ZVAL(observers, 1, 0);
1091 }
1092
1093 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getProgressInfo, 0, 0, 1)
1094 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
1095 ZEND_END_ARG_INFO();
1096 static PHP_METHOD(HttpClient, getProgressInfo)
1097 {
1098 zval *request;
1099 php_http_client_object_t *obj;
1100 php_http_message_object_t *req_obj;
1101 php_http_client_progress_state_t *progress;
1102
1103 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_get_client_request_class_entry()), invalid_arg, return);
1104
1105 obj = PHP_HTTP_OBJ(NULL, getThis());
1106 req_obj = PHP_HTTP_OBJ(NULL, request);
1107
1108 php_http_expect(SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, req_obj->message, &progress), unexpected_val, return);
1109
1110 object_init(return_value);
1111 add_property_bool(return_value, "started", progress->started);
1112 add_property_bool(return_value, "finished", progress->finished);
1113 add_property_string(return_value, "info", STR_PTR(progress->info));
1114 add_property_double(return_value, "dltotal", progress->dl.total);
1115 add_property_double(return_value, "dlnow", progress->dl.now);
1116 add_property_double(return_value, "ultotal", progress->ul.total);
1117 add_property_double(return_value, "ulnow", progress->ul.now);
1118 }
1119
1120 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getTransferInfo, 0, 0, 1)
1121 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
1122 ZEND_END_ARG_INFO();
1123 static PHP_METHOD(HttpClient, getTransferInfo)
1124 {
1125 zval *request;
1126 HashTable *info;
1127 php_http_client_object_t *obj;
1128 php_http_message_object_t *req_obj;
1129
1130 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_get_client_request_class_entry()), invalid_arg, return);
1131
1132 obj = PHP_HTTP_OBJ(NULL, getThis());
1133 req_obj = PHP_HTTP_OBJ(NULL, request);
1134
1135 object_init(return_value);
1136 info = HASH_OF(return_value);
1137 php_http_expect(SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, req_obj->message, &info), unexpected_val, return);
1138 }
1139
1140 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setOptions, 0, 0, 0)
1141 ZEND_ARG_ARRAY_INFO(0, options, 1)
1142 ZEND_END_ARG_INFO();
1143 static PHP_METHOD(HttpClient, setOptions)
1144 {
1145 zval *opts = NULL;
1146
1147 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
1148
1149 php_http_client_options_set(getThis(), opts);
1150
1151 RETVAL_ZVAL(getThis(), 1, 0);
1152 }
1153
1154 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getOptions, 0, 0, 0)
1155 ZEND_END_ARG_INFO();
1156 static PHP_METHOD(HttpClient, getOptions)
1157 {
1158 if (SUCCESS == zend_parse_parameters_none()) {
1159 zval options_tmp, *options = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0, &options_tmp);
1160 RETVAL_ZVAL(options, 1, 0);
1161 }
1162 }
1163
1164 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setSslOptions, 0, 0, 0)
1165 ZEND_ARG_ARRAY_INFO(0, ssl_option, 1)
1166 ZEND_END_ARG_INFO();
1167 static PHP_METHOD(HttpClient, setSslOptions)
1168 {
1169 zval *opts = NULL;
1170
1171 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
1172
1173 php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 1);
1174
1175 RETVAL_ZVAL(getThis(), 1, 0);
1176 }
1177
1178 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addSslOptions, 0, 0, 0)
1179 ZEND_ARG_ARRAY_INFO(0, ssl_options, 1)
1180 ZEND_END_ARG_INFO();
1181 static PHP_METHOD(HttpClient, addSslOptions)
1182 {
1183 zval *opts = NULL;
1184
1185 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
1186
1187 php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 0);
1188
1189 RETVAL_ZVAL(getThis(), 1, 0);
1190 }
1191
1192 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getSslOptions, 0, 0, 0)
1193 ZEND_END_ARG_INFO();
1194 static PHP_METHOD(HttpClient, getSslOptions)
1195 {
1196 if (SUCCESS == zend_parse_parameters_none()) {
1197 php_http_client_options_get_subr(getThis(), ZEND_STRL("ssl"), return_value);
1198 }
1199 }
1200
1201 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setCookies, 0, 0, 0)
1202 ZEND_ARG_ARRAY_INFO(0, cookies, 1)
1203 ZEND_END_ARG_INFO();
1204 static PHP_METHOD(HttpClient, setCookies)
1205 {
1206 zval *opts = NULL;
1207
1208 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
1209
1210 php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts, 1);
1211
1212 RETVAL_ZVAL(getThis(), 1, 0);
1213 }
1214
1215 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addCookies, 0, 0, 0)
1216 ZEND_ARG_ARRAY_INFO(0, cookies, 1)
1217 ZEND_END_ARG_INFO();
1218 static PHP_METHOD(HttpClient, addCookies)
1219 {
1220 zval *opts = NULL;
1221
1222 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
1223
1224 php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts, 0);
1225
1226 RETVAL_ZVAL(getThis(), 1, 0);
1227 }
1228
1229 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getCookies, 0, 0, 0)
1230 ZEND_END_ARG_INFO();
1231 static PHP_METHOD(HttpClient, getCookies)
1232 {
1233 if (SUCCESS == zend_parse_parameters_none()) {
1234 php_http_client_options_get_subr(getThis(), ZEND_STRL("cookies"), return_value);
1235 }
1236 }
1237
1238 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableDrivers, 0, 0, 0)
1239 ZEND_END_ARG_INFO();
1240 static PHP_METHOD(HttpClient, getAvailableDrivers)
1241 {
1242 if (SUCCESS == zend_parse_parameters_none()) {
1243 array_init(return_value);
1244 php_http_client_driver_list(Z_ARRVAL_P(return_value));
1245 }
1246 }
1247
1248 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableOptions, 0, 0, 0)
1249 ZEND_END_ARG_INFO();
1250 static PHP_METHOD(HttpClient, getAvailableOptions)
1251 {
1252 if (SUCCESS == zend_parse_parameters_none()) {
1253 php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1254
1255 array_init(return_value);
1256 php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS, NULL, &Z_ARRVAL_P(return_value));
1257 }
1258 }
1259
1260 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableConfiguration, 0, 0, 0)
1261 ZEND_END_ARG_INFO();
1262 static PHP_METHOD(HttpClient, getAvailableConfiguration)
1263 {
1264 if (SUCCESS == zend_parse_parameters_none()) {
1265 php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
1266
1267 array_init(return_value);
1268 php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION, NULL, &Z_ARRVAL_P(return_value));
1269 }
1270 }
1271
1272 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setDebug, 0, 0, 1)
1273 ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 1)
1274 ZEND_END_ARG_INFO();
1275 static PHP_METHOD(HttpClient, setDebug)
1276 {
1277 zend_fcall_info fci;
1278 zend_fcall_info_cache fcc;
1279 php_http_client_object_t *client_obj;
1280
1281 fci.size = 0;
1282 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|f", &fci, &fcc), invalid_arg, return);
1283
1284 client_obj = PHP_HTTP_OBJ(NULL, getThis());
1285
1286 if (client_obj->debug.fci.size > 0) {
1287 zval_ptr_dtor(&client_obj->debug.fci.function_name);
1288 client_obj->debug.fci.size = 0;
1289 }
1290 if (fci.size > 0) {
1291 memcpy(&client_obj->debug.fci, &fci, sizeof(fci));
1292 memcpy(&client_obj->debug.fcc, &fcc, sizeof(fcc));
1293 Z_ADDREF_P(&fci.function_name);
1294 client_obj->client->callback.debug.func = handle_debug;
1295 client_obj->client->callback.debug.arg = client_obj;
1296 } else {
1297 client_obj->client->callback.debug.func = NULL;
1298 client_obj->client->callback.debug.arg = NULL;
1299 }
1300
1301 RETVAL_ZVAL(getThis(), 1, 0);
1302 }
1303
1304 static zend_function_entry php_http_client_methods[] = {
1305 PHP_ME(HttpClient, __construct, ai_HttpClient_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1306 PHP_ME(HttpClient, reset, ai_HttpClient_reset, ZEND_ACC_PUBLIC)
1307 PHP_ME(HttpClient, enqueue, ai_HttpClient_enqueue, ZEND_ACC_PUBLIC)
1308 PHP_ME(HttpClient, dequeue, ai_HttpClient_dequeue, ZEND_ACC_PUBLIC)
1309 PHP_ME(HttpClient, requeue, ai_HttpClient_requeue, ZEND_ACC_PUBLIC)
1310 PHP_ME(HttpClient, count, ai_HttpClient_count, ZEND_ACC_PUBLIC)
1311 PHP_ME(HttpClient, send, ai_HttpClient_send, ZEND_ACC_PUBLIC)
1312 PHP_ME(HttpClient, once, ai_HttpClient_once, ZEND_ACC_PUBLIC)
1313 PHP_ME(HttpClient, wait, ai_HttpClient_wait, ZEND_ACC_PUBLIC)
1314 PHP_ME(HttpClient, getResponse, ai_HttpClient_getResponse, ZEND_ACC_PUBLIC)
1315 PHP_ME(HttpClient, getHistory, ai_HttpClient_getHistory, ZEND_ACC_PUBLIC)
1316 PHP_ME(HttpClient, configure, ai_HttpClient_configure, ZEND_ACC_PUBLIC)
1317 PHP_ME(HttpClient, enablePipelining, ai_HttpClient_enablePipelining, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
1318 PHP_ME(HttpClient, enableEvents, ai_HttpClient_enableEvents, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
1319 PHP_ME(HttpClient, notify, ai_HttpClient_notify, ZEND_ACC_PUBLIC)
1320 PHP_ME(HttpClient, attach, ai_HttpClient_attach, ZEND_ACC_PUBLIC)
1321 PHP_ME(HttpClient, detach, ai_HttpClient_detach, ZEND_ACC_PUBLIC)
1322 PHP_ME(HttpClient, getObservers, ai_HttpClient_getObservers, ZEND_ACC_PUBLIC)
1323 PHP_ME(HttpClient, getProgressInfo, ai_HttpClient_getProgressInfo, ZEND_ACC_PUBLIC)
1324 PHP_ME(HttpClient, getTransferInfo, ai_HttpClient_getTransferInfo, ZEND_ACC_PUBLIC)
1325 PHP_ME(HttpClient, setOptions, ai_HttpClient_setOptions, ZEND_ACC_PUBLIC)
1326 PHP_ME(HttpClient, getOptions, ai_HttpClient_getOptions, ZEND_ACC_PUBLIC)
1327 PHP_ME(HttpClient, setSslOptions, ai_HttpClient_setSslOptions, ZEND_ACC_PUBLIC)
1328 PHP_ME(HttpClient, addSslOptions, ai_HttpClient_addSslOptions, ZEND_ACC_PUBLIC)
1329 PHP_ME(HttpClient, getSslOptions, ai_HttpClient_getSslOptions, ZEND_ACC_PUBLIC)
1330 PHP_ME(HttpClient, setCookies, ai_HttpClient_setCookies, ZEND_ACC_PUBLIC)
1331 PHP_ME(HttpClient, addCookies, ai_HttpClient_addCookies, ZEND_ACC_PUBLIC)
1332 PHP_ME(HttpClient, getCookies, ai_HttpClient_getCookies, ZEND_ACC_PUBLIC)
1333 PHP_ME(HttpClient, getAvailableDrivers, ai_HttpClient_getAvailableDrivers, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1334 PHP_ME(HttpClient, getAvailableOptions, ai_HttpClient_getAvailableOptions, ZEND_ACC_PUBLIC)
1335 PHP_ME(HttpClient, getAvailableConfiguration, ai_HttpClient_getAvailableConfiguration, ZEND_ACC_PUBLIC)
1336 PHP_ME(HttpClient, setDebug, ai_HttpClient_setDebug, ZEND_ACC_PUBLIC)
1337 EMPTY_FUNCTION_ENTRY
1338 };
1339
1340 PHP_MINIT_FUNCTION(http_client)
1341 {
1342 zend_class_entry ce = {0};
1343
1344 INIT_NS_CLASS_ENTRY(ce, "http", "Client", php_http_client_methods);
1345 php_http_client_class_entry = zend_register_internal_class_ex(&ce, NULL);
1346 php_http_client_class_entry->create_object = php_http_client_object_new;
1347 zend_class_implements(php_http_client_class_entry, 2, spl_ce_SplSubject, spl_ce_Countable);
1348 memcpy(&php_http_client_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1349 php_http_client_object_handlers.offset = XtOffsetOf(php_http_client_object_t, zo);
1350 php_http_client_object_handlers.free_obj = php_http_client_object_free;
1351 php_http_client_object_handlers.clone_obj = NULL;
1352 php_http_client_object_handlers.get_gc = php_http_client_object_get_gc;
1353 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE);
1354 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED);
1355 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("history"), ZEND_ACC_PROTECTED);
1356 zend_declare_property_bool(php_http_client_class_entry, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC);
1357
1358 zend_declare_class_constant_long(php_http_client_class_entry, ZEND_STRL("DEBUG_INFO"), PHP_HTTP_CLIENT_DEBUG_INFO);
1359 zend_declare_class_constant_long(php_http_client_class_entry, ZEND_STRL("DEBUG_IN"), PHP_HTTP_CLIENT_DEBUG_IN);
1360 zend_declare_class_constant_long(php_http_client_class_entry, ZEND_STRL("DEBUG_OUT"), PHP_HTTP_CLIENT_DEBUG_OUT);
1361 zend_declare_class_constant_long(php_http_client_class_entry, ZEND_STRL("DEBUG_HEADER"), PHP_HTTP_CLIENT_DEBUG_HEADER);
1362 zend_declare_class_constant_long(php_http_client_class_entry, ZEND_STRL("DEBUG_BODY"), PHP_HTTP_CLIENT_DEBUG_BODY);
1363 zend_declare_class_constant_long(php_http_client_class_entry, ZEND_STRL("DEBUG_SSL"), PHP_HTTP_CLIENT_DEBUG_SSL);
1364
1365 zend_hash_init(&php_http_client_drivers, 2, NULL, php_http_client_driver_hash_dtor, 1);
1366
1367 return SUCCESS;
1368 }
1369
1370 PHP_MSHUTDOWN_FUNCTION(http_client)
1371 {
1372 zend_hash_destroy(&php_http_client_drivers);
1373 return SUCCESS;
1374 }
1375
1376 /*
1377 * Local variables:
1378 * tab-width: 4
1379 * c-basic-offset: 4
1380 * End:
1381 * vim600: noet sw=4 ts=4 fdm=marker
1382 * vim<600: noet sw=4 ts=4
1383 */