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