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
);
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 while (CURLM_CALL_MULTI_PERFORM
== code
) {
127 #ifdef HTTP_HAVE_EVENT
128 if (pool
->useevents
) {
129 code
= curl_multi_socket_all(pool
->ch
, &pool
->unfinished
);
132 code
= curl_multi_perform(pool
->ch
, &pool
->unfinished
);
133 #ifdef HTTP_HAVE_EVENT
137 if (CURLM_OK
!= code
) {
138 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
));
142 ZVAL_ADDREF(request
);
143 zend_llist_add_element(&pool
->handles
, &request
);
145 #if HTTP_DEBUG_REQPOOLS
146 fprintf(stderr
, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool
->handles
), pool
);
155 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
156 PHP_HTTP_API STATUS
_http_request_pool_detach(http_request_pool
*pool
, zval
*request
)
159 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
160 getObjectEx(http_request_object
, req
, request
);
162 #if HTTP_DEBUG_REQPOOLS
163 fprintf(stderr
, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request
), req
, pool
);
167 /* not attached to any pool */
168 #if HTTP_DEBUG_REQPOOLS
169 fprintf(stderr
, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request
), req
);
171 } else if (req
->pool
!= pool
) {
172 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request
));
173 } else if (req
->request
->_in_progress_cb
) {
174 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
));
175 } else if (CURLM_OK
!= (code
= curl_multi_remove_handle(pool
->ch
, req
->request
->ch
))) {
176 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
));
179 zend_llist_del_element(&pool
->finished
, request
, http_request_pool_compare_handles
);
180 zend_llist_del_element(&pool
->handles
, request
, http_request_pool_compare_handles
);
182 #if HTTP_DEBUG_REQPOOLS
183 fprintf(stderr
, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool
->handles
), pool
);
192 /* {{{ void http_request_pool_apply(http_request_pool *, http_request_pool_apply_func) */
193 PHP_HTTP_API
void _http_request_pool_apply(http_request_pool
*pool
, http_request_pool_apply_func cb
)
195 int count
= zend_llist_count(&pool
->handles
);
199 zend_llist_position pos
;
200 zval
**handle
, **handles
= emalloc(count
* sizeof(zval
*));
202 for (handle
= zend_llist_get_first_ex(&pool
->handles
, &pos
); handle
; handle
= zend_llist_get_next_ex(&pool
->handles
, &pos
)) {
203 handles
[i
++] = *handle
;
206 /* should never happen */
208 zend_error(E_ERROR
, "number of fetched request handles do not match overall count");
212 for (i
= 0; i
< count
; ++i
) {
213 if (cb(pool
, handles
[i
])) {
222 /* {{{ void http_request_pool_apply_with_arg(http_request_pool *, http_request_pool_apply_with_arg_func, void *) */
223 PHP_HTTP_API
void _http_request_pool_apply_with_arg(http_request_pool
*pool
, http_request_pool_apply_with_arg_func cb
, void *arg
)
225 int count
= zend_llist_count(&pool
->handles
);
229 zend_llist_position pos
;
230 zval
**handle
, **handles
= emalloc(count
* sizeof(zval
*));
232 for (handle
= zend_llist_get_first_ex(&pool
->handles
, &pos
); handle
; handle
= zend_llist_get_next_ex(&pool
->handles
, &pos
)) {
233 handles
[i
++] = *handle
;
236 /* should never happen */
238 zend_error(E_ERROR
, "number of fetched request handles do not match overall count");
242 for (i
= 0; i
< count
; ++i
) {
243 if (cb(pool
, handles
[i
], arg
)) {
252 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
253 PHP_HTTP_API
void _http_request_pool_detach_all(http_request_pool
*pool
)
255 #if HTTP_DEBUG_REQPOOLS
256 fprintf(stderr
, "Detaching %d requests from pool %p\n", zend_llist_count(&pool
->handles
), pool
);
258 http_request_pool_apply(pool
, _http_request_pool_detach
);
262 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
263 PHP_HTTP_API STATUS
_http_request_pool_send(http_request_pool
*pool
)
265 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
267 #if HTTP_DEBUG_REQPOOLS
268 fprintf(stderr
, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool
->handles
), pool
);
271 #ifdef HTTP_HAVE_EVENT
272 if (pool
->useevents
) {
273 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_socket_all(pool
->ch
, &pool
->unfinished
));
274 event_base_dispatch(HTTP_G
->request
.pool
.event
.base
);
278 while (http_request_pool_perform(pool
)) {
279 if (SUCCESS
!= http_request_pool_select(pool
)) {
281 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
282 http_error_ex(HE_WARNING
, HTTP_E_SOCKET
, "WinSock error: %d", WSAGetLastError());
284 http_error(HE_WARNING
, HTTP_E_SOCKET
, strerror(errno
));
291 #if HTTP_DEBUG_REQPOOLS
292 fprintf(stderr
, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool
->handles
), pool
, pool
->unfinished
);
299 /* {{{ void http_request_pool_dtor(http_request_pool *) */
300 PHP_HTTP_API
void _http_request_pool_dtor(http_request_pool
*pool
)
302 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
304 #if HTTP_DEBUG_REQPOOLS
305 fprintf(stderr
, "Destructing request pool %p\n", pool
);
309 efree(pool
->timeout
);
312 pool
->unfinished
= 0;
313 zend_llist_clean(&pool
->finished
);
314 zend_llist_clean(&pool
->handles
);
315 http_persistent_handle_release("http_request_pool", &pool
->ch
);
320 # define SELECT_ERROR SOCKET_ERROR
322 # define SELECT_ERROR -1
325 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
326 PHP_HTTP_API STATUS
_http_request_pool_select(http_request_pool
*pool
)
330 struct timeval timeout
;
332 #ifdef HTTP_HAVE_EVENT
333 if (pool
->useevents
) {
334 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
335 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "not implemented; use HttpRequest callbacks");
340 http_request_pool_timeout(pool
, &timeout
);
346 if (CURLM_OK
== curl_multi_fdset(pool
->ch
, &R
, &W
, &E
, &MAX
)) {
348 http_sleep((double) timeout
.tv_sec
+ (double) (timeout
.tv_usec
/ HTTP_MCROSEC
));
350 } else if (SELECT_ERROR
!= select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) {
358 /* {{{ int http_request_pool_perform(http_request_pool *) */
359 PHP_HTTP_API
int _http_request_pool_perform(http_request_pool
*pool
)
361 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
363 #ifdef HTTP_HAVE_EVENT
364 if (pool
->useevents
) {
365 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "not implemented; use HttpRequest callbacks");
370 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(pool
->ch
, &pool
->unfinished
));
372 #if HTTP_DEBUG_REQPOOLS
373 fprintf(stderr
, "%u unfinished requests of pool %p remaining\n", pool
->unfinished
, pool
);
376 http_request_pool_responsehandler(pool
);
378 return pool
->unfinished
;
382 /* {{{ void http_request_pool_responsehandler(http_request_pool *) */
383 void _http_request_pool_responsehandler(http_request_pool
*pool
)
387 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
390 msg
= curl_multi_info_read(pool
->ch
, &remaining
);
391 if (msg
&& CURLMSG_DONE
== msg
->msg
) {
392 if (CURLE_OK
!= msg
->data
.result
) {
393 http_request
*r
= NULL
;
394 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
, &r
);
395 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(msg
->data
.result
), r
?r
->_error
:"", r
?r
->url
:"");
397 http_request_pool_apply_with_arg(pool
, _http_request_pool_apply_responsehandler
, msg
->easy_handle
);
403 /* {{{ int http_request_pool_apply_responsehandler(http_request_pool *, zval *, void *) */
404 int _http_request_pool_apply_responsehandler(http_request_pool
*pool
, zval
*req
, void *ch
)
406 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
407 getObjectEx(http_request_object
, obj
, req
);
409 if ((!ch
) || obj
->request
->ch
== (CURL
*) ch
) {
411 #if HTTP_DEBUG_REQPOOLS
412 fprintf(stderr
, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_P(req
), obj
, obj
->pool
);
416 zend_llist_add_element(&obj
->pool
->finished
, &req
);
417 http_request_object_responsehandler(obj
, req
);
424 /* {{{ struct timeval *_http_request_pool_timeout(http_request_pool *, struct timeval *) */
425 struct timeval
*_http_request_pool_timeout(http_request_pool
*pool
, struct timeval
*timeout
)
427 #ifdef HAVE_CURL_MULTI_TIMEOUT
428 long max_tout
= 1000;
430 if ((CURLM_OK
== curl_multi_timeout(pool
->ch
, &max_tout
)) && (max_tout
!= -1)) {
431 timeout
->tv_sec
= max_tout
/ 1000;
432 timeout
->tv_usec
= (max_tout
% 1000) * 1000;
436 timeout
->tv_usec
= 0;
437 #ifdef HAVE_CURL_MULTI_TIMEOUT
441 #if HTTP_DEBUG_REQPOOLS
442 fprintf(stderr
, "Calculating timeout (%lu, %lu) of pool %p\n", (ulong
) timeout
->tv_sec
, (ulong
) timeout
->tv_usec
, pool
);
451 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
452 static int http_request_pool_compare_handles(void *h1
, void *h2
)
454 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
458 #ifdef HTTP_HAVE_EVENT
459 /* {{{ static void http_request_pool_timeout_callback(int, short, void *) */
460 static void http_request_pool_timeout_callback(int socket
, short action
, void *event_data
)
463 http_request_pool
*pool
= event_data
;
464 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
466 #if HTTP_DEBUG_REQPOOLS
467 fprintf(stderr
, "Timeout occurred of pool %p\n", pool
);
470 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket(pool
->ch
, CURL_SOCKET_TIMEOUT
, &pool
->unfinished
)));
472 if (CURLM_OK
!= rc
) {
473 http_error(HE_WARNING
, HTTP_E_SOCKET
, curl_multi_strerror(rc
));
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 CURLMcode rc
= CURLE_OK
;
482 http_request_pool_event
*ev
= event_data
;
483 http_request_pool
*pool
= ev
->pool
;
484 TSRMLS_FETCH_FROM_CTX(ev
->pool
->tsrm_ls
);
486 #if HTTP_DEBUG_REQPOOLS
488 static const char event_strings
[][20] = {"NONE","TIMEOUT","READ","TIMEOUT|READ","WRITE","TIMEOUT|WRITE","READ|WRITE","TIMEOUT|READ|WRITE","SIGNAL"};
489 fprintf(stderr
, "Event on socket %d (%s) event %p of pool %p\n", socket
, event_strings
[action
], ev
, pool
);
493 /* don't use 'ev' below this loop as it might 've been freed in the socket callback */
495 #ifdef HAVE_CURL_MULTI_SOCKET_ACTION
496 switch (action
& (EV_READ
|EV_WRITE
)) {
498 rc
= curl_multi_socket_action(pool
->ch
, socket
, CURL_CSELECT_IN
, &pool
->unfinished
);
501 rc
= curl_multi_socket_action(pool
->ch
, socket
, CURL_CSELECT_OUT
, &pool
->unfinished
);
503 case EV_READ
|EV_WRITE
:
504 rc
= curl_multi_socket_action(pool
->chm socket
, CURL_CSELECT_IN
|CURL_CSELECT_OUT
, &pool
->unfinished
);
507 http_error(HE_WARNING
, HTTP_E_SOCKET
, "Unknown event %d", (int) action
);
511 rc
= curl_multi_socket(pool
->ch
, socket
, &pool
->unfinished
);
513 } while (CURLM_CALL_MULTI_PERFORM
== rc
);
515 if (CURLM_OK
!= rc
) {
516 http_error(HE_WARNING
, HTTP_E_SOCKET
, curl_multi_strerror(rc
));
519 http_request_pool_responsehandler(pool
);
521 /* remove timeout if there are no transfers left */
522 if (!pool
->unfinished
&& event_initialized(pool
->timeout
) && event_pending(pool
->timeout
, EV_TIMEOUT
, NULL
)) {
523 event_del(pool
->timeout
);
524 #if HTTP_DEBUG_REQPOOLS
525 fprintf(stderr
, "Removed timeout of pool %p\n", pool
);
531 /* {{{ static int http_request_pool_socket_callback(CURL *, curl_socket_t, int, void *, void *) */
532 static int http_request_pool_socket_callback(CURL
*easy
, curl_socket_t sock
, int action
, void *socket_data
, void *assign_data
)
534 int events
= EV_PERSIST
;
535 http_request_pool
*pool
= socket_data
;
536 http_request_pool_event
*ev
= assign_data
;
537 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
540 ev
= ecalloc(1, sizeof(http_request_pool_event
));
542 curl_multi_assign(pool
->ch
, sock
, ev
);
544 event_del(&ev
->evnt
);
547 #if HTTP_DEBUG_REQPOOLS
549 static const char action_strings
[][8] = {"NONE", "IN", "OUT", "INOUT", "REMOVE"};
551 curl_easy_getinfo(easy
, CURLINFO_PRIVATE
, &r
);
552 fprintf(stderr
, "Callback on socket %d (%s) event %p of pool %p (%s)\n", (int) sock
, action_strings
[action
], ev
, pool
, r
->url
);
563 case CURL_POLL_INOUT
:
564 events
|= EV_READ
|EV_WRITE
;
567 case CURL_POLL_REMOVE
:
573 http_error_ex(HE_WARNING
, HTTP_E_SOCKET
, "Unknown socket action %d", action
);
577 event_set(&ev
->evnt
, sock
, events
, http_request_pool_event_callback
, ev
);
578 event_base_set(HTTP_G
->request
.pool
.event
.base
, &ev
->evnt
);
579 event_add(&ev
->evnt
, NULL
);
585 /* {{{ static void http_request_pool_timer_callback(CURLM *, long, void*) */
586 static void http_request_pool_timer_callback(CURLM
*multi
, long timeout_ms
, void *timer_data
)
588 http_request_pool
*pool
= timer_data
;
589 TSRMLS_FETCH_FROM_CTX(pool
->tsrm_ls
);
590 struct timeval timeout
= {timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000};
592 if (event_initialized(pool
->timeout
) && event_pending(pool
->timeout
, EV_TIMEOUT
, NULL
)) {
593 event_del(pool
->timeout
);
596 if (pool
->unfinished
) {
597 event_set(pool
->timeout
, -1, 0, http_request_pool_timeout_callback
, pool
);
598 event_base_set(HTTP_G
->request
.pool
.event
.base
, pool
->timeout
);
599 event_add(pool
->timeout
, &timeout
);
601 #if HTTP_DEBUG_REQPOOLS
602 fprintf(stderr
, "Updating timeout (%lu, %lu) of pool %p\n", (ulong
) timeout
.tv_sec
, (ulong
) timeout
.tv_usec
, pool
);
605 #if HTTP_DEBUG_REQPOOLS
606 else fprintf(stderr
, "Removed timeout of pool %p\n", pool
);
610 #endif /* HTTP_HAVE_EVENT */
612 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
620 * vim600: noet sw=4 ts=4 fdm=marker
621 * vim<600: noet sw=4 ts=4