build and file maintenance
[m6w6/ext-http] / php_http_request_pool.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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http.h"
14
15 #include <Zend/zend_interfaces.h>
16 #include <ext/spl/spl_iterators.h>
17
18 PHP_HTTP_API php_http_request_pool_t *php_http_request_pool_init(php_http_request_pool_t *h, php_http_request_pool_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC)
19 {
20 php_http_request_pool_t *free_h = NULL;
21
22 if (!h) {
23 free_h = h = emalloc(sizeof(*h));
24 }
25 memset(h, 0, sizeof(*h));
26
27 h->ops = ops;
28 h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, NULL, NULL);
29 zend_llist_init(&h->requests.attached, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
30 zend_llist_init(&h->requests.finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
31 TSRMLS_SET_CTX(h->ts);
32
33 if (h->ops->init) {
34 if (!(h = h->ops->init(h, init_arg))) {
35 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not initialize request pool");
36 if (free_h) {
37 efree(h);
38 }
39 }
40 }
41
42 return h;
43 }
44
45 PHP_HTTP_API php_http_request_pool_t *php_http_request_pool_copy(php_http_request_pool_t *from, php_http_request_pool_t *to)
46 {
47 if (from->ops->copy) {
48 return from->ops->copy(from, to);
49 }
50
51 return NULL;
52 }
53
54 PHP_HTTP_API void php_http_request_pool_dtor(php_http_request_pool_t *h)
55 {
56 if (h->ops->dtor) {
57 h->ops->dtor(h);
58 }
59
60 zend_llist_clean(&h->requests.finished);
61 zend_llist_clean(&h->requests.attached);
62
63 if (h->persistent_handle_id) {
64 zval_ptr_dtor(&h->persistent_handle_id);
65 }
66 }
67
68 PHP_HTTP_API void php_http_request_pool_free(php_http_request_pool_t **h) {
69 if (*h) {
70 php_http_request_pool_dtor(*h);
71 efree(*h);
72 *h = NULL;
73 }
74 }
75
76 PHP_HTTP_API STATUS php_http_request_pool_attach(php_http_request_pool_t *h, zval *request)
77 {
78 TSRMLS_FETCH_FROM_CTX(h->ts);
79
80 if (h->ops->attach) {
81 char *url = NULL;
82 php_http_request_method_t m = PHP_HTTP_NO_REQUEST_METHOD;
83 php_http_message_body_t *body = NULL;
84 php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC);
85
86 if (SUCCESS != php_http_request_object_requesthandler(obj, request, &m, &url, &body TSRMLS_CC)) {
87 return FAILURE;
88 }
89 if (SUCCESS == h->ops->attach(h, obj->request, m, url, body)) {
90 STR_FREE(url);
91 Z_ADDREF_P(request);
92 zend_llist_add_element(&h->requests.attached, &request);
93 return SUCCESS;
94 }
95 STR_FREE(url);
96 }
97
98 return FAILURE;
99 }
100
101 static int php_http_request_pool_compare_handles(void *h1, void *h2)
102 {
103 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
104 }
105
106
107 PHP_HTTP_API STATUS php_http_request_pool_detach(php_http_request_pool_t *h, zval *request)
108 {
109 TSRMLS_FETCH_FROM_CTX(h->ts);
110
111 if (h->ops->detach) {
112 php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC);
113
114 if (SUCCESS == h->ops->detach(h, obj->request)) {
115 zend_llist_del_element(&h->requests.finished, request, php_http_request_pool_compare_handles);
116 zend_llist_del_element(&h->requests.attached, request, php_http_request_pool_compare_handles);
117 return SUCCESS;
118 }
119 }
120
121 return FAILURE;
122 }
123
124 PHP_HTTP_API STATUS php_http_request_pool_wait(php_http_request_pool_t *h, struct timeval *custom_timeout)
125 {
126 if (h->ops->wait) {
127 return h->ops->wait(h, custom_timeout);
128 }
129
130 return FAILURE;
131 }
132
133 PHP_HTTP_API int php_http_request_pool_once(php_http_request_pool_t *h)
134 {
135 if (h->ops->once) {
136 return h->ops->once(h);
137 }
138
139 return FAILURE;
140 }
141
142 PHP_HTTP_API STATUS php_http_request_pool_exec(php_http_request_pool_t *h)
143 {
144 if (h->ops->exec) {
145 return h->ops->exec(h);
146 }
147
148 return FAILURE;
149 }
150
151 static void detach(void *r, void *h TSRMLS_DC)
152 {
153 ((php_http_request_pool_t *) h)->ops->detach(h, ((php_http_request_object_t *) zend_object_store_get_object(*((zval **) r) TSRMLS_CC))->request);
154 }
155
156 PHP_HTTP_API void php_http_request_pool_reset(php_http_request_pool_t *h)
157 {
158 if (h->ops->reset) {
159 h->ops->reset(h);
160 } else if (h->ops->detach) {
161 TSRMLS_FETCH_FROM_CTX(h->ts);
162
163 zend_llist_apply_with_argument(&h->requests.attached, detach, h TSRMLS_CC);
164 }
165
166 zend_llist_clean(&h->requests.attached);
167 zend_llist_clean(&h->requests.finished);
168 }
169
170 PHP_HTTP_API STATUS php_http_request_pool_setopt(php_http_request_pool_t *h, php_http_request_pool_setopt_opt_t opt, void *arg)
171 {
172 if (h->ops->setopt) {
173 return h->ops->setopt(h, opt, arg);
174 }
175
176 return FAILURE;
177 }
178
179 PHP_HTTP_API void php_http_request_pool_requests(php_http_request_pool_t *h, zval ***attached, zval ***finished)
180 {
181 zval **handle;
182 int i, count;
183
184 if (attached) {
185 if ((count = zend_llist_count(&h->requests.attached))) {
186 *attached = ecalloc(count + 1 /* terminating NULL */, sizeof(zval *));
187
188 for (i = 0, handle = zend_llist_get_first(&h->requests.attached); handle; handle = zend_llist_get_next(&h->requests.attached)) {
189 Z_ADDREF_PP(handle);
190 (*attached)[i++] = *handle;
191 }
192 } else {
193 *attached = NULL;
194 }
195 }
196
197 if (finished) {
198 if ((count = zend_llist_count(&h->requests.finished))) {
199 *finished = ecalloc(count + 1 /* terminating NULL */, sizeof(zval *));
200
201 for (i = 0, handle = zend_llist_get_first(&h->requests.finished); handle; handle = zend_llist_get_next(&h->requests.finished)) {
202 Z_ADDREF_PP(handle);
203 (*finished)[i++] = *handle;
204 }
205 } else {
206 *finished = NULL;
207 }
208 }
209 }
210
211 /*#*/
212
213 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args)
214 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, 0)
215 #define PHP_HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, PHP_HTTP_ARGS(HttpRequestPool, method), visibility)
216
217 PHP_HTTP_EMPTY_ARGS(__construct);
218
219 PHP_HTTP_EMPTY_ARGS(__destruct);
220 PHP_HTTP_EMPTY_ARGS(reset);
221
222 PHP_HTTP_BEGIN_ARGS(attach, 1)
223 PHP_HTTP_ARG_OBJ(http\\Request, request, 0)
224 PHP_HTTP_END_ARGS;
225
226 PHP_HTTP_BEGIN_ARGS(detach, 1)
227 PHP_HTTP_ARG_OBJ(http\\Request, request, 0)
228 PHP_HTTP_END_ARGS;
229
230 PHP_HTTP_EMPTY_ARGS(send);
231 PHP_HTTP_EMPTY_ARGS(once);
232 PHP_HTTP_BEGIN_ARGS(wait, 0)
233 PHP_HTTP_ARG_VAL(timeout, 0)
234 PHP_HTTP_END_ARGS;
235
236 PHP_HTTP_EMPTY_ARGS(valid);
237 PHP_HTTP_EMPTY_ARGS(current);
238 PHP_HTTP_EMPTY_ARGS(key);
239 PHP_HTTP_EMPTY_ARGS(next);
240 PHP_HTTP_EMPTY_ARGS(rewind);
241
242 PHP_HTTP_EMPTY_ARGS(count);
243
244 PHP_HTTP_EMPTY_ARGS(getAttachedRequests);
245 PHP_HTTP_EMPTY_ARGS(getFinishedRequests);
246
247 PHP_HTTP_BEGIN_ARGS(enablePipelining, 0)
248 PHP_HTTP_ARG_VAL(enable, 0)
249 PHP_HTTP_END_ARGS;
250
251 PHP_HTTP_BEGIN_ARGS(enableEvents, 0)
252 PHP_HTTP_ARG_VAL(enable, 0)
253 PHP_HTTP_END_ARGS;
254
255 zend_class_entry *php_http_request_pool_class_entry;
256 zend_function_entry php_http_request_pool_method_entry[] = {
257 PHP_HTTP_REQPOOL_ME(__construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
258 PHP_HTTP_REQPOOL_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
259 PHP_HTTP_REQPOOL_ME(attach, ZEND_ACC_PUBLIC)
260 PHP_HTTP_REQPOOL_ME(detach, ZEND_ACC_PUBLIC)
261 PHP_HTTP_REQPOOL_ME(send, ZEND_ACC_PUBLIC)
262 PHP_HTTP_REQPOOL_ME(reset, ZEND_ACC_PUBLIC)
263
264 PHP_HTTP_REQPOOL_ME(once, ZEND_ACC_PROTECTED)
265 PHP_HTTP_REQPOOL_ME(wait, ZEND_ACC_PROTECTED)
266
267 /* implements Iterator */
268 PHP_HTTP_REQPOOL_ME(valid, ZEND_ACC_PUBLIC)
269 PHP_HTTP_REQPOOL_ME(current, ZEND_ACC_PUBLIC)
270 PHP_HTTP_REQPOOL_ME(key, ZEND_ACC_PUBLIC)
271 PHP_HTTP_REQPOOL_ME(next, ZEND_ACC_PUBLIC)
272 PHP_HTTP_REQPOOL_ME(rewind, ZEND_ACC_PUBLIC)
273
274 /* implmenents Countable */
275 PHP_HTTP_REQPOOL_ME(count, ZEND_ACC_PUBLIC)
276
277 PHP_HTTP_REQPOOL_ME(getAttachedRequests, ZEND_ACC_PUBLIC)
278 PHP_HTTP_REQPOOL_ME(getFinishedRequests, ZEND_ACC_PUBLIC)
279
280 PHP_HTTP_REQPOOL_ME(enablePipelining, ZEND_ACC_PUBLIC)
281 PHP_HTTP_REQPOOL_ME(enableEvents, ZEND_ACC_PUBLIC)
282
283 EMPTY_FUNCTION_ENTRY
284 };
285 static zend_object_handlers php_http_request_pool_object_handlers;
286
287 zend_object_value php_http_request_pool_object_new(zend_class_entry *ce TSRMLS_DC)
288 {
289 return php_http_request_pool_object_new_ex(ce, NULL, NULL TSRMLS_CC);
290 }
291
292 zend_object_value php_http_request_pool_object_new_ex(zend_class_entry *ce, php_http_request_pool_t *p, php_http_request_pool_object_t **ptr TSRMLS_DC)
293 {
294 zend_object_value ov;
295 php_http_request_pool_object_t *o;
296
297 o = ecalloc(1, sizeof(php_http_request_pool_object_t));
298 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
299 object_properties_init((zend_object *) o, ce);
300
301 if (!(o->pool = p)) {
302 o->pool = php_http_request_pool_init(NULL, NULL, NULL, NULL TSRMLS_CC);
303 }
304
305 if (ptr) {
306 *ptr = o;
307 }
308
309 ov.handle = zend_objects_store_put(o, NULL, php_http_request_pool_object_free, NULL TSRMLS_CC);
310 ov.handlers = &php_http_request_pool_object_handlers;
311
312 return ov;
313 }
314
315 void php_http_request_pool_object_free(void *object TSRMLS_DC)
316 {
317 php_http_request_pool_object_t *o = (php_http_request_pool_object_t *) object;
318
319 php_http_request_pool_free(&o->pool);
320 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
321 efree(o);
322 }
323
324 static void php_http_request_pool_object_llist2array(zval **req, zval *array TSRMLS_DC)
325 {
326 Z_ADDREF_P(*req);
327 add_next_index_zval(array, *req);
328 }
329
330
331 PHP_METHOD(HttpRequestPool, __construct)
332 {
333 with_error_handling(EH_THROW, php_http_exception_class_entry) {
334 zend_parse_parameters_none();
335 } end_error_handling();
336 }
337
338 PHP_METHOD(HttpRequestPool, __destruct)
339 {
340 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
341
342 if (SUCCESS != zend_parse_parameters_none()) {
343 ; /* we always want to clean up */
344 }
345
346 php_http_request_pool_reset(obj->pool);
347 }
348
349 PHP_METHOD(HttpRequestPool, reset)
350 {
351 if (SUCCESS == zend_parse_parameters_none()) {
352 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
353
354 obj->iterator.pos = 0;
355 php_http_request_pool_reset(obj->pool);
356 }
357 RETVAL_ZVAL(getThis(), 1, 0);
358 }
359
360 PHP_METHOD(HttpRequestPool, attach)
361 {
362 with_error_handling(EH_THROW, php_http_exception_class_entry) {
363 zval *request;
364
365 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) {
366 with_error_handling(EH_THROW, php_http_exception_class_entry) {
367 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
368
369 if (obj->iterator.pos > 0 && obj->iterator.pos < zend_llist_count(&obj->pool->requests.attached)) {
370 php_http_error(HE_THROW, PHP_HTTP_E_REQUEST_POOL, "Cannot attach to the HttpRequestPool while the iterator is active");
371 } else {
372 php_http_request_pool_attach(obj->pool, request);
373 }
374 } end_error_handling();
375 }
376 } end_error_handling();
377
378 RETVAL_ZVAL(getThis(), 1, 0);
379 }
380
381 PHP_METHOD(HttpRequestPool, detach)
382 {
383 RETVAL_FALSE;
384
385 with_error_handling(EH_THROW, php_http_exception_class_entry) {
386 zval *request;
387
388 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) {
389 with_error_handling(EH_THROW, php_http_exception_class_entry) {
390 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
391
392 obj->iterator.pos = -1;
393 php_http_request_pool_detach(obj->pool, request);
394 } end_error_handling();
395 }
396 } end_error_handling();
397
398 RETVAL_ZVAL(getThis(), 1, 0);
399 }
400
401 PHP_METHOD(HttpRequestPool, send)
402 {
403 RETVAL_FALSE;
404
405 with_error_handling(EH_THROW, php_http_exception_class_entry) {
406 if (SUCCESS == zend_parse_parameters_none()) {
407 with_error_handling(EH_THROW, php_http_exception_class_entry) {
408 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
409
410 php_http_request_pool_exec(obj->pool);
411 } end_error_handling();
412 }
413 } end_error_handling();
414
415 RETVAL_ZVAL(getThis(), 1, 0);
416 }
417
418 PHP_METHOD(HttpRequestPool, once)
419 {
420 if (SUCCESS == zend_parse_parameters_none()) {
421 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
422
423 if (0 < php_http_request_pool_once(obj->pool)) {
424 RETURN_TRUE;
425 }
426 }
427 RETURN_FALSE;
428 }
429
430 PHP_METHOD(HttpRequestPool, wait)
431 {
432 double timeout = 0;
433
434 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|d", &timeout)) {
435 struct timeval timeout_val;
436 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
437
438 timeout_val.tv_sec = (time_t) timeout;
439 timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC;
440
441 RETURN_SUCCESS(php_http_request_pool_wait(obj->pool, timeout ? &timeout_val : NULL));
442 }
443 RETURN_FALSE;
444 }
445
446 PHP_METHOD(HttpRequestPool, valid)
447 {
448 if (SUCCESS == zend_parse_parameters_none()) {
449 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
450
451 RETURN_BOOL(obj->iterator.pos >= 0 && obj->iterator.pos < zend_llist_count(&obj->pool->requests.attached));
452 }
453 RETURN_FALSE;
454 }
455
456 PHP_METHOD(HttpRequestPool, current)
457 {
458 if (SUCCESS == zend_parse_parameters_none()) {
459 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
460
461 if (obj->iterator.pos < zend_llist_count(&obj->pool->requests.attached)) {
462 long pos = 0;
463 zval **current = NULL;
464 zend_llist_position lpos;
465
466 for ( current = zend_llist_get_first_ex(&obj->pool->requests.attached, &lpos);
467 current && obj->iterator.pos != pos++;
468 current = zend_llist_get_next_ex(&obj->pool->requests.attached, &lpos));
469 if (current) {
470 RETURN_OBJECT(*current, 1);
471 }
472 }
473 }
474 RETURN_FALSE;
475 }
476
477 PHP_METHOD(HttpRequestPool, key)
478 {
479 if (SUCCESS == zend_parse_parameters_none()) {
480 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
481
482 RETURN_LONG(obj->iterator.pos);
483 }
484 RETURN_FALSE;
485 }
486
487 PHP_METHOD(HttpRequestPool, next)
488 {
489 if (SUCCESS == zend_parse_parameters_none()) {
490 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
491
492 ++obj->iterator.pos;
493 }
494 }
495
496 PHP_METHOD(HttpRequestPool, rewind)
497 {
498 if (SUCCESS == zend_parse_parameters_none()) {
499 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
500
501 obj->iterator.pos = 0;
502 }
503 }
504
505 PHP_METHOD(HttpRequestPool, count)
506 {
507 if (SUCCESS == zend_parse_parameters_none()) {
508 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
509
510 RETURN_LONG((long) zend_llist_count(&obj->pool->requests.attached));
511 }
512 }
513
514 PHP_METHOD(HttpRequestPool, getAttachedRequests)
515 {
516 if (SUCCESS == zend_parse_parameters_none()) {
517 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
518
519 array_init(return_value);
520 zend_llist_apply_with_argument(&obj->pool->requests.attached,
521 (llist_apply_with_arg_func_t) php_http_request_pool_object_llist2array,
522 return_value TSRMLS_CC);
523 return;
524 }
525 RETURN_FALSE;
526 }
527
528 PHP_METHOD(HttpRequestPool, getFinishedRequests)
529 {
530 if (SUCCESS == zend_parse_parameters_none()) {
531 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
532
533 array_init(return_value);
534 zend_llist_apply_with_argument(&obj->pool->requests.finished,
535 (llist_apply_with_arg_func_t) php_http_request_pool_object_llist2array,
536 return_value TSRMLS_CC);
537 return;
538 }
539 RETURN_FALSE;
540 }
541
542 PHP_METHOD(HttpRequestPool, enablePipelining)
543 {
544 zend_bool enable = 1;
545
546 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) {
547 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
548
549 php_http_request_pool_setopt(obj->pool, PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING, &enable);
550 }
551 RETVAL_ZVAL(getThis(), 1, 0);
552 }
553
554 PHP_METHOD(HttpRequestPool, enableEvents)
555 {
556 zend_bool enable = 1;
557
558 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) {
559 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
560
561 php_http_request_pool_setopt(obj->pool, PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS, &enable);
562 }
563 RETVAL_ZVAL(getThis(), 1, 0);
564 }
565
566 PHP_MINIT_FUNCTION(http_request_pool)
567 {
568 PHP_HTTP_REGISTER_CLASS(http\\Request, Pool, http_request_pool, php_http_object_class_entry, 0);
569 php_http_request_pool_class_entry->create_object = php_http_request_pool_object_new;
570 memcpy(&php_http_request_pool_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
571 php_http_request_pool_object_handlers.clone_obj = NULL;
572
573 zend_class_implements(php_http_request_pool_class_entry TSRMLS_CC, 2, spl_ce_Countable, zend_ce_iterator);
574
575 return SUCCESS;
576 }
577
578
579 /*
580 * Local variables:
581 * tab-width: 4
582 * c-basic-offset: 4
583 * End:
584 * vim600: noet sw=4 ts=4 fdm=marker
585 * vim<600: noet sw=4 ts=4
586 */
587