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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
22 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
24 #include "zend_interfaces.h"
26 #include "php_http_api.h"
27 #include "php_http_exception_object.h"
28 #include "php_http_request_api.h"
29 #include "php_http_request_object.h"
30 #include "php_http_request_pool_api.h"
31 #include "php_http_requestpool_object.h"
33 #if defined(HAVE_SPL) && !defined(WONKY)
34 /* SPL doesn't install its headers */
35 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
38 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args)
39 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, ret_ref)
40 #define HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, HTTP_ARGS(HttpRequestPool, method), visibility)
42 HTTP_BEGIN_ARGS(__construct
, 0)
43 HTTP_ARG_OBJ(HttpRequest
, request0
, 0)
44 HTTP_ARG_OBJ(HttpRequest
, request1
, 0)
45 HTTP_ARG_OBJ(HttpRequest
, requestN
, 0)
48 HTTP_EMPTY_ARGS(__destruct
, 0);
49 HTTP_EMPTY_ARGS(reset
, 0);
51 HTTP_BEGIN_ARGS(attach
, 1)
52 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
55 HTTP_BEGIN_ARGS(detach
, 1)
56 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
59 HTTP_EMPTY_ARGS(send
, 0);
60 HTTP_EMPTY_ARGS(socketPerform
, 0);
61 HTTP_EMPTY_ARGS(socketSelect
, 0);
63 HTTP_EMPTY_ARGS(valid
, 0);
64 HTTP_EMPTY_ARGS(current
, 1);
65 HTTP_EMPTY_ARGS(key
, 0);
66 HTTP_EMPTY_ARGS(next
, 0);
67 HTTP_EMPTY_ARGS(rewind
, 0);
69 HTTP_EMPTY_ARGS(count
, 0);
71 HTTP_EMPTY_ARGS(getAttachedRequests
, 0);
72 HTTP_EMPTY_ARGS(getFinishedRequests
, 0);
74 #define http_requestpool_object_declare_default_properties() _http_requestpool_object_declare_default_properties(TSRMLS_C)
75 static inline void _http_requestpool_object_declare_default_properties(TSRMLS_D
);
77 zend_class_entry
*http_requestpool_object_ce
;
78 zend_function_entry http_requestpool_object_fe
[] = {
79 HTTP_REQPOOL_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
80 HTTP_REQPOOL_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
81 HTTP_REQPOOL_ME(attach
, ZEND_ACC_PUBLIC
)
82 HTTP_REQPOOL_ME(detach
, ZEND_ACC_PUBLIC
)
83 HTTP_REQPOOL_ME(send
, ZEND_ACC_PUBLIC
)
84 HTTP_REQPOOL_ME(reset
, ZEND_ACC_PUBLIC
)
86 HTTP_REQPOOL_ME(socketPerform
, ZEND_ACC_PROTECTED
)
87 HTTP_REQPOOL_ME(socketSelect
, ZEND_ACC_PROTECTED
)
89 /* implements Iterator */
90 HTTP_REQPOOL_ME(valid
, ZEND_ACC_PUBLIC
)
91 HTTP_REQPOOL_ME(current
, ZEND_ACC_PUBLIC
)
92 HTTP_REQPOOL_ME(key
, ZEND_ACC_PUBLIC
)
93 HTTP_REQPOOL_ME(next
, ZEND_ACC_PUBLIC
)
94 HTTP_REQPOOL_ME(rewind
, ZEND_ACC_PUBLIC
)
96 /* implmenents Countable */
97 HTTP_REQPOOL_ME(count
, ZEND_ACC_PUBLIC
)
99 HTTP_REQPOOL_ME(getAttachedRequests
, ZEND_ACC_PUBLIC
)
100 HTTP_REQPOOL_ME(getFinishedRequests
, ZEND_ACC_PUBLIC
)
104 static zend_object_handlers http_requestpool_object_handlers
;
106 PHP_MINIT_FUNCTION(http_requestpool_object
)
108 HTTP_REGISTER_CLASS_EX(HttpRequestPool
, http_requestpool_object
, NULL
, 0);
109 #if defined(HAVE_SPL) && !defined(WONKY)
110 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 2, spl_ce_Countable
, zend_ce_iterator
);
112 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
115 http_requestpool_object_handlers
.clone_obj
= NULL
;
119 zend_object_value
_http_requestpool_object_new(zend_class_entry
*ce TSRMLS_DC
)
121 zend_object_value ov
;
122 http_requestpool_object
*o
;
124 o
= ecalloc(1, sizeof(http_requestpool_object
));
127 http_request_pool_init(&o
->pool
);
129 ALLOC_HASHTABLE(OBJ_PROP(o
));
130 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
131 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
133 ov
.handle
= putObject(http_requestpool_object
, o
);
134 ov
.handlers
= &http_requestpool_object_handlers
;
139 static inline void _http_requestpool_object_declare_default_properties(TSRMLS_D
)
141 zend_class_entry
*ce
= http_requestpool_object_ce
;
143 DCL_PROP_N(PROTECTED
, pool
);
146 void _http_requestpool_object_free(zend_object
*object TSRMLS_DC
)
148 http_requestpool_object
*o
= (http_requestpool_object
*) object
;
151 zend_hash_destroy(OBJ_PROP(o
));
152 FREE_HASHTABLE(OBJ_PROP(o
));
154 http_request_pool_dtor(&o
->pool
);
158 #define http_requestpool_object_llist2array _http_requestpool_object_llist2array
159 static void _http_requestpool_object_llist2array(zval
**req
, zval
*array TSRMLS_DC
)
162 add_next_index_zval(array
, *req
);
165 /* ### USERLAND ### */
167 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
169 * Instantiate a new HttpRequestPool object. An HttpRequestPool is
170 * able to send several HttpRequests in parallel.
172 * WARNING: Don't attach/detach HttpRequest objects to the HttpRequestPool
173 * object while you're using the implemented Iterator interface.
175 * Accepts virtual infinite optional parameters each referencing an
176 * HttpRequest object.
178 * Throws HttpRequestPoolException (HttpRequestException, HttpInvalidParamException).
184 * $pool = new HttpRequestPool(
185 * new HttpRequest('http://www.google.com/', HttpRequest::METH_HEAD),
186 * new HttpRequest('http://www.php.net/', HttpRequest::METH_HEAD)
189 * foreach($pool as $request) {
190 * printf("%s is %s (%d)\n",
191 * $request->getUrl(),
192 * $request->getResponseCode() ? 'alive' : 'not alive',
193 * $request->getResponseCode()
196 * } catch (HttpException $e) {
202 PHP_METHOD(HttpRequestPool
, __construct
)
204 int argc
= ZEND_NUM_ARGS();
205 zval
***argv
= safe_emalloc(argc
, sizeof(zval
*), 0);
206 getObject(http_requestpool_object
, obj
);
209 if (SUCCESS
== zend_get_parameters_array_ex(argc
, argv
)) {
212 for (i
= 0; i
< argc
; ++i
) {
213 if (Z_TYPE_PP(argv
[i
]) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(argv
[i
]), http_request_object_ce TSRMLS_CC
)) {
214 http_request_pool_try
{
215 http_request_pool_attach(&obj
->pool
, *(argv
[i
]));
216 } http_request_pool_catch();
219 http_request_pool_final();
226 /* {{{ proto void HttpRequestPool::__destruct()
228 * Clean up HttpRequestPool object.
230 PHP_METHOD(HttpRequestPool
, __destruct
)
232 getObject(http_requestpool_object
, obj
);
236 http_request_pool_detach_all(&obj
->pool
);
240 /* {{{ proto void HttpRequestPool::reset()
242 * Detach all attached HttpRequest objects.
244 PHP_METHOD(HttpRequestPool
, reset
)
246 getObject(http_requestpool_object
, obj
);
250 obj
->iterator
.pos
= 0;
251 http_request_pool_detach_all(&obj
->pool
);
254 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
256 * Attach an HttpRequest object to this HttpRequestPool.
257 * WARNING: set all options prior attaching!
259 * Expects the parameter to be an HttpRequest object not alread attached to
260 * antother HttpRequestPool object.
262 * Returns TRUE on success, or FALSE on failure.
264 * Throws HttpInvalidParamException, HttpRequestException,
265 * HttpRequestPoolException, HttpEncodingException.
267 PHP_METHOD(HttpRequestPool
, attach
)
270 STATUS status
= FAILURE
;
271 getObject(http_requestpool_object
, obj
);
274 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
275 if (obj
->iterator
.pos
> 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
276 http_error(HE_THROW
, HTTP_E_REQUEST_POOL
, "Cannot attach to the HttpRequestPool while the iterator is active");
278 status
= http_request_pool_attach(&obj
->pool
, request
);
282 RETURN_SUCCESS(status
);
286 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
288 * Detach an HttpRequest object from this HttpRequestPool.
290 * Expects the parameter to be an HttpRequest object attached to this
291 * HttpRequestPool object.
293 * Returns TRUE on success, or FALSE on failure.
295 * Throws HttpInvalidParamException, HttpRequestPoolException.
297 PHP_METHOD(HttpRequestPool
, detach
)
300 STATUS status
= FAILURE
;
301 getObject(http_requestpool_object
, obj
);
304 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
305 obj
->iterator
.pos
= -1;
306 status
= http_request_pool_detach(&obj
->pool
, request
);
309 RETURN_SUCCESS(status
);
313 /* {{{ proto bool HttpRequestPool::send()
315 * Send all attached HttpRequest objects in parallel.
317 * Returns TRUE on success, or FALSE on failure.
319 * Throws HttpRequestPoolException (HttpSocketException, HttpRequestException, HttpMalformedHeaderException).
321 PHP_METHOD(HttpRequestPool
, send
)
324 getObject(http_requestpool_object
, obj
);
329 status
= http_request_pool_send(&obj
->pool
);
332 RETURN_SUCCESS(status
);
336 /* {{{ proto protected bool HttpRequestPool::socketPerform()
338 * Returns TRUE until each request has finished its transaction.
343 * class MyPool extends HttpRequestPool
345 * public function send()
347 * while ($this->socketPerform()) {
348 * if (!$this->socketSelect()) {
349 * throw new HttpSocketExcpetion;
354 * protected final function socketPerform()
356 * $result = parent::socketPerform();
357 * foreach ($this->getFinishedRequests() as $r) {
359 * // handle response of finished request
367 PHP_METHOD(HttpRequestPool
, socketPerform
)
369 getObject(http_requestpool_object
, obj
);
373 if (0 < http_request_pool_perform(&obj
->pool
)) {
381 /* {{{ proto protected bool HttpRequestPool::socketSelect()
383 * See HttpRequestPool::socketPerform().
385 * Returns TRUE on success, or FALSE on failure.
387 PHP_METHOD(HttpRequestPool
, socketSelect
)
389 getObject(http_requestpool_object
, obj
);
393 RETURN_SUCCESS(http_request_pool_select(&obj
->pool
));
397 /* implements Iterator */
399 /* {{{ proto bool HttpRequestPool::valid()
401 * Implements Iterator::valid().
403 PHP_METHOD(HttpRequestPool
, valid
)
408 getObject(http_requestpool_object
, obj
);
409 RETURN_BOOL(obj
->iterator
.pos
>= 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
));
414 /* {{{ proto HttpRequest HttpRequestPool::current()
416 * Implements Iterator::current().
418 PHP_METHOD(HttpRequestPool
, current
)
424 zval
**current
= NULL
;
425 zend_llist_position lpos
;
426 getObject(http_requestpool_object
, obj
);
428 if (obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
429 for ( current
= zend_llist_get_first_ex(&obj
->pool
.handles
, &lpos
);
430 current
&& obj
->iterator
.pos
!= pos
++;
431 current
= zend_llist_get_next_ex(&obj
->pool
.handles
, &lpos
));
433 RETURN_OBJECT(*current
);
441 /* {{{ proto int HttpRequestPool::key()
443 * Implements Iterator::key().
445 PHP_METHOD(HttpRequestPool
, key
)
450 getObject(http_requestpool_object
, obj
);
451 RETURN_LONG(obj
->iterator
.pos
);
456 /* {{{ proto void HttpRequestPool::next()
458 * Implements Iterator::next().
460 PHP_METHOD(HttpRequestPool
, next
)
463 getObject(http_requestpool_object
, obj
);
464 ++(obj
->iterator
.pos
);
469 /* {{{ proto void HttpRequestPool::rewind()
471 * Implements Iterator::rewind().
473 PHP_METHOD(HttpRequestPool
, rewind
)
476 getObject(http_requestpool_object
, obj
);
477 obj
->iterator
.pos
= 0;
482 /* {{{ proto int HttpRequestPool::count()
484 * Implements Countable.
486 * Returns the number of attached HttpRequest objects.
488 PHP_METHOD(HttpRequestPool
, count
)
491 getObject(http_requestpool_object
, obj
);
492 RETURN_LONG((long) zend_llist_count(&obj
->pool
.handles
));
497 /* {{{ proto array HttpRequestPool::getAttachedRequests()
499 * Get attached HttpRequest objects.
501 * Returns an array containing all currently attached HttpRequest objects.
503 PHP_METHOD(HttpRequestPool
, getAttachedRequests
)
505 getObject(http_requestpool_object
, obj
);
509 array_init(return_value
);
510 zend_llist_apply_with_argument(&obj
->pool
.handles
,
511 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
512 return_value TSRMLS_CC
);
516 /* {{{ proto array HttpRequestPool::getFinishedRequests()
518 * Get attached HttpRequest objects that already have finished their work.
520 * Returns an array containing all attached HttpRequest objects that
521 * already have finished their work.
523 PHP_METHOD(HttpRequestPool
, getFinishedRequests
)
525 getObject(http_requestpool_object
, obj
);
529 array_init(return_value
);
530 zend_llist_apply_with_argument(&obj
->pool
.finished
,
531 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
532 return_value TSRMLS_CC
);
536 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
543 * vim600: noet sw=4 ts=4 fdm=marker
544 * vim<600: noet sw=4 ts=4