- update
[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 o->iterator.pos = 0;
112
113 ALLOC_HASHTABLE(OBJ_PROP(o));
114 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
115 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
116
117 ov.handle = putObject(http_requestpool_object, o);
118 ov.handlers = &http_requestpool_object_handlers;
119
120 return ov;
121 }
122
123 static inline void _http_requestpool_object_declare_default_properties(TSRMLS_D)
124 {
125 zend_class_entry *ce = http_requestpool_object_ce;
126
127 DCL_PROP_N(PROTECTED, pool);
128 }
129
130 void _http_requestpool_object_free(zend_object *object TSRMLS_DC)
131 {
132 http_requestpool_object *o = (http_requestpool_object *) object;
133
134 if (OBJ_PROP(o)) {
135 zend_hash_destroy(OBJ_PROP(o));
136 FREE_HASHTABLE(OBJ_PROP(o));
137 }
138 http_request_pool_dtor(&o->pool);
139 efree(o);
140 }
141
142 /* ### USERLAND ### */
143
144 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
145 *
146 * Instantiate a new HttpRequestPool object. An HttpRequestPool is
147 * able to send several HttpRequests in parallel.
148 *
149 * Example:
150 * <pre>
151 * <?php
152 * try {
153 * $pool = new HttpRequestPool(
154 * new HttpRequest('http://www.google.com/', HTTP_HEAD),
155 * new HttpRequest('http://www.php.net/', HTTP_HEAD)
156 * );
157 * $pool->send();
158 * foreach($pool as $request) {
159 * printf("%s is %s (%d)\n",
160 * $request->getUrl(),
161 * $request->getResponseCode() ? 'alive' : 'not alive',
162 * $request->getResponseCode()
163 * );
164 * }
165 * } catch (HttpException $e) {
166 * echo $e;
167 * }
168 * ?>
169 * </pre>
170 */
171 PHP_METHOD(HttpRequestPool, __construct)
172 {
173 int argc = ZEND_NUM_ARGS();
174 zval ***argv = safe_emalloc(argc, sizeof(zval *), 0);
175 getObject(http_requestpool_object, obj);
176
177 if (SUCCESS == zend_get_parameters_array_ex(argc, argv)) {
178 int i;
179
180 for (i = 0; i < argc; ++i) {
181 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), http_request_object_ce TSRMLS_CC)) {
182 http_request_pool_attach(&obj->pool, *(argv[i]));
183 }
184 }
185 }
186 efree(argv);
187 }
188 /* }}} */
189
190 /* {{{ proto void HttpRequestPool::__destruct()
191 *
192 * Clean up HttpRequestPool object.
193 */
194 PHP_METHOD(HttpRequestPool, __destruct)
195 {
196 getObject(http_requestpool_object, obj);
197
198 NO_ARGS;
199
200 http_request_pool_detach_all(&obj->pool);
201 }
202 /* }}} */
203
204 /* {{{ proto void HttpRequestPool::reset()
205 *
206 * Detach all attached HttpRequest objects.
207 */
208 PHP_METHOD(HttpRequestPool, reset)
209 {
210 getObject(http_requestpool_object, obj);
211
212 NO_ARGS;
213
214 http_request_pool_detach_all(&obj->pool);
215 }
216
217 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
218 *
219 * Attach an HttpRequest object to this HttpRequestPool.
220 * NOTE: set all options prior attaching!
221 */
222 PHP_METHOD(HttpRequestPool, attach)
223 {
224 zval *request;
225 STATUS status = FAILURE;
226 getObject(http_requestpool_object, obj);
227
228 SET_EH_THROW_HTTP();
229 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
230 status = http_request_pool_attach(&obj->pool, request);
231 }
232 SET_EH_NORMAL();
233 RETURN_SUCCESS(status);
234 }
235 /* }}} */
236
237 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
238 *
239 * Detach an HttpRequest object from this HttpRequestPool.
240 */
241 PHP_METHOD(HttpRequestPool, detach)
242 {
243 zval *request;
244 STATUS status = FAILURE;
245 getObject(http_requestpool_object, obj);
246
247 SET_EH_THROW_HTTP();
248 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
249 status = http_request_pool_detach(&obj->pool, request);
250 }
251 SET_EH_NORMAL();
252 RETURN_SUCCESS(status);
253 }
254 /* }}} */
255
256 /* {{{ proto bool HttpRequestPool::send()
257 *
258 * Send all attached HttpRequest objects in parallel.
259 */
260 PHP_METHOD(HttpRequestPool, send)
261 {
262 STATUS status;
263 getObject(http_requestpool_object, obj);
264
265 NO_ARGS;
266
267 SET_EH_THROW_HTTP();
268 status = http_request_pool_send(&obj->pool);
269 SET_EH_NORMAL();
270
271 RETURN_SUCCESS(status);
272 }
273 /* }}} */
274
275 /* {{{ proto protected bool HttpRequestPool::socketSend()
276 *
277 * Usage:
278 * <pre>
279 * <?php
280 * while ($pool->socketPerform()) {
281 * do_something_else();
282 * if (!$pool->socketSelect()) {
283 * die('Socket error');
284 * }
285 * }
286 * ?>
287 * </pre>
288 */
289 PHP_METHOD(HttpRequestPool, socketPerform)
290 {
291 getObject(http_requestpool_object, obj);
292
293 NO_ARGS;
294
295 if (0 < http_request_pool_perform(&obj->pool)) {
296 RETURN_TRUE;
297 } else {
298 zend_llist_apply(&obj->pool.handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
299 RETURN_FALSE;
300 }
301 }
302 /* }}} */
303
304 /* {{{ proto protected bool HttpRequestPool::socketSelect()
305 *
306 * See HttpRequestPool::socketPerform().
307 */
308 PHP_METHOD(HttpRequestPool, socketSelect)
309 {
310 getObject(http_requestpool_object, obj);
311
312 NO_ARGS;
313
314 RETURN_SUCCESS(http_request_pool_select(&obj->pool));
315 }
316 /* }}} */
317
318 /* implements Iterator */
319
320 /* {{{ proto bool HttpRequestPool::valid()
321 *
322 * Implements Iterator::valid().
323 */
324 PHP_METHOD(HttpRequestPool, valid)
325 {
326 NO_ARGS;
327
328 IF_RETVAL_USED {
329 getObject(http_requestpool_object, obj);
330 RETURN_BOOL(obj->iterator.pos < zend_llist_count(&obj->pool.handles));
331 }
332 }
333 /* }}} */
334
335 /* {{{ proto HttpRequest HttpRequestPool::current()
336 *
337 * Implements Iterator::current().
338 */
339 PHP_METHOD(HttpRequestPool, current)
340 {
341 NO_ARGS;
342
343 IF_RETVAL_USED {
344 long pos = 0;
345 zval **current = NULL;
346 zend_llist_position lpos;
347 getObject(http_requestpool_object, obj);
348
349 if (obj->iterator.pos < zend_llist_count(&obj->pool.handles)) {
350 for ( current = zend_llist_get_first_ex(&obj->pool.handles, &lpos);
351 current && obj->iterator.pos != pos++;
352 current = zend_llist_get_next_ex(&obj->pool.handles, &lpos));
353 if (current) {
354 RETURN_OBJECT(*current);
355 }
356 }
357 RETURN_NULL();
358 }
359 }
360 /* }}} */
361
362 /* {{{ proto long HttpRequestPool::key()
363 *
364 * Implements Iterator::key().
365 */
366 PHP_METHOD(HttpRequestPool, key)
367 {
368 NO_ARGS;
369
370 IF_RETVAL_USED {
371 getObject(http_requestpool_object, obj);
372 RETURN_LONG(obj->iterator.pos);
373 }
374 }
375 /* }}} */
376
377 /* {{{ proto void HttpRequestPool::next()
378 *
379 * Implements Iterator::next().
380 */
381 PHP_METHOD(HttpRequestPool, next)
382 {
383 NO_ARGS {
384 getObject(http_requestpool_object, obj);
385 ++(obj->iterator.pos);
386 }
387 }
388 /* }}} */
389
390 /* {{{ proto void HttpRequestPool::rewind()
391 *
392 * Implements Iterator::rewind().
393 */
394 PHP_METHOD(HttpRequestPool, rewind)
395 {
396 NO_ARGS {
397 getObject(http_requestpool_object, obj);
398 obj->iterator.pos = 0;
399 }
400 }
401 /* }}} */
402
403 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
404
405 /*
406 * Local variables:
407 * tab-width: 4
408 * c-basic-offset: 4
409 * End:
410 * vim600: noet sw=4 ts=4 fdm=marker
411 * vim<600: noet sw=4 ts=4
412 */
413