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-2006, 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_BEGIN_ARGS(__construct
, 0)
39 HTTP_ARG_OBJ(HttpRequest
, request0
, 0)
40 HTTP_ARG_OBJ(HttpRequest
, request1
, 0)
41 HTTP_ARG_OBJ(HttpRequest
, requestN
, 0)
44 HTTP_EMPTY_ARGS(__destruct
);
45 HTTP_EMPTY_ARGS(reset
);
47 HTTP_BEGIN_ARGS(attach
, 1)
48 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
51 HTTP_BEGIN_ARGS(detach
, 1)
52 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
55 HTTP_EMPTY_ARGS(send
);
56 HTTP_EMPTY_ARGS(socketPerform
);
57 HTTP_EMPTY_ARGS(socketSelect
);
59 HTTP_EMPTY_ARGS(valid
);
60 HTTP_EMPTY_ARGS(current
);
62 HTTP_EMPTY_ARGS(next
);
63 HTTP_EMPTY_ARGS(rewind
);
65 HTTP_EMPTY_ARGS(count
);
67 HTTP_EMPTY_ARGS(getAttachedRequests
);
68 HTTP_EMPTY_ARGS(getFinishedRequests
);
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
)
97 static zend_object_handlers http_requestpool_object_handlers
;
99 PHP_MINIT_FUNCTION(http_requestpool_object
)
101 HTTP_REGISTER_CLASS_EX(HttpRequestPool
, http_requestpool_object
, NULL
, 0);
102 http_requestpool_object_handlers
.clone_obj
= NULL
;
104 #if defined(HAVE_SPL) && !defined(WONKY)
105 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 2, spl_ce_Countable
, zend_ce_iterator
);
107 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
113 zend_object_value
_http_requestpool_object_new(zend_class_entry
*ce TSRMLS_DC
)
115 zend_object_value ov
;
116 http_requestpool_object
*o
;
118 o
= ecalloc(1, sizeof(http_requestpool_object
));
121 http_request_pool_init(&o
->pool
);
123 ALLOC_HASHTABLE(OBJ_PROP(o
));
124 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
125 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
127 ov
.handle
= putObject(http_requestpool_object
, o
);
128 ov
.handlers
= &http_requestpool_object_handlers
;
133 void _http_requestpool_object_free(zend_object
*object TSRMLS_DC
)
135 http_requestpool_object
*o
= (http_requestpool_object
*) object
;
138 zend_hash_destroy(OBJ_PROP(o
));
139 FREE_HASHTABLE(OBJ_PROP(o
));
141 http_request_pool_dtor(&o
->pool
);
145 #define http_requestpool_object_llist2array _http_requestpool_object_llist2array
146 static void _http_requestpool_object_llist2array(zval
**req
, zval
*array TSRMLS_DC
)
149 add_next_index_zval(array
, *req
);
152 /* ### USERLAND ### */
154 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
156 * Instantiate a new HttpRequestPool object. An HttpRequestPool is
157 * able to send several HttpRequests in parallel.
159 * WARNING: Don't attach/detach HttpRequest objects to the HttpRequestPool
160 * object while you're using the implemented Iterator interface.
162 * Accepts virtual infinite optional parameters each referencing an
163 * HttpRequest object.
165 * Throws HttpRequestPoolException (HttpRequestException, HttpInvalidParamException).
171 * $pool = new HttpRequestPool(
172 * new HttpRequest('http://www.google.com/', HttpRequest::METH_HEAD),
173 * new HttpRequest('http://www.php.net/', HttpRequest::METH_HEAD)
176 * foreach($pool as $request) {
177 * printf("%s is %s (%d)\n",
178 * $request->getUrl(),
179 * $request->getResponseCode() ? 'alive' : 'not alive',
180 * $request->getResponseCode()
183 * } catch (HttpException $e) {
189 PHP_METHOD(HttpRequestPool
, __construct
)
191 int argc
= ZEND_NUM_ARGS();
192 zval
***argv
= safe_emalloc(argc
, sizeof(zval
*), 0);
193 getObject(http_requestpool_object
, obj
);
196 if (SUCCESS
== zend_get_parameters_array_ex(argc
, argv
)) {
199 for (i
= 0; i
< argc
; ++i
) {
200 if (Z_TYPE_PP(argv
[i
]) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(argv
[i
]), http_request_object_ce TSRMLS_CC
)) {
201 http_request_pool_try
{
202 http_request_pool_attach(&obj
->pool
, *(argv
[i
]));
203 } http_request_pool_catch();
206 http_request_pool_final();
213 /* {{{ proto void HttpRequestPool::__destruct()
215 * Clean up HttpRequestPool object.
217 PHP_METHOD(HttpRequestPool
, __destruct
)
219 getObject(http_requestpool_object
, obj
);
223 http_request_pool_detach_all(&obj
->pool
);
227 /* {{{ proto void HttpRequestPool::reset()
229 * Detach all attached HttpRequest objects.
231 PHP_METHOD(HttpRequestPool
, reset
)
233 getObject(http_requestpool_object
, obj
);
237 obj
->iterator
.pos
= 0;
238 http_request_pool_detach_all(&obj
->pool
);
241 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
243 * Attach an HttpRequest object to this HttpRequestPool.
244 * WARNING: set all options prior attaching!
246 * Expects the parameter to be an HttpRequest object not already attached to
247 * antother HttpRequestPool object.
249 * Returns TRUE on success, or FALSE on failure.
251 * Throws HttpInvalidParamException, HttpRequestException,
252 * HttpRequestPoolException, HttpEncodingException.
254 PHP_METHOD(HttpRequestPool
, attach
)
257 STATUS status
= FAILURE
;
258 getObject(http_requestpool_object
, obj
);
261 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
262 if (obj
->iterator
.pos
> 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
263 http_error(HE_THROW
, HTTP_E_REQUEST_POOL
, "Cannot attach to the HttpRequestPool while the iterator is active");
265 status
= http_request_pool_attach(&obj
->pool
, request
);
269 RETURN_SUCCESS(status
);
273 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
275 * Detach an HttpRequest object from this HttpRequestPool.
277 * Expects the parameter to be an HttpRequest object attached to this
278 * HttpRequestPool object.
280 * Returns TRUE on success, or FALSE on failure.
282 * Throws HttpInvalidParamException, HttpRequestPoolException.
284 PHP_METHOD(HttpRequestPool
, detach
)
287 STATUS status
= FAILURE
;
288 getObject(http_requestpool_object
, obj
);
291 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
292 obj
->iterator
.pos
= -1;
293 status
= http_request_pool_detach(&obj
->pool
, request
);
296 RETURN_SUCCESS(status
);
300 /* {{{ proto bool HttpRequestPool::send()
302 * Send all attached HttpRequest objects in parallel.
304 * Returns TRUE on success, or FALSE on failure.
306 * Throws HttpRequestPoolException (HttpSocketException, HttpRequestException, HttpMalformedHeaderException).
308 PHP_METHOD(HttpRequestPool
, send
)
311 getObject(http_requestpool_object
, obj
);
316 status
= http_request_pool_send(&obj
->pool
);
319 RETURN_SUCCESS(status
);
323 /* {{{ proto protected bool HttpRequestPool::socketPerform()
325 * Returns TRUE until each request has finished its transaction.
330 * class MyPool extends HttpRequestPool
332 * public function send()
334 * while ($this->socketPerform()) {
335 * if (!$this->socketSelect()) {
336 * throw new HttpSocketExcpetion;
341 * protected final function socketPerform()
343 * $result = parent::socketPerform();
344 * foreach ($this->getFinishedRequests() as $r) {
346 * // handle response of finished request
354 PHP_METHOD(HttpRequestPool
, socketPerform
)
356 getObject(http_requestpool_object
, obj
);
360 if (0 < http_request_pool_perform(&obj
->pool
)) {
368 /* {{{ proto protected bool HttpRequestPool::socketSelect()
370 * See HttpRequestPool::socketPerform().
372 * Returns TRUE on success, or FALSE on failure.
374 PHP_METHOD(HttpRequestPool
, socketSelect
)
376 getObject(http_requestpool_object
, obj
);
380 RETURN_SUCCESS(http_request_pool_select(&obj
->pool
));
384 /* implements Iterator */
386 /* {{{ proto bool HttpRequestPool::valid()
388 * Implements Iterator::valid().
390 PHP_METHOD(HttpRequestPool
, valid
)
395 getObject(http_requestpool_object
, obj
);
396 RETURN_BOOL(obj
->iterator
.pos
>= 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
));
401 /* {{{ proto HttpRequest HttpRequestPool::current()
403 * Implements Iterator::current().
405 PHP_METHOD(HttpRequestPool
, current
)
411 zval
**current
= NULL
;
412 zend_llist_position lpos
;
413 getObject(http_requestpool_object
, obj
);
415 if (obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
416 for ( current
= zend_llist_get_first_ex(&obj
->pool
.handles
, &lpos
);
417 current
&& obj
->iterator
.pos
!= pos
++;
418 current
= zend_llist_get_next_ex(&obj
->pool
.handles
, &lpos
));
420 RETURN_OBJECT(*current
, 1);
428 /* {{{ proto int HttpRequestPool::key()
430 * Implements Iterator::key().
432 PHP_METHOD(HttpRequestPool
, key
)
437 getObject(http_requestpool_object
, obj
);
438 RETURN_LONG(obj
->iterator
.pos
);
443 /* {{{ proto void HttpRequestPool::next()
445 * Implements Iterator::next().
447 PHP_METHOD(HttpRequestPool
, next
)
450 getObject(http_requestpool_object
, obj
);
451 ++(obj
->iterator
.pos
);
456 /* {{{ proto void HttpRequestPool::rewind()
458 * Implements Iterator::rewind().
460 PHP_METHOD(HttpRequestPool
, rewind
)
463 getObject(http_requestpool_object
, obj
);
464 obj
->iterator
.pos
= 0;
469 /* {{{ proto int HttpRequestPool::count()
471 * Implements Countable.
473 * Returns the number of attached HttpRequest objects.
475 PHP_METHOD(HttpRequestPool
, count
)
478 getObject(http_requestpool_object
, obj
);
479 RETURN_LONG((long) zend_llist_count(&obj
->pool
.handles
));
484 /* {{{ proto array HttpRequestPool::getAttachedRequests()
486 * Get attached HttpRequest objects.
488 * Returns an array containing all currently attached HttpRequest objects.
490 PHP_METHOD(HttpRequestPool
, getAttachedRequests
)
492 getObject(http_requestpool_object
, obj
);
496 array_init(return_value
);
497 zend_llist_apply_with_argument(&obj
->pool
.handles
,
498 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
499 return_value TSRMLS_CC
);
503 /* {{{ proto array HttpRequestPool::getFinishedRequests()
505 * Get attached HttpRequest objects that already have finished their work.
507 * Returns an array containing all attached HttpRequest objects that
508 * already have finished their work.
510 PHP_METHOD(HttpRequestPool
, getFinishedRequests
)
512 getObject(http_requestpool_object
, obj
);
516 array_init(return_value
);
517 zend_llist_apply_with_argument(&obj
->pool
.finished
,
518 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
519 return_value TSRMLS_CC
);
523 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
530 * vim600: noet sw=4 ts=4 fdm=marker
531 * vim<600: noet sw=4 ts=4