4 #include <Zend/zend_interfaces.h>
5 #include <ext/spl/spl_iterators.h>
7 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
)
9 php_http_request_pool_t
*free_h
= NULL
;
12 free_h
= h
= emalloc(sizeof(*h
));
14 memset(h
, 0, sizeof(*h
));
17 h
->rf
= rf
? rf
: php_http_resource_factory_init(NULL
, h
->ops
->rsrc
, NULL
, NULL TSRMLS_CC
);
18 zend_llist_init(&h
->requests
.attached
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
19 zend_llist_init(&h
->requests
.finished
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
20 TSRMLS_SET_CTX(h
->ts
);
23 if (!(h
= h
->ops
->init(h
, init_arg
))) {
24 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_POOL
, "Could not initialize request pool");
34 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
)
36 if (from
->ops
->copy
) {
37 return from
->ops
->copy(from
, to
);
43 PHP_HTTP_API
void php_http_request_pool_dtor(php_http_request_pool_t
*h
)
49 zend_llist_clean(&h
->requests
.finished
);
50 zend_llist_clean(&h
->requests
.attached
);
52 if (h
->persistent_handle_id
) {
53 zval_ptr_dtor(&h
->persistent_handle_id
);
57 PHP_HTTP_API
void php_http_request_pool_free(php_http_request_pool_t
**h
) {
59 php_http_request_pool_dtor(*h
);
65 PHP_HTTP_API STATUS
php_http_request_pool_attach(php_http_request_pool_t
*h
, zval
*request
)
67 TSRMLS_FETCH_FROM_CTX(h
->ts
);
71 php_http_request_method_t m
= PHP_HTTP_NO_REQUEST_METHOD
;
72 php_http_message_body_t
*body
= NULL
;
73 php_http_request_object_t
*obj
= zend_object_store_get_object(request TSRMLS_CC
);
75 if (SUCCESS
!= php_http_request_object_requesthandler(obj
, request
, &m
, &url
, &body
)) {
78 if (SUCCESS
== h
->ops
->attach(h
, obj
->request
, m
, url
, body
)) {
81 zend_llist_add_element(&h
->requests
.attached
, &request
);
90 static int php_http_request_pool_compare_handles(void *h1
, void *h2
)
92 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
96 PHP_HTTP_API STATUS
php_http_request_pool_detach(php_http_request_pool_t
*h
, zval
*request
)
98 TSRMLS_FETCH_FROM_CTX(h
->ts
);
100 if (h
->ops
->detach
) {
101 php_http_request_object_t
*obj
= zend_object_store_get_object(request TSRMLS_CC
);
103 if (SUCCESS
== h
->ops
->detach(h
, obj
->request
)) {
104 zend_llist_del_element(&h
->requests
.finished
, request
, php_http_request_pool_compare_handles
);
105 zend_llist_del_element(&h
->requests
.attached
, request
, php_http_request_pool_compare_handles
);
113 PHP_HTTP_API STATUS
php_http_request_pool_wait(php_http_request_pool_t
*h
, struct timeval
*custom_timeout
)
116 return h
->ops
->wait(h
, custom_timeout
);
122 PHP_HTTP_API
int php_http_request_pool_once(php_http_request_pool_t
*h
)
125 return h
->ops
->once(h
);
131 PHP_HTTP_API STATUS
php_http_request_pool_exec(php_http_request_pool_t
*h
)
133 TSRMLS_FETCH_FROM_CTX(h
->ts
);
136 return h
->ops
->exec(h
);
142 static void detach(void *r
, void *h TSRMLS_DC
)
144 ((php_http_request_pool_t
*) h
)->ops
->detach(h
, ((php_http_request_object_t
*) zend_object_store_get_object(*((zval
**) r
) TSRMLS_CC
))->request
);
147 PHP_HTTP_API
void php_http_request_pool_reset(php_http_request_pool_t
*h
)
151 } else if (h
->ops
->detach
) {
152 TSRMLS_FETCH_FROM_CTX(h
->ts
);
154 zend_llist_apply_with_argument(&h
->requests
.attached
, detach
, h TSRMLS_CC
);
157 zend_llist_clean(&h
->requests
.attached
);
158 zend_llist_clean(&h
->requests
.finished
);
161 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
)
163 if (h
->ops
->setopt
) {
164 return h
->ops
->setopt(h
, opt
, arg
);
170 PHP_HTTP_API
void php_http_request_pool_requests(php_http_request_pool_t
*h
, zval
***attached
, zval
***finished
)
176 if ((count
= zend_llist_count(&h
->requests
.attached
))) {
177 *attached
= ecalloc(count
+ 1 /* terminating NULL */, sizeof(zval
*));
179 for (i
= 0, handle
= zend_llist_get_first(&h
->requests
.attached
); handle
; handle
= zend_llist_get_next(&h
->requests
.attached
)) {
181 (*attached
)[i
++] = *handle
;
189 if ((count
= zend_llist_count(&h
->requests
.finished
))) {
190 *finished
= ecalloc(count
+ 1 /* terminating NULL */, sizeof(zval
*));
192 for (i
= 0, handle
= zend_llist_get_first(&h
->requests
.finished
); handle
; handle
= zend_llist_get_next(&h
->requests
.finished
)) {
194 (*finished
)[i
++] = *handle
;
204 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args)
205 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, 0)
206 #define PHP_HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, PHP_HTTP_ARGS(HttpRequestPool, method), visibility)
208 PHP_HTTP_EMPTY_ARGS(__construct
);
210 PHP_HTTP_EMPTY_ARGS(__destruct
);
211 PHP_HTTP_EMPTY_ARGS(reset
);
213 PHP_HTTP_BEGIN_ARGS(attach
, 1)
214 PHP_HTTP_ARG_OBJ(http
\\Request
, request
, 0)
217 PHP_HTTP_BEGIN_ARGS(detach
, 1)
218 PHP_HTTP_ARG_OBJ(http
\\Request
, request
, 0)
221 PHP_HTTP_EMPTY_ARGS(send
);
222 PHP_HTTP_EMPTY_ARGS(once
);
223 PHP_HTTP_BEGIN_ARGS(wait
, 0)
224 PHP_HTTP_ARG_VAL(timeout
, 0)
227 PHP_HTTP_EMPTY_ARGS(valid
);
228 PHP_HTTP_EMPTY_ARGS(current
);
229 PHP_HTTP_EMPTY_ARGS(key
);
230 PHP_HTTP_EMPTY_ARGS(next
);
231 PHP_HTTP_EMPTY_ARGS(rewind
);
233 PHP_HTTP_EMPTY_ARGS(count
);
235 PHP_HTTP_EMPTY_ARGS(getAttachedRequests
);
236 PHP_HTTP_EMPTY_ARGS(getFinishedRequests
);
238 PHP_HTTP_BEGIN_ARGS(enablePipelining
, 0)
239 PHP_HTTP_ARG_VAL(enable
, 0)
242 PHP_HTTP_BEGIN_ARGS(enableEvents
, 0)
243 PHP_HTTP_ARG_VAL(enable
, 0)
246 zend_class_entry
*php_http_request_pool_class_entry
;
247 zend_function_entry php_http_request_pool_method_entry
[] = {
248 PHP_HTTP_REQPOOL_ME(__construct
, ZEND_ACC_PRIVATE
|ZEND_ACC_CTOR
)
249 PHP_HTTP_REQPOOL_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
250 PHP_HTTP_REQPOOL_ME(attach
, ZEND_ACC_PUBLIC
)
251 PHP_HTTP_REQPOOL_ME(detach
, ZEND_ACC_PUBLIC
)
252 PHP_HTTP_REQPOOL_ME(send
, ZEND_ACC_PUBLIC
)
253 PHP_HTTP_REQPOOL_ME(reset
, ZEND_ACC_PUBLIC
)
255 PHP_HTTP_REQPOOL_ME(once
, ZEND_ACC_PROTECTED
)
256 PHP_HTTP_REQPOOL_ME(wait
, ZEND_ACC_PROTECTED
)
258 /* implements Iterator */
259 PHP_HTTP_REQPOOL_ME(valid
, ZEND_ACC_PUBLIC
)
260 PHP_HTTP_REQPOOL_ME(current
, ZEND_ACC_PUBLIC
)
261 PHP_HTTP_REQPOOL_ME(key
, ZEND_ACC_PUBLIC
)
262 PHP_HTTP_REQPOOL_ME(next
, ZEND_ACC_PUBLIC
)
263 PHP_HTTP_REQPOOL_ME(rewind
, ZEND_ACC_PUBLIC
)
265 /* implmenents Countable */
266 PHP_HTTP_REQPOOL_ME(count
, ZEND_ACC_PUBLIC
)
268 PHP_HTTP_REQPOOL_ME(getAttachedRequests
, ZEND_ACC_PUBLIC
)
269 PHP_HTTP_REQPOOL_ME(getFinishedRequests
, ZEND_ACC_PUBLIC
)
271 PHP_HTTP_REQPOOL_ME(enablePipelining
, ZEND_ACC_PUBLIC
)
272 PHP_HTTP_REQPOOL_ME(enableEvents
, ZEND_ACC_PUBLIC
)
276 static zend_object_handlers php_http_request_pool_object_handlers
;
278 zend_object_value
php_http_request_pool_object_new(zend_class_entry
*ce TSRMLS_DC
)
280 return php_http_request_pool_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
283 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
)
285 zend_object_value ov
;
286 php_http_request_pool_object_t
*o
;
288 o
= ecalloc(1, sizeof(php_http_request_pool_object_t
));
289 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
290 object_properties_init((zend_object
*) o
, ce
);
292 if (!(o
->pool
= p
)) {
293 o
->pool
= php_http_request_pool_init(NULL
, NULL
, NULL
, NULL TSRMLS_CC
);
300 ov
.handle
= zend_objects_store_put(o
, NULL
, php_http_request_pool_object_free
, NULL TSRMLS_CC
);
301 ov
.handlers
= &php_http_request_pool_object_handlers
;
306 void php_http_request_pool_object_free(void *object TSRMLS_DC
)
308 php_http_request_pool_object_t
*o
= (php_http_request_pool_object_t
*) object
;
310 php_http_request_pool_free(&o
->pool
);
311 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
315 static void php_http_request_pool_object_llist2array(zval
**req
, zval
*array TSRMLS_DC
)
318 add_next_index_zval(array
, *req
);
322 PHP_METHOD(HttpRequestPool
, __construct
)
324 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
325 zend_parse_parameters_none();
326 } end_error_handling();
329 PHP_METHOD(HttpRequestPool
, __destruct
)
331 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
333 if (SUCCESS
!= zend_parse_parameters_none()) {
334 ; /* we always want to clean up */
337 php_http_request_pool_reset(obj
->pool
);
340 PHP_METHOD(HttpRequestPool
, reset
)
342 if (SUCCESS
== zend_parse_parameters_none()) {
343 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
345 obj
->iterator
.pos
= 0;
346 php_http_request_pool_reset(obj
->pool
);
348 RETVAL_ZVAL(getThis(), 1, 0);
351 PHP_METHOD(HttpRequestPool
, attach
)
353 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
356 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, php_http_request_class_entry
)) {
357 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
358 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
360 if (obj
->iterator
.pos
> 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
->requests
.attached
)) {
361 php_http_error(HE_THROW
, PHP_HTTP_E_REQUEST_POOL
, "Cannot attach to the HttpRequestPool while the iterator is active");
363 php_http_request_pool_attach(obj
->pool
, request
);
365 } end_error_handling();
367 } end_error_handling();
369 RETVAL_ZVAL(getThis(), 1, 0);
372 PHP_METHOD(HttpRequestPool
, detach
)
376 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
379 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, php_http_request_class_entry
)) {
380 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
381 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
383 obj
->iterator
.pos
= -1;
384 php_http_request_pool_detach(obj
->pool
, request
);
385 } end_error_handling();
387 } end_error_handling();
389 RETVAL_ZVAL(getThis(), 1, 0);
392 PHP_METHOD(HttpRequestPool
, send
)
396 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
397 if (SUCCESS
== zend_parse_parameters_none()) {
398 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
399 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
401 php_http_request_pool_exec(obj
->pool
);
402 } end_error_handling();
404 } end_error_handling();
406 RETVAL_ZVAL(getThis(), 1, 0);
409 PHP_METHOD(HttpRequestPool
, once
)
411 if (SUCCESS
== zend_parse_parameters_none()) {
412 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
414 if (0 < php_http_request_pool_once(obj
->pool
)) {
421 PHP_METHOD(HttpRequestPool
, wait
)
425 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|d", &timeout
)) {
426 struct timeval timeout_val
;
427 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
429 timeout_val
.tv_sec
= (time_t) timeout
;
430 timeout_val
.tv_usec
= PHP_HTTP_USEC(timeout
) % PHP_HTTP_MCROSEC
;
432 RETURN_SUCCESS(php_http_request_pool_wait(obj
->pool
, timeout
? &timeout_val
: NULL
));
437 PHP_METHOD(HttpRequestPool
, valid
)
439 if (SUCCESS
== zend_parse_parameters_none()) {
440 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
442 RETURN_BOOL(obj
->iterator
.pos
>= 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
->requests
.attached
));
447 PHP_METHOD(HttpRequestPool
, current
)
449 if (SUCCESS
== zend_parse_parameters_none()) {
450 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
452 if (obj
->iterator
.pos
< zend_llist_count(&obj
->pool
->requests
.attached
)) {
454 zval
**current
= NULL
;
455 zend_llist_position lpos
;
457 for ( current
= zend_llist_get_first_ex(&obj
->pool
->requests
.attached
, &lpos
);
458 current
&& obj
->iterator
.pos
!= pos
++;
459 current
= zend_llist_get_next_ex(&obj
->pool
->requests
.attached
, &lpos
));
461 RETURN_OBJECT(*current
, 1);
468 PHP_METHOD(HttpRequestPool
, key
)
470 if (SUCCESS
== zend_parse_parameters_none()) {
471 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
473 RETURN_LONG(obj
->iterator
.pos
);
478 PHP_METHOD(HttpRequestPool
, next
)
480 if (SUCCESS
== zend_parse_parameters_none()) {
481 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
487 PHP_METHOD(HttpRequestPool
, rewind
)
489 if (SUCCESS
== zend_parse_parameters_none()) {
490 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
492 obj
->iterator
.pos
= 0;
496 PHP_METHOD(HttpRequestPool
, count
)
498 if (SUCCESS
== zend_parse_parameters_none()) {
499 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
501 RETURN_LONG((long) zend_llist_count(&obj
->pool
->requests
.attached
));
505 PHP_METHOD(HttpRequestPool
, getAttachedRequests
)
507 if (SUCCESS
== zend_parse_parameters_none()) {
508 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
510 array_init(return_value
);
511 zend_llist_apply_with_argument(&obj
->pool
->requests
.attached
,
512 (llist_apply_with_arg_func_t
) php_http_request_pool_object_llist2array
,
513 return_value TSRMLS_CC
);
519 PHP_METHOD(HttpRequestPool
, getFinishedRequests
)
521 if (SUCCESS
== zend_parse_parameters_none()) {
522 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
524 array_init(return_value
);
525 zend_llist_apply_with_argument(&obj
->pool
->requests
.finished
,
526 (llist_apply_with_arg_func_t
) php_http_request_pool_object_llist2array
,
527 return_value TSRMLS_CC
);
533 PHP_METHOD(HttpRequestPool
, enablePipelining
)
535 zend_bool enable
= 1;
537 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &enable
)) {
538 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
540 php_http_request_pool_setopt(obj
->pool
, PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING
, &enable
);
542 RETVAL_ZVAL(getThis(), 1, 0);
545 PHP_METHOD(HttpRequestPool
, enableEvents
)
547 zend_bool enable
= 1;
549 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &enable
)) {
550 php_http_request_pool_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
552 php_http_request_pool_setopt(obj
->pool
, PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS
, &enable
);
554 RETVAL_ZVAL(getThis(), 1, 0);
557 PHP_MINIT_FUNCTION(http_request_pool
)
559 PHP_HTTP_REGISTER_CLASS(http
\\request
, Pool
, http_request_pool
, php_http_object_class_entry
, 0);
560 php_http_request_pool_class_entry
->create_object
= php_http_request_pool_object_new
;
561 memcpy(&php_http_request_pool_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
562 php_http_request_pool_object_handlers
.clone_obj
= NULL
;
564 zend_class_implements(php_http_request_pool_class_entry TSRMLS_CC
, 3, spl_ce_Countable
, zend_ce_iterator
, php_http_fluently_callable_class_entry
);