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