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