2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
15 #include <Zend/zend_interfaces.h>
16 #include <ext/spl/spl_iterators.h>
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
)
20 php_http_request_pool_t
*free_h
= NULL
;
23 free_h
= h
= emalloc(sizeof(*h
));
25 memset(h
, 0, sizeof(*h
));
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
);
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");
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
)
47 if (from
->ops
->copy
) {
48 return from
->ops
->copy(from
, to
);
54 PHP_HTTP_API
void php_http_request_pool_dtor(php_http_request_pool_t
*h
)
60 zend_llist_clean(&h
->requests
.finished
);
61 zend_llist_clean(&h
->requests
.attached
);
63 if (h
->persistent_handle_id
) {
64 zval_ptr_dtor(&h
->persistent_handle_id
);
67 php_http_resource_factory_free(&h
->rf
);
70 PHP_HTTP_API
void php_http_request_pool_free(php_http_request_pool_t
**h
) {
72 php_http_request_pool_dtor(*h
);
78 PHP_HTTP_API STATUS
php_http_request_pool_attach(php_http_request_pool_t
*h
, zval
*request
)
80 TSRMLS_FETCH_FROM_CTX(h
->ts
);
84 php_http_request_method_t m
= PHP_HTTP_NO_REQUEST_METHOD
;
85 php_http_message_body_t
*body
= NULL
;
86 php_http_request_object_t
*obj
= zend_object_store_get_object(request TSRMLS_CC
);
88 if (SUCCESS
!= php_http_request_object_requesthandler(obj
, request
, &m
, &url
, &body TSRMLS_CC
)) {
91 if (SUCCESS
== h
->ops
->attach(h
, obj
->request
, m
, url
, body
)) {
94 zend_llist_add_element(&h
->requests
.attached
, &request
);
103 static int php_http_request_pool_compare_handles(void *h1
, void *h2
)
105 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
109 PHP_HTTP_API STATUS
php_http_request_pool_detach(php_http_request_pool_t
*h
, zval
*request
)
111 TSRMLS_FETCH_FROM_CTX(h
->ts
);
113 if (h
->ops
->detach
) {
114 php_http_request_object_t
*obj
= zend_object_store_get_object(request TSRMLS_CC
);
116 if (SUCCESS
== h
->ops
->detach(h
, obj
->request
)) {
117 zend_llist_del_element(&h
->requests
.finished
, request
, php_http_request_pool_compare_handles
);
118 zend_llist_del_element(&h
->requests
.attached
, request
, php_http_request_pool_compare_handles
);
126 PHP_HTTP_API STATUS
php_http_request_pool_wait(php_http_request_pool_t
*h
, struct timeval
*custom_timeout
)
129 return h
->ops
->wait(h
, custom_timeout
);
135 PHP_HTTP_API
int php_http_request_pool_once(php_http_request_pool_t
*h
)
138 return h
->ops
->once(h
);
144 PHP_HTTP_API STATUS
php_http_request_pool_exec(php_http_request_pool_t
*h
)
147 return h
->ops
->exec(h
);
153 static void detach(void *r
, void *h TSRMLS_DC
)
155 ((php_http_request_pool_t
*) h
)->ops
->detach(h
, ((php_http_request_object_t
*) zend_object_store_get_object(*((zval
**) r
) TSRMLS_CC
))->request
);
158 PHP_HTTP_API
void php_http_request_pool_reset(php_http_request_pool_t
*h
)
162 } else if (h
->ops
->detach
) {
163 TSRMLS_FETCH_FROM_CTX(h
->ts
);
165 zend_llist_apply_with_argument(&h
->requests
.attached
, detach
, h TSRMLS_CC
);
168 zend_llist_clean(&h
->requests
.attached
);
169 zend_llist_clean(&h
->requests
.finished
);
172 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
)
174 if (h
->ops
->setopt
) {
175 return h
->ops
->setopt(h
, opt
, arg
);
181 PHP_HTTP_API
void php_http_request_pool_requests(php_http_request_pool_t
*h
, zval
***attached
, zval
***finished
)
187 if ((count
= zend_llist_count(&h
->requests
.attached
))) {
188 *attached
= ecalloc(count
+ 1 /* terminating NULL */, sizeof(zval
*));
190 for (i
= 0, handle
= zend_llist_get_first(&h
->requests
.attached
); handle
; handle
= zend_llist_get_next(&h
->requests
.attached
)) {
192 (*attached
)[i
++] = *handle
;
200 if ((count
= zend_llist_count(&h
->requests
.finished
))) {
201 *finished
= ecalloc(count
+ 1 /* terminating NULL */, sizeof(zval
*));
203 for (i
= 0, handle
= zend_llist_get_first(&h
->requests
.finished
); handle
; handle
= zend_llist_get_next(&h
->requests
.finished
)) {
205 (*finished
)[i
++] = *handle
;
215 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args)
216 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, 0)
217 #define PHP_HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, PHP_HTTP_ARGS(HttpRequestPool, method), visibility)
219 PHP_HTTP_EMPTY_ARGS(__construct
);
221 PHP_HTTP_EMPTY_ARGS(__destruct
);
222 PHP_HTTP_EMPTY_ARGS(reset
);
224 PHP_HTTP_BEGIN_ARGS(attach
, 1)
225 PHP_HTTP_ARG_OBJ(http
\\Request
, request
, 0)
228 PHP_HTTP_BEGIN_ARGS(detach
, 1)
229 PHP_HTTP_ARG_OBJ(http
\\Request
, request
, 0)
232 PHP_HTTP_EMPTY_ARGS(send
);
233 PHP_HTTP_EMPTY_ARGS(once
);
234 PHP_HTTP_BEGIN_ARGS(wait
, 0)
235 PHP_HTTP_ARG_VAL(timeout
, 0)
238 PHP_HTTP_EMPTY_ARGS(valid
);
239 PHP_HTTP_EMPTY_ARGS(current
);
240 PHP_HTTP_EMPTY_ARGS(key
);
241 PHP_HTTP_EMPTY_ARGS(next
);
242 PHP_HTTP_EMPTY_ARGS(rewind
);
244 PHP_HTTP_EMPTY_ARGS(count
);
246 PHP_HTTP_EMPTY_ARGS(getAttachedRequests
);
247 PHP_HTTP_EMPTY_ARGS(getFinishedRequests
);
249 PHP_HTTP_BEGIN_ARGS(enablePipelining
, 0)
250 PHP_HTTP_ARG_VAL(enable
, 0)
253 PHP_HTTP_BEGIN_ARGS(enableEvents
, 0)
254 PHP_HTTP_ARG_VAL(enable
, 0)
257 zend_class_entry
*php_http_request_pool_class_entry
;
258 zend_function_entry php_http_request_pool_method_entry
[] = {
259 PHP_HTTP_REQPOOL_ME(__construct
, ZEND_ACC_PRIVATE
|ZEND_ACC_CTOR
)
260 PHP_HTTP_REQPOOL_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
261 PHP_HTTP_REQPOOL_ME(attach
, ZEND_ACC_PUBLIC
)
262 PHP_HTTP_REQPOOL_ME(detach
, ZEND_ACC_PUBLIC
)
263 PHP_HTTP_REQPOOL_ME(send
, ZEND_ACC_PUBLIC
)
264 PHP_HTTP_REQPOOL_ME(reset
, ZEND_ACC_PUBLIC
)
266 PHP_HTTP_REQPOOL_ME(once
, ZEND_ACC_PROTECTED
)
267 PHP_HTTP_REQPOOL_ME(wait
, ZEND_ACC_PROTECTED
)
269 /* implements Iterator */
270 PHP_HTTP_REQPOOL_ME(valid
, ZEND_ACC_PUBLIC
)
271 PHP_HTTP_REQPOOL_ME(current
, ZEND_ACC_PUBLIC
)
272 PHP_HTTP_REQPOOL_ME(key
, ZEND_ACC_PUBLIC
)
273 PHP_HTTP_REQPOOL_ME(next
, ZEND_ACC_PUBLIC
)
274 PHP_HTTP_REQPOOL_ME(rewind
, ZEND_ACC_PUBLIC
)
276 /* implmenents Countable */
277 PHP_HTTP_REQPOOL_ME(count
, ZEND_ACC_PUBLIC
)
279 PHP_HTTP_REQPOOL_ME(getAttachedRequests
, ZEND_ACC_PUBLIC
)
280 PHP_HTTP_REQPOOL_ME(getFinishedRequests
, ZEND_ACC_PUBLIC
)
282 PHP_HTTP_REQPOOL_ME(enablePipelining
, ZEND_ACC_PUBLIC
)
283 PHP_HTTP_REQPOOL_ME(enableEvents
, ZEND_ACC_PUBLIC
)
287 static zend_object_handlers php_http_request_pool_object_handlers
;
289 zend_object_value
php_http_request_pool_object_new(zend_class_entry
*ce TSRMLS_DC
)
291 return php_http_request_pool_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
294 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
)
296 zend_object_value ov
;
297 php_http_request_pool_object_t
*o
;
299 o
= ecalloc(1, sizeof(php_http_request_pool_object_t
));
300 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
301 object_properties_init((zend_object
*) o
, ce
);
303 if (!(o
->pool
= p
)) {
304 o
->pool
= php_http_request_pool_init(NULL
, NULL
, NULL
, NULL TSRMLS_CC
);
311 ov
.handle
= zend_objects_store_put(o
, NULL
, php_http_request_pool_object_free
, NULL TSRMLS_CC
);
312 ov
.handlers
= &php_http_request_pool_object_handlers
;
317 void php_http_request_pool_object_free(void *object TSRMLS_DC
)
319 php_http_request_pool_object_t
*o
= (php_http_request_pool_object_t
*) object
;
321 php_http_request_pool_free(&o
->pool
);
322 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
326 static void php_http_request_pool_object_llist2array(zval
**req
, zval
*array TSRMLS_DC
)
329 add_next_index_zval(array
, *req
);
333 PHP_METHOD(HttpRequestPool
, __construct
)
335 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
336 zend_parse_parameters_none();
337 } end_error_handling();
340 PHP_METHOD(HttpRequestPool
, __destruct
)
342 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
344 if (SUCCESS
!= zend_parse_parameters_none()) {
345 ; /* we always want to clean up */
348 php_http_request_pool_reset(obj
->pool
);
351 PHP_METHOD(HttpRequestPool
, reset
)
353 if (SUCCESS
== zend_parse_parameters_none()) {
354 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
356 obj
->iterator
.pos
= 0;
357 php_http_request_pool_reset(obj
->pool
);
359 RETVAL_ZVAL(getThis(), 1, 0);
362 PHP_METHOD(HttpRequestPool
, attach
)
364 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
367 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, php_http_request_class_entry
)) {
368 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
369 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
371 if (obj
->iterator
.pos
> 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
->requests
.attached
)) {
372 php_http_error(HE_THROW
, PHP_HTTP_E_REQUEST_POOL
, "Cannot attach to the HttpRequestPool while the iterator is active");
374 php_http_request_pool_attach(obj
->pool
, request
);
376 } end_error_handling();
378 } end_error_handling();
380 RETVAL_ZVAL(getThis(), 1, 0);
383 PHP_METHOD(HttpRequestPool
, detach
)
387 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
390 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, php_http_request_class_entry
)) {
391 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
392 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
394 obj
->iterator
.pos
= -1;
395 php_http_request_pool_detach(obj
->pool
, request
);
396 } end_error_handling();
398 } end_error_handling();
400 RETVAL_ZVAL(getThis(), 1, 0);
403 PHP_METHOD(HttpRequestPool
, send
)
407 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
408 if (SUCCESS
== zend_parse_parameters_none()) {
409 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
410 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
412 php_http_request_pool_exec(obj
->pool
);
413 } end_error_handling();
415 } end_error_handling();
417 RETVAL_ZVAL(getThis(), 1, 0);
420 PHP_METHOD(HttpRequestPool
, once
)
422 if (SUCCESS
== zend_parse_parameters_none()) {
423 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
425 if (0 < php_http_request_pool_once(obj
->pool
)) {
432 PHP_METHOD(HttpRequestPool
, wait
)
436 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|d", &timeout
)) {
437 struct timeval timeout_val
;
438 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
440 timeout_val
.tv_sec
= (time_t) timeout
;
441 timeout_val
.tv_usec
= PHP_HTTP_USEC(timeout
) % PHP_HTTP_MCROSEC
;
443 RETURN_SUCCESS(php_http_request_pool_wait(obj
->pool
, timeout
? &timeout_val
: NULL
));
448 PHP_METHOD(HttpRequestPool
, valid
)
450 if (SUCCESS
== zend_parse_parameters_none()) {
451 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
453 RETURN_BOOL(obj
->iterator
.pos
>= 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
->requests
.attached
));
458 PHP_METHOD(HttpRequestPool
, current
)
460 if (SUCCESS
== zend_parse_parameters_none()) {
461 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
463 if (obj
->iterator
.pos
< zend_llist_count(&obj
->pool
->requests
.attached
)) {
465 zval
**current
= NULL
;
466 zend_llist_position lpos
;
468 for ( current
= zend_llist_get_first_ex(&obj
->pool
->requests
.attached
, &lpos
);
469 current
&& obj
->iterator
.pos
!= pos
++;
470 current
= zend_llist_get_next_ex(&obj
->pool
->requests
.attached
, &lpos
));
472 RETURN_OBJECT(*current
, 1);
479 PHP_METHOD(HttpRequestPool
, key
)
481 if (SUCCESS
== zend_parse_parameters_none()) {
482 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
484 RETURN_LONG(obj
->iterator
.pos
);
489 PHP_METHOD(HttpRequestPool
, next
)
491 if (SUCCESS
== zend_parse_parameters_none()) {
492 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
498 PHP_METHOD(HttpRequestPool
, rewind
)
500 if (SUCCESS
== zend_parse_parameters_none()) {
501 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
503 obj
->iterator
.pos
= 0;
507 PHP_METHOD(HttpRequestPool
, count
)
509 if (SUCCESS
== zend_parse_parameters_none()) {
510 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
512 RETURN_LONG((long) zend_llist_count(&obj
->pool
->requests
.attached
));
516 PHP_METHOD(HttpRequestPool
, getAttachedRequests
)
518 if (SUCCESS
== zend_parse_parameters_none()) {
519 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
521 array_init(return_value
);
522 zend_llist_apply_with_argument(&obj
->pool
->requests
.attached
,
523 (llist_apply_with_arg_func_t
) php_http_request_pool_object_llist2array
,
524 return_value TSRMLS_CC
);
530 PHP_METHOD(HttpRequestPool
, getFinishedRequests
)
532 if (SUCCESS
== zend_parse_parameters_none()) {
533 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
535 array_init(return_value
);
536 zend_llist_apply_with_argument(&obj
->pool
->requests
.finished
,
537 (llist_apply_with_arg_func_t
) php_http_request_pool_object_llist2array
,
538 return_value TSRMLS_CC
);
544 PHP_METHOD(HttpRequestPool
, enablePipelining
)
546 zend_bool enable
= 1;
548 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &enable
)) {
549 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
551 php_http_request_pool_setopt(obj
->pool
, PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING
, &enable
);
553 RETVAL_ZVAL(getThis(), 1, 0);
556 PHP_METHOD(HttpRequestPool
, enableEvents
)
558 zend_bool enable
= 1;
560 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &enable
)) {
561 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
563 php_http_request_pool_setopt(obj
->pool
, PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS
, &enable
);
565 RETVAL_ZVAL(getThis(), 1, 0);
568 PHP_MINIT_FUNCTION(http_request_pool
)
570 PHP_HTTP_REGISTER_CLASS(http
\\Request
, Pool
, http_request_pool
, php_http_object_class_entry
, 0);
571 php_http_request_pool_class_entry
->create_object
= php_http_request_pool_object_new
;
572 memcpy(&php_http_request_pool_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
573 php_http_request_pool_object_handlers
.clone_obj
= NULL
;
575 zend_class_implements(php_http_request_pool_class_entry TSRMLS_CC
, 2, spl_ce_Countable
, zend_ce_iterator
);
586 * vim600: noet sw=4 ts=4 fdm=marker
587 * vim<600: noet sw=4 ts=4