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