- rename http_absolute_uri to http_build_uri (complements http_build_query)
[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_requestpool_object.h"
28 #include "php_http_request_pool_api.h"
29 #include "php_http_request_object.h"
30 #include "php_http_exception_object.h"
31
32 #include "zend_interfaces.h"
33
34 #ifdef PHP_WIN32
35 # include <winsock2.h>
36 #endif
37 #include <curl/curl.h>
38
39 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args)
40 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, ret_ref)
41 #define HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, HTTP_ARGS(HttpRequestPool, method), visibility)
42
43 HTTP_BEGIN_ARGS_AR(HttpRequestPool, __construct, 0, 0)
44 HTTP_ARG_OBJ(HttpRequest, request0, 0)
45 HTTP_ARG_OBJ(HttpRequest, request1, 0)
46 HTTP_ARG_OBJ(HttpRequest, requestN, 0)
47 HTTP_END_ARGS;
48
49 HTTP_EMPTY_ARGS(__destruct, 0);
50 HTTP_EMPTY_ARGS(reset, 0);
51
52 HTTP_BEGIN_ARGS(attach, 1)
53 HTTP_ARG_OBJ(HttpRequest, request, 0)
54 HTTP_END_ARGS;
55
56 HTTP_BEGIN_ARGS(detach, 1)
57 HTTP_ARG_OBJ(HttpRequest, request, 0)
58 HTTP_END_ARGS;
59
60 HTTP_EMPTY_ARGS(send, 0);
61 HTTP_EMPTY_ARGS(socketPerform, 0);
62 HTTP_EMPTY_ARGS(socketSelect, 0);
63
64 HTTP_EMPTY_ARGS(valid, 0);
65 HTTP_EMPTY_ARGS(current, 1);
66 HTTP_EMPTY_ARGS(key, 0);
67 HTTP_EMPTY_ARGS(next, 0);
68 HTTP_EMPTY_ARGS(rewind, 0);
69
70 #define http_requestpool_object_declare_default_properties() _http_requestpool_object_declare_default_properties(TSRMLS_C)
71 static inline void _http_requestpool_object_declare_default_properties(TSRMLS_D);
72
73 zend_class_entry *http_requestpool_object_ce;
74 zend_function_entry http_requestpool_object_fe[] = {
75 HTTP_REQPOOL_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
76 HTTP_REQPOOL_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
77 HTTP_REQPOOL_ME(attach, ZEND_ACC_PUBLIC)
78 HTTP_REQPOOL_ME(detach, ZEND_ACC_PUBLIC)
79 HTTP_REQPOOL_ME(send, ZEND_ACC_PUBLIC)
80 HTTP_REQPOOL_ME(reset, ZEND_ACC_PUBLIC)
81
82 HTTP_REQPOOL_ME(socketPerform, ZEND_ACC_PROTECTED)
83 HTTP_REQPOOL_ME(socketSelect, ZEND_ACC_PROTECTED)
84
85 /* implements Interator */
86 HTTP_REQPOOL_ME(valid, ZEND_ACC_PUBLIC)
87 HTTP_REQPOOL_ME(current, ZEND_ACC_PUBLIC)
88 HTTP_REQPOOL_ME(key, ZEND_ACC_PUBLIC)
89 HTTP_REQPOOL_ME(next, ZEND_ACC_PUBLIC)
90 HTTP_REQPOOL_ME(rewind, ZEND_ACC_PUBLIC)
91
92 EMPTY_FUNCTION_ENTRY
93 };
94 static zend_object_handlers http_requestpool_object_handlers;
95
96 void _http_requestpool_object_init(INIT_FUNC_ARGS)
97 {
98 HTTP_REGISTER_CLASS_EX(HttpRequestPool, http_requestpool_object, NULL, 0);
99 zend_class_implements(http_requestpool_object_ce TSRMLS_CC, 1, zend_ce_iterator);
100 }
101
102 zend_object_value _http_requestpool_object_new(zend_class_entry *ce TSRMLS_DC)
103 {
104 zend_object_value ov;
105 http_requestpool_object *o;
106
107 o = ecalloc(1, sizeof(http_requestpool_object));
108 o->zo.ce = ce;
109
110 http_request_pool_init(&o->pool);
111
112 ALLOC_HASHTABLE(OBJ_PROP(o));
113 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
114 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
115
116 ov.handle = putObject(http_requestpool_object, o);
117 ov.handlers = &http_requestpool_object_handlers;
118
119 return ov;
120 }
121
122 static inline void _http_requestpool_object_declare_default_properties(TSRMLS_D)
123 {
124 zend_class_entry *ce = http_requestpool_object_ce;
125
126 DCL_PROP_N(PROTECTED, pool);
127 }
128
129 void _http_requestpool_object_free(zend_object *object TSRMLS_DC)
130 {
131 http_requestpool_object *o = (http_requestpool_object *) object;
132
133 if (OBJ_PROP(o)) {
134 zend_hash_destroy(OBJ_PROP(o));
135 FREE_HASHTABLE(OBJ_PROP(o));
136 }
137 http_request_pool_dtor(&o->pool);
138 efree(o);
139 }
140
141 /* ### USERLAND ### */
142
143 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
144 *
145 * Instantiate a new HttpRequestPool object. An HttpRequestPool is
146 * able to send several HttpRequests in parallel.
147 *
148 * Example:
149 * <pre>
150 * <?php
151 * try {
152 * $pool = new HttpRequestPool(
153 * new HttpRequest('http://www.google.com/', HTTP_HEAD),
154 * new HttpRequest('http://www.php.net/', HTTP_HEAD)
155 * );
156 * $pool->send();
157 * foreach($pool as $request) {
158 * printf("%s is %s (%d)\n",
159 * $request->getUrl(),
160 * $request->getResponseCode() ? 'alive' : 'not alive',
161 * $request->getResponseCode()
162 * );
163 * }
164 * } catch (HttpException $e) {
165 * echo $e;
166 * }
167 * ?>
168 * </pre>
169 */
170 PHP_METHOD(HttpRequestPool, __construct)
171 {
172 int argc = ZEND_NUM_ARGS();
173 zval ***argv = safe_emalloc(argc, sizeof(zval *), 0);
174 getObject(http_requestpool_object, obj);
175
176 if (SUCCESS == zend_get_parameters_array_ex(argc, argv)) {
177 int i;
178
179 for (i = 0; i < argc; ++i) {
180 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), http_request_object_ce TSRMLS_CC)) {
181 http_request_pool_attach(&obj->pool, *(argv[i]));
182 }
183 }
184 }
185 efree(argv);
186 }
187 /* }}} */
188
189 /* {{{ proto void HttpRequestPool::__destruct()
190 *
191 * Clean up HttpRequestPool object.
192 */
193 PHP_METHOD(HttpRequestPool, __destruct)
194 {
195 getObject(http_requestpool_object, obj);
196
197 NO_ARGS;
198
199 http_request_pool_detach_all(&obj->pool);
200 }
201 /* }}} */
202
203 /* {{{ proto void HttpRequestPool::reset()
204 *
205 * Detach all attached HttpRequest objects.
206 */
207 PHP_METHOD(HttpRequestPool, reset)
208 {
209 getObject(http_requestpool_object, obj);
210
211 NO_ARGS;
212
213 http_request_pool_detach_all(&obj->pool);
214 }
215
216 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
217 *
218 * Attach an HttpRequest object to this HttpRequestPool.
219 * NOTE: set all options prior attaching!
220 */
221 PHP_METHOD(HttpRequestPool, attach)
222 {
223 zval *request;
224 STATUS status = FAILURE;
225 getObject(http_requestpool_object, obj);
226
227 SET_EH_THROW_HTTP();
228 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
229 status = http_request_pool_attach(&obj->pool, request);
230 }
231 SET_EH_NORMAL();
232 RETURN_SUCCESS(status);
233 }
234 /* }}} */
235
236 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
237 *
238 * Detach an HttpRequest object from this HttpRequestPool.
239 */
240 PHP_METHOD(HttpRequestPool, detach)
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 status = http_request_pool_detach(&obj->pool, request);
249 }
250 SET_EH_NORMAL();
251 RETURN_SUCCESS(status);
252 }
253 /* }}} */
254
255 /* {{{ proto bool HttpRequestPool::send()
256 *
257 * Send all attached HttpRequest objects in parallel.
258 */
259 PHP_METHOD(HttpRequestPool, send)
260 {
261 STATUS status;
262 getObject(http_requestpool_object, obj);
263
264 NO_ARGS;
265
266 SET_EH_THROW_HTTP();
267 status = http_request_pool_send(&obj->pool);
268 SET_EH_NORMAL();
269
270 RETURN_SUCCESS(status);
271 }
272 /* }}} */
273
274 /* {{{ proto protected bool HttpRequestPool::socketSend()
275 *
276 * Usage:
277 * <pre>
278 * <?php
279 * while ($pool->socketPerform()) {
280 * do_something_else();
281 * if (!$pool->socketSelect()) {
282 * die('Socket error');
283 * }
284 * }
285 * ?>
286 * </pre>
287 */
288 PHP_METHOD(HttpRequestPool, socketPerform)
289 {
290 getObject(http_requestpool_object, obj);
291
292 NO_ARGS;
293
294 if (0 < http_request_pool_perform(&obj->pool)) {
295 RETURN_TRUE;
296 } else {
297 zend_llist_apply(&obj->pool.handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
298 RETURN_FALSE;
299 }
300 }
301 /* }}} */
302
303 /* {{{ proto protected bool HttpRequestPool::socketSelect()
304 *
305 * See HttpRequestPool::socketPerform().
306 */
307 PHP_METHOD(HttpRequestPool, socketSelect)
308 {
309 getObject(http_requestpool_object, obj);
310
311 NO_ARGS;
312
313 RETURN_SUCCESS(http_request_pool_select(&obj->pool));
314 }
315 /* }}} */
316
317 /* implements Iterator */
318
319 /* {{{ proto bool HttpRequestPool::valid()
320 *
321 * Implements Iterator::valid().
322 */
323 PHP_METHOD(HttpRequestPool, valid)
324 {
325 NO_ARGS;
326
327 IF_RETVAL_USED {
328 getObject(http_requestpool_object, obj);
329 RETURN_BOOL(obj->iterator.pos < zend_llist_count(&obj->pool.handles));
330 }
331 }
332 /* }}} */
333
334 /* {{{ proto HttpRequest HttpRequestPool::current()
335 *
336 * Implements Iterator::current().
337 */
338 PHP_METHOD(HttpRequestPool, current)
339 {
340 NO_ARGS;
341
342 IF_RETVAL_USED {
343 long pos = 0;
344 zval **current = NULL;
345 zend_llist_position lpos;
346 getObject(http_requestpool_object, obj);
347
348 if (obj->iterator.pos < zend_llist_count(&obj->pool.handles)) {
349 for ( current = zend_llist_get_first_ex(&obj->pool.handles, &lpos);
350 current && obj->iterator.pos != pos++;
351 current = zend_llist_get_next_ex(&obj->pool.handles, &lpos));
352 if (current) {
353 RETURN_OBJECT(*current);
354 }
355 }
356 RETURN_NULL();
357 }
358 }
359 /* }}} */
360
361 /* {{{ proto long HttpRequestPool::key()
362 *
363 * Implements Iterator::key().
364 */
365 PHP_METHOD(HttpRequestPool, key)
366 {
367 NO_ARGS;
368
369 IF_RETVAL_USED {
370 getObject(http_requestpool_object, obj);
371 RETURN_LONG(obj->iterator.pos);
372 }
373 }
374 /* }}} */
375
376 /* {{{ proto void HttpRequestPool::next()
377 *
378 * Implements Iterator::next().
379 */
380 PHP_METHOD(HttpRequestPool, next)
381 {
382 NO_ARGS {
383 getObject(http_requestpool_object, obj);
384 ++(obj->iterator.pos);
385 }
386 }
387 /* }}} */
388
389 /* {{{ proto void HttpRequestPool::rewind()
390 *
391 * Implements Iterator::rewind().
392 */
393 PHP_METHOD(HttpRequestPool, rewind)
394 {
395 NO_ARGS {
396 getObject(http_requestpool_object, obj);
397 obj->iterator.pos = 0;
398 }
399 }
400 /* }}} */
401
402 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
403
404 /*
405 * Local variables:
406 * tab-width: 4
407 * c-basic-offset: 4
408 * End:
409 * vim600: noet sw=4 ts=4 fdm=marker
410 * vim<600: noet sw=4 ts=4
411 */
412