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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_CURL
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
20 #include "zend_interfaces.h"
22 #include "php_http_api.h"
23 #include "php_http_exception_object.h"
24 #include "php_http_request_api.h"
25 #include "php_http_request_object.h"
26 #include "php_http_request_pool_api.h"
27 #include "php_http_requestpool_object.h"
29 #if defined(HAVE_SPL) && !defined(WONKY)
30 /* SPL doesn't install its headers */
31 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
34 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args)
35 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, 0)
36 #define HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, HTTP_ARGS(HttpRequestPool, method), visibility)
38 HTTP_EMPTY_ARGS(__construct
);
40 HTTP_EMPTY_ARGS(__destruct
);
41 HTTP_EMPTY_ARGS(reset
);
43 HTTP_BEGIN_ARGS(attach
, 1)
44 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
47 HTTP_BEGIN_ARGS(detach
, 1)
48 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
51 HTTP_EMPTY_ARGS(send
);
52 HTTP_EMPTY_ARGS(socketPerform
);
53 HTTP_BEGIN_ARGS(socketSelect
, 0)
54 HTTP_ARG_VAL(timeout
, 0)
57 HTTP_EMPTY_ARGS(valid
);
58 HTTP_EMPTY_ARGS(current
);
60 HTTP_EMPTY_ARGS(next
);
61 HTTP_EMPTY_ARGS(rewind
);
63 HTTP_EMPTY_ARGS(count
);
65 HTTP_EMPTY_ARGS(getAttachedRequests
);
66 HTTP_EMPTY_ARGS(getFinishedRequests
);
68 HTTP_BEGIN_ARGS(enablePipelining
, 0)
69 HTTP_ARG_VAL(enable
, 0)
72 HTTP_BEGIN_ARGS(enableEvents
, 0)
73 HTTP_ARG_VAL(enable
, 0)
76 zend_class_entry
*http_requestpool_object_ce
;
77 zend_function_entry http_requestpool_object_fe
[] = {
78 HTTP_REQPOOL_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
79 HTTP_REQPOOL_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
80 HTTP_REQPOOL_ME(attach
, ZEND_ACC_PUBLIC
)
81 HTTP_REQPOOL_ME(detach
, ZEND_ACC_PUBLIC
)
82 HTTP_REQPOOL_ME(send
, ZEND_ACC_PUBLIC
)
83 HTTP_REQPOOL_ME(reset
, ZEND_ACC_PUBLIC
)
85 HTTP_REQPOOL_ME(socketPerform
, ZEND_ACC_PROTECTED
)
86 HTTP_REQPOOL_ME(socketSelect
, ZEND_ACC_PROTECTED
)
88 /* implements Iterator */
89 HTTP_REQPOOL_ME(valid
, ZEND_ACC_PUBLIC
)
90 HTTP_REQPOOL_ME(current
, ZEND_ACC_PUBLIC
)
91 HTTP_REQPOOL_ME(key
, ZEND_ACC_PUBLIC
)
92 HTTP_REQPOOL_ME(next
, ZEND_ACC_PUBLIC
)
93 HTTP_REQPOOL_ME(rewind
, ZEND_ACC_PUBLIC
)
95 /* implmenents Countable */
96 HTTP_REQPOOL_ME(count
, ZEND_ACC_PUBLIC
)
98 HTTP_REQPOOL_ME(getAttachedRequests
, ZEND_ACC_PUBLIC
)
99 HTTP_REQPOOL_ME(getFinishedRequests
, ZEND_ACC_PUBLIC
)
101 HTTP_REQPOOL_ME(enablePipelining
, ZEND_ACC_PUBLIC
)
102 HTTP_REQPOOL_ME(enableEvents
, ZEND_ACC_PUBLIC
)
106 static zend_object_handlers http_requestpool_object_handlers
;
108 PHP_MINIT_FUNCTION(http_requestpool_object
)
110 HTTP_REGISTER_CLASS_EX(HttpRequestPool
, http_requestpool_object
, NULL
, 0);
111 http_requestpool_object_handlers
.clone_obj
= NULL
;
113 #if defined(HAVE_SPL) && !defined(WONKY)
114 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 2, spl_ce_Countable
, zend_ce_iterator
);
116 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
122 zend_object_value
_http_requestpool_object_new(zend_class_entry
*ce TSRMLS_DC
)
124 zend_object_value ov
;
125 http_requestpool_object
*o
;
127 o
= ecalloc(1, sizeof(http_requestpool_object
));
130 http_request_pool_init(&o
->pool
);
132 ALLOC_HASHTABLE(OBJ_PROP(o
));
133 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
134 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
136 ov
.handle
= putObject(http_requestpool_object
, o
);
137 ov
.handlers
= &http_requestpool_object_handlers
;
142 void _http_requestpool_object_free(zend_object
*object TSRMLS_DC
)
144 http_requestpool_object
*o
= (http_requestpool_object
*) object
;
146 http_request_pool_dtor(&o
->pool
);
150 #define http_requestpool_object_llist2array _http_requestpool_object_llist2array
151 static void _http_requestpool_object_llist2array(zval
**req
, zval
*array TSRMLS_DC
)
154 add_next_index_zval(array
, *req
);
157 /* ### USERLAND ### */
159 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
160 Creates a new HttpRequestPool object instance. */
161 PHP_METHOD(HttpRequestPool
, __construct
)
163 int argc
= ZEND_NUM_ARGS();
164 zval
***argv
= safe_emalloc(argc
, sizeof(zval
*), 0);
165 getObject(http_requestpool_object
, obj
);
168 if (SUCCESS
== zend_get_parameters_array_ex(argc
, argv
)) {
171 for (i
= 0; i
< argc
; ++i
) {
172 if (Z_TYPE_PP(argv
[i
]) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(argv
[i
]), http_request_object_ce TSRMLS_CC
)) {
173 http_request_pool_attach(&obj
->pool
, *(argv
[i
]));
178 http_final(HTTP_EX_CE(request_pool
));
183 /* {{{ proto void HttpRequestPool::__destruct()
184 Clean up HttpRequestPool object. */
185 PHP_METHOD(HttpRequestPool
, __destruct
)
187 getObject(http_requestpool_object
, obj
);
191 http_request_pool_detach_all(&obj
->pool
);
195 /* {{{ proto void HttpRequestPool::reset()
196 Detach all attached HttpRequest objects. */
197 PHP_METHOD(HttpRequestPool
, reset
)
199 getObject(http_requestpool_object
, obj
);
203 obj
->iterator
.pos
= 0;
204 http_request_pool_detach_all(&obj
->pool
);
207 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
208 Attach an HttpRequest object to this HttpRequestPool. WARNING: set all options prior attaching! */
209 PHP_METHOD(HttpRequestPool
, attach
)
212 STATUS status
= FAILURE
;
213 getObject(http_requestpool_object
, obj
);
216 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
217 if (obj
->iterator
.pos
> 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
218 http_error(HE_THROW
, HTTP_E_REQUEST_POOL
, "Cannot attach to the HttpRequestPool while the iterator is active");
220 status
= http_request_pool_attach(&obj
->pool
, request
);
224 RETURN_SUCCESS(status
);
228 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
229 Detach an HttpRequest object from this HttpRequestPool. */
230 PHP_METHOD(HttpRequestPool
, detach
)
233 STATUS status
= FAILURE
;
234 getObject(http_requestpool_object
, obj
);
237 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
238 obj
->iterator
.pos
= -1;
239 status
= http_request_pool_detach(&obj
->pool
, request
);
242 RETURN_SUCCESS(status
);
246 /* {{{ proto bool HttpRequestPool::send()
247 Send all attached HttpRequest objects in parallel. */
248 PHP_METHOD(HttpRequestPool
, send
)
251 getObject(http_requestpool_object
, obj
);
256 status
= http_request_pool_send(&obj
->pool
);
259 /* rethrow as HttpRequestPoolException */
260 http_final(HTTP_EX_CE(request_pool
));
262 RETURN_SUCCESS(status
);
266 /* {{{ proto protected bool HttpRequestPool::socketPerform()
267 Returns TRUE until each request has finished its transaction. */
268 PHP_METHOD(HttpRequestPool
, socketPerform
)
270 getObject(http_requestpool_object
, obj
);
274 if (0 < http_request_pool_perform(&obj
->pool
)) {
282 /* {{{ proto protected bool HttpRequestPool::socketSelect([double timeout]) */
283 PHP_METHOD(HttpRequestPool
, socketSelect
)
286 struct timeval custom_timeout
, *custom_timeout_ptr
= NULL
;
287 getObject(http_requestpool_object
, obj
);
289 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|d", &timeout
)) {
292 if (ZEND_NUM_ARGS() && timeout
> 0) {
293 custom_timeout
.tv_sec
= (time_t) timeout
;
294 custom_timeout
.tv_usec
= HTTP_USEC(timeout
) % HTTP_MCROSEC
;
295 custom_timeout_ptr
= &custom_timeout
;
298 RETURN_SUCCESS(http_request_pool_select_ex(&obj
->pool
, custom_timeout_ptr
));
302 /* {{{ proto bool HttpRequestPool::valid()
303 Implements Iterator::valid(). */
304 PHP_METHOD(HttpRequestPool
, valid
)
308 if (return_value_used
) {
309 getObject(http_requestpool_object
, obj
);
310 RETURN_BOOL(obj
->iterator
.pos
>= 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
));
315 /* {{{ proto HttpRequest HttpRequestPool::current()
316 Implements Iterator::current(). */
317 PHP_METHOD(HttpRequestPool
, current
)
321 if (return_value_used
) {
323 zval
**current
= NULL
;
324 zend_llist_position lpos
;
325 getObject(http_requestpool_object
, obj
);
327 if (obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
328 for ( current
= zend_llist_get_first_ex(&obj
->pool
.handles
, &lpos
);
329 current
&& obj
->iterator
.pos
!= pos
++;
330 current
= zend_llist_get_next_ex(&obj
->pool
.handles
, &lpos
));
332 RETURN_OBJECT(*current
, 1);
340 /* {{{ proto int HttpRequestPool::key()
341 Implements Iterator::key(). */
342 PHP_METHOD(HttpRequestPool
, key
)
346 if (return_value_used
) {
347 getObject(http_requestpool_object
, obj
);
348 RETURN_LONG(obj
->iterator
.pos
);
353 /* {{{ proto void HttpRequestPool::next()
354 Implements Iterator::next(). */
355 PHP_METHOD(HttpRequestPool
, next
)
358 getObject(http_requestpool_object
, obj
);
359 ++(obj
->iterator
.pos
);
364 /* {{{ proto void HttpRequestPool::rewind()
365 Implements Iterator::rewind(). */
366 PHP_METHOD(HttpRequestPool
, rewind
)
369 getObject(http_requestpool_object
, obj
);
370 obj
->iterator
.pos
= 0;
375 /* {{{ proto int HttpRequestPool::count()
376 Implements Countable::count(). */
377 PHP_METHOD(HttpRequestPool
, count
)
380 getObject(http_requestpool_object
, obj
);
381 RETURN_LONG((long) zend_llist_count(&obj
->pool
.handles
));
386 /* {{{ proto array HttpRequestPool::getAttachedRequests()
387 Get attached HttpRequest objects. */
388 PHP_METHOD(HttpRequestPool
, getAttachedRequests
)
390 getObject(http_requestpool_object
, obj
);
394 array_init(return_value
);
395 zend_llist_apply_with_argument(&obj
->pool
.handles
,
396 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
397 return_value TSRMLS_CC
);
401 /* {{{ proto array HttpRequestPool::getFinishedRequests()
402 Get attached HttpRequest objects that already have finished their work. */
403 PHP_METHOD(HttpRequestPool
, getFinishedRequests
)
405 getObject(http_requestpool_object
, obj
);
409 array_init(return_value
);
410 zend_llist_apply_with_argument(&obj
->pool
.finished
,
411 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
412 return_value TSRMLS_CC
);
416 /* {{{ proto bool HttpRequestPool::enablePipelining([bool enable = true])
417 Enables pipelining support for all attached requests if support in libcurl is given. */
418 PHP_METHOD(HttpRequestPool
, enablePipelining
)
420 zend_bool enable
= 1;
421 #if defined(HAVE_CURL_MULTI_SETOPT) && HTTP_CURL_VERSION(7,16,0)
422 getObject(http_requestpool_object
, obj
);
425 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &enable
)) {
428 #if defined(HAVE_CURL_MULTI_SETOPT) && HTTP_CURL_VERSION(7,16,0)
429 if (CURLM_OK
== curl_multi_setopt(obj
->pool
.ch
, CURLMOPT_PIPELINING
, (long) enable
)) {
437 /* {{{ proto bool HttpRequestPool::enableEvents([bool enable = true])
438 Enables event-driven I/O if support in libcurl is given. */
439 PHP_METHOD(HttpRequestPool
, enableEvents
)
441 zend_bool enable
= 1;
442 #if defined(HTTP_HAVE_EVENT)
443 getObject(http_requestpool_object
, obj
);
446 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &enable
)) {
447 #if defined(HTTP_HAVE_EVENT)
448 obj
->pool
.useevents
= enable
;
456 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
463 * vim600: noet sw=4 ts=4 fdm=marker
464 * vim<600: noet sw=4 ts=4