fix bug #16700 - array properties of class extending HttpMessage
[m6w6/ext-http] / http_request_pool_api.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-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_CURL
16 #define HTTP_WANT_EVENT
17 #include "php_http.h"
18
19 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
20
21 #include "php_http_api.h"
22 #include "php_http_exception_object.h"
23 #include "php_http_persistent_handle_api.h"
24 #include "php_http_request_api.h"
25 #include "php_http_request_object.h"
26 #include "php_http_request_pool_api.h"
27 #include "php_http_requestpool_object.h"
28
29 #ifndef HTTP_DEBUG_REQPOOLS
30 # define HTTP_DEBUG_REQPOOLS 0
31 #endif
32
33 #ifdef HTTP_HAVE_EVENT
34 typedef struct _http_request_pool_event_t {
35 struct event evnt;
36 http_request_pool *pool;
37 } http_request_pool_event;
38
39 static void http_request_pool_timeout_callback(int socket, short action, void *event_data);
40 static void http_request_pool_event_callback(int socket, short action, void *event_data);
41 static int http_request_pool_socket_callback(CURL *easy, curl_socket_t s, int action, void *, void *);
42 static void http_request_pool_timer_callback(CURLM *multi, long timeout_ms, void *timer_data);
43 #endif
44
45 static int http_request_pool_compare_handles(void *h1, void *h2);
46
47 PHP_MINIT_FUNCTION(http_request_pool)
48 {
49 if (SUCCESS != http_persistent_handle_provide("http_request_pool", curl_multi_init, (http_persistent_handle_dtor) curl_multi_cleanup, NULL)) {
50 return FAILURE;
51 }
52 return SUCCESS;
53 }
54
55 #ifdef HTTP_HAVE_EVENT
56 PHP_RINIT_FUNCTION(http_request_pool)
57 {
58 if (!HTTP_G->request.pool.event.base && !(HTTP_G->request.pool.event.base = event_init())) {
59 return FAILURE;
60 }
61
62 return SUCCESS;
63 }
64 #endif
65
66 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
67 PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool TSRMLS_DC)
68 {
69 zend_bool free_pool;
70
71 #if HTTP_DEBUG_REQPOOLS
72 fprintf(stderr, "Initializing request pool %p\n", pool);
73 #endif
74
75 if ((free_pool = (!pool))) {
76 pool = emalloc(sizeof(http_request_pool));
77 pool->ch = NULL;
78 }
79
80 if (SUCCESS != http_persistent_handle_acquire("http_request_pool", &pool->ch)) {
81 if (free_pool) {
82 efree(pool);
83 }
84 return NULL;
85 }
86
87 TSRMLS_SET_CTX(pool->tsrm_ls);
88
89 #ifdef HTTP_HAVE_EVENT
90 pool->timeout = ecalloc(1, sizeof(struct event));
91 curl_multi_setopt(pool->ch, CURLMOPT_SOCKETDATA, pool);
92 curl_multi_setopt(pool->ch, CURLMOPT_SOCKETFUNCTION, http_request_pool_socket_callback);
93 curl_multi_setopt(pool->ch, CURLMOPT_TIMERDATA, pool);
94 curl_multi_setopt(pool->ch, CURLMOPT_TIMERFUNCTION, http_request_pool_timer_callback);
95 #endif
96
97 pool->unfinished = 0;
98 zend_llist_init(&pool->finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
99 zend_llist_init(&pool->handles, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
100
101 #if HTTP_DEBUG_REQPOOLS
102 fprintf(stderr, "Initialized request pool %p\n", pool);
103 #endif
104
105 return pool;
106 }
107 /* }}} */
108
109 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
110 PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *request)
111 {
112 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
113 getObjectEx(http_request_object, req, request);
114
115 #if HTTP_DEBUG_REQPOOLS
116 fprintf(stderr, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
117 #endif
118
119 if (req->pool) {
120 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is already member of %s HttpRequestPool", Z_OBJ_HANDLE_P(request), req->pool == pool ? "this" : "another");
121 } else if (SUCCESS != http_request_object_requesthandler(req, request)) {
122 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not initialize HttpRequest object(#%d) for attaching to the HttpRequestPool", Z_OBJ_HANDLE_P(request));
123 } else {
124 CURLMcode code = curl_multi_add_handle(pool->ch, req->request->ch);
125
126 if (CURLM_OK != code) {
127 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not attach HttpRequest object(#%d) to the HttpRequestPool: %s", Z_OBJ_HANDLE_P(request), curl_multi_strerror(code));
128 } else {
129 req->pool = pool;
130
131 ZVAL_ADDREF(request);
132 zend_llist_add_element(&pool->handles, &request);
133 ++pool->unfinished;
134
135 #if HTTP_DEBUG_REQPOOLS
136 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
137 #endif
138 return SUCCESS;
139 }
140 }
141 return FAILURE;
142 }
143 /* }}} */
144
145 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
146 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request)
147 {
148 CURLMcode code;
149 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
150 getObjectEx(http_request_object, req, request);
151
152 #if HTTP_DEBUG_REQPOOLS
153 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
154 #endif
155
156 if (!req->pool) {
157 /* not attached to any pool */
158 #if HTTP_DEBUG_REQPOOLS
159 fprintf(stderr, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request), req);
160 #endif
161 } else if (req->pool != pool) {
162 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
163 } else if (req->request->_in_progress_cb) {
164 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "HttpRequest object(#%d) cannot be detached from the HttpRequestPool while executing the progress callback", Z_OBJ_HANDLE_P(request));
165 } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->request->ch))) {
166 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object(#%d) from the HttpRequestPool: %s", Z_OBJ_HANDLE_P(request), curl_multi_strerror(code));
167 } else {
168 req->pool = NULL;
169 zend_llist_del_element(&pool->finished, request, http_request_pool_compare_handles);
170 zend_llist_del_element(&pool->handles, request, http_request_pool_compare_handles);
171
172 #if HTTP_DEBUG_REQPOOLS
173 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
174 #endif
175
176 return SUCCESS;
177 }
178 return FAILURE;
179 }
180 /* }}} */
181
182 /* {{{ void http_request_pool_apply(http_request_pool *, http_request_pool_apply_func) */
183 PHP_HTTP_API void _http_request_pool_apply(http_request_pool *pool, http_request_pool_apply_func cb)
184 {
185 int count = zend_llist_count(&pool->handles);
186
187 if (count) {
188 int i = 0;
189 zend_llist_position pos;
190 zval **handle, **handles = emalloc(count * sizeof(zval *));
191
192 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
193 handles[i++] = *handle;
194 }
195
196 /* should never happen */
197 if (i != count) {
198 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
199 count = i;
200 }
201
202 for (i = 0; i < count; ++i) {
203 if (cb(pool, handles[i])) {
204 break;
205 }
206 }
207 efree(handles);
208 }
209 }
210 /* }}} */
211
212 /* {{{ void http_request_pool_apply_with_arg(http_request_pool *, http_request_pool_apply_with_arg_func, void *) */
213 PHP_HTTP_API void _http_request_pool_apply_with_arg(http_request_pool *pool, http_request_pool_apply_with_arg_func cb, void *arg)
214 {
215 int count = zend_llist_count(&pool->handles);
216
217 if (count) {
218 int i = 0;
219 zend_llist_position pos;
220 zval **handle, **handles = emalloc(count * sizeof(zval *));
221
222 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
223 handles[i++] = *handle;
224 }
225
226 /* should never happen */
227 if (i != count) {
228 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
229 count = i;
230 }
231
232 for (i = 0; i < count; ++i) {
233 if (cb(pool, handles[i], arg)) {
234 break;
235 }
236 }
237 efree(handles);
238 }
239 }
240 /* }}} */
241
242 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
243 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool)
244 {
245 #if HTTP_DEBUG_REQPOOLS
246 fprintf(stderr, "Detaching %d requests from pool %p\n", zend_llist_count(&pool->handles), pool);
247 #endif
248 http_request_pool_apply(pool, _http_request_pool_detach);
249 }
250 /* }}} */
251
252 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
253 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool)
254 {
255 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
256
257 #if HTTP_DEBUG_REQPOOLS
258 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
259 #endif
260
261 #ifdef HTTP_HAVE_EVENT
262 if (pool->useevents) {
263 do {
264 while (CURLM_CALL_MULTI_PERFORM == curl_multi_socket_all(pool->ch, &pool->unfinished));
265 event_base_dispatch(HTTP_G->request.pool.event.base);
266 } while (pool->unfinished);
267 } else
268 #endif
269 {
270 while (http_request_pool_perform(pool)) {
271 if (SUCCESS != http_request_pool_select(pool)) {
272 #ifdef PHP_WIN32
273 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
274 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError());
275 #else
276 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
277 #endif
278 return FAILURE;
279 }
280 }
281 }
282
283 #if HTTP_DEBUG_REQPOOLS
284 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
285 #endif
286
287 return SUCCESS;
288 }
289 /* }}} */
290
291 /* {{{ void http_request_pool_dtor(http_request_pool *) */
292 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool)
293 {
294 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
295
296 #if HTTP_DEBUG_REQPOOLS
297 fprintf(stderr, "Destructing request pool %p\n", pool);
298 #endif
299
300 #ifdef HTTP_HAVE_EVENT
301 efree(pool->timeout);
302 #endif
303
304 http_request_pool_detach_all(pool);
305
306 pool->unfinished = 0;
307 zend_llist_clean(&pool->finished);
308 zend_llist_clean(&pool->handles);
309 http_persistent_handle_release("http_request_pool", &pool->ch);
310 }
311 /* }}} */
312
313 #ifdef PHP_WIN32
314 # define SELECT_ERROR SOCKET_ERROR
315 #else
316 # define SELECT_ERROR -1
317 #endif
318
319 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
320 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
321 {
322 int MAX;
323 fd_set R, W, E;
324 struct timeval timeout;
325
326 #ifdef HTTP_HAVE_EVENT
327 if (pool->useevents) {
328 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
329 http_error(HE_WARNING, HTTP_E_RUNTIME, "not implemented; use HttpRequest callbacks");
330 return FAILURE;
331 }
332 #endif
333
334 http_request_pool_timeout(pool, &timeout);
335
336 FD_ZERO(&R);
337 FD_ZERO(&W);
338 FD_ZERO(&E);
339
340 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
341 if (MAX == -1) {
342 http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / HTTP_MCROSEC));
343 return SUCCESS;
344 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
345 return SUCCESS;
346 }
347 }
348 return FAILURE;
349 }
350 /* }}} */
351
352 /* {{{ int http_request_pool_perform(http_request_pool *) */
353 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool)
354 {
355 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
356
357 #ifdef HTTP_HAVE_EVENT
358 if (pool->useevents) {
359 http_error(HE_WARNING, HTTP_E_RUNTIME, "not implemented; use HttpRequest callbacks");
360 return FAILURE;
361 }
362 #endif
363
364 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
365
366 #if HTTP_DEBUG_REQPOOLS
367 fprintf(stderr, "%u unfinished requests of pool %p remaining\n", pool->unfinished, pool);
368 #endif
369
370 http_request_pool_responsehandler(pool);
371
372 return pool->unfinished;
373 }
374 /* }}} */
375
376 /* {{{ void http_request_pool_responsehandler(http_request_pool *) */
377 void _http_request_pool_responsehandler(http_request_pool *pool)
378 {
379 CURLMsg *msg;
380 int remaining = 0;
381 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
382
383 do {
384 msg = curl_multi_info_read(pool->ch, &remaining);
385 if (msg && CURLMSG_DONE == msg->msg) {
386 if (CURLE_OK != msg->data.result) {
387 http_request_storage *st = http_request_storage_get(msg->easy_handle);
388 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), st?st->errorbuffer:"", st?st->url:"");
389 }
390 http_request_pool_apply_with_arg(pool, _http_request_pool_apply_responsehandler, msg->easy_handle);
391 }
392 } while (remaining);
393 }
394 /* }}} */
395
396 /* {{{ int http_request_pool_apply_responsehandler(http_request_pool *, zval *, void *) */
397 int _http_request_pool_apply_responsehandler(http_request_pool *pool, zval *req, void *ch)
398 {
399 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
400 getObjectEx(http_request_object, obj, req);
401
402 if ((!ch) || obj->request->ch == (CURL *) ch) {
403
404 #if HTTP_DEBUG_REQPOOLS
405 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_P(req), obj, obj->pool);
406 #endif
407
408 ZVAL_ADDREF(req);
409 zend_llist_add_element(&obj->pool->finished, &req);
410 http_request_object_responsehandler(obj, req);
411 return 1;
412 }
413 return 0;
414 }
415 /* }}} */
416
417 /* {{{ struct timeval *_http_request_pool_timeout(http_request_pool *, struct timeval *) */
418 struct timeval *_http_request_pool_timeout(http_request_pool *pool, struct timeval *timeout)
419 {
420 #ifdef HAVE_CURL_MULTI_TIMEOUT
421 long max_tout = 1000;
422
423 if ((CURLM_OK == curl_multi_timeout(pool->ch, &max_tout)) && (max_tout != -1)) {
424 timeout->tv_sec = max_tout / 1000;
425 timeout->tv_usec = (max_tout % 1000) * 1000;
426 } else {
427 #endif
428 timeout->tv_sec = 1;
429 timeout->tv_usec = 0;
430 #ifdef HAVE_CURL_MULTI_TIMEOUT
431 }
432 #endif
433
434 #if HTTP_DEBUG_REQPOOLS
435 fprintf(stderr, "Calculating timeout (%lu, %lu) of pool %p\n", (ulong) timeout->tv_sec, (ulong) timeout->tv_usec, pool);
436 #endif
437
438 return timeout;
439 }
440 /* }}} */
441
442 /*#*/
443
444 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
445 static int http_request_pool_compare_handles(void *h1, void *h2)
446 {
447 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
448 }
449 /* }}} */
450
451 #ifdef HTTP_HAVE_EVENT
452 /* {{{ static void http_request_pool_timeout_callback(int, short, void *) */
453 static void http_request_pool_timeout_callback(int socket, short action, void *event_data)
454 {
455 http_request_pool *pool = event_data;
456
457 if (pool->useevents) {
458 CURLMcode rc;
459 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
460
461 #if HTTP_DEBUG_REQPOOLS
462 fprintf(stderr, "Timeout occurred of pool %p\n", pool);
463 #endif
464
465 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket(pool->ch, CURL_SOCKET_TIMEOUT, &pool->unfinished)));
466
467 if (CURLM_OK != rc) {
468 http_error(HE_WARNING, HTTP_E_SOCKET, curl_multi_strerror(rc));
469 }
470 #if 0
471 http_request_pool_timer_callback(pool->ch, 1000, pool);
472 #endif
473 }
474 }
475 /* }}} */
476
477 /* {{{ static void http_request_pool_event_callback(int, short, void *) */
478 static void http_request_pool_event_callback(int socket, short action, void *event_data)
479 {
480 http_request_pool_event *ev = event_data;
481 http_request_pool *pool = ev->pool;
482
483 if (pool->useevents) {
484 CURLMcode rc = CURLE_OK;
485 TSRMLS_FETCH_FROM_CTX(ev->pool->tsrm_ls);
486
487 #if HTTP_DEBUG_REQPOOLS
488 {
489 static const char event_strings[][20] = {"NONE","TIMEOUT","READ","TIMEOUT|READ","WRITE","TIMEOUT|WRITE","READ|WRITE","TIMEOUT|READ|WRITE","SIGNAL"};
490 fprintf(stderr, "Event on socket %d (%s) event %p of pool %p\n", socket, event_strings[action], ev, pool);
491 }
492 #endif
493
494 /* don't use 'ev' below this loop as it might 've been freed in the socket callback */
495 do {
496 #ifdef HAVE_CURL_MULTI_SOCKET_ACTION
497 switch (action & (EV_READ|EV_WRITE)) {
498 case EV_READ:
499 rc = curl_multi_socket_action(pool->ch, socket, CURL_CSELECT_IN, &pool->unfinished);
500 break;
501 case EV_WRITE:
502 rc = curl_multi_socket_action(pool->ch, socket, CURL_CSELECT_OUT, &pool->unfinished);
503 break;
504 case EV_READ|EV_WRITE:
505 rc = curl_multi_socket_action(pool->ch, socket, CURL_CSELECT_IN|CURL_CSELECT_OUT, &pool->unfinished);
506 break;
507 default:
508 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "Unknown event %d", (int) action);
509 return;
510 }
511 #else
512 rc = curl_multi_socket(pool->ch, socket, &pool->unfinished);
513 #endif
514 } while (CURLM_CALL_MULTI_PERFORM == rc);
515
516 switch (rc) {
517 case CURLM_BAD_SOCKET:
518 #if 0
519 fprintf(stderr, "!!! Bad socket: %d (%d)\n", socket, (int) action);
520 #endif
521 case CURLM_OK:
522 break;
523 default:
524 http_error(HE_WARNING, HTTP_E_SOCKET, curl_multi_strerror(rc));
525 break;
526 }
527
528 http_request_pool_responsehandler(pool);
529
530 /* remove timeout if there are no transfers left */
531 if (!pool->unfinished && event_initialized(pool->timeout) && event_pending(pool->timeout, EV_TIMEOUT, NULL)) {
532 event_del(pool->timeout);
533 #if HTTP_DEBUG_REQPOOLS
534 fprintf(stderr, "Removed timeout of pool %p\n", pool);
535 #endif
536 }
537 }
538 }
539 /* }}} */
540
541 /* {{{ static int http_request_pool_socket_callback(CURL *, curl_socket_t, int, void *, void *) */
542 static int http_request_pool_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data)
543 {
544 http_request_pool *pool = socket_data;
545
546 if (pool->useevents) {
547 int events = EV_PERSIST;
548 http_request_pool_event *ev = assign_data;
549 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
550
551 if (!ev) {
552 ev = ecalloc(1, sizeof(http_request_pool_event));
553 ev->pool = pool;
554 curl_multi_assign(pool->ch, sock, ev);
555 } else {
556 event_del(&ev->evnt);
557 }
558
559 #if HTTP_DEBUG_REQPOOLS
560 {
561 static const char action_strings[][8] = {"NONE", "IN", "OUT", "INOUT", "REMOVE"};
562 http_request *r;
563 curl_easy_getinfo(easy, CURLINFO_PRIVATE, &r);
564 fprintf(stderr, "Callback on socket %d (%s) event %p of pool %p (%s)\n", (int) sock, action_strings[action], ev, pool, r->url);
565 }
566 #endif
567
568 switch (action) {
569 case CURL_POLL_IN:
570 events |= EV_READ;
571 break;
572 case CURL_POLL_OUT:
573 events |= EV_WRITE;
574 break;
575 case CURL_POLL_INOUT:
576 events |= EV_READ|EV_WRITE;
577 break;
578
579 case CURL_POLL_REMOVE:
580 efree(ev);
581 case CURL_POLL_NONE:
582 return 0;
583
584 default:
585 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "Unknown socket action %d", action);
586 return -1;
587 }
588
589 event_set(&ev->evnt, sock, events, http_request_pool_event_callback, ev);
590 event_base_set(HTTP_G->request.pool.event.base, &ev->evnt);
591 event_add(&ev->evnt, NULL);
592 }
593
594 return 0;
595 }
596 /* }}} */
597
598 /* {{{ static void http_request_pool_timer_callback(CURLM *, long, void*) */
599 static void http_request_pool_timer_callback(CURLM *multi, long timeout_ms, void *timer_data)
600 {
601 http_request_pool *pool = timer_data;
602
603 if (pool->useevents) {
604 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
605 struct timeval timeout = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
606
607 if (event_initialized(pool->timeout) && event_pending(pool->timeout, EV_TIMEOUT, NULL)) {
608 event_del(pool->timeout);
609 }
610
611 event_set(pool->timeout, -1, 0, http_request_pool_timeout_callback, pool);
612 event_base_set(HTTP_G->request.pool.event.base, pool->timeout);
613 event_add(pool->timeout, &timeout);
614
615 #if HTTP_DEBUG_REQPOOLS
616 fprintf(stderr, "Updating timeout (%lu, %lu) of pool %p\n", (ulong) timeout.tv_sec, (ulong) timeout.tv_usec, pool);
617 #endif
618 }
619 }
620 /* }}} */
621 #endif /* HTTP_HAVE_EVENT */
622
623 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
624
625
626 /*
627 * Local variables:
628 * tab-width: 4
629 * c-basic-offset: 4
630 * End:
631 * vim600: noet sw=4 ts=4 fdm=marker
632 * vim<600: noet sw=4 ts=4
633 */
634