- implement feature request: 'bodyonly' request option;
[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_AR(HttpRequestPool, __construct, 0, 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 HttpRequestException, HttpRequestPoolException, 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 if (SUCCESS == zend_get_parameters_array_ex(argc, argv)) {
209 int i;
210
211 for (i = 0; i < argc; ++i) {
212 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), http_request_object_ce TSRMLS_CC)) {
213 http_request_pool_attach(&obj->pool, *(argv[i]));
214 }
215 }
216 }
217 efree(argv);
218 }
219 /* }}} */
220
221 /* {{{ proto void HttpRequestPool::__destruct()
222 *
223 * Clean up HttpRequestPool object.
224 */
225 PHP_METHOD(HttpRequestPool, __destruct)
226 {
227 getObject(http_requestpool_object, obj);
228
229 NO_ARGS;
230
231 http_request_pool_detach_all(&obj->pool);
232 }
233 /* }}} */
234
235 /* {{{ proto void HttpRequestPool::reset()
236 *
237 * Detach all attached HttpRequest objects.
238 */
239 PHP_METHOD(HttpRequestPool, reset)
240 {
241 getObject(http_requestpool_object, obj);
242
243 NO_ARGS;
244
245 obj->iterator.pos = 0;
246 http_request_pool_detach_all(&obj->pool);
247 }
248
249 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
250 *
251 * Attach an HttpRequest object to this HttpRequestPool.
252 * WARNING: set all options prior attaching!
253 *
254 * Expects the parameter to be an HttpRequest object not alread attached to
255 * antother HttpRequestPool object.
256 *
257 * Returns TRUE on success, or FALSE on failure.
258 *
259 * Throws HttpInvalidParamException, HttpRequestException,
260 * HttpRequestPoolException, HttpEncodingException.
261 */
262 PHP_METHOD(HttpRequestPool, attach)
263 {
264 zval *request;
265 STATUS status = FAILURE;
266 getObject(http_requestpool_object, obj);
267
268 SET_EH_THROW_HTTP();
269 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
270 if (obj->iterator.pos > 0 && obj->iterator.pos < zend_llist_count(&obj->pool.handles)) {
271 http_error(HE_THROW, HTTP_E_REQUEST_POOL, "Cannot attach to the HttpRequestPool while the iterator is active");
272 } else {
273 status = http_request_pool_attach(&obj->pool, request);
274 }
275 }
276 SET_EH_NORMAL();
277 RETURN_SUCCESS(status);
278 }
279 /* }}} */
280
281 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
282 *
283 * Detach an HttpRequest object from this HttpRequestPool.
284 *
285 * Expects the parameter to be an HttpRequest object attached to this
286 * HttpRequestPool object.
287 *
288 * Returns TRUE on success, or FALSE on failure.
289 *
290 * Throws HttpInvalidParamException, HttpRequestPoolException.
291 */
292 PHP_METHOD(HttpRequestPool, detach)
293 {
294 zval *request;
295 STATUS status = FAILURE;
296 getObject(http_requestpool_object, obj);
297
298 SET_EH_THROW_HTTP();
299 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
300 obj->iterator.pos = -1;
301 status = http_request_pool_detach(&obj->pool, request);
302 }
303 SET_EH_NORMAL();
304 RETURN_SUCCESS(status);
305 }
306 /* }}} */
307
308 /* {{{ proto bool HttpRequestPool::send()
309 *
310 * Send all attached HttpRequest objects in parallel.
311 *
312 * Returns TRUE on success, or FALSE on failure.
313 *
314 * Throws HttpSocketException, HttpRequestException,
315 * HttpRequestPoolException, HttpMalformedHeaderException.
316 */
317 PHP_METHOD(HttpRequestPool, send)
318 {
319 STATUS status;
320 getObject(http_requestpool_object, obj);
321
322 NO_ARGS;
323
324 SET_EH_THROW_HTTP();
325 status = http_request_pool_send(&obj->pool);
326 SET_EH_NORMAL();
327
328 RETURN_SUCCESS(status);
329 }
330 /* }}} */
331
332 /* {{{ proto protected bool HttpRequestPool::socketPerform()
333 *
334 * Returns TRUE until each request has finished its transaction.
335 *
336 * Usage:
337 * <pre>
338 * <?php
339 * class MyPool extends HttpRequestPool
340 * {
341 * public function send()
342 * {
343 * while ($this->socketPerform()) {
344 * if (!$this->socketSelect()) {
345 * throw new HttpSocketExcpetion;
346 * }
347 * }
348 * }
349 *
350 * protected final function socketPerform()
351 * {
352 * $result = parent::socketPerform();
353 * foreach ($this->getFinishedRequests() as $r) {
354 * $this->detach($r);
355 * // handle response of finished request
356 * }
357 * return $result;
358 * }
359 * }
360 * ?>
361 * </pre>
362 */
363 PHP_METHOD(HttpRequestPool, socketPerform)
364 {
365 getObject(http_requestpool_object, obj);
366
367 NO_ARGS;
368
369 if (0 < http_request_pool_perform(&obj->pool)) {
370 RETURN_TRUE;
371 } else {
372 RETURN_FALSE;
373 }
374 }
375 /* }}} */
376
377 /* {{{ proto protected bool HttpRequestPool::socketSelect()
378 *
379 * See HttpRequestPool::socketPerform().
380 *
381 * Returns TRUE on success, or FALSE on failure.
382 */
383 PHP_METHOD(HttpRequestPool, socketSelect)
384 {
385 getObject(http_requestpool_object, obj);
386
387 NO_ARGS;
388
389 RETURN_SUCCESS(http_request_pool_select(&obj->pool));
390 }
391 /* }}} */
392
393 /* implements Iterator */
394
395 /* {{{ proto bool HttpRequestPool::valid()
396 *
397 * Implements Iterator::valid().
398 */
399 PHP_METHOD(HttpRequestPool, valid)
400 {
401 NO_ARGS;
402
403 IF_RETVAL_USED {
404 getObject(http_requestpool_object, obj);
405 RETURN_BOOL(obj->iterator.pos >= 0 && obj->iterator.pos < zend_llist_count(&obj->pool.handles));
406 }
407 }
408 /* }}} */
409
410 /* {{{ proto HttpRequest HttpRequestPool::current()
411 *
412 * Implements Iterator::current().
413 */
414 PHP_METHOD(HttpRequestPool, current)
415 {
416 NO_ARGS;
417
418 IF_RETVAL_USED {
419 long pos = 0;
420 zval **current = NULL;
421 zend_llist_position lpos;
422 getObject(http_requestpool_object, obj);
423
424 if (obj->iterator.pos < zend_llist_count(&obj->pool.handles)) {
425 for ( current = zend_llist_get_first_ex(&obj->pool.handles, &lpos);
426 current && obj->iterator.pos != pos++;
427 current = zend_llist_get_next_ex(&obj->pool.handles, &lpos));
428 if (current) {
429 RETURN_OBJECT(*current);
430 }
431 }
432 RETURN_NULL();
433 }
434 }
435 /* }}} */
436
437 /* {{{ proto int HttpRequestPool::key()
438 *
439 * Implements Iterator::key().
440 */
441 PHP_METHOD(HttpRequestPool, key)
442 {
443 NO_ARGS;
444
445 IF_RETVAL_USED {
446 getObject(http_requestpool_object, obj);
447 RETURN_LONG(obj->iterator.pos);
448 }
449 }
450 /* }}} */
451
452 /* {{{ proto void HttpRequestPool::next()
453 *
454 * Implements Iterator::next().
455 */
456 PHP_METHOD(HttpRequestPool, next)
457 {
458 NO_ARGS {
459 getObject(http_requestpool_object, obj);
460 ++(obj->iterator.pos);
461 }
462 }
463 /* }}} */
464
465 /* {{{ proto void HttpRequestPool::rewind()
466 *
467 * Implements Iterator::rewind().
468 */
469 PHP_METHOD(HttpRequestPool, rewind)
470 {
471 NO_ARGS {
472 getObject(http_requestpool_object, obj);
473 obj->iterator.pos = 0;
474 }
475 }
476 /* }}} */
477
478 /* {{{ proto int HttpRequestPool::count()
479 *
480 * Implements Countable.
481 *
482 * Returns the number of attached HttpRequest objects.
483 */
484 PHP_METHOD(HttpRequestPool, count)
485 {
486 NO_ARGS {
487 getObject(http_requestpool_object, obj);
488 RETURN_LONG((long) zend_llist_count(&obj->pool.handles));
489 }
490 }
491 /* }}} */
492
493 /* {{{ proto array HttpRequestPool::getAttachedRequests()
494 *
495 * Get attached HttpRequest objects.
496 *
497 * Returns an array containing all currently attached HttpRequest objects.
498 */
499 PHP_METHOD(HttpRequestPool, getAttachedRequests)
500 {
501 getObject(http_requestpool_object, obj);
502
503 NO_ARGS;
504
505 array_init(return_value);
506 zend_llist_apply_with_argument(&obj->pool.handles,
507 (llist_apply_with_arg_func_t) http_requestpool_object_llist2array,
508 return_value TSRMLS_CC);
509 }
510 /* }}} */
511
512 /* {{{ proto array HttpRequestPool::getFinishedRequests()
513 *
514 * Get attached HttpRequest objects that already have finished their work.
515 *
516 * Returns an array containing all attached HttpRequest objects that
517 * already have finished their work.
518 */
519 PHP_METHOD(HttpRequestPool, getFinishedRequests)
520 {
521 getObject(http_requestpool_object, obj);
522
523 NO_ARGS;
524
525 array_init(return_value);
526 zend_llist_apply_with_argument(&obj->pool.finished,
527 (llist_apply_with_arg_func_t) http_requestpool_object_llist2array,
528 return_value TSRMLS_CC);
529 }
530 /* }}} */
531
532 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
533
534 /*
535 * Local variables:
536 * tab-width: 4
537 * c-basic-offset: 4
538 * End:
539 * vim600: noet sw=4 ts=4 fdm=marker
540 * vim<600: noet sw=4 ts=4
541 */
542