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 +--------------------------------------------------------------------+
21 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
23 #include "php_http_std_defs.h"
24 #include "php_http_api.h"
25 #include "php_http_requestpool_object.h"
26 #include "php_http_request_pool_api.h"
27 #include "php_http_request_object.h"
28 #include "php_http_exception_object.h"
30 #include "zend_interfaces.h"
32 # include "ext/spl/spl_array.h"
33 # include "ext/spl/spl_iterators.h"
37 # include <winsock2.h>
39 #include <curl/curl.h>
41 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args)
42 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, ret_ref)
43 #define HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, HTTP_ARGS(HttpRequestPool, method), visibility)
45 HTTP_BEGIN_ARGS_AR(HttpRequestPool
, __construct
, 0, 0)
46 HTTP_ARG_OBJ(HttpRequest
, request0
, 0)
47 HTTP_ARG_OBJ(HttpRequest
, request1
, 0)
48 HTTP_ARG_OBJ(HttpRequest
, requestN
, 0)
51 HTTP_EMPTY_ARGS(__destruct
, 0);
52 HTTP_EMPTY_ARGS(reset
, 0);
54 HTTP_BEGIN_ARGS(attach
, 1)
55 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
58 HTTP_BEGIN_ARGS(detach
, 1)
59 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
62 HTTP_EMPTY_ARGS(send
, 0);
63 HTTP_EMPTY_ARGS(socketPerform
, 0);
64 HTTP_EMPTY_ARGS(socketSelect
, 0);
66 HTTP_EMPTY_ARGS(valid
, 0);
67 HTTP_EMPTY_ARGS(current
, 1);
68 HTTP_EMPTY_ARGS(key
, 0);
69 HTTP_EMPTY_ARGS(next
, 0);
70 HTTP_EMPTY_ARGS(rewind
, 0);
72 HTTP_EMPTY_ARGS(count
, 0);
74 HTTP_EMPTY_ARGS(getAttachedRequests
, 0);
75 HTTP_EMPTY_ARGS(getFinishedRequests
, 0);
77 #define http_requestpool_object_declare_default_properties() _http_requestpool_object_declare_default_properties(TSRMLS_C)
78 static inline void _http_requestpool_object_declare_default_properties(TSRMLS_D
);
80 zend_class_entry
*http_requestpool_object_ce
;
81 zend_function_entry http_requestpool_object_fe
[] = {
82 HTTP_REQPOOL_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
83 HTTP_REQPOOL_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
84 HTTP_REQPOOL_ME(attach
, ZEND_ACC_PUBLIC
)
85 HTTP_REQPOOL_ME(detach
, ZEND_ACC_PUBLIC
)
86 HTTP_REQPOOL_ME(send
, ZEND_ACC_PUBLIC
)
87 HTTP_REQPOOL_ME(reset
, ZEND_ACC_PUBLIC
)
89 HTTP_REQPOOL_ME(socketPerform
, ZEND_ACC_PROTECTED
)
90 HTTP_REQPOOL_ME(socketSelect
, ZEND_ACC_PROTECTED
)
92 /* implements Interator */
93 HTTP_REQPOOL_ME(valid
, ZEND_ACC_PUBLIC
)
94 HTTP_REQPOOL_ME(current
, ZEND_ACC_PUBLIC
)
95 HTTP_REQPOOL_ME(key
, ZEND_ACC_PUBLIC
)
96 HTTP_REQPOOL_ME(next
, ZEND_ACC_PUBLIC
)
97 HTTP_REQPOOL_ME(rewind
, ZEND_ACC_PUBLIC
)
99 /* implmenents Countable */
100 HTTP_REQPOOL_ME(count
, ZEND_ACC_PUBLIC
)
102 HTTP_REQPOOL_ME(getAttachedRequests
, ZEND_ACC_PUBLIC
)
103 HTTP_REQPOOL_ME(getFinishedRequests
, ZEND_ACC_PUBLIC
)
107 static zend_object_handlers http_requestpool_object_handlers
;
109 PHP_MINIT_FUNCTION(http_requestpool_object
)
111 HTTP_REGISTER_CLASS_EX(HttpRequestPool
, http_requestpool_object
, NULL
, 0);
113 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 2, spl_ce_Countable
, spl_ce_Iterator
);
115 zend_class_implements(http_requestpool_object_ce TSRMLS_CC
, 1, zend_ce_iterator
);
118 http_requestpool_object_handlers
.clone_obj
= NULL
;
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
), 0, 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 static inline void _http_requestpool_object_declare_default_properties(TSRMLS_D
)
144 zend_class_entry
*ce
= http_requestpool_object_ce
;
146 DCL_PROP_N(PROTECTED
, pool
);
149 void _http_requestpool_object_free(zend_object
*object TSRMLS_DC
)
151 http_requestpool_object
*o
= (http_requestpool_object
*) object
;
154 zend_hash_destroy(OBJ_PROP(o
));
155 FREE_HASHTABLE(OBJ_PROP(o
));
157 http_request_pool_dtor(&o
->pool
);
161 #define http_requestpool_object_llist2array _http_requestpool_object_llist2array
162 static void _http_requestpool_object_llist2array(zval
**req
, zval
*array TSRMLS_DC
)
165 add_next_index_zval(array
, *req
);
168 /* ### USERLAND ### */
170 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
172 * Instantiate a new HttpRequestPool object. An HttpRequestPool is
173 * able to send several HttpRequests in parallel.
175 * WARNING: Don't attach/detach HttpRequest objects to the HttpRequestPool
176 * object while you're using the implemented Interator interface.
178 * Accepts virtual infinite optional parameters each referencing an
179 * HttpRequest object.
181 * Throws HttpRequestException, HttpRequestPoolException, HttpInvalidParamException.
187 * $pool = new HttpRequestPool(
188 * new HttpRequest('http://www.google.com/', HttpRequest::METH_HEAD),
189 * new HttpRequest('http://www.php.net/', HttpRequest::METH_HEAD)
192 * foreach($pool as $request) {
193 * printf("%s is %s (%d)\n",
194 * $request->getUrl(),
195 * $request->getResponseCode() ? 'alive' : 'not alive',
196 * $request->getResponseCode()
199 * } catch (HttpException $e) {
205 PHP_METHOD(HttpRequestPool
, __construct
)
207 int argc
= ZEND_NUM_ARGS();
208 zval
***argv
= safe_emalloc(argc
, sizeof(zval
*), 0);
209 getObject(http_requestpool_object
, obj
);
211 if (SUCCESS
== zend_get_parameters_array_ex(argc
, argv
)) {
214 for (i
= 0; i
< argc
; ++i
) {
215 if (Z_TYPE_PP(argv
[i
]) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(argv
[i
]), http_request_object_ce TSRMLS_CC
)) {
216 http_request_pool_attach(&obj
->pool
, *(argv
[i
]));
224 /* {{{ proto void HttpRequestPool::__destruct()
226 * Clean up HttpRequestPool object.
228 PHP_METHOD(HttpRequestPool
, __destruct
)
230 getObject(http_requestpool_object
, obj
);
234 http_request_pool_detach_all(&obj
->pool
);
238 /* {{{ proto void HttpRequestPool::reset()
240 * Detach all attached HttpRequest objects.
242 PHP_METHOD(HttpRequestPool
, reset
)
244 getObject(http_requestpool_object
, obj
);
248 obj
->iterator
.pos
= 0;
249 http_request_pool_detach_all(&obj
->pool
);
252 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
254 * Attach an HttpRequest object to this HttpRequestPool.
255 * WARNING: set all options prior attaching!
257 * Expects the parameter to be an HttpRequest object not alread attached to
258 * antother HttpRequestPool object.
260 * Returns TRUE on success, or FALSE on failure.
262 * Throws HttpInvalidParamException, HttpRequestException,
263 * HttpRequestPoolException, HttpEncodingException.
265 PHP_METHOD(HttpRequestPool
, attach
)
268 STATUS status
= FAILURE
;
269 getObject(http_requestpool_object
, obj
);
272 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
273 if (obj
->iterator
.pos
> 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
274 http_error(HE_THROW
, HTTP_E_REQUEST_POOL
, "Cannot attach to the HttpRequestPool while the iterator is active");
276 status
= http_request_pool_attach(&obj
->pool
, request
);
280 RETURN_SUCCESS(status
);
284 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
286 * Detach an HttpRequest object from this HttpRequestPool.
288 * Expects the parameter to be an HttpRequest object attached to this
289 * HttpRequestPool object.
291 * Returns TRUE on success, or FALSE on failure.
293 * Throws HttpInvalidParamException, HttpRequestPoolException.
295 PHP_METHOD(HttpRequestPool
, detach
)
298 STATUS status
= FAILURE
;
299 getObject(http_requestpool_object
, obj
);
302 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
303 obj
->iterator
.pos
= -1;
304 status
= http_request_pool_detach(&obj
->pool
, request
);
307 RETURN_SUCCESS(status
);
311 /* {{{ proto bool HttpRequestPool::send()
313 * Send all attached HttpRequest objects in parallel.
315 * Returns TRUE on success, or FALSE on failure.
317 * Throws HttpSocketException, HttpRequestException,
318 * HttpRequestPoolException, HttpMalformedHeaderException.
320 PHP_METHOD(HttpRequestPool
, send
)
323 getObject(http_requestpool_object
, obj
);
328 status
= http_request_pool_send(&obj
->pool
);
331 RETURN_SUCCESS(status
);
335 /* {{{ proto protected bool HttpRequestPool::socketPerform()
337 * Returns TRUE until each request has finished its transaction.
342 * class MyPool extends HttpRequestPool
344 * public function send()
346 * while ($this->socketPerform()) {
347 * if (!$this->socketSelect()) {
348 * throw new HttpSocketExcpetion;
353 * protected final function socketPerform()
355 * $result = parent::socketPerform();
356 * foreach ($this->getFinishedRequests() as $r) {
358 * // handle response of finished request
366 PHP_METHOD(HttpRequestPool
, socketPerform
)
368 getObject(http_requestpool_object
, obj
);
372 if (0 < http_request_pool_perform(&obj
->pool
)) {
380 /* {{{ proto protected bool HttpRequestPool::socketSelect()
382 * See HttpRequestPool::socketPerform().
384 * Returns TRUE on success, or FALSE on failure.
386 PHP_METHOD(HttpRequestPool
, socketSelect
)
388 getObject(http_requestpool_object
, obj
);
392 RETURN_SUCCESS(http_request_pool_select(&obj
->pool
));
396 /* implements Iterator */
398 /* {{{ proto bool HttpRequestPool::valid()
400 * Implements Iterator::valid().
402 PHP_METHOD(HttpRequestPool
, valid
)
407 getObject(http_requestpool_object
, obj
);
408 RETURN_BOOL(obj
->iterator
.pos
>= 0 && obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
));
413 /* {{{ proto HttpRequest HttpRequestPool::current()
415 * Implements Iterator::current().
417 PHP_METHOD(HttpRequestPool
, current
)
423 zval
**current
= NULL
;
424 zend_llist_position lpos
;
425 getObject(http_requestpool_object
, obj
);
427 if (obj
->iterator
.pos
< zend_llist_count(&obj
->pool
.handles
)) {
428 for ( current
= zend_llist_get_first_ex(&obj
->pool
.handles
, &lpos
);
429 current
&& obj
->iterator
.pos
!= pos
++;
430 current
= zend_llist_get_next_ex(&obj
->pool
.handles
, &lpos
));
432 RETURN_OBJECT(*current
);
440 /* {{{ proto int HttpRequestPool::key()
442 * Implements Iterator::key().
444 PHP_METHOD(HttpRequestPool
, key
)
449 getObject(http_requestpool_object
, obj
);
450 RETURN_LONG(obj
->iterator
.pos
);
455 /* {{{ proto void HttpRequestPool::next()
457 * Implements Iterator::next().
459 PHP_METHOD(HttpRequestPool
, next
)
462 getObject(http_requestpool_object
, obj
);
463 ++(obj
->iterator
.pos
);
468 /* {{{ proto void HttpRequestPool::rewind()
470 * Implements Iterator::rewind().
472 PHP_METHOD(HttpRequestPool
, rewind
)
475 getObject(http_requestpool_object
, obj
);
476 obj
->iterator
.pos
= 0;
481 /* {{{ proto int HttpRequestPool::count()
483 * Implements Countable.
485 * Returns the number of attached HttpRequest objects.
487 PHP_METHOD(HttpRequestPool
, count
)
490 getObject(http_requestpool_object
, obj
);
491 RETURN_LONG((long) zend_llist_count(&obj
->pool
.handles
));
496 /* {{{ proto array HttpRequestPool::getAttachedRequests()
498 * Get attached HttpRequest objects.
500 * Returns an array containing all currently attached HttpRequest objects.
502 PHP_METHOD(HttpRequestPool
, getAttachedRequests
)
504 getObject(http_requestpool_object
, obj
);
508 array_init(return_value
);
509 zend_llist_apply_with_argument(&obj
->pool
.handles
,
510 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
511 return_value TSRMLS_CC
);
515 /* {{{ proto array HttpRequestPool::getFinishedRequests()
517 * Get attached HttpRequest objects that already have finished their work.
519 * Returns an array containing all attached HttpRequest objects that
520 * already have finished their work.
522 PHP_METHOD(HttpRequestPool
, getFinishedRequests
)
524 getObject(http_requestpool_object
, obj
);
528 array_init(return_value
);
529 zend_llist_apply_with_argument(&obj
->pool
.finished
,
530 (llist_apply_with_arg_func_t
) http_requestpool_object_llist2array
,
531 return_value TSRMLS_CC
);
535 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
542 * vim600: noet sw=4 ts=4 fdm=marker
543 * vim<600: noet sw=4 ts=4