ec3e5ca9cc8df8998d66fd12189d838086a9e422
[m6w6/ext-http] / php_http_request_pool.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http.h"
14
15 #include <Zend/zend_interfaces.h>
16 #include <ext/spl/spl_iterators.h>
17
18 PHP_HTTP_API php_http_request_pool_t *php_http_request_pool_init(php_http_request_pool_t *h, php_http_request_pool_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC)
19 {
20 php_http_request_pool_t *free_h = NULL;
21
22 if (!h) {
23 free_h = h = emalloc(sizeof(*h));
24 }
25 memset(h, 0, sizeof(*h));
26
27 h->ops = ops;
28 h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, NULL, NULL);
29 zend_llist_init(&h->requests.attached, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
30 zend_llist_init(&h->requests.finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
31 TSRMLS_SET_CTX(h->ts);
32
33 if (h->ops->init) {
34 if (!(h = h->ops->init(h, init_arg))) {
35 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not initialize request pool");
36 if (free_h) {
37 efree(h);
38 }
39 }
40 }
41
42 return h;
43 }
44
45 PHP_HTTP_API php_http_request_pool_t *php_http_request_pool_copy(php_http_request_pool_t *from, php_http_request_pool_t *to)
46 {
47 if (from->ops->copy) {
48 return from->ops->copy(from, to);
49 }
50
51 return NULL;
52 }
53
54 PHP_HTTP_API void php_http_request_pool_dtor(php_http_request_pool_t *h)
55 {
56 if (h->ops->dtor) {
57 h->ops->dtor(h);
58 }
59
60 zend_llist_clean(&h->requests.finished);
61 zend_llist_clean(&h->requests.attached);
62
63 if (h->persistent_handle_id) {
64 zval_ptr_dtor(&h->persistent_handle_id);
65 }
66
67 php_http_resource_factory_free(&h->rf);
68 }
69
70 PHP_HTTP_API void php_http_request_pool_free(php_http_request_pool_t **h) {
71 if (*h) {
72 php_http_request_pool_dtor(*h);
73 efree(*h);
74 *h = NULL;
75 }
76 }
77
78 PHP_HTTP_API STATUS php_http_request_pool_attach(php_http_request_pool_t *h, zval *request)
79 {
80 TSRMLS_FETCH_FROM_CTX(h->ts);
81
82 if (h->ops->attach) {
83 char *url = NULL;
84 php_http_request_method_t m = PHP_HTTP_NO_REQUEST_METHOD;
85 php_http_message_body_t *body = NULL;
86 php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC);
87
88 if (SUCCESS != php_http_request_object_requesthandler(obj, request, &m, &url, &body TSRMLS_CC)) {
89 return FAILURE;
90 }
91 if (SUCCESS == h->ops->attach(h, obj->request, m, url, body)) {
92 STR_FREE(url);
93 Z_ADDREF_P(request);
94 zend_llist_add_element(&h->requests.attached, &request);
95 return SUCCESS;
96 }
97 STR_FREE(url);
98 }
99
100 return FAILURE;
101 }
102
103 static int php_http_request_pool_compare_handles(void *h1, void *h2)
104 {
105 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
106 }
107
108
109 PHP_HTTP_API STATUS php_http_request_pool_detach(php_http_request_pool_t *h, zval *request)
110 {
111 TSRMLS_FETCH_FROM_CTX(h->ts);
112
113 if (h->ops->detach) {
114 php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC);
115
116 if (SUCCESS == h->ops->detach(h, obj->request)) {
117 zend_llist_del_element(&h->requests.finished, request, php_http_request_pool_compare_handles);
118 zend_llist_del_element(&h->requests.attached, request, php_http_request_pool_compare_handles);
119 return SUCCESS;
120 }
121 }
122
123 return FAILURE;
124 }
125
126 PHP_HTTP_API STATUS php_http_request_pool_wait(php_http_request_pool_t *h, struct timeval *custom_timeout)
127 {
128 if (h->ops->wait) {
129 return h->ops->wait(h, custom_timeout);
130 }
131
132 return FAILURE;
133 }
134
135 PHP_HTTP_API int php_http_request_pool_once(php_http_request_pool_t *h)
136 {
137 if (h->ops->once) {
138 return h->ops->once(h);
139 }
140
141 return FAILURE;
142 }
143
144 PHP_HTTP_API STATUS php_http_request_pool_exec(php_http_request_pool_t *h)
145 {
146 if (h->ops->exec) {
147 return h->ops->exec(h);
148 }
149
150 return FAILURE;
151 }
152
153 static void detach(void *r, void *h TSRMLS_DC)
154 {
155 ((php_http_request_pool_t *) h)->ops->detach(h, ((php_http_request_object_t *) zend_object_store_get_object(*((zval **) r) TSRMLS_CC))->request);
156 }
157
158 PHP_HTTP_API void php_http_request_pool_reset(php_http_request_pool_t *h)
159 {
160 if (h->ops->reset) {
161 h->ops->reset(h);
162 } else if (h->ops->detach) {
163 TSRMLS_FETCH_FROM_CTX(h->ts);
164
165 zend_llist_apply_with_argument(&h->requests.attached, detach, h TSRMLS_CC);
166 }
167
168 zend_llist_clean(&h->requests.attached);
169 zend_llist_clean(&h->requests.finished);
170 }
171
172 PHP_HTTP_API STATUS php_http_request_pool_setopt(php_http_request_pool_t *h, php_http_request_pool_setopt_opt_t opt, void *arg)
173 {
174 if (h->ops->setopt) {
175 return h->ops->setopt(h, opt, arg);
176 }
177
178 return FAILURE;
179 }
180
181 PHP_HTTP_API void php_http_request_pool_requests(php_http_request_pool_t *h, zval ***attached, zval ***finished)
182 {
183 zval **handle;
184 int i, count;
185
186 if (attached) {
187 if ((count = zend_llist_count(&h->requests.attached))) {
188 *attached = ecalloc(count + 1 /* terminating NULL */, sizeof(zval *));
189
190 for (i = 0, handle = zend_llist_get_first(&h->requests.attached); handle; handle = zend_llist_get_next(&h->requests.attached)) {
191 Z_ADDREF_PP(handle);
192 (*attached)[i++] = *handle;
193 }
194 } else {
195 *attached = NULL;
196 }
197 }
198
199 if (finished) {
200 if ((count = zend_llist_count(&h->requests.finished))) {
201 *finished = ecalloc(count + 1 /* terminating NULL */, sizeof(zval *));
202
203 for (i = 0, handle = zend_llist_get_first(&h->requests.finished); handle; handle = zend_llist_get_next(&h->requests.finished)) {
204 Z_ADDREF_PP(handle);
205 (*finished)[i++] = *handle;
206 }
207 } else {
208 *finished = NULL;
209 }
210 }
211 }
212
213 /*#*/
214
215 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args)
216 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, 0)
217 #define PHP_HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, PHP_HTTP_ARGS(HttpRequestPool, method), visibility)
218
219 PHP_HTTP_EMPTY_ARGS(__construct);
220
221 PHP_HTTP_EMPTY_ARGS(__destruct);
222 PHP_HTTP_EMPTY_ARGS(reset);
223
224 PHP_HTTP_BEGIN_ARGS(attach, 1)
225 PHP_HTTP_ARG_OBJ(http\\Request, request, 0)
226 PHP_HTTP_END_ARGS;
227
228 PHP_HTTP_BEGIN_ARGS(detach, 1)
229 PHP_HTTP_ARG_OBJ(http\\Request, request, 0)
230 PHP_HTTP_END_ARGS;
231
232 PHP_HTTP_EMPTY_ARGS(send);
233 PHP_HTTP_EMPTY_ARGS(once);
234 PHP_HTTP_BEGIN_ARGS(wait, 0)
235 PHP_HTTP_ARG_VAL(timeout, 0)
236 PHP_HTTP_END_ARGS;
237
238 PHP_HTTP_EMPTY_ARGS(valid);
239 PHP_HTTP_EMPTY_ARGS(current);
240 PHP_HTTP_EMPTY_ARGS(key);
241 PHP_HTTP_EMPTY_ARGS(next);
242 PHP_HTTP_EMPTY_ARGS(rewind);
243
244 PHP_HTTP_EMPTY_ARGS(count);
245
246 PHP_HTTP_EMPTY_ARGS(getAttachedRequests);
247 PHP_HTTP_EMPTY_ARGS(getFinishedRequests);
248
249 PHP_HTTP_BEGIN_ARGS(enablePipelining, 0)
250 PHP_HTTP_ARG_VAL(enable, 0)
251 PHP_HTTP_END_ARGS;
252
253 PHP_HTTP_BEGIN_ARGS(enableEvents, 0)
254 PHP_HTTP_ARG_VAL(enable, 0)
255 PHP_HTTP_END_ARGS;
256
257 zend_class_entry *php_http_request_pool_class_entry;
258 zend_function_entry php_http_request_pool_method_entry[] = {
259 PHP_HTTP_REQPOOL_ME(__construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
260 PHP_HTTP_REQPOOL_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
261 PHP_HTTP_REQPOOL_ME(attach, ZEND_ACC_PUBLIC)
262 PHP_HTTP_REQPOOL_ME(detach, ZEND_ACC_PUBLIC)
263 PHP_HTTP_REQPOOL_ME(send, ZEND_ACC_PUBLIC)
264 PHP_HTTP_REQPOOL_ME(reset, ZEND_ACC_PUBLIC)
265
266 PHP_HTTP_REQPOOL_ME(once, ZEND_ACC_PROTECTED)
267 PHP_HTTP_REQPOOL_ME(wait, ZEND_ACC_PROTECTED)
268
269 /* implements Iterator */
270 PHP_HTTP_REQPOOL_ME(valid, ZEND_ACC_PUBLIC)
271 PHP_HTTP_REQPOOL_ME(current, ZEND_ACC_PUBLIC)
272 PHP_HTTP_REQPOOL_ME(key, ZEND_ACC_PUBLIC)
273 PHP_HTTP_REQPOOL_ME(next, ZEND_ACC_PUBLIC)
274 PHP_HTTP_REQPOOL_ME(rewind, ZEND_ACC_PUBLIC)
275
276 /* implmenents Countable */
277 PHP_HTTP_REQPOOL_ME(count, ZEND_ACC_PUBLIC)
278
279 PHP_HTTP_REQPOOL_ME(getAttachedRequests, ZEND_ACC_PUBLIC)
280 PHP_HTTP_REQPOOL_ME(getFinishedRequests, ZEND_ACC_PUBLIC)
281
282 PHP_HTTP_REQPOOL_ME(enablePipelining, ZEND_ACC_PUBLIC)
283 PHP_HTTP_REQPOOL_ME(enableEvents, ZEND_ACC_PUBLIC)
284
285 EMPTY_FUNCTION_ENTRY
286 };
287 static zend_object_handlers php_http_request_pool_object_handlers;
288
289 zend_object_value php_http_request_pool_object_new(zend_class_entry *ce TSRMLS_DC)
290 {
291 return php_http_request_pool_object_new_ex(ce, NULL, NULL TSRMLS_CC);
292 }
293
294 zend_object_value php_http_request_pool_object_new_ex(zend_class_entry *ce, php_http_request_pool_t *p, php_http_request_pool_object_t **ptr TSRMLS_DC)
295 {
296 zend_object_value ov;
297 php_http_request_pool_object_t *o;
298
299 o = ecalloc(1, sizeof(php_http_request_pool_object_t));
300 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
301 object_properties_init((zend_object *) o, ce);
302
303 if (!(o->pool = p)) {
304 o->pool = php_http_request_pool_init(NULL, NULL, NULL, NULL TSRMLS_CC);
305 }
306
307 if (ptr) {
308 *ptr = o;
309 }
310
311 ov.handle = zend_objects_store_put(o, NULL, php_http_request_pool_object_free, NULL TSRMLS_CC);
312 ov.handlers = &php_http_request_pool_object_handlers;
313
314 return ov;
315 }
316
317 void php_http_request_pool_object_free(void *object TSRMLS_DC)
318 {
319 php_http_request_pool_object_t *o = (php_http_request_pool_object_t *) object;
320
321 php_http_request_pool_free(&o->pool);
322 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
323 efree(o);
324 }
325
326 static void php_http_request_pool_object_llist2array(zval **req, zval *array TSRMLS_DC)
327 {
328 Z_ADDREF_P(*req);
329 add_next_index_zval(array, *req);
330 }
331
332
333 PHP_METHOD(HttpRequestPool, __construct)
334 {
335 with_error_handling(EH_THROW, php_http_exception_class_entry) {
336 zend_parse_parameters_none();
337 } end_error_handling();
338 }
339
340 PHP_METHOD(HttpRequestPool, __destruct)
341 {
342 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
343
344 if (SUCCESS != zend_parse_parameters_none()) {
345 ; /* we always want to clean up */
346 }
347
348 php_http_request_pool_reset(obj->pool);
349 }
350
351 PHP_METHOD(HttpRequestPool, reset)
352 {
353 if (SUCCESS == zend_parse_parameters_none()) {
354 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
355
356 obj->iterator.pos = 0;
357 php_http_request_pool_reset(obj->pool);
358 }
359 RETVAL_ZVAL(getThis(), 1, 0);
360 }
361
362 PHP_METHOD(HttpRequestPool, attach)
363 {
364 with_error_handling(EH_THROW, php_http_exception_class_entry) {
365 zval *request;
366
367 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) {
368 with_error_handling(EH_THROW, php_http_exception_class_entry) {
369 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
370
371 if (obj->iterator.pos > 0 && obj->iterator.pos < zend_llist_count(&obj->pool->requests.attached)) {
372 php_http_error(HE_THROW, PHP_HTTP_E_REQUEST_POOL, "Cannot attach to the HttpRequestPool while the iterator is active");
373 } else {
374 php_http_request_pool_attach(obj->pool, request);
375 }
376 } end_error_handling();
377 }
378 } end_error_handling();
379
380 RETVAL_ZVAL(getThis(), 1, 0);
381 }
382
383 PHP_METHOD(HttpRequestPool, detach)
384 {
385 RETVAL_FALSE;
386
387 with_error_handling(EH_THROW, php_http_exception_class_entry) {
388 zval *request;
389
390 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) {
391 with_error_handling(EH_THROW, php_http_exception_class_entry) {
392 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
393
394 obj->iterator.pos = -1;
395 php_http_request_pool_detach(obj->pool, request);
396 } end_error_handling();
397 }
398 } end_error_handling();
399
400 RETVAL_ZVAL(getThis(), 1, 0);
401 }
402
403 PHP_METHOD(HttpRequestPool, send)
404 {
405 RETVAL_FALSE;
406
407 with_error_handling(EH_THROW, php_http_exception_class_entry) {
408 if (SUCCESS == zend_parse_parameters_none()) {
409 with_error_handling(EH_THROW, php_http_exception_class_entry) {
410 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
411
412 php_http_request_pool_exec(obj->pool);
413 } end_error_handling();
414 }
415 } end_error_handling();
416
417 RETVAL_ZVAL(getThis(), 1, 0);
418 }
419
420 PHP_METHOD(HttpRequestPool, once)
421 {
422 if (SUCCESS == zend_parse_parameters_none()) {
423 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
424
425 if (0 < php_http_request_pool_once(obj->pool)) {
426 RETURN_TRUE;
427 }
428 }
429 RETURN_FALSE;
430 }
431
432 PHP_METHOD(HttpRequestPool, wait)
433 {
434 double timeout = 0;
435
436 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|d", &timeout)) {
437 struct timeval timeout_val;
438 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
439
440 timeout_val.tv_sec = (time_t) timeout;
441 timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC;
442
443 RETURN_SUCCESS(php_http_request_pool_wait(obj->pool, timeout ? &timeout_val : NULL));
444 }
445 RETURN_FALSE;
446 }
447
448 PHP_METHOD(HttpRequestPool, valid)
449 {
450 if (SUCCESS == zend_parse_parameters_none()) {
451 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
452
453 RETURN_BOOL(obj->iterator.pos >= 0 && obj->iterator.pos < zend_llist_count(&obj->pool->requests.attached));
454 }
455 RETURN_FALSE;
456 }
457
458 PHP_METHOD(HttpRequestPool, current)
459 {
460 if (SUCCESS == zend_parse_parameters_none()) {
461 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
462
463 if (obj->iterator.pos < zend_llist_count(&obj->pool->requests.attached)) {
464 long pos = 0;
465 zval **current = NULL;
466 zend_llist_position lpos;
467
468 for ( current = zend_llist_get_first_ex(&obj->pool->requests.attached, &lpos);
469 current && obj->iterator.pos != pos++;
470 current = zend_llist_get_next_ex(&obj->pool->requests.attached, &lpos));
471 if (current) {
472 RETURN_OBJECT(*current, 1);
473 }
474 }
475 }
476 RETURN_FALSE;
477 }
478
479 PHP_METHOD(HttpRequestPool, key)
480 {
481 if (SUCCESS == zend_parse_parameters_none()) {
482 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
483
484 RETURN_LONG(obj->iterator.pos);
485 }
486 RETURN_FALSE;
487 }
488
489 PHP_METHOD(HttpRequestPool, next)
490 {
491 if (SUCCESS == zend_parse_parameters_none()) {
492 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
493
494 ++obj->iterator.pos;
495 }
496 }
497
498 PHP_METHOD(HttpRequestPool, rewind)
499 {
500 if (SUCCESS == zend_parse_parameters_none()) {
501 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
502
503 obj->iterator.pos = 0;
504 }
505 }
506
507 PHP_METHOD(HttpRequestPool, count)
508 {
509 if (SUCCESS == zend_parse_parameters_none()) {
510 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
511
512 RETURN_LONG((long) zend_llist_count(&obj->pool->requests.attached));
513 }
514 }
515
516 PHP_METHOD(HttpRequestPool, getAttachedRequests)
517 {
518 if (SUCCESS == zend_parse_parameters_none()) {
519 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
520
521 array_init(return_value);
522 zend_llist_apply_with_argument(&obj->pool->requests.attached,
523 (llist_apply_with_arg_func_t) php_http_request_pool_object_llist2array,
524 return_value TSRMLS_CC);
525 return;
526 }
527 RETURN_FALSE;
528 }
529
530 PHP_METHOD(HttpRequestPool, getFinishedRequests)
531 {
532 if (SUCCESS == zend_parse_parameters_none()) {
533 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
534
535 array_init(return_value);
536 zend_llist_apply_with_argument(&obj->pool->requests.finished,
537 (llist_apply_with_arg_func_t) php_http_request_pool_object_llist2array,
538 return_value TSRMLS_CC);
539 return;
540 }
541 RETURN_FALSE;
542 }
543
544 PHP_METHOD(HttpRequestPool, enablePipelining)
545 {
546 zend_bool enable = 1;
547
548 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) {
549 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
550
551 php_http_request_pool_setopt(obj->pool, PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING, &enable);
552 }
553 RETVAL_ZVAL(getThis(), 1, 0);
554 }
555
556 PHP_METHOD(HttpRequestPool, enableEvents)
557 {
558 zend_bool enable = 1;
559
560 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) {
561 php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
562
563 php_http_request_pool_setopt(obj->pool, PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS, &enable);
564 }
565 RETVAL_ZVAL(getThis(), 1, 0);
566 }
567
568 PHP_MINIT_FUNCTION(http_request_pool)
569 {
570 PHP_HTTP_REGISTER_CLASS(http\\Request, Pool, http_request_pool, php_http_object_class_entry, 0);
571 php_http_request_pool_class_entry->create_object = php_http_request_pool_object_new;
572 memcpy(&php_http_request_pool_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
573 php_http_request_pool_object_handlers.clone_obj = NULL;
574
575 zend_class_implements(php_http_request_pool_class_entry TSRMLS_CC, 2, spl_ce_Countable, zend_ce_iterator);
576
577 return SUCCESS;
578 }
579
580
581 /*
582 * Local variables:
583 * tab-width: 4
584 * c-basic-offset: 4
585 * End:
586 * vim600: noet sw=4 ts=4 fdm=marker
587 * vim<600: noet sw=4 ts=4
588 */
589