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