- proper type checking, thanks Sara
[m6w6/ext-http] / http_requestpool_object.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #define HTTP_WANT_CURL
20 #include "php_http.h"
21
22 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
23
24 #include "zend_interfaces.h"
25
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"
32
33 #if defined(HAVE_SPL) && !defined(WONKY)
34 /* SPL doesn't install its headers */
35 extern PHPAPI zend_class_entry *spl_ce_Countable;
36 #endif
37
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)
41
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)
46 HTTP_END_ARGS;
47
48 HTTP_EMPTY_ARGS(__destruct, 0);
49 HTTP_EMPTY_ARGS(reset, 0);
50
51 HTTP_BEGIN_ARGS(attach, 1)
52 HTTP_ARG_OBJ(HttpRequest, request, 0)
53 HTTP_END_ARGS;
54
55 HTTP_BEGIN_ARGS(detach, 1)
56 HTTP_ARG_OBJ(HttpRequest, request, 0)
57 HTTP_END_ARGS;
58
59 HTTP_EMPTY_ARGS(send, 0);
60 HTTP_EMPTY_ARGS(socketPerform, 0);
61 HTTP_EMPTY_ARGS(socketSelect, 0);
62
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);
68
69 HTTP_EMPTY_ARGS(count, 0);
70
71 HTTP_EMPTY_ARGS(getAttachedRequests, 0);
72 HTTP_EMPTY_ARGS(getFinishedRequests, 0);
73
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);
76
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)
85
86 HTTP_REQPOOL_ME(socketPerform, ZEND_ACC_PROTECTED)
87 HTTP_REQPOOL_ME(socketSelect, ZEND_ACC_PROTECTED)
88
89 /* implements Interator */
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)
95
96 /* implmenents Countable */
97 HTTP_REQPOOL_ME(count, ZEND_ACC_PUBLIC)
98
99 HTTP_REQPOOL_ME(getAttachedRequests, ZEND_ACC_PUBLIC)
100 HTTP_REQPOOL_ME(getFinishedRequests, ZEND_ACC_PUBLIC)
101
102 EMPTY_FUNCTION_ENTRY
103 };
104 static zend_object_handlers http_requestpool_object_handlers;
105
106 PHP_MINIT_FUNCTION(http_requestpool_object)
107 {
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);
111 #else
112 zend_class_implements(http_requestpool_object_ce TSRMLS_CC, 1, zend_ce_iterator);
113 #endif
114
115 http_requestpool_object_handlers.clone_obj = NULL;
116 return SUCCESS;
117 }
118
119 zend_object_value _http_requestpool_object_new(zend_class_entry *ce TSRMLS_DC)
120 {
121 zend_object_value ov;
122 http_requestpool_object *o;
123
124 o = ecalloc(1, sizeof(http_requestpool_object));
125 o->zo.ce = ce;
126
127 http_request_pool_init(&o->pool);
128
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 *));
132
133 ov.handle = putObject(http_requestpool_object, o);
134 ov.handlers = &http_requestpool_object_handlers;
135
136 return ov;
137 }
138
139 static inline void _http_requestpool_object_declare_default_properties(TSRMLS_D)
140 {
141 zend_class_entry *ce = http_requestpool_object_ce;
142
143 DCL_PROP_N(PROTECTED, pool);
144 }
145
146 void _http_requestpool_object_free(zend_object *object TSRMLS_DC)
147 {
148 http_requestpool_object *o = (http_requestpool_object *) object;
149
150 if (OBJ_PROP(o)) {
151 zend_hash_destroy(OBJ_PROP(o));
152 FREE_HASHTABLE(OBJ_PROP(o));
153 }
154 http_request_pool_dtor(&o->pool);
155 efree(o);
156 }
157
158 #define http_requestpool_object_llist2array _http_requestpool_object_llist2array
159 static void _http_requestpool_object_llist2array(zval **req, zval *array TSRMLS_DC)
160 {
161 ZVAL_ADDREF(*req);
162 add_next_index_zval(array, *req);
163 }
164
165 /* ### USERLAND ### */
166
167 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
168 *
169 * Instantiate a new HttpRequestPool object. An HttpRequestPool is
170 * able to send several HttpRequests in parallel.
171 *
172 * WARNING: Don't attach/detach HttpRequest objects to the HttpRequestPool
173 * object while you're using the implemented Interator interface.
174 *
175 * Accepts virtual infinite optional parameters each referencing an
176 * HttpRequest object.
177 *
178 * Throws HttpRequestPoolException (HttpRequestException, HttpInvalidParamException).
179 *
180 * Example:
181 * <pre>
182 * <?php
183 * try {
184 * $pool = new HttpRequestPool(
185 * new HttpRequest('http://www.google.com/', HttpRequest::METH_HEAD),
186 * new HttpRequest('http://www.php.net/', HttpRequest::METH_HEAD)
187 * );
188 * $pool->send();
189 * foreach($pool as $request) {
190 * printf("%s is %s (%d)\n",
191 * $request->getUrl(),
192 * $request->getResponseCode() ? 'alive' : 'not alive',
193 * $request->getResponseCode()
194 * );
195 * }
196 * } catch (HttpException $e) {
197 * echo $e;
198 * }
199 * ?>
200 * </pre>
201 */
202 PHP_METHOD(HttpRequestPool, __construct)
203 {
204 int argc = ZEND_NUM_ARGS();
205 zval ***argv = safe_emalloc(argc, sizeof(zval *), 0);
206 getObject(http_requestpool_object, obj);
207
208 SET_EH_THROW_HTTP();
209 if (SUCCESS == zend_get_parameters_array_ex(argc, argv)) {
210 int i;
211
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();
217 }
218 }
219 http_request_pool_final();
220 }
221 efree(argv);
222 SET_EH_NORMAL();
223 }
224 /* }}} */
225
226 /* {{{ proto void HttpRequestPool::__destruct()
227 *
228 * Clean up HttpRequestPool object.
229 */
230 PHP_METHOD(HttpRequestPool, __destruct)
231 {
232 getObject(http_requestpool_object, obj);
233
234 NO_ARGS;
235
236 http_request_pool_detach_all(&obj->pool);
237 }
238 /* }}} */
239
240 /* {{{ proto void HttpRequestPool::reset()
241 *
242 * Detach all attached HttpRequest objects.
243 */
244 PHP_METHOD(HttpRequestPool, reset)
245 {
246 getObject(http_requestpool_object, obj);
247
248 NO_ARGS;
249
250 obj->iterator.pos = 0;
251 http_request_pool_detach_all(&obj->pool);
252 }
253
254 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
255 *
256 * Attach an HttpRequest object to this HttpRequestPool.
257 * WARNING: set all options prior attaching!
258 *
259 * Expects the parameter to be an HttpRequest object not alread attached to
260 * antother HttpRequestPool object.
261 *
262 * Returns TRUE on success, or FALSE on failure.
263 *
264 * Throws HttpInvalidParamException, HttpRequestException,
265 * HttpRequestPoolException, HttpEncodingException.
266 */
267 PHP_METHOD(HttpRequestPool, attach)
268 {
269 zval *request;
270 STATUS status = FAILURE;
271 getObject(http_requestpool_object, obj);
272
273 SET_EH_THROW_HTTP();
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");
277 } else {
278 status = http_request_pool_attach(&obj->pool, request);
279 }
280 }
281 SET_EH_NORMAL();
282 RETURN_SUCCESS(status);
283 }
284 /* }}} */
285
286 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
287 *
288 * Detach an HttpRequest object from this HttpRequestPool.
289 *
290 * Expects the parameter to be an HttpRequest object attached to this
291 * HttpRequestPool object.
292 *
293 * Returns TRUE on success, or FALSE on failure.
294 *
295 * Throws HttpInvalidParamException, HttpRequestPoolException.
296 */
297 PHP_METHOD(HttpRequestPool, detach)
298 {
299 zval *request;
300 STATUS status = FAILURE;
301 getObject(http_requestpool_object, obj);
302
303 SET_EH_THROW_HTTP();
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);
307 }
308 SET_EH_NORMAL();
309 RETURN_SUCCESS(status);
310 }
311 /* }}} */
312
313 /* {{{ proto bool HttpRequestPool::send()
314 *
315 * Send all attached HttpRequest objects in parallel.
316 *
317 * Returns TRUE on success, or FALSE on failure.
318 *
319 * Throws HttpRequestPoolException (HttpSocketException, HttpRequestException, HttpMalformedHeaderException).
320 */
321 PHP_METHOD(HttpRequestPool, send)
322 {
323 STATUS status;
324 getObject(http_requestpool_object, obj);
325
326 NO_ARGS;
327
328 SET_EH_THROW_HTTP();
329 status = http_request_pool_send(&obj->pool);
330 SET_EH_NORMAL();
331
332 RETURN_SUCCESS(status);
333 }
334 /* }}} */
335
336 /* {{{ proto protected bool HttpRequestPool::socketPerform()
337 *
338 * Returns TRUE until each request has finished its transaction.
339 *
340 * Usage:
341 * <pre>
342 * <?php
343 * class MyPool extends HttpRequestPool
344 * {
345 * public function send()
346 * {
347 * while ($this->socketPerform()) {
348 * if (!$this->socketSelect()) {
349 * throw new HttpSocketExcpetion;
350 * }
351 * }
352 * }
353 *
354 * protected final function socketPerform()
355 * {
356 * $result = parent::socketPerform();
357 * foreach ($this->getFinishedRequests() as $r) {
358 * $this->detach($r);
359 * // handle response of finished request
360 * }
361 * return $result;
362 * }
363 * }
364 * ?>
365 * </pre>
366 */
367 PHP_METHOD(HttpRequestPool, socketPerform)
368 {
369 getObject(http_requestpool_object, obj);
370
371 NO_ARGS;
372
373 if (0 < http_request_pool_perform(&obj->pool)) {
374 RETURN_TRUE;
375 } else {
376 RETURN_FALSE;
377 }
378 }
379 /* }}} */
380
381 /* {{{ proto protected bool HttpRequestPool::socketSelect()
382 *
383 * See HttpRequestPool::socketPerform().
384 *
385 * Returns TRUE on success, or FALSE on failure.
386 */
387 PHP_METHOD(HttpRequestPool, socketSelect)
388 {
389 getObject(http_requestpool_object, obj);
390
391 NO_ARGS;
392
393 RETURN_SUCCESS(http_request_pool_select(&obj->pool));
394 }
395 /* }}} */
396
397 /* implements Iterator */
398
399 /* {{{ proto bool HttpRequestPool::valid()
400 *
401 * Implements Iterator::valid().
402 */
403 PHP_METHOD(HttpRequestPool, valid)
404 {
405 NO_ARGS;
406
407 IF_RETVAL_USED {
408 getObject(http_requestpool_object, obj);
409 RETURN_BOOL(obj->iterator.pos >= 0 && obj->iterator.pos < zend_llist_count(&obj->pool.handles));
410 }
411 }
412 /* }}} */
413
414 /* {{{ proto HttpRequest HttpRequestPool::current()
415 *
416 * Implements Iterator::current().
417 */
418 PHP_METHOD(HttpRequestPool, current)
419 {
420 NO_ARGS;
421
422 IF_RETVAL_USED {
423 long pos = 0;
424 zval **current = NULL;
425 zend_llist_position lpos;
426 getObject(http_requestpool_object, obj);
427
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));
432 if (current) {
433 RETURN_OBJECT(*current);
434 }
435 }
436 RETURN_NULL();
437 }
438 }
439 /* }}} */
440
441 /* {{{ proto int HttpRequestPool::key()
442 *
443 * Implements Iterator::key().
444 */
445 PHP_METHOD(HttpRequestPool, key)
446 {
447 NO_ARGS;
448
449 IF_RETVAL_USED {
450 getObject(http_requestpool_object, obj);
451 RETURN_LONG(obj->iterator.pos);
452 }
453 }
454 /* }}} */
455
456 /* {{{ proto void HttpRequestPool::next()
457 *
458 * Implements Iterator::next().
459 */
460 PHP_METHOD(HttpRequestPool, next)
461 {
462 NO_ARGS {
463 getObject(http_requestpool_object, obj);
464 ++(obj->iterator.pos);
465 }
466 }
467 /* }}} */
468
469 /* {{{ proto void HttpRequestPool::rewind()
470 *
471 * Implements Iterator::rewind().
472 */
473 PHP_METHOD(HttpRequestPool, rewind)
474 {
475 NO_ARGS {
476 getObject(http_requestpool_object, obj);
477 obj->iterator.pos = 0;
478 }
479 }
480 /* }}} */
481
482 /* {{{ proto int HttpRequestPool::count()
483 *
484 * Implements Countable.
485 *
486 * Returns the number of attached HttpRequest objects.
487 */
488 PHP_METHOD(HttpRequestPool, count)
489 {
490 NO_ARGS {
491 getObject(http_requestpool_object, obj);
492 RETURN_LONG((long) zend_llist_count(&obj->pool.handles));
493 }
494 }
495 /* }}} */
496
497 /* {{{ proto array HttpRequestPool::getAttachedRequests()
498 *
499 * Get attached HttpRequest objects.
500 *
501 * Returns an array containing all currently attached HttpRequest objects.
502 */
503 PHP_METHOD(HttpRequestPool, getAttachedRequests)
504 {
505 getObject(http_requestpool_object, obj);
506
507 NO_ARGS;
508
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);
513 }
514 /* }}} */
515
516 /* {{{ proto array HttpRequestPool::getFinishedRequests()
517 *
518 * Get attached HttpRequest objects that already have finished their work.
519 *
520 * Returns an array containing all attached HttpRequest objects that
521 * already have finished their work.
522 */
523 PHP_METHOD(HttpRequestPool, getFinishedRequests)
524 {
525 getObject(http_requestpool_object, obj);
526
527 NO_ARGS;
528
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);
533 }
534 /* }}} */
535
536 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
537
538 /*
539 * Local variables:
540 * tab-width: 4
541 * c-basic-offset: 4
542 * End:
543 * vim600: noet sw=4 ts=4 fdm=marker
544 * vim<600: noet sw=4 ts=4
545 */
546