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