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-2007, 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_EMPTY_ARGS(socketSelect
);
55 HTTP_EMPTY_ARGS(valid
);
56 HTTP_EMPTY_ARGS(current
);
58 HTTP_EMPTY_ARGS(next
);
59 HTTP_EMPTY_ARGS(rewind
);
61 HTTP_EMPTY_ARGS(count
);
63 HTTP_EMPTY_ARGS(getAttachedRequests
);
64 HTTP_EMPTY_ARGS(getFinishedRequests
);
66 HTTP_BEGIN_ARGS(enablePipelining
, 0)
67 HTTP_ARG_VAL(enable
, 0)
70 zend_class_entry
*http_requestpool_object_ce
;
71 zend_function_entry http_requestpool_object_fe
[] = {
72 HTTP_REQPOOL_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
73 HTTP_REQPOOL_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
74 HTTP_REQPOOL_ME(attach
, ZEND_ACC_PUBLIC
)
75 HTTP_REQPOOL_ME(detach
, ZEND_ACC_PUBLIC
)
76 HTTP_REQPOOL_ME(send
, ZEND_ACC_PUBLIC
)
77 HTTP_REQPOOL_ME(reset
, ZEND_ACC_PUBLIC
)
79 HTTP_REQPOOL_ME(socketPerform
, ZEND_ACC_PROTECTED
)
80 HTTP_REQPOOL_ME(socketSelect
, ZEND_ACC_PROTECTED
)
82 /* implements Iterator */
83 HTTP_REQPOOL_ME(valid
, ZEND_ACC_PUBLIC
)
84 HTTP_REQPOOL_ME(current
, ZEND_ACC_PUBLIC
)
85 HTTP_REQPOOL_ME(key
, ZEND_ACC_PUBLIC
)
86 HTTP_REQPOOL_ME(next
, ZEND_ACC_PUBLIC
)
87 HTTP_REQPOOL_ME(rewind
, ZEND_ACC_PUBLIC
)
89 /* implmenents Countable */
90 HTTP_REQPOOL_ME(count
, ZEND_ACC_PUBLIC
)
92 HTTP_REQPOOL_ME(getAttachedRequests
, ZEND_ACC_PUBLIC
)
93 HTTP_REQPOOL_ME(getFinishedRequests
, ZEND_ACC_PUBLIC
)
95 HTTP_REQPOOL_ME(enablePipelining
, ZEND_ACC_PUBLIC
)
99 static zend_object_handlers http_requestpool_object_handlers
;
101 PHP_MINIT_FUNCTION(http_requestpool_object
)
103 HTTP_REGISTER_CLASS_EX(HttpRequestPool
, http_requestpool_object
, NULL
, 0);
104 http_requestpool_object_handlers
.clone_obj
= NULL
;
106 #if defined(HAVE_SPL) && !defined(WONKY)
107 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 2, spl_ce_Countable
, zend_ce_iterator
);
109 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
115 zend_object_value
_http_requestpool_object_new(zend_class_entry
*ce TSRMLS_DC
)
117 zend_object_value ov
;
118 http_requestpool_object
*o
;
120 o
= ecalloc(1, sizeof(http_requestpool_object
));
123 http_request_pool_init(&o
->pool
);
125 ALLOC_HASHTABLE(OBJ_PROP(o
));
126 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
127 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
129 ov
.handle
= putObject(http_requestpool_object
, o
);
130 ov
.handlers
= &http_requestpool_object_handlers
;
135 void _http_requestpool_object_free(zend_object
*object TSRMLS_DC
)
137 http_requestpool_object
*o
= (http_requestpool_object
*) object
;
139 http_request_pool_dtor(&o
->pool
);
143 #define http_requestpool_object_llist2array _http_requestpool_object_llist2array
144 static void _http_requestpool_object_llist2array(zval
**req
, zval
*array TSRMLS_DC
)
147 add_next_index_zval(array
, *req
);
150 /* ### USERLAND ### */
152 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
153 Creates a new HttpRequestPool object instance. */
154 PHP_METHOD(HttpRequestPool
, __construct
)
156 int argc
= ZEND_NUM_ARGS();
157 zval
***argv
= safe_emalloc(argc
, sizeof(zval
*), 0);
158 getObject(http_requestpool_object
, obj
);
161 if (SUCCESS
== zend_get_parameters_array_ex(argc
, argv
)) {
164 for (i
= 0; i
< argc
; ++i
) {
165 if (Z_TYPE_PP(argv
[i
]) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(argv
[i
]), http_request_object_ce TSRMLS_CC
)) {
166 http_request_pool_attach(&obj
->pool
, *(argv
[i
]));
172 http_final(HTTP_EX_CE(request_pool
));
176 /* {{{ proto void HttpRequestPool::__destruct()
177 Clean up HttpRequestPool object. */
178 PHP_METHOD(HttpRequestPool
, __destruct
)
180 getObject(http_requestpool_object
, obj
);
184 http_request_pool_detach_all(&obj
->pool
);
188 /* {{{ proto void HttpRequestPool::reset()
189 Detach all attached HttpRequest objects. */
190 PHP_METHOD(HttpRequestPool
, reset
)
192 getObject(http_requestpool_object
, obj
);
196 obj
->iterator
.pos
= 0;
197 http_request_pool_detach_all(&obj
->pool
);
200 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
201 Attach an HttpRequest object to this HttpRequestPool. WARNING: set all options prior attaching! */
202 PHP_METHOD(HttpRequestPool
, attach
)
205 STATUS status
= FAILURE
;
206 getObject(http_requestpool_object
, obj
);
209 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
210 if (obj
->iterator
.pos
> 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
211 http_error(HE_THROW
, HTTP_E_REQUEST_POOL
, "Cannot attach to the HttpRequestPool while the iterator is active");
213 status
= http_request_pool_attach(&obj
->pool
, request
);
217 RETURN_SUCCESS(status
);
221 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
222 Detach an HttpRequest object from this HttpRequestPool. */
223 PHP_METHOD(HttpRequestPool
, detach
)
226 STATUS status
= FAILURE
;
227 getObject(http_requestpool_object
, obj
);
230 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
231 obj
->iterator
.pos
= -1;
232 status
= http_request_pool_detach(&obj
->pool
, request
);
235 RETURN_SUCCESS(status
);
239 /* {{{ proto bool HttpRequestPool::send()
240 Send all attached HttpRequest objects in parallel. */
241 PHP_METHOD(HttpRequestPool
, send
)
244 getObject(http_requestpool_object
, obj
);
249 status
= http_request_pool_send(&obj
->pool
);
252 /* rethrow as HttpRequestPoolException */
253 http_final(HTTP_EX_CE(request_pool
));
255 RETURN_SUCCESS(status
);
259 /* {{{ proto protected bool HttpRequestPool::socketPerform()
260 Returns TRUE until each request has finished its transaction. */
261 PHP_METHOD(HttpRequestPool
, socketPerform
)
263 getObject(http_requestpool_object
, obj
);
267 if (0 < http_request_pool_perform(&obj
->pool
)) {
275 /* {{{ proto protected bool HttpRequestPool::socketSelect() */
276 PHP_METHOD(HttpRequestPool
, socketSelect
)
278 getObject(http_requestpool_object
, obj
);
282 RETURN_SUCCESS(http_request_pool_select(&obj
->pool
));
286 /* {{{ proto bool HttpRequestPool::valid()
287 Implements Iterator::valid(). */
288 PHP_METHOD(HttpRequestPool
, valid
)
292 if (return_value_used
) {
293 getObject(http_requestpool_object
, obj
);
294 RETURN_BOOL(obj
->iterator
.pos
>= 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
));
299 /* {{{ proto HttpRequest HttpRequestPool::current()
300 Implements Iterator::current(). */
301 PHP_METHOD(HttpRequestPool
, current
)
305 if (return_value_used
) {
307 zval
**current
= NULL
;
308 zend_llist_position lpos
;
309 getObject(http_requestpool_object
, obj
);
311 if (obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
312 for ( current
= zend_llist_get_first_ex(&obj
->pool
.handles
, &lpos
);
313 current
&& obj
->iterator
.pos
!= pos
++;
314 current
= zend_llist_get_next_ex(&obj
->pool
.handles
, &lpos
));
316 RETURN_OBJECT(*current
, 1);
324 /* {{{ proto int HttpRequestPool::key()
325 Implements Iterator::key(). */
326 PHP_METHOD(HttpRequestPool
, key
)
330 if (return_value_used
) {
331 getObject(http_requestpool_object
, obj
);
332 RETURN_LONG(obj
->iterator
.pos
);
337 /* {{{ proto void HttpRequestPool::next()
338 Implements Iterator::next(). */
339 PHP_METHOD(HttpRequestPool
, next
)
342 getObject(http_requestpool_object
, obj
);
343 ++(obj
->iterator
.pos
);
348 /* {{{ proto void HttpRequestPool::rewind()
349 Implements Iterator::rewind(). */
350 PHP_METHOD(HttpRequestPool
, rewind
)
353 getObject(http_requestpool_object
, obj
);
354 obj
->iterator
.pos
= 0;
359 /* {{{ proto int HttpRequestPool::count()
360 Implements Countable::count(). */
361 PHP_METHOD(HttpRequestPool
, count
)
364 getObject(http_requestpool_object
, obj
);
365 RETURN_LONG((long) zend_llist_count(&obj
->pool
.handles
));
370 /* {{{ proto array HttpRequestPool::getAttachedRequests()
371 Get attached HttpRequest objects. */
372 PHP_METHOD(HttpRequestPool
, getAttachedRequests
)
374 getObject(http_requestpool_object
, obj
);
378 array_init(return_value
);
379 zend_llist_apply_with_argument(&obj
->pool
.handles
,
380 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
381 return_value TSRMLS_CC
);
385 /* {{{ proto array HttpRequestPool::getFinishedRequests()
386 Get attached HttpRequest objects that already have finished their work. */
387 PHP_METHOD(HttpRequestPool
, getFinishedRequests
)
389 getObject(http_requestpool_object
, obj
);
393 array_init(return_value
);
394 zend_llist_apply_with_argument(&obj
->pool
.finished
,
395 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
396 return_value TSRMLS_CC
);
400 /* {{{ proto bool HttpRequest::enablePiplelinig([bool enable = true])
401 Enables pipelining support for all attached requests if support in libcurl is given. */
402 PHP_METHOD(HttpRequestPool
, enablePipelining
)
404 zend_bool enable
= 1;
405 getObject(http_requestpool_object
, obj
);
407 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &enable
)) {
410 #if defined(HAVE_CURL_MULTI_SETOPT) && HTTP_CURL_VERSION(7,16,0)
411 if (CURLM_OK
== curl_multi_setopt(obj
->pool
.ch
, CURLMOPT_PIPELINING
, (long) enable
)) {
419 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
426 * vim600: noet sw=4 ts=4 fdm=marker
427 * vim<600: noet sw=4 ts=4