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