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