- update to package.xml v2
[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 #if HTTP_DEBUG_REQPOOLS
265 fprintf(stderr, "& Starting event dispatcher of pool %p\n", pool);
266 #endif
267 event_base_dispatch(HTTP_G->request.pool.event.base);
268 } while (pool->unfinished);
269 } else
270 #endif
271 {
272 while (http_request_pool_perform(pool)) {
273 if (SUCCESS != http_request_pool_select(pool)) {
274 #ifdef PHP_WIN32
275 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
276 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError());
277 #else
278 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
279 #endif
280 return FAILURE;
281 }
282 }
283 }
284
285 #if HTTP_DEBUG_REQPOOLS
286 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
287 #endif
288
289 return SUCCESS;
290 }
291 /* }}} */
292
293 /* {{{ void http_request_pool_dtor(http_request_pool *) */
294 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool)
295 {
296 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
297
298 #if HTTP_DEBUG_REQPOOLS
299 fprintf(stderr, "Destructing request pool %p\n", pool);
300 #endif
301
302 #ifdef HTTP_HAVE_EVENT
303 efree(pool->timeout);
304 #endif
305
306 http_request_pool_detach_all(pool);
307
308 pool->unfinished = 0;
309 zend_llist_clean(&pool->finished);
310 zend_llist_clean(&pool->handles);
311 http_persistent_handle_release("http_request_pool", &pool->ch);
312 }
313 /* }}} */
314
315 #ifdef PHP_WIN32
316 # define SELECT_ERROR SOCKET_ERROR
317 #else
318 # define SELECT_ERROR -1
319 #endif
320
321 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
322 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
323 {
324 return http_request_pool_select_ex(pool, NULL);
325 }
326 /* }}} */
327
328 /* {{{ STATUS http_request_pool_select_ex(http_request_pool *, struct timeval *) */
329 PHP_HTTP_API STATUS _http_request_pool_select_ex(http_request_pool *pool, struct timeval *custom_timeout)
330 {
331 int MAX;
332 fd_set R, W, E;
333 struct timeval timeout;
334
335 #ifdef HTTP_HAVE_EVENT
336 if (pool->useevents) {
337 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
338 http_error(HE_WARNING, HTTP_E_RUNTIME, "not implemented; use HttpRequest callbacks");
339 return FAILURE;
340 }
341 #endif
342
343 if (custom_timeout && timerisset(custom_timeout)) {
344 timeout = *custom_timeout;
345 } else {
346 http_request_pool_timeout(pool, &timeout);
347 }
348
349 FD_ZERO(&R);
350 FD_ZERO(&W);
351 FD_ZERO(&E);
352
353 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
354 if (MAX == -1) {
355 http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / HTTP_MCROSEC));
356 return SUCCESS;
357 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
358 return SUCCESS;
359 }
360 }
361 return FAILURE;
362 }
363 /* }}} */
364
365 /* {{{ int http_request_pool_perform(http_request_pool *) */
366 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool)
367 {
368 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
369
370 #ifdef HTTP_HAVE_EVENT
371 if (pool->useevents) {
372 http_error(HE_WARNING, HTTP_E_RUNTIME, "not implemented; use HttpRequest callbacks");
373 return FAILURE;
374 }
375 #endif
376
377 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
378
379 #if HTTP_DEBUG_REQPOOLS
380 fprintf(stderr, "%u unfinished requests of pool %p remaining\n", pool->unfinished, pool);
381 #endif
382
383 http_request_pool_responsehandler(pool);
384
385 return pool->unfinished;
386 }
387 /* }}} */
388
389 /* {{{ void http_request_pool_responsehandler(http_request_pool *) */
390 void _http_request_pool_responsehandler(http_request_pool *pool)
391 {
392 CURLMsg *msg;
393 int remaining = 0;
394 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
395
396 do {
397 msg = curl_multi_info_read(pool->ch, &remaining);
398 if (msg && CURLMSG_DONE == msg->msg) {
399 if (CURLE_OK != msg->data.result) {
400 http_request_storage *st = http_request_storage_get(msg->easy_handle);
401 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), st?st->errorbuffer:"", st?st->url:"");
402 }
403 http_request_pool_apply_with_arg(pool, _http_request_pool_apply_responsehandler, msg->easy_handle);
404 }
405 } while (remaining);
406 }
407 /* }}} */
408
409 /* {{{ int http_request_pool_apply_responsehandler(http_request_pool *, zval *, void *) */
410 int _http_request_pool_apply_responsehandler(http_request_pool *pool, zval *req, void *ch)
411 {
412 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
413 getObjectEx(http_request_object, obj, req);
414
415 if ((!ch) || obj->request->ch == (CURL *) ch) {
416
417 #if HTTP_DEBUG_REQPOOLS
418 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_P(req), obj, obj->pool);
419 #endif
420
421 ZVAL_ADDREF(req);
422 zend_llist_add_element(&obj->pool->finished, &req);
423 http_request_object_responsehandler(obj, req);
424 return 1;
425 }
426 return 0;
427 }
428 /* }}} */
429
430 /* {{{ struct timeval *_http_request_pool_timeout(http_request_pool *, struct timeval *) */
431 struct timeval *_http_request_pool_timeout(http_request_pool *pool, struct timeval *timeout)
432 {
433 #ifdef HAVE_CURL_MULTI_TIMEOUT
434 long max_tout = 1000;
435
436 if ((CURLM_OK == curl_multi_timeout(pool->ch, &max_tout)) && (max_tout > 0)) {
437 timeout->tv_sec = max_tout / 1000;
438 timeout->tv_usec = (max_tout % 1000) * 1000;
439 } else {
440 #endif
441 timeout->tv_sec = 0;
442 timeout->tv_usec = 1000;
443 #ifdef HAVE_CURL_MULTI_TIMEOUT
444 }
445 #endif
446
447 #if HTTP_DEBUG_REQPOOLS
448 fprintf(stderr, "Calculating timeout (%lu, %lu) of pool %p\n", (ulong) timeout->tv_sec, (ulong) timeout->tv_usec, pool);
449 #endif
450
451 return timeout;
452 }
453 /* }}} */
454
455 /*#*/
456
457 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
458 static int http_request_pool_compare_handles(void *h1, void *h2)
459 {
460 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
461 }
462 /* }}} */
463
464 #ifdef HTTP_HAVE_EVENT
465 /* {{{ static void http_request_pool_timeout_callback(int, short, void *) */
466 static void http_request_pool_timeout_callback(int socket, short action, void *event_data)
467 {
468 http_request_pool *pool = event_data;
469
470 if (pool->useevents) {
471 CURLMcode rc;
472 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
473
474 #if HTTP_DEBUG_REQPOOLS
475 fprintf(stderr, "Timeout occurred of pool %p\n", pool);
476 #endif
477
478 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket(pool->ch, CURL_SOCKET_TIMEOUT, &pool->unfinished)));
479
480 if (CURLM_OK != rc) {
481 http_error(HE_WARNING, HTTP_E_SOCKET, curl_multi_strerror(rc));
482 }
483
484 http_request_pool_responsehandler(pool);
485 }
486 }
487 /* }}} */
488
489 /* {{{ static void http_request_pool_event_callback(int, short, void *) */
490 static void http_request_pool_event_callback(int socket, short action, void *event_data)
491 {
492 http_request_pool_event *ev = event_data;
493 http_request_pool *pool = ev->pool;
494
495 if (pool->useevents) {
496 CURLMcode rc = CURLE_OK;
497 TSRMLS_FETCH_FROM_CTX(ev->pool->tsrm_ls);
498
499 #if HTTP_DEBUG_REQPOOLS
500 {
501 static const char event_strings[][20] = {"NONE","TIMEOUT","READ","TIMEOUT|READ","WRITE","TIMEOUT|WRITE","READ|WRITE","TIMEOUT|READ|WRITE","SIGNAL"};
502 fprintf(stderr, "Event on socket %d (%s) event %p of pool %p\n", socket, event_strings[action], ev, pool);
503 }
504 #endif
505
506 /* don't use 'ev' below this loop as it might 've been freed in the socket callback */
507 do {
508 #ifdef HAVE_CURL_MULTI_SOCKET_ACTION
509 switch (action & (EV_READ|EV_WRITE)) {
510 case EV_READ:
511 rc = curl_multi_socket_action(pool->ch, socket, CURL_CSELECT_IN, &pool->unfinished);
512 break;
513 case EV_WRITE:
514 rc = curl_multi_socket_action(pool->ch, socket, CURL_CSELECT_OUT, &pool->unfinished);
515 break;
516 case EV_READ|EV_WRITE:
517 rc = curl_multi_socket_action(pool->ch, socket, CURL_CSELECT_IN|CURL_CSELECT_OUT, &pool->unfinished);
518 break;
519 default:
520 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "Unknown event %d", (int) action);
521 return;
522 }
523 #else
524 rc = curl_multi_socket(pool->ch, socket, &pool->unfinished);
525 #endif
526 } while (CURLM_CALL_MULTI_PERFORM == rc);
527
528 switch (rc) {
529 case CURLM_BAD_SOCKET:
530 #if 0
531 fprintf(stderr, "!!! Bad socket: %d (%d)\n", socket, (int) action);
532 #endif
533 case CURLM_OK:
534 break;
535 default:
536 http_error(HE_WARNING, HTTP_E_SOCKET, curl_multi_strerror(rc));
537 break;
538 }
539
540 http_request_pool_responsehandler(pool);
541
542 /* remove timeout if there are no transfers left */
543 if (!pool->unfinished && event_initialized(pool->timeout) && event_pending(pool->timeout, EV_TIMEOUT, NULL)) {
544 event_del(pool->timeout);
545 #if HTTP_DEBUG_REQPOOLS
546 fprintf(stderr, "Removed timeout of pool %p\n", pool);
547 #endif
548 }
549 }
550 }
551 /* }}} */
552
553 /* {{{ static int http_request_pool_socket_callback(CURL *, curl_socket_t, int, void *, void *) */
554 static int http_request_pool_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data)
555 {
556 http_request_pool *pool = socket_data;
557
558 if (pool->useevents) {
559 int events = EV_PERSIST;
560 http_request_pool_event *ev = assign_data;
561 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
562
563 if (!ev) {
564 ev = ecalloc(1, sizeof(http_request_pool_event));
565 ev->pool = pool;
566 curl_multi_assign(pool->ch, sock, ev);
567 event_base_set(HTTP_G->request.pool.event.base, &ev->evnt);
568 } else {
569 event_del(&ev->evnt);
570 }
571
572 #if HTTP_DEBUG_REQPOOLS
573 {
574 static const char action_strings[][8] = {"NONE", "IN", "OUT", "INOUT", "REMOVE"};
575 http_request *r;
576 curl_easy_getinfo(easy, CURLINFO_PRIVATE, &r);
577 fprintf(stderr, "Callback on socket %2d (%8s) event %p of pool %p (%d)\n", (int) sock, action_strings[action], ev, pool, pool->unfinished);
578 }
579 #endif
580
581 switch (action) {
582 case CURL_POLL_IN:
583 events |= EV_READ;
584 break;
585 case CURL_POLL_OUT:
586 events |= EV_WRITE;
587 break;
588 case CURL_POLL_INOUT:
589 events |= EV_READ|EV_WRITE;
590 break;
591
592 case CURL_POLL_REMOVE:
593 efree(ev);
594 case CURL_POLL_NONE:
595 return 0;
596
597 default:
598 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "Unknown socket action %d", action);
599 return -1;
600 }
601
602 event_set(&ev->evnt, sock, events, http_request_pool_event_callback, ev);
603 event_add(&ev->evnt, NULL);
604 }
605
606 return 0;
607 }
608 /* }}} */
609
610 /* {{{ static void http_request_pool_timer_callback(CURLM *, long, void*) */
611 static void http_request_pool_timer_callback(CURLM *multi, long timeout_ms, void *timer_data)
612 {
613 http_request_pool *pool = timer_data;
614
615 if (pool->useevents) {
616 TSRMLS_FETCH_FROM_CTX(pool->tsrm_ls);
617 struct timeval timeout;
618
619 if (!event_initialized(pool->timeout)) {
620 event_set(pool->timeout, -1, 0, http_request_pool_timeout_callback, pool);
621 event_base_set(HTTP_G->request.pool.event.base, pool->timeout);
622 } else if (event_pending(pool->timeout, EV_TIMEOUT, NULL)) {
623 event_del(pool->timeout);
624 }
625
626 if (timeout_ms > 0) {
627 timeout.tv_sec = timeout_ms / 1000;
628 timeout.tv_usec = (timeout_ms % 1000) * 1000;
629 } else {
630 http_request_pool_timeout(pool, &timeout);
631 }
632
633 event_add(pool->timeout, &timeout);
634
635 #if HTTP_DEBUG_REQPOOLS
636 fprintf(stderr, "Updating timeout %lu (%lu, %lu) of pool %p\n", (ulong) timeout_ms, (ulong) timeout.tv_sec, (ulong) timeout.tv_usec, pool);
637 #endif
638 }
639 }
640 /* }}} */
641 #endif /* HTTP_HAVE_EVENT */
642
643 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
644
645
646 /*
647 * Local variables:
648 * tab-width: 4
649 * c-basic-offset: 4
650 * End:
651 * vim600: noet sw=4 ts=4 fdm=marker
652 * vim<600: noet sw=4 ts=4
653 */
654