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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 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
)
17 php_http_request_pool_t
*free_h
= NULL
;
20 free_h
= h
= emalloc(sizeof(*h
));
22 memset(h
, 0, sizeof(*h
));
25 h
->rf
= rf
? rf
: php_http_resource_factory_init(NULL
, h
->ops
->rsrc
, NULL
, NULL
);
26 zend_llist_init(&h
->requests
.attached
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
27 zend_llist_init(&h
->requests
.finished
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
28 TSRMLS_SET_CTX(h
->ts
);
31 if (!(h
= h
->ops
->init(h
, init_arg
))) {
32 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_POOL
, "Could not initialize request pool");
42 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
)
44 if (from
->ops
->copy
) {
45 return from
->ops
->copy(from
, to
);
51 PHP_HTTP_API
void php_http_request_pool_dtor(php_http_request_pool_t
*h
)
57 zend_llist_clean(&h
->requests
.finished
);
58 zend_llist_clean(&h
->requests
.attached
);
60 if (h
->persistent_handle_id
) {
61 zval_ptr_dtor(&h
->persistent_handle_id
);
64 php_http_resource_factory_free(&h
->rf
);
67 PHP_HTTP_API
void php_http_request_pool_free(php_http_request_pool_t
**h
) {
69 php_http_request_pool_dtor(*h
);
75 PHP_HTTP_API STATUS
php_http_request_pool_attach(php_http_request_pool_t
*h
, zval
*request
)
77 TSRMLS_FETCH_FROM_CTX(h
->ts
);
81 php_http_request_method_t m
= PHP_HTTP_NO_REQUEST_METHOD
;
82 php_http_message_body_t
*body
= NULL
;
83 php_http_request_object_t
*obj
= zend_object_store_get_object(request TSRMLS_CC
);
85 if (SUCCESS
!= php_http_request_object_requesthandler(obj
, request
, &m
, &url
, &body TSRMLS_CC
)) {
88 if (SUCCESS
== h
->ops
->attach(h
, obj
->request
, m
, url
, body
)) {
91 zend_llist_add_element(&h
->requests
.attached
, &request
);
100 static int php_http_request_pool_compare_handles(void *h1
, void *h2
)
102 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
106 PHP_HTTP_API STATUS
php_http_request_pool_detach(php_http_request_pool_t
*h
, zval
*request
)
108 TSRMLS_FETCH_FROM_CTX(h
->ts
);
110 if (h
->ops
->detach
) {
111 php_http_request_object_t
*obj
= zend_object_store_get_object(request TSRMLS_CC
);
113 if (SUCCESS
== h
->ops
->detach(h
, obj
->request
)) {
114 zend_llist_del_element(&h
->requests
.finished
, request
, php_http_request_pool_compare_handles
);
115 zend_llist_del_element(&h
->requests
.attached
, request
, php_http_request_pool_compare_handles
);
123 PHP_HTTP_API STATUS
php_http_request_pool_wait(php_http_request_pool_t
*h
, struct timeval
*custom_timeout
)
126 return h
->ops
->wait(h
, custom_timeout
);
132 PHP_HTTP_API
int php_http_request_pool_once(php_http_request_pool_t
*h
)
135 return h
->ops
->once(h
);
141 PHP_HTTP_API STATUS
php_http_request_pool_exec(php_http_request_pool_t
*h
)
144 return h
->ops
->exec(h
);
150 static void detach(void *r
, void *h TSRMLS_DC
)
152 ((php_http_request_pool_t
*) h
)->ops
->detach(h
, ((php_http_request_object_t
*) zend_object_store_get_object(*((zval
**) r
) TSRMLS_CC
))->request
);
155 PHP_HTTP_API
void php_http_request_pool_reset(php_http_request_pool_t
*h
)
159 } else if (h
->ops
->detach
) {
160 TSRMLS_FETCH_FROM_CTX(h
->ts
);
162 zend_llist_apply_with_argument(&h
->requests
.attached
, detach
, h TSRMLS_CC
);
165 zend_llist_clean(&h
->requests
.attached
);
166 zend_llist_clean(&h
->requests
.finished
);
169 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 if (h
->ops
->setopt
) {
172 return h
->ops
->setopt(h
, opt
, arg
);
178 PHP_HTTP_API
void php_http_request_pool_requests(php_http_request_pool_t
*h
, zval
***attached
, zval
***finished
)
184 if ((count
= zend_llist_count(&h
->requests
.attached
))) {
185 *attached
= ecalloc(count
+ 1 /* terminating NULL */, sizeof(zval
*));
187 for (i
= 0, handle
= zend_llist_get_first(&h
->requests
.attached
); handle
; handle
= zend_llist_get_next(&h
->requests
.attached
)) {
189 (*attached
)[i
++] = *handle
;
197 if ((count
= zend_llist_count(&h
->requests
.finished
))) {
198 *finished
= ecalloc(count
+ 1 /* terminating NULL */, sizeof(zval
*));
200 for (i
= 0, handle
= zend_llist_get_first(&h
->requests
.finished
); handle
; handle
= zend_llist_get_next(&h
->requests
.finished
)) {
202 (*finished
)[i
++] = *handle
;
212 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args)
213 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, 0)
214 #define PHP_HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, PHP_HTTP_ARGS(HttpRequestPool, method), visibility)
216 PHP_HTTP_EMPTY_ARGS(__construct
);
218 PHP_HTTP_EMPTY_ARGS(__destruct
);
219 PHP_HTTP_EMPTY_ARGS(reset
);
221 PHP_HTTP_BEGIN_ARGS(attach
, 1)
222 PHP_HTTP_ARG_OBJ(http
\\Request
, request
, 0)
225 PHP_HTTP_BEGIN_ARGS(detach
, 1)
226 PHP_HTTP_ARG_OBJ(http
\\Request
, request
, 0)
229 PHP_HTTP_EMPTY_ARGS(send
);
230 PHP_HTTP_EMPTY_ARGS(once
);
231 PHP_HTTP_BEGIN_ARGS(wait
, 0)
232 PHP_HTTP_ARG_VAL(timeout
, 0)
235 PHP_HTTP_EMPTY_ARGS(valid
);
236 PHP_HTTP_EMPTY_ARGS(current
);
237 PHP_HTTP_EMPTY_ARGS(key
);
238 PHP_HTTP_EMPTY_ARGS(next
);
239 PHP_HTTP_EMPTY_ARGS(rewind
);
241 PHP_HTTP_EMPTY_ARGS(count
);
243 PHP_HTTP_EMPTY_ARGS(getAttachedRequests
);
244 PHP_HTTP_EMPTY_ARGS(getFinishedRequests
);
246 PHP_HTTP_BEGIN_ARGS(enablePipelining
, 0)
247 PHP_HTTP_ARG_VAL(enable
, 0)
250 PHP_HTTP_BEGIN_ARGS(enableEvents
, 0)
251 PHP_HTTP_ARG_VAL(enable
, 0)
254 zend_class_entry
*php_http_request_pool_class_entry
;
255 zend_function_entry php_http_request_pool_method_entry
[] = {
256 PHP_HTTP_REQPOOL_ME(__construct
, ZEND_ACC_PRIVATE
|ZEND_ACC_CTOR
)
257 PHP_HTTP_REQPOOL_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
258 PHP_HTTP_REQPOOL_ME(attach
, ZEND_ACC_PUBLIC
)
259 PHP_HTTP_REQPOOL_ME(detach
, ZEND_ACC_PUBLIC
)
260 PHP_HTTP_REQPOOL_ME(send
, ZEND_ACC_PUBLIC
)
261 PHP_HTTP_REQPOOL_ME(reset
, ZEND_ACC_PUBLIC
)
263 PHP_HTTP_REQPOOL_ME(once
, ZEND_ACC_PROTECTED
)
264 PHP_HTTP_REQPOOL_ME(wait
, ZEND_ACC_PROTECTED
)
266 /* implements Iterator */
267 PHP_HTTP_REQPOOL_ME(valid
, ZEND_ACC_PUBLIC
)
268 PHP_HTTP_REQPOOL_ME(current
, ZEND_ACC_PUBLIC
)
269 PHP_HTTP_REQPOOL_ME(key
, ZEND_ACC_PUBLIC
)
270 PHP_HTTP_REQPOOL_ME(next
, ZEND_ACC_PUBLIC
)
271 PHP_HTTP_REQPOOL_ME(rewind
, ZEND_ACC_PUBLIC
)
273 /* implmenents Countable */
274 PHP_HTTP_REQPOOL_ME(count
, ZEND_ACC_PUBLIC
)
276 PHP_HTTP_REQPOOL_ME(getAttachedRequests
, ZEND_ACC_PUBLIC
)
277 PHP_HTTP_REQPOOL_ME(getFinishedRequests
, ZEND_ACC_PUBLIC
)
279 PHP_HTTP_REQPOOL_ME(enablePipelining
, ZEND_ACC_PUBLIC
)
280 PHP_HTTP_REQPOOL_ME(enableEvents
, ZEND_ACC_PUBLIC
)
284 static zend_object_handlers php_http_request_pool_object_handlers
;
286 zend_object_value
php_http_request_pool_object_new(zend_class_entry
*ce TSRMLS_DC
)
288 return php_http_request_pool_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
291 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 zend_object_value ov
;
294 php_http_request_pool_object_t
*o
;
296 o
= ecalloc(1, sizeof(php_http_request_pool_object_t
));
297 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
298 object_properties_init((zend_object
*) o
, ce
);
300 if (!(o
->pool
= p
)) {
301 o
->pool
= php_http_request_pool_init(NULL
, NULL
, NULL
, NULL TSRMLS_CC
);
308 ov
.handle
= zend_objects_store_put(o
, NULL
, php_http_request_pool_object_free
, NULL TSRMLS_CC
);
309 ov
.handlers
= &php_http_request_pool_object_handlers
;
314 void php_http_request_pool_object_free(void *object TSRMLS_DC
)
316 php_http_request_pool_object_t
*o
= (php_http_request_pool_object_t
*) object
;
318 php_http_request_pool_free(&o
->pool
);
319 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
323 static void php_http_request_pool_object_llist2array(zval
**req
, zval
*array TSRMLS_DC
)
326 add_next_index_zval(array
, *req
);
330 PHP_METHOD(HttpRequestPool
, __construct
)
332 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
333 zend_parse_parameters_none();
334 } end_error_handling();
337 PHP_METHOD(HttpRequestPool
, __destruct
)
339 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
341 if (SUCCESS
!= zend_parse_parameters_none()) {
342 ; /* we always want to clean up */
345 php_http_request_pool_reset(obj
->pool
);
348 PHP_METHOD(HttpRequestPool
, reset
)
350 if (SUCCESS
== zend_parse_parameters_none()) {
351 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
353 obj
->iterator
.pos
= 0;
354 php_http_request_pool_reset(obj
->pool
);
356 RETVAL_ZVAL(getThis(), 1, 0);
359 PHP_METHOD(HttpRequestPool
, attach
)
361 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
364 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, php_http_request_class_entry
)) {
365 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
366 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
368 if (obj
->iterator
.pos
> 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
->requests
.attached
)) {
369 php_http_error(HE_THROW
, PHP_HTTP_E_REQUEST_POOL
, "Cannot attach to the HttpRequestPool while the iterator is active");
371 php_http_request_pool_attach(obj
->pool
, request
);
373 } end_error_handling();
375 } end_error_handling();
377 RETVAL_ZVAL(getThis(), 1, 0);
380 PHP_METHOD(HttpRequestPool
, detach
)
384 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
387 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, php_http_request_class_entry
)) {
388 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
389 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
391 obj
->iterator
.pos
= -1;
392 php_http_request_pool_detach(obj
->pool
, request
);
393 } end_error_handling();
395 } end_error_handling();
397 RETVAL_ZVAL(getThis(), 1, 0);
400 PHP_METHOD(HttpRequestPool
, send
)
404 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
405 if (SUCCESS
== zend_parse_parameters_none()) {
406 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
407 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
409 php_http_request_pool_exec(obj
->pool
);
410 } end_error_handling();
412 } end_error_handling();
414 RETVAL_ZVAL(getThis(), 1, 0);
417 PHP_METHOD(HttpRequestPool
, once
)
419 if (SUCCESS
== zend_parse_parameters_none()) {
420 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
422 if (0 < php_http_request_pool_once(obj
->pool
)) {
429 PHP_METHOD(HttpRequestPool
, wait
)
433 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|d", &timeout
)) {
434 struct timeval timeout_val
;
435 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
437 timeout_val
.tv_sec
= (time_t) timeout
;
438 timeout_val
.tv_usec
= PHP_HTTP_USEC(timeout
) % PHP_HTTP_MCROSEC
;
440 RETURN_SUCCESS(php_http_request_pool_wait(obj
->pool
, timeout
? &timeout_val
: NULL
));
445 PHP_METHOD(HttpRequestPool
, valid
)
447 if (SUCCESS
== zend_parse_parameters_none()) {
448 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
450 RETURN_BOOL(obj
->iterator
.pos
>= 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
->requests
.attached
));
455 PHP_METHOD(HttpRequestPool
, current
)
457 if (SUCCESS
== zend_parse_parameters_none()) {
458 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
460 if (obj
->iterator
.pos
< zend_llist_count(&obj
->pool
->requests
.attached
)) {
462 zval
**current
= NULL
;
463 zend_llist_position lpos
;
465 for ( current
= zend_llist_get_first_ex(&obj
->pool
->requests
.attached
, &lpos
);
466 current
&& obj
->iterator
.pos
!= pos
++;
467 current
= zend_llist_get_next_ex(&obj
->pool
->requests
.attached
, &lpos
));
469 RETURN_OBJECT(*current
, 1);
476 PHP_METHOD(HttpRequestPool
, key
)
478 if (SUCCESS
== zend_parse_parameters_none()) {
479 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
481 RETURN_LONG(obj
->iterator
.pos
);
486 PHP_METHOD(HttpRequestPool
, next
)
488 if (SUCCESS
== zend_parse_parameters_none()) {
489 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
495 PHP_METHOD(HttpRequestPool
, rewind
)
497 if (SUCCESS
== zend_parse_parameters_none()) {
498 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
500 obj
->iterator
.pos
= 0;
504 PHP_METHOD(HttpRequestPool
, count
)
506 if (SUCCESS
== zend_parse_parameters_none()) {
507 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
509 RETURN_LONG((long) zend_llist_count(&obj
->pool
->requests
.attached
));
513 PHP_METHOD(HttpRequestPool
, getAttachedRequests
)
515 if (SUCCESS
== zend_parse_parameters_none()) {
516 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
518 array_init(return_value
);
519 zend_llist_apply_with_argument(&obj
->pool
->requests
.attached
,
520 (llist_apply_with_arg_func_t
) php_http_request_pool_object_llist2array
,
521 return_value TSRMLS_CC
);
527 PHP_METHOD(HttpRequestPool
, getFinishedRequests
)
529 if (SUCCESS
== zend_parse_parameters_none()) {
530 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
532 array_init(return_value
);
533 zend_llist_apply_with_argument(&obj
->pool
->requests
.finished
,
534 (llist_apply_with_arg_func_t
) php_http_request_pool_object_llist2array
,
535 return_value TSRMLS_CC
);
541 PHP_METHOD(HttpRequestPool
, enablePipelining
)
543 zend_bool enable
= 1;
545 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &enable
)) {
546 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
548 php_http_request_pool_setopt(obj
->pool
, PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING
, &enable
);
550 RETVAL_ZVAL(getThis(), 1, 0);
553 PHP_METHOD(HttpRequestPool
, enableEvents
)
555 zend_bool enable
= 1;
557 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &enable
)) {
558 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
560 php_http_request_pool_setopt(obj
->pool
, PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS
, &enable
);
562 RETVAL_ZVAL(getThis(), 1, 0);
565 PHP_MINIT_FUNCTION(http_request_pool
)
567 PHP_HTTP_REGISTER_CLASS(http
\\Request
, Pool
, http_request_pool
, php_http_object_class_entry
, 0);
568 php_http_request_pool_class_entry
->create_object
= php_http_request_pool_object_new
;
569 memcpy(&php_http_request_pool_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
570 php_http_request_pool_object_handlers
.clone_obj
= NULL
;
572 zend_class_implements(php_http_request_pool_class_entry TSRMLS_CC
, 2, spl_ce_Countable
, zend_ce_iterator
);
583 * vim600: noet sw=4 ts=4 fdm=marker
584 * vim<600: noet sw=4 ts=4