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