f23322d12a457ca8a6bc116176f164e9773b8638
[m6w6/ext-http] / php_http_client.c
1 #include "php_http_api.h"
2 #include "php_http_client.h"
3
4 #include <ext/spl/spl_observer.h>
5
6 /*
7 * array of name => php_http_client_driver_t*
8 */
9 static HashTable php_http_client_drivers;
10
11 PHP_HTTP_API STATUS php_http_client_driver_add(php_http_client_driver_t *driver)
12 {
13 return zend_hash_add(&php_http_client_drivers, driver->name_str, driver->name_len + 1, (void *) driver, sizeof(php_http_client_driver_t), NULL);
14 }
15
16 PHP_HTTP_API STATUS php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver)
17 {
18 php_http_client_driver_t *tmp;
19
20 if ((name_str && SUCCESS == zend_hash_find(&php_http_client_drivers, name_str, name_len + 1, (void *) &tmp))
21 || (SUCCESS == zend_hash_get_current_data(&php_http_client_drivers, (void *) &tmp))) {
22 *driver = *tmp;
23 return SUCCESS;
24 }
25 return FAILURE;
26 }
27
28 static int apply_driver_list(void *p, void *arg TSRMLS_DC)
29 {
30 php_http_client_driver_t *d = p;
31 zval *zname;
32
33 MAKE_STD_ZVAL(zname);
34 ZVAL_STRINGL(zname, d->name_str, d->name_len, 1);
35
36 zend_hash_next_index_insert(arg, &zname, sizeof(zval *), NULL);
37 return ZEND_HASH_APPLY_KEEP;
38 }
39
40 PHP_HTTP_API void php_http_client_driver_list(HashTable *ht TSRMLS_DC)
41 {
42 zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht TSRMLS_CC);
43 }
44
45 void php_http_client_options_set_subr(zval *this_ptr, char *key, size_t len, zval *opts, int overwrite TSRMLS_DC)
46 {
47 if (overwrite || (opts && zend_hash_num_elements(Z_ARRVAL_P(opts)))) {
48 zend_class_entry *this_ce = Z_OBJCE_P(getThis());
49 zval *old_opts, *new_opts, **entry = NULL;
50
51 MAKE_STD_ZVAL(new_opts);
52 array_init(new_opts);
53 old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
54 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
55 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
56 }
57
58 if (overwrite) {
59 if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
60 Z_ADDREF_P(opts);
61 zend_symtable_update(Z_ARRVAL_P(new_opts), key, len, (void *) &opts, sizeof(zval *), NULL);
62 } else {
63 zend_symtable_del(Z_ARRVAL_P(new_opts), key, len);
64 }
65 } else if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
66 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
67 array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, 0);
68 } else {
69 Z_ADDREF_P(opts);
70 zend_symtable_update(Z_ARRVAL_P(new_opts), key, len, (void *) &opts, sizeof(zval *), NULL);
71 }
72 }
73
74 zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
75 zval_ptr_dtor(&new_opts);
76 }
77 }
78
79 void php_http_client_options_set(zval *this_ptr, zval *opts TSRMLS_DC)
80 {
81 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
82 HashPosition pos;
83 zval *new_opts;
84 zend_class_entry *this_ce = Z_OBJCE_P(getThis());
85 zend_bool is_client = instanceof_function(this_ce, php_http_client_class_entry TSRMLS_CC);
86
87 MAKE_STD_ZVAL(new_opts);
88 array_init(new_opts);
89
90 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
91 zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
92 zval_ptr_dtor(&new_opts);
93 } else {
94 zval *old_opts, *add_opts, **opt;
95
96 MAKE_STD_ZVAL(add_opts);
97 array_init(add_opts);
98 /* some options need extra attention -- thus cannot use array_merge() directly */
99 FOREACH_KEYVAL(pos, opts, key, opt) {
100 if (key.type == HASH_KEY_IS_STRING) {
101 #define KEYMATCH(k, s) ((sizeof(s)==k.len) && !strcasecmp(k.str, s))
102 if (Z_TYPE_PP(opt) == IS_ARRAY && (KEYMATCH(key, "ssl") || KEYMATCH(key, "cookies"))) {
103 php_http_client_options_set_subr(getThis(), key.str, key.len, *opt, 0 TSRMLS_CC);
104 } else if (is_client && (KEYMATCH(key, "recordHistory") || KEYMATCH(key, "responseMessageClass"))) {
105 zend_update_property(this_ce, getThis(), key.str, key.len-1, *opt TSRMLS_CC);
106 } else if (Z_TYPE_PP(opt) == IS_NULL) {
107 old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
108 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
109 zend_symtable_del(Z_ARRVAL_P(old_opts), key.str, key.len);
110 }
111 } else {
112 Z_ADDREF_P(*opt);
113 add_assoc_zval_ex(add_opts, key.str, key.len, *opt);
114 }
115 }
116 }
117
118 old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
119 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
120 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
121 }
122 array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0);
123 zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
124 zval_ptr_dtor(&new_opts);
125 zval_ptr_dtor(&add_opts);
126 }
127 }
128
129 void php_http_client_options_get_subr(zval *this_ptr, char *key, size_t len, zval *return_value TSRMLS_DC)
130 {
131 zend_class_entry *this_ce = Z_OBJCE_P(getThis());
132 zval **options, *opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
133
134 if ((Z_TYPE_P(opts) == IS_ARRAY) && (SUCCESS == zend_symtable_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
135 RETVAL_ZVAL(*options, 1, 0);
136 }
137 }
138
139 static void queue_dtor(void *enqueued)
140 {
141 php_http_client_enqueue_t *e = enqueued;
142
143 if (e->dtor) {
144 e->dtor(e);
145 }
146 }
147
148 PHP_HTTP_API 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)
149 {
150 php_http_client_t *free_h = NULL;
151
152 if (!h) {
153 free_h = h = emalloc(sizeof(*h));
154 }
155 memset(h, 0, sizeof(*h));
156
157 h->ops = ops;
158 if (rf) {
159 h->rf = rf;
160 } else if (ops->rsrc) {
161 h->rf = php_resource_factory_init(NULL, h->ops->rsrc, h, NULL);
162 }
163 zend_llist_init(&h->requests, sizeof(php_http_client_enqueue_t), queue_dtor, 0);
164 zend_llist_init(&h->responses, sizeof(void *), NULL, 0);
165 TSRMLS_SET_CTX(h->ts);
166
167 if (h->ops->init) {
168 if (!(h = h->ops->init(h, init_arg))) {
169 php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Could not initialize client");
170 if (free_h) {
171 efree(h);
172 }
173 }
174 }
175
176 return h;
177 }
178
179 PHP_HTTP_API php_http_client_t *php_http_client_copy(php_http_client_t *from, php_http_client_t *to)
180 {
181 if (from->ops->copy) {
182 return from->ops->copy(from, to);
183 }
184
185 return NULL;
186 }
187
188 PHP_HTTP_API void php_http_client_dtor(php_http_client_t *h)
189 {
190 if (h->ops->dtor) {
191 h->ops->dtor(h);
192 }
193
194 zend_llist_clean(&h->requests);
195 zend_llist_clean(&h->responses);
196
197 php_resource_factory_free(&h->rf);
198 }
199
200 PHP_HTTP_API void php_http_client_free(php_http_client_t **h) {
201 if (*h) {
202 php_http_client_dtor(*h);
203 efree(*h);
204 *h = NULL;
205 }
206 }
207
208 PHP_HTTP_API STATUS php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
209 {
210 TSRMLS_FETCH_FROM_CTX(h->ts);
211
212 if (h->ops->enqueue) {
213 if (php_http_client_enqueued(h, enqueue->request, NULL)) {
214 php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to enqueue request; request already in queue");
215 return FAILURE;
216 }
217 return h->ops->enqueue(h, enqueue);
218 }
219
220 return FAILURE;
221 }
222
223 PHP_HTTP_API STATUS php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request)
224 {
225 TSRMLS_FETCH_FROM_CTX(h->ts);
226
227 if (h->ops->dequeue) {
228 php_http_client_enqueue_t *enqueue = php_http_client_enqueued(h, request, NULL);
229
230 if (!enqueue) {
231 php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to dequeue request; request not in queue");
232 return FAILURE;
233 }
234 return h->ops->dequeue(h, enqueue);
235 }
236 return FAILURE;
237 }
238
239 PHP_HTTP_API 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)
240 {
241 zend_llist_element *el = NULL;
242
243 if (compare_func) {
244 for (el = h->requests.head; el; el = el->next) {
245 if (compare_func((php_http_client_enqueue_t *) el->data, compare_arg)) {
246 break;
247 }
248 }
249 } else {
250 for (el = h->requests.head; el; el = el->next) {
251 if (((php_http_client_enqueue_t *) el->data)->request == compare_arg) {
252 break;
253 }
254 }
255 }
256 return el ? (php_http_client_enqueue_t *) el->data : NULL;
257 }
258
259 PHP_HTTP_API STATUS php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout)
260 {
261 if (h->ops->wait) {
262 return h->ops->wait(h, custom_timeout);
263 }
264
265 return FAILURE;
266 }
267
268 PHP_HTTP_API int php_http_client_once(php_http_client_t *h)
269 {
270 if (h->ops->once) {
271 return h->ops->once(h);
272 }
273
274 return FAILURE;
275 }
276
277 PHP_HTTP_API STATUS php_http_client_exec(php_http_client_t *h)
278 {
279 if (h->ops->exec) {
280 return h->ops->exec(h);
281 }
282
283 return FAILURE;
284 }
285
286 PHP_HTTP_API void php_http_client_reset(php_http_client_t *h)
287 {
288 if (h->ops->reset) {
289 h->ops->reset(h);
290 }
291
292 zend_llist_clean(&h->requests);
293 zend_llist_clean(&h->responses);
294 }
295
296 PHP_HTTP_API STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
297 {
298 if (h->ops->setopt) {
299 return h->ops->setopt(h, opt, arg);
300 }
301
302 return FAILURE;
303 }
304
305 PHP_HTTP_API STATUS php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr)
306 {
307 if (h->ops->getopt) {
308 return h->ops->getopt(h, opt, arg, res_ptr);
309 }
310 return FAILURE;
311 }
312
313 zend_class_entry *php_http_client_class_entry;
314 static zend_object_handlers php_http_client_object_handlers;
315
316 void php_http_client_object_free(void *object TSRMLS_DC)
317 {
318 php_http_client_object_t *o = (php_http_client_object_t *) object;
319
320 php_http_client_free(&o->client);
321 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
322 efree(o);
323 }
324
325 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)
326 {
327 php_http_client_object_t *o;
328
329 o = ecalloc(1, sizeof(php_http_client_object_t));
330 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
331 object_properties_init((zend_object *) o, ce);
332
333 o->client = client;
334
335 if (ptr) {
336 *ptr = o;
337 }
338
339 o->zv.handle = zend_objects_store_put(o, NULL, php_http_client_object_free, NULL TSRMLS_CC);
340 o->zv.handlers = &php_http_client_object_handlers;
341
342 return o->zv;
343 }
344
345 zend_object_value php_http_client_object_new(zend_class_entry *ce TSRMLS_DC)
346 {
347 return php_http_client_object_new_ex(ce, NULL, NULL TSRMLS_CC);
348 }
349
350 static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response TSRMLS_DC)
351 {
352 zval *new_hist, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0 TSRMLS_CC);
353 php_http_message_t *zipped = php_http_message_zip(response, request);
354 zend_object_value ov = php_http_message_object_new_ex(php_http_message_get_class_entry(), zipped, NULL TSRMLS_CC);
355
356 MAKE_STD_ZVAL(new_hist);
357 ZVAL_OBJVAL(new_hist, ov, 0);
358
359 if (Z_TYPE_P(old_hist) == IS_OBJECT) {
360 php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC);
361 }
362
363 zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), new_hist TSRMLS_CC);
364 zval_ptr_dtor(&new_hist);
365 }
366
367 static STATUS 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)
368 {
369 zval zclient;
370 php_http_message_t *msg;
371 php_http_client_progress_state_t *progress;
372 TSRMLS_FETCH_FROM_CTX(client->ts);
373
374 INIT_PZVAL(&zclient);
375 ZVAL_OBJVAL(&zclient, ((php_http_client_object_t*) arg)->zv, 0);
376
377 if ((msg = *response)) {
378 php_http_message_object_t *msg_obj;
379 zval *info, *zresponse, *zrequest;
380
381 if (i_zend_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) {
382 handle_history(&zclient, *request, *response TSRMLS_CC);
383 }
384
385 /* hard detach, redirects etc. are in the history */
386 php_http_message_free(&msg->parent);
387 *response = NULL;
388
389 MAKE_STD_ZVAL(zresponse);
390 ZVAL_OBJVAL(zresponse, php_http_message_object_new_ex(php_http_client_response_get_class_entry(), msg, &msg_obj TSRMLS_CC), 0);
391
392 MAKE_STD_ZVAL(zrequest);
393 ZVAL_OBJVAL(zrequest, ((php_http_message_object_t *) e->opaque)->zv, 1);
394
395 php_http_message_object_prepend(zresponse, zrequest, 1 TSRMLS_CC);
396
397 MAKE_STD_ZVAL(info);
398 array_init(info);
399 php_http_client_getopt(client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, e->request, &Z_ARRVAL_P(info));
400 zend_update_property(php_http_client_response_get_class_entry(), zresponse, ZEND_STRL("transferInfo"), info TSRMLS_CC);
401 zval_ptr_dtor(&info);
402
403 zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
404 zend_llist_add_element(&client->responses, &msg_obj);
405
406 if (e->closure.fci.size) {
407 zval *retval = NULL;
408
409 zend_fcall_info_argn(&e->closure.fci TSRMLS_CC, 1, &zresponse);
410 with_error_handling(EH_NORMAL, NULL) {
411 zend_fcall_info_call(&e->closure.fci, &e->closure.fcc, &retval, NULL TSRMLS_CC);
412 } end_error_handling();
413 zend_fcall_info_argn(&e->closure.fci TSRMLS_CC, 0);
414
415 if (retval) {
416 if (Z_TYPE_P(retval) == IS_BOOL && Z_BVAL_P(retval)) {
417 php_http_client_dequeue(client, e->request);
418 }
419 zval_ptr_dtor(&retval);
420 }
421 }
422
423 zval_ptr_dtor(&zresponse);
424 zval_ptr_dtor(&zrequest);
425 }
426
427 if (SUCCESS == php_http_client_getopt(client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, e->request, &progress)) {
428 progress->info = "finished";
429 progress->finished = 1;
430 client->callback.progress.func(client->callback.progress.arg, client, e, progress);
431 }
432
433 return SUCCESS;
434 }
435
436 static void handle_progress(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_client_progress_state_t *progress)
437 {
438 zval *zrequest, *zprogress, *retval = NULL, *zclient;
439 TSRMLS_FETCH_FROM_CTX(client->ts);
440
441 MAKE_STD_ZVAL(zclient);
442 ZVAL_OBJVAL(zclient, ((php_http_client_object_t *) arg)->zv, 1);
443 MAKE_STD_ZVAL(zrequest);
444 ZVAL_OBJVAL(zrequest, ((php_http_message_object_t *) e->opaque)->zv, 1);
445 MAKE_STD_ZVAL(zprogress);
446 object_init(zprogress);
447 add_property_bool(zprogress, "started", progress->started);
448 add_property_bool(zprogress, "finished", progress->finished);
449 add_property_string(zprogress, "info", STR_PTR(progress->info), 1);
450 add_property_double(zprogress, "dltotal", progress->dl.total);
451 add_property_double(zprogress, "dlnow", progress->dl.now);
452 add_property_double(zprogress, "ultotal", progress->ul.total);
453 add_property_double(zprogress, "ulnow", progress->ul.now);
454 with_error_handling(EH_NORMAL, NULL) {
455 zend_call_method_with_2_params(&zclient, NULL, NULL, "notify", &retval, zrequest, zprogress);
456 } end_error_handling();
457 zval_ptr_dtor(&zclient);
458 zval_ptr_dtor(&zrequest);
459 zval_ptr_dtor(&zprogress);
460 if (retval) {
461 zval_ptr_dtor(&retval);
462 }
463 }
464
465 static void response_dtor(void *data)
466 {
467 php_http_message_object_t *msg_obj = *(php_http_message_object_t **) data;
468 TSRMLS_FETCH_FROM_CTX(msg_obj->message->ts);
469
470 zend_objects_store_del_ref_by_handle_ex(msg_obj->zv.handle, msg_obj->zv.handlers TSRMLS_CC);
471 }
472
473 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_construct, 0, 0, 0)
474 ZEND_ARG_INFO(0, driver)
475 ZEND_ARG_INFO(0, persistent_handle_id)
476 ZEND_END_ARG_INFO();
477 static PHP_METHOD(HttpClient, __construct)
478 {
479 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
480 char *driver_str = NULL, *persistent_handle_str = NULL;
481 int driver_len = 0, persistent_handle_len = 0;
482
483 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &driver_str, &driver_len, &persistent_handle_str, &persistent_handle_len)) {
484 php_http_client_driver_t driver;
485
486 if (SUCCESS == php_http_client_driver_get(driver_str, driver_len, &driver)) {
487 php_resource_factory_t *rf = NULL;
488 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
489 zval *os;
490
491 MAKE_STD_ZVAL(os);
492 object_init_ex(os, spl_ce_SplObjectStorage);
493 zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), os TSRMLS_CC);
494 zval_ptr_dtor(&os);
495
496 if (persistent_handle_len) {
497 char *name_str;
498 size_t name_len;
499 php_persistent_handle_factory_t *pf;
500
501 name_len = spprintf(&name_str, 0, "http\\Client\\%s", driver.name_str);
502 php_http_pretty_key(name_str + sizeof("http\\Client"), driver.name_len, 1, 1);
503
504 if ((pf = php_persistent_handle_concede(NULL , name_str, name_len, persistent_handle_str, persistent_handle_len, NULL, NULL TSRMLS_CC))) {
505 rf = php_resource_factory_init(NULL, php_persistent_handle_get_resource_factory_ops(), pf, (void (*)(void *)) php_persistent_handle_abandon);
506 }
507
508 efree(name_str);
509 }
510
511 if ((obj->client = php_http_client_init(NULL, driver.client_ops, rf, NULL TSRMLS_CC))) {
512 obj->client->callback.response.func = handle_response;
513 obj->client->callback.response.arg = obj;
514 obj->client->callback.progress.func = handle_progress;
515 obj->client->callback.progress.arg = obj;
516
517 obj->client->responses.dtor = response_dtor;
518 }
519 } else {
520 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "Failed to locate \"%s\" client request handler", driver_str);
521 }
522 }
523 } end_error_handling();
524 }
525
526 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_reset, 0, 0, 0)
527 ZEND_END_ARG_INFO();
528 static PHP_METHOD(HttpClient, reset)
529 {
530 if (SUCCESS == zend_parse_parameters_none()) {
531 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
532
533 obj->iterator = 0;
534 php_http_client_reset(obj->client);
535 }
536 RETVAL_ZVAL(getThis(), 1, 0);
537 }
538
539 static HashTable *combined_options(zval *client, zval *request TSRMLS_DC)
540 {
541 HashTable *options;
542 int num_options = 0;
543 zval *z_roptions = NULL, *z_coptions = zend_read_property(php_http_client_class_entry, client, ZEND_STRL("options"), 0 TSRMLS_CC);
544
545 if (Z_TYPE_P(z_coptions) == IS_ARRAY) {
546 num_options = zend_hash_num_elements(Z_ARRVAL_P(z_coptions));
547 }
548 zend_call_method_with_0_params(&request, NULL, NULL, "getOptions", &z_roptions);
549 if (z_roptions && Z_TYPE_P(z_roptions) == IS_ARRAY) {
550 int num = zend_hash_num_elements(Z_ARRVAL_P(z_roptions));
551 if (num > num_options) {
552 num_options = num;
553 }
554 }
555 ALLOC_HASHTABLE(options);
556 ZEND_INIT_SYMTABLE_EX(options, num_options, 0);
557 if (Z_TYPE_P(z_coptions) == IS_ARRAY) {
558 array_copy(Z_ARRVAL_P(z_coptions), options);
559 }
560 if (z_roptions) {
561 if (Z_TYPE_P(z_roptions) == IS_ARRAY) {
562 array_join(Z_ARRVAL_P(z_roptions), options, 0, 0);
563 }
564 zval_ptr_dtor(&z_roptions);
565 }
566 return options;
567 }
568
569 static void msg_queue_dtor(php_http_client_enqueue_t *e)
570 {
571 php_http_message_object_t *msg_obj = e->opaque;
572 TSRMLS_FETCH_FROM_CTX(msg_obj->message->ts);
573
574 zend_objects_store_del_ref_by_handle_ex(msg_obj->zv.handle, msg_obj->zv.handlers TSRMLS_CC);
575 zend_hash_destroy(e->options);
576 FREE_HASHTABLE(e->options);
577
578 if (e->closure.fci.size) {
579 zval_ptr_dtor(&e->closure.fci.function_name);
580 if (e->closure.fci.object_ptr) {
581 zval_ptr_dtor(&e->closure.fci.object_ptr);
582 }
583 }
584 }
585
586 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enqueue, 0, 0, 1)
587 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
588 ZEND_ARG_INFO(0, callable)
589 ZEND_END_ARG_INFO();
590 static PHP_METHOD(HttpClient, enqueue)
591 {
592 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
593 zval *request;
594 zend_fcall_info fci = empty_fcall_info;
595 zend_fcall_info_cache fcc = empty_fcall_info_cache;
596
597 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|f", &request, php_http_client_request_get_class_entry(), &fci, &fcc)) {
598 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
599 php_http_message_object_t *msg_obj = zend_object_store_get_object(request TSRMLS_CC);
600
601 if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
602 php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to enqueue request; request already in queue");
603 } else {
604 php_http_client_enqueue_t q = {
605 msg_obj->message,
606 combined_options(getThis(), request TSRMLS_CC),
607 msg_queue_dtor,
608 msg_obj,
609 {fci, fcc}
610 };
611
612 if (fci.size) {
613 Z_ADDREF_P(fci.function_name);
614 if (fci.object_ptr) {
615 Z_ADDREF_P(fci.object_ptr);
616 }
617 }
618
619 zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
620 php_http_client_enqueue(obj->client, &q);
621 }
622 }
623 } end_error_handling();
624
625 RETVAL_ZVAL(getThis(), 1, 0);
626 }
627
628 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_dequeue, 0, 0, 1)
629 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
630 ZEND_END_ARG_INFO();
631 static PHP_METHOD(HttpClient, dequeue)
632 {
633 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
634 zval *request;
635
636 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_get_class_entry())) {
637 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
638 php_http_message_object_t *msg_obj = zend_object_store_get_object(request TSRMLS_CC);
639
640 php_http_client_dequeue(obj->client, msg_obj->message);
641 }
642 } end_error_handling();
643
644 RETVAL_ZVAL(getThis(), 1, 0);
645 }
646
647 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_requeue, 0, 0, 1)
648 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
649 ZEND_ARG_INFO(0, callable)
650 ZEND_END_ARG_INFO();
651 static PHP_METHOD(HttpClient, requeue)
652 {
653 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
654 zval *request;
655 zend_fcall_info fci = empty_fcall_info;
656 zend_fcall_info_cache fcc = empty_fcall_info_cache;
657
658 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|f", &request, php_http_client_request_get_class_entry(), &fci, &fcc)) {
659 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
660 php_http_message_object_t *msg_obj = zend_object_store_get_object(request TSRMLS_CC);
661 php_http_client_enqueue_t q = {
662 msg_obj->message,
663 combined_options(getThis(), request TSRMLS_CC),
664 msg_queue_dtor,
665 msg_obj,
666 {fci, fcc}
667 };
668
669 if (fci.size) {
670 Z_ADDREF_P(fci.function_name);
671 if (fci.object_ptr) {
672 Z_ADDREF_P(fci.object_ptr);
673 }
674 }
675
676 zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
677 if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
678 php_http_client_dequeue(obj->client, msg_obj->message);
679 }
680 php_http_client_enqueue(obj->client, &q);
681 }
682 } end_error_handling();
683
684 RETVAL_ZVAL(getThis(), 1, 0);
685 }
686
687 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getResponse, 0, 0, 0)
688 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 1)
689 ZEND_END_ARG_INFO();
690 static PHP_METHOD(HttpClient, getResponse)
691 {
692 zval *zrequest = NULL;
693
694 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &zrequest, php_http_client_request_get_class_entry())) {
695 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
696
697 if (!zrequest) {
698 /* pop off the last respone */
699 if (obj->client->responses.tail) {
700 php_http_message_object_t *response_obj = *(php_http_message_object_t **) obj->client->responses.tail->data;
701
702 /* pop off and go */
703 if (response_obj) {
704 RETVAL_OBJVAL(response_obj->zv, 1);
705 zend_llist_remove_tail(&obj->client->responses);
706 }
707 }
708 } else {
709 /* lookup the response with the request */
710 zend_llist_element *el = NULL;
711 php_http_message_object_t *req_obj = zend_object_store_get_object(zrequest TSRMLS_CC);
712
713 for (el = obj->client->responses.head; el; el = el->next) {
714 php_http_message_object_t *response_obj = *(php_http_message_object_t **) el->data;
715
716 if (response_obj->message->parent == req_obj->message) {
717 RETVAL_OBJVAL(response_obj->zv, 1);
718 break;
719 }
720 }
721 }
722 }
723 }
724
725 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getHistory, 0, 0, 0)
726 ZEND_END_ARG_INFO();
727 static PHP_METHOD(HttpClient, getHistory)
728 {
729 if (SUCCESS == zend_parse_parameters_none()) {
730 zval *zhistory = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC);
731 RETVAL_ZVAL(zhistory, 1, 0);
732 }
733 }
734
735 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_send, 0, 0, 0)
736 ZEND_END_ARG_INFO();
737 static PHP_METHOD(HttpClient, send)
738 {
739 RETVAL_FALSE;
740
741 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
742 if (SUCCESS == zend_parse_parameters_none()) {
743 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
744 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
745
746 php_http_client_exec(obj->client);
747 } end_error_handling();
748 }
749 } end_error_handling();
750
751 RETVAL_ZVAL(getThis(), 1, 0);
752 }
753
754 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_once, 0, 0, 0)
755 ZEND_END_ARG_INFO();
756 static PHP_METHOD(HttpClient, once)
757 {
758 if (SUCCESS == zend_parse_parameters_none()) {
759 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
760
761 if (0 < php_http_client_once(obj->client)) {
762 RETURN_TRUE;
763 }
764 }
765 RETURN_FALSE;
766 }
767
768 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_wait, 0, 0, 0)
769 ZEND_ARG_INFO(0, timeout)
770 ZEND_END_ARG_INFO();
771 static PHP_METHOD(HttpClient, wait)
772 {
773 double timeout = 0;
774
775 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|d", &timeout)) {
776 struct timeval timeout_val;
777 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
778
779 timeout_val.tv_sec = (time_t) timeout;
780 timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC;
781
782 RETURN_SUCCESS(php_http_client_wait(obj->client, timeout > 0 ? &timeout_val : NULL));
783 }
784 RETURN_FALSE;
785 }
786
787 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enablePipelining, 0, 0, 0)
788 ZEND_ARG_INFO(0, enable)
789 ZEND_END_ARG_INFO();
790 static PHP_METHOD(HttpClient, enablePipelining)
791 {
792 zend_bool enable = 1;
793
794 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) {
795 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
796
797 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING, &enable);
798 }
799 RETVAL_ZVAL(getThis(), 1, 0);
800 }
801
802 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enableEvents, 0, 0, 0)
803 ZEND_ARG_INFO(0, enable)
804 ZEND_END_ARG_INFO();
805 static PHP_METHOD(HttpClient, enableEvents)
806 {
807 zend_bool enable = 1;
808
809 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) {
810 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
811
812 php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_USE_EVENTS, &enable);
813 }
814 RETVAL_ZVAL(getThis(), 1, 0);
815 }
816
817 static int notify(zend_object_iterator *iter, void *puser TSRMLS_DC)
818 {
819 zval **observer = NULL, ***args = puser;
820
821 iter->funcs->get_current_data(iter, &observer TSRMLS_CC);
822 if (observer) {
823 return php_http_method_call(*observer, ZEND_STRL("update"), args[2]?3:args[1]?2:args[0]?1:0, args, NULL TSRMLS_CC);
824 }
825 return FAILURE;
826 }
827
828 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_notify, 0, 0, 0)
829 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 1)
830 ZEND_END_ARG_INFO();
831 static PHP_METHOD(HttpClient, notify)
832 {
833 zval *request = NULL, *zprogress = NULL;
834
835 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O!o!", &request, php_http_client_request_get_class_entry(), &zprogress)) {
836 zval **args[3], *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
837
838 if (Z_TYPE_P(observers) == IS_OBJECT) {
839 Z_ADDREF_P(getThis());
840 args[0] = &getThis();
841 if (request) {
842 Z_ADDREF_P(request);
843 }
844 args[1] = &request;
845 if (zprogress) {
846 Z_ADDREF_P(zprogress);
847 }
848 args[2] = &zprogress;
849 spl_iterator_apply(observers, notify, args TSRMLS_CC);
850 zval_ptr_dtor(&getThis());
851 if (request) {
852 zval_ptr_dtor(&request);
853 }
854 if (zprogress) {
855 zval_ptr_dtor(&zprogress);
856 }
857 }
858 }
859
860 RETVAL_ZVAL(getThis(), 1, 0);
861 }
862
863 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_attach, 0, 0, 1)
864 ZEND_ARG_OBJ_INFO(0, observer, SplObserver, 0)
865 ZEND_END_ARG_INFO();
866 static PHP_METHOD(HttpClient, attach)
867 {
868 zval *observer;
869
870 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) {
871 zval *retval = NULL, *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
872 zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer);
873 if (retval) {
874 zval_ptr_dtor(&retval);
875 }
876 }
877
878 RETVAL_ZVAL(getThis(), 1, 0);
879 }
880
881 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_detach, 0, 0, 1)
882 ZEND_ARG_OBJ_INFO(0, observer, SplObserver, 0)
883 ZEND_END_ARG_INFO();
884 static PHP_METHOD(HttpClient, detach)
885 {
886 zval *observer;
887
888 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) {
889 zval *retval, *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
890 zend_call_method_with_1_params(&observers, NULL, NULL, "detach", &retval, observer);
891 zval_ptr_dtor(&retval);
892 }
893
894 RETVAL_ZVAL(getThis(), 1, 0);
895 }
896
897 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getObservers, 0, 0, 0)
898 ZEND_END_ARG_INFO();
899 static PHP_METHOD(HttpClient, getObservers)
900 {
901 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
902 if (SUCCESS == zend_parse_parameters_none()) {
903 zval *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
904 RETVAL_ZVAL(observers, 1, 0);
905 }
906 } end_error_handling();
907 }
908
909 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getProgressInfo, 0, 0, 1)
910 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
911 ZEND_END_ARG_INFO();
912 static PHP_METHOD(HttpClient, getProgressInfo)
913 {
914 zval *request;
915
916 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
917 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_get_class_entry())) {
918 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
919 php_http_message_object_t *req_obj = zend_object_store_get_object(request TSRMLS_CC);
920 php_http_client_progress_state_t *progress;
921
922 if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, req_obj->message, &progress)) {
923 object_init(return_value);
924 add_property_bool(return_value, "started", progress->started);
925 add_property_bool(return_value, "finished", progress->finished);
926 add_property_string(return_value, "info", STR_PTR(progress->info), 1);
927 add_property_double(return_value, "dltotal", progress->dl.total);
928 add_property_double(return_value, "dlnow", progress->dl.now);
929 add_property_double(return_value, "ultotal", progress->ul.total);
930 add_property_double(return_value, "ulnow", progress->ul.now);
931 }
932 }
933 } end_error_handling();
934 }
935
936 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getTransferInfo, 0, 0, 1)
937 ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
938 ZEND_END_ARG_INFO();
939 static PHP_METHOD(HttpClient, getTransferInfo)
940 {
941 zval *request;
942
943 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
944 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_get_class_entry())) {
945 php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
946 php_http_message_object_t *req_obj = zend_object_store_get_object(request TSRMLS_CC);
947
948 array_init(return_value);
949 php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, req_obj->message, &Z_ARRVAL_P(return_value));
950 }
951 } end_error_handling();
952 }
953
954 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setOptions, 0, 0, 0)
955 ZEND_ARG_ARRAY_INFO(0, options, 1)
956 ZEND_END_ARG_INFO();
957 static PHP_METHOD(HttpClient, setOptions)
958 {
959 zval *opts = NULL;
960
961 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
962 php_http_client_options_set(getThis(), opts TSRMLS_CC);
963
964 RETVAL_ZVAL(getThis(), 1, 0);
965 }
966 }
967
968 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getOptions, 0, 0, 0)
969 ZEND_END_ARG_INFO();
970 static PHP_METHOD(HttpClient, getOptions)
971 {
972 if (SUCCESS == zend_parse_parameters_none()) {
973 zval *options = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
974 RETVAL_ZVAL(options, 1, 0);
975 }
976 }
977
978 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setSslOptions, 0, 0, 0)
979 ZEND_ARG_ARRAY_INFO(0, ssl_option, 1)
980 ZEND_END_ARG_INFO();
981 static PHP_METHOD(HttpClient, setSslOptions)
982 {
983 zval *opts = NULL;
984
985 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
986 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC);
987
988 RETVAL_ZVAL(getThis(), 1, 0);
989 }
990 }
991
992 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addSslOptions, 0, 0, 0)
993 ZEND_ARG_ARRAY_INFO(0, ssl_options, 1)
994 ZEND_END_ARG_INFO();
995 static PHP_METHOD(HttpClient, addSslOptions)
996 {
997 zval *opts = NULL;
998
999 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
1000 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
1001
1002 RETVAL_ZVAL(getThis(), 1, 0);
1003 }
1004 }
1005
1006 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getSslOptions, 0, 0, 0)
1007 ZEND_END_ARG_INFO();
1008 static PHP_METHOD(HttpClient, getSslOptions)
1009 {
1010 if (SUCCESS == zend_parse_parameters_none()) {
1011 php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC);
1012 }
1013 }
1014
1015 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setCookies, 0, 0, 0)
1016 ZEND_ARG_ARRAY_INFO(0, cookies, 1)
1017 ZEND_END_ARG_INFO();
1018 static PHP_METHOD(HttpClient, setCookies)
1019 {
1020 zval *opts = NULL;
1021
1022 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
1023 php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 1 TSRMLS_CC);
1024
1025 RETVAL_ZVAL(getThis(), 1, 0);
1026 }
1027 }
1028
1029 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addCookies, 0, 0, 0)
1030 ZEND_ARG_ARRAY_INFO(0, cookies, 1)
1031 ZEND_END_ARG_INFO();
1032 static PHP_METHOD(HttpClient, addCookies)
1033 {
1034 zval *opts = NULL;
1035
1036 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
1037 php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 0 TSRMLS_CC);
1038
1039 RETVAL_ZVAL(getThis(), 1, 0);
1040 }
1041 }
1042
1043 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getCookies, 0, 0, 0)
1044 ZEND_END_ARG_INFO();
1045 static PHP_METHOD(HttpClient, getCookies)
1046 {
1047 if (SUCCESS == zend_parse_parameters_none()) {
1048 php_http_client_options_get_subr(getThis(), ZEND_STRS("cookies"), return_value TSRMLS_CC);
1049 }
1050 }
1051
1052 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableDrivers, 0, 0, 0)
1053 ZEND_END_ARG_INFO();
1054 static PHP_METHOD(HttpClient, getAvailableDrivers) {
1055 if (SUCCESS == zend_parse_parameters_none()) {
1056 array_init(return_value);
1057 php_http_client_driver_list(Z_ARRVAL_P(return_value) TSRMLS_CC);
1058 }
1059 }
1060
1061 static zend_function_entry php_http_client_methods[] = {
1062 PHP_ME(HttpClient, __construct, ai_HttpClient_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1063 PHP_ME(HttpClient, reset, ai_HttpClient_reset, ZEND_ACC_PUBLIC)
1064 PHP_ME(HttpClient, enqueue, ai_HttpClient_enqueue, ZEND_ACC_PUBLIC)
1065 PHP_ME(HttpClient, dequeue, ai_HttpClient_dequeue, ZEND_ACC_PUBLIC)
1066 PHP_ME(HttpClient, requeue, ai_HttpClient_requeue, ZEND_ACC_PUBLIC)
1067 PHP_ME(HttpClient, send, ai_HttpClient_send, ZEND_ACC_PUBLIC)
1068 PHP_ME(HttpClient, once, ai_HttpClient_once, ZEND_ACC_PUBLIC)
1069 PHP_ME(HttpClient, wait, ai_HttpClient_wait, ZEND_ACC_PUBLIC)
1070 PHP_ME(HttpClient, getResponse, ai_HttpClient_getResponse, ZEND_ACC_PUBLIC)
1071 PHP_ME(HttpClient, getHistory, ai_HttpClient_getHistory, ZEND_ACC_PUBLIC)
1072 PHP_ME(HttpClient, enablePipelining, ai_HttpClient_enablePipelining, ZEND_ACC_PUBLIC)
1073 PHP_ME(HttpClient, enableEvents, ai_HttpClient_enableEvents, ZEND_ACC_PUBLIC)
1074 PHP_ME(HttpClient, notify, ai_HttpClient_notify, ZEND_ACC_PUBLIC)
1075 PHP_ME(HttpClient, attach, ai_HttpClient_attach, ZEND_ACC_PUBLIC)
1076 PHP_ME(HttpClient, detach, ai_HttpClient_detach, ZEND_ACC_PUBLIC)
1077 PHP_ME(HttpClient, getObservers, ai_HttpClient_getObservers, ZEND_ACC_PUBLIC)
1078 PHP_ME(HttpClient, getProgressInfo, ai_HttpClient_getProgressInfo, ZEND_ACC_PUBLIC)
1079 PHP_ME(HttpClient, getTransferInfo, ai_HttpClient_getTransferInfo, ZEND_ACC_PUBLIC)
1080 PHP_ME(HttpClient, setOptions, ai_HttpClient_setOptions, ZEND_ACC_PUBLIC)
1081 PHP_ME(HttpClient, getOptions, ai_HttpClient_getOptions, ZEND_ACC_PUBLIC)
1082 PHP_ME(HttpClient, setSslOptions, ai_HttpClient_setSslOptions, ZEND_ACC_PUBLIC)
1083 PHP_ME(HttpClient, addSslOptions, ai_HttpClient_addSslOptions, ZEND_ACC_PUBLIC)
1084 PHP_ME(HttpClient, getSslOptions, ai_HttpClient_getSslOptions, ZEND_ACC_PUBLIC)
1085 PHP_ME(HttpClient, setCookies, ai_HttpClient_setCookies, ZEND_ACC_PUBLIC)
1086 PHP_ME(HttpClient, addCookies, ai_HttpClient_addCookies, ZEND_ACC_PUBLIC)
1087 PHP_ME(HttpClient, getCookies, ai_HttpClient_getCookies, ZEND_ACC_PUBLIC)
1088 PHP_ME(HttpClient, getAvailableDrivers, ai_HttpClient_getAvailableDrivers, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1089 EMPTY_FUNCTION_ENTRY
1090 };
1091
1092 PHP_MINIT_FUNCTION(http_client)
1093 {
1094 zend_class_entry ce = {0};
1095
1096 INIT_NS_CLASS_ENTRY(ce, "http", "Client", php_http_client_methods);
1097 php_http_client_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
1098 php_http_client_class_entry->create_object = php_http_client_object_new;
1099 zend_class_implements(php_http_client_class_entry TSRMLS_CC, 1, spl_ce_SplSubject);
1100 memcpy(&php_http_client_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1101 php_http_client_object_handlers.clone_obj = NULL;
1102 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE TSRMLS_CC);
1103 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC);
1104 zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("history"), ZEND_ACC_PROTECTED TSRMLS_CC);
1105 zend_declare_property_bool(php_http_client_class_entry, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
1106
1107 zend_hash_init(&php_http_client_drivers, 2, NULL, NULL, 1);
1108
1109 return SUCCESS;
1110 }
1111
1112 PHP_MSHUTDOWN_FUNCTION(http_client)
1113 {
1114 zend_hash_destroy(&php_http_client_drivers);
1115 return SUCCESS;
1116 }