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