2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
15 #define HTTP_WANT_CURL
16 #define HTTP_WANT_EVENT
19 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
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"
29 #ifndef HTTP_DEBUG_REQPOOLS
30 # define HTTP_DEBUG_REQPOOLS 0
33 #ifdef HTTP_HAVE_EVENT
34 typedef struct _http_request_pool_event_t
{
36 http_request_pool
*pool
;
37 } http_request_pool_event
;
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
);
45 static int http_request_pool_compare_handles(void *h1
, void *h2
);
47 PHP_MINIT_FUNCTION(http_request_pool
)
49 if (SUCCESS
!= http_persistent_handle_provide("http_request_pool", curl_multi_init
, (http_persistent_handle_dtor
) curl_multi_cleanup
, NULL
)) {
55 #ifdef HTTP_HAVE_EVENT
56 PHP_RINIT_FUNCTION(http_request_pool
)
58 if (!HTTP_G
->request
.pool
.event
.base
&& !(HTTP_G
->request
.pool
.event
.base
= event_init())) {
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
)
71 #if HTTP_DEBUG_REQPOOLS
72 fprintf(stderr
, "Initializing request pool %p\n", pool
);
75 if ((free_pool
= (!pool
))) {
76 pool
= emalloc(sizeof(http_request_pool
));
80 if (SUCCESS
!= http_persistent_handle_acquire("http_request_pool", &pool
->ch
)) {
87 TSRMLS_SET_CTX(pool
->tsrm_ls
);
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
);
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);
101 #if HTTP_DEBUG_REQPOOLS
102 fprintf(stderr
, "Initialized request pool %p\n", pool
);
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
)
112 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
113 getObjectEx(http_request_object
, req
, request
);
115 #if HTTP_DEBUG_REQPOOLS
116 fprintf(stderr
, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request
), 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
));
124 CURLMcode code
= curl_multi_add_handle(pool
->ch
, req
->request
->ch
);
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
));
131 ZVAL_ADDREF(request
);
132 zend_llist_add_element(&pool
->handles
, &request
);
135 #if HTTP_DEBUG_REQPOOLS
136 fprintf(stderr
, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool
->handles
), pool
);
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
)
149 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
150 getObjectEx(http_request_object
, req
, request
);
152 #if HTTP_DEBUG_REQPOOLS
153 fprintf(stderr
, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request
), 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
);
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
));
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
);
172 #if HTTP_DEBUG_REQPOOLS
173 fprintf(stderr
, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool
->handles
), pool
);
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
)
185 int count
= zend_llist_count(&pool
->handles
);
189 zend_llist_position pos
;
190 zval
**handle
, **handles
= emalloc(count
* sizeof(zval
*));
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
;
196 /* should never happen */
198 zend_error(E_ERROR
, "number of fetched request handles do not match overall count");
202 for (i
= 0; i
< count
; ++i
) {
203 if (cb(pool
, handles
[i
])) {
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
)
215 int count
= zend_llist_count(&pool
->handles
);
219 zend_llist_position pos
;
220 zval
**handle
, **handles
= emalloc(count
* sizeof(zval
*));
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
;
226 /* should never happen */
228 zend_error(E_ERROR
, "number of fetched request handles do not match overall count");
232 for (i
= 0; i
< count
; ++i
) {
233 if (cb(pool
, handles
[i
], arg
)) {
242 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
243 PHP_HTTP_API
void _http_request_pool_detach_all(http_request_pool
*pool
)
245 #if HTTP_DEBUG_REQPOOLS
246 fprintf(stderr
, "Detaching %d requests from pool %p\n", zend_llist_count(&pool
->handles
), pool
);
248 http_request_pool_apply(pool
, _http_request_pool_detach
);
252 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
253 PHP_HTTP_API STATUS
_http_request_pool_send(http_request_pool
*pool
)
255 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
257 #if HTTP_DEBUG_REQPOOLS
258 fprintf(stderr
, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool
->handles
), pool
);
261 #ifdef HTTP_HAVE_EVENT
262 if (pool
->useevents
) {
264 #if HTTP_DEBUG_REQPOOLS
265 fprintf(stderr
, "& Starting event dispatcher of pool %p\n", pool
);
267 event_base_dispatch(HTTP_G
->request
.pool
.event
.base
);
268 } while (pool
->unfinished
);
272 while (http_request_pool_perform(pool
)) {
273 if (SUCCESS
!= http_request_pool_select(pool
)) {
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());
278 http_error(HE_WARNING
, HTTP_E_SOCKET
, strerror(errno
));
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
);
293 /* {{{ void http_request_pool_dtor(http_request_pool *) */
294 PHP_HTTP_API
void _http_request_pool_dtor(http_request_pool
*pool
)
296 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
298 #if HTTP_DEBUG_REQPOOLS
299 fprintf(stderr
, "Destructing request pool %p\n", pool
);
302 #ifdef HTTP_HAVE_EVENT
303 efree(pool
->timeout
);
306 http_request_pool_detach_all(pool
);
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
);
316 # define SELECT_ERROR SOCKET_ERROR
318 # define SELECT_ERROR -1
321 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
322 PHP_HTTP_API STATUS
_http_request_pool_select(http_request_pool
*pool
)
326 struct timeval timeout
;
328 #ifdef HTTP_HAVE_EVENT
329 if (pool
->useevents
) {
330 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
331 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "not implemented; use HttpRequest callbacks");
336 http_request_pool_timeout(pool
, &timeout
);
342 if (CURLM_OK
== curl_multi_fdset(pool
->ch
, &R
, &W
, &E
, &MAX
)) {
344 http_sleep((double) timeout
.tv_sec
+ (double) (timeout
.tv_usec
/ HTTP_MCROSEC
));
346 } else if (SELECT_ERROR
!= select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) {
354 /* {{{ int http_request_pool_perform(http_request_pool *) */
355 PHP_HTTP_API
int _http_request_pool_perform(http_request_pool
*pool
)
357 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
359 #ifdef HTTP_HAVE_EVENT
360 if (pool
->useevents
) {
361 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "not implemented; use HttpRequest callbacks");
366 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(pool
->ch
, &pool
->unfinished
));
368 #if HTTP_DEBUG_REQPOOLS
369 fprintf(stderr
, "%u unfinished requests of pool %p remaining\n", pool
->unfinished
, pool
);
372 http_request_pool_responsehandler(pool
);
374 return pool
->unfinished
;
378 /* {{{ void http_request_pool_responsehandler(http_request_pool *) */
379 void _http_request_pool_responsehandler(http_request_pool
*pool
)
383 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
386 msg
= curl_multi_info_read(pool
->ch
, &remaining
);
387 if (msg
&& CURLMSG_DONE
== msg
->msg
) {
388 if (CURLE_OK
!= msg
->data
.result
) {
389 http_request_storage
*st
= http_request_storage_get(msg
->easy_handle
);
390 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(msg
->data
.result
), st
?st
->errorbuffer
:"", st
?st
->url
:"");
392 http_request_pool_apply_with_arg(pool
, _http_request_pool_apply_responsehandler
, msg
->easy_handle
);
398 /* {{{ int http_request_pool_apply_responsehandler(http_request_pool *, zval *, void *) */
399 int _http_request_pool_apply_responsehandler(http_request_pool
*pool
, zval
*req
, void *ch
)
401 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
402 getObjectEx(http_request_object
, obj
, req
);
404 if ((!ch
) || obj
->request
->ch
== (CURL
*) ch
) {
406 #if HTTP_DEBUG_REQPOOLS
407 fprintf(stderr
, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_P(req
), obj
, obj
->pool
);
411 zend_llist_add_element(&obj
->pool
->finished
, &req
);
412 http_request_object_responsehandler(obj
, req
);
419 /* {{{ struct timeval *_http_request_pool_timeout(http_request_pool *, struct timeval *) */
420 struct timeval
*_http_request_pool_timeout(http_request_pool
*pool
, struct timeval
*timeout
)
422 #ifdef HAVE_CURL_MULTI_TIMEOUT
423 long max_tout
= 1000;
425 if ((CURLM_OK
== curl_multi_timeout(pool
->ch
, &max_tout
)) && (max_tout
> 0)) {
426 timeout
->tv_sec
= max_tout
/ 1000;
427 timeout
->tv_usec
= (max_tout
% 1000) * 1000;
431 timeout
->tv_usec
= 1000;
432 #ifdef HAVE_CURL_MULTI_TIMEOUT
436 #if HTTP_DEBUG_REQPOOLS
437 fprintf(stderr
, "Calculating timeout (%lu, %lu) of pool %p\n", (ulong
) timeout
->tv_sec
, (ulong
) timeout
->tv_usec
, pool
);
446 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
447 static int http_request_pool_compare_handles(void *h1
, void *h2
)
449 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
453 #ifdef HTTP_HAVE_EVENT
454 /* {{{ static void http_request_pool_timeout_callback(int, short, void *) */
455 static void http_request_pool_timeout_callback(int socket
, short action
, void *event_data
)
457 http_request_pool
*pool
= event_data
;
459 if (pool
->useevents
) {
461 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
463 #if HTTP_DEBUG_REQPOOLS
464 fprintf(stderr
, "Timeout occurred of pool %p\n", pool
);
467 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket(pool
->ch
, CURL_SOCKET_TIMEOUT
, &pool
->unfinished
)));
469 if (CURLM_OK
!= rc
) {
470 http_error(HE_WARNING
, HTTP_E_SOCKET
, curl_multi_strerror(rc
));
473 http_request_pool_responsehandler(pool
);
478 /* {{{ static void http_request_pool_event_callback(int, short, void *) */
479 static void http_request_pool_event_callback(int socket
, short action
, void *event_data
)
481 http_request_pool_event
*ev
= event_data
;
482 http_request_pool
*pool
= ev
->pool
;
484 if (pool
->useevents
) {
485 CURLMcode rc
= CURLE_OK
;
486 TSRMLS_FETCH_FROM_CTX(ev
->pool
->tsrm_ls
);
488 #if HTTP_DEBUG_REQPOOLS
490 static const char event_strings
[][20] = {"NONE","TIMEOUT","READ","TIMEOUT|READ","WRITE","TIMEOUT|WRITE","READ|WRITE","TIMEOUT|READ|WRITE","SIGNAL"};
491 fprintf(stderr
, "Event on socket %d (%s) event %p of pool %p\n", socket
, event_strings
[action
], ev
, pool
);
495 /* don't use 'ev' below this loop as it might 've been freed in the socket callback */
497 #ifdef HAVE_CURL_MULTI_SOCKET_ACTION
498 switch (action
& (EV_READ
|EV_WRITE
)) {
500 rc
= curl_multi_socket_action(pool
->ch
, socket
, CURL_CSELECT_IN
, &pool
->unfinished
);
503 rc
= curl_multi_socket_action(pool
->ch
, socket
, CURL_CSELECT_OUT
, &pool
->unfinished
);
505 case EV_READ
|EV_WRITE
:
506 rc
= curl_multi_socket_action(pool
->ch
, socket
, CURL_CSELECT_IN
|CURL_CSELECT_OUT
, &pool
->unfinished
);
509 http_error_ex(HE_WARNING
, HTTP_E_SOCKET
, "Unknown event %d", (int) action
);
513 rc
= curl_multi_socket(pool
->ch
, socket
, &pool
->unfinished
);
515 } while (CURLM_CALL_MULTI_PERFORM
== rc
);
518 case CURLM_BAD_SOCKET
:
520 fprintf(stderr
, "!!! Bad socket: %d (%d)\n", socket
, (int) action
);
525 http_error(HE_WARNING
, HTTP_E_SOCKET
, curl_multi_strerror(rc
));
529 http_request_pool_responsehandler(pool
);
531 /* remove timeout if there are no transfers left */
532 if (!pool
->unfinished
&& event_initialized(pool
->timeout
) && event_pending(pool
->timeout
, EV_TIMEOUT
, NULL
)) {
533 event_del(pool
->timeout
);
534 #if HTTP_DEBUG_REQPOOLS
535 fprintf(stderr
, "Removed timeout of pool %p\n", pool
);
542 /* {{{ static int http_request_pool_socket_callback(CURL *, curl_socket_t, int, void *, void *) */
543 static int http_request_pool_socket_callback(CURL
*easy
, curl_socket_t sock
, int action
, void *socket_data
, void *assign_data
)
545 http_request_pool
*pool
= socket_data
;
547 if (pool
->useevents
) {
548 int events
= EV_PERSIST
;
549 http_request_pool_event
*ev
= assign_data
;
550 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
553 ev
= ecalloc(1, sizeof(http_request_pool_event
));
555 curl_multi_assign(pool
->ch
, sock
, ev
);
556 event_base_set(HTTP_G
->request
.pool
.event
.base
, &ev
->evnt
);
558 event_del(&ev
->evnt
);
561 #if HTTP_DEBUG_REQPOOLS
563 static const char action_strings
[][8] = {"NONE", "IN", "OUT", "INOUT", "REMOVE"};
565 curl_easy_getinfo(easy
, CURLINFO_PRIVATE
, &r
);
566 fprintf(stderr
, "Callback on socket %2d (%8s) event %p of pool %p (%d)\n", (int) sock
, action_strings
[action
], ev
, pool
, pool
->unfinished
);
577 case CURL_POLL_INOUT
:
578 events
|= EV_READ
|EV_WRITE
;
581 case CURL_POLL_REMOVE
:
587 http_error_ex(HE_WARNING
, HTTP_E_SOCKET
, "Unknown socket action %d", action
);
591 event_set(&ev
->evnt
, sock
, events
, http_request_pool_event_callback
, ev
);
592 event_add(&ev
->evnt
, NULL
);
599 /* {{{ static void http_request_pool_timer_callback(CURLM *, long, void*) */
600 static void http_request_pool_timer_callback(CURLM
*multi
, long timeout_ms
, void *timer_data
)
602 http_request_pool
*pool
= timer_data
;
604 if (pool
->useevents
) {
605 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
606 struct timeval timeout
;
608 if (!event_initialized(pool
->timeout
)) {
609 event_set(pool
->timeout
, -1, 0, http_request_pool_timeout_callback
, pool
);
610 event_base_set(HTTP_G
->request
.pool
.event
.base
, pool
->timeout
);
611 } else if (event_pending(pool
->timeout
, EV_TIMEOUT
, NULL
)) {
612 event_del(pool
->timeout
);
615 if (timeout_ms
> 0) {
616 timeout
.tv_sec
= timeout_ms
/ 1000;
617 timeout
.tv_usec
= (timeout_ms
% 1000) * 1000;
619 http_request_pool_timeout(pool
, &timeout
);
622 event_add(pool
->timeout
, &timeout
);
624 #if HTTP_DEBUG_REQPOOLS
625 fprintf(stderr
, "Updating timeout %lu (%lu, %lu) of pool %p\n", (ulong
) timeout_ms
, (ulong
) timeout
.tv_sec
, (ulong
) timeout
.tv_usec
, pool
);
630 #endif /* HTTP_HAVE_EVENT */
632 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
640 * vim600: noet sw=4 ts=4 fdm=marker
641 * vim<600: noet sw=4 ts=4