2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
23 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
26 #include "php_http_std_defs.h"
27 #include "php_http_api.h"
28 #include "php_http_request_api.h"
29 #include "php_http_request_pool_api.h"
30 #include "php_http_request_object.h"
31 #include "php_http_requestpool_object.h"
33 #ifndef HTTP_DEBUG_REQPOOLS
34 # define HTTP_DEBUG_REQPOOLS 0
37 ZEND_EXTERN_MODULE_GLOBALS(http
);
39 #ifndef HAVE_CURL_MULTI_STRERROR
40 # define curl_multi_strerror(dummy) "unknown error"
43 static void http_request_pool_freebody(http_request_callback_ctx
**body
);
44 static int http_request_pool_compare_handles(void *h1
, void *h2
);
46 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
47 PHP_HTTP_API http_request_pool
*_http_request_pool_init(http_request_pool
*pool TSRMLS_DC
)
51 #if HTTP_DEBUG_REQPOOLS
52 fprintf(stderr
, "Initializing request pool %p\n", pool
);
55 if ((free_pool
= (!pool
))) {
56 pool
= emalloc(sizeof(http_request_pool
));
60 HTTP_CHECK_CURL_INIT(pool
->ch
, curl_multi_init(), ;);
69 zend_llist_init(&pool
->finished
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
70 zend_llist_init(&pool
->handles
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
71 zend_llist_init(&pool
->bodies
, sizeof(http_request_callback_ctx
*), (llist_dtor_func_t
) http_request_pool_freebody
, 0);
73 #if HTTP_DEBUG_REQPOOLS
74 fprintf(stderr
, "Initialized request pool %p\n", pool
);
81 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
82 PHP_HTTP_API STATUS
_http_request_pool_attach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
84 getObjectEx(http_request_object
, req
, request
);
86 #if HTTP_DEBUG_REQPOOLS
87 fprintf(stderr
, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request
), req
, pool
);
91 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");
93 http_request_callback_ctx
*body
= http_request_callback_data_ex(http_request_body_new(), 0);
95 if (SUCCESS
!= http_request_pool_requesthandler(request
, body
->data
)) {
96 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
98 CURLMcode code
= curl_multi_add_handle(pool
->ch
, req
->ch
);
100 if ((CURLM_OK
!= code
) && (CURLM_CALL_MULTI_PERFORM
!= code
)) {
101 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_POOL
, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code
));
105 zend_llist_add_element(&pool
->handles
, &request
);
106 zend_llist_add_element(&pool
->bodies
, &body
);
108 zval_add_ref(&request
);
109 zend_objects_store_add_ref(request TSRMLS_CC
);
111 #if HTTP_DEBUG_REQPOOLS
112 fprintf(stderr
, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool
->handles
), pool
);
124 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
125 PHP_HTTP_API STATUS
_http_request_pool_detach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
127 getObjectEx(http_request_object
, req
, request
);
129 #if HTTP_DEBUG_REQPOOLS
130 fprintf(stderr
, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request
), req
, pool
);
134 /* not attached to any pool */
135 #if HTTP_DEBUG_REQPOOLS
136 fprintf(stderr
, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request
), req
);
138 } else if (req
->pool
!= pool
) {
139 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request
));
144 zend_llist_del_element(&pool
->handles
, request
, http_request_pool_compare_handles
);
145 zend_llist_del_element(&pool
->finished
, request
, http_request_pool_compare_handles
);
147 #if HTTP_DEBUG_REQPOOLS
148 fprintf(stderr
, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool
->handles
), pool
);
151 if (CURLM_OK
!= (code
= curl_multi_remove_handle(pool
->ch
, req
->ch
))) {
152 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_POOL
, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code
));
161 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
162 PHP_HTTP_API
void _http_request_pool_detach_all(http_request_pool
*pool TSRMLS_DC
)
164 int count
= zend_llist_count(&pool
->handles
);
166 #if HTTP_DEBUG_REQPOOLS
167 fprintf(stderr
, "Detaching %d requests from pool %p\n", count
, pool
);
171 * we cannot apply a function to the llist which actually detaches
172 * the curl handle *and* removes the llist element --
173 * so let's get our hands dirty
177 zend_llist_position pos
;
178 zval
**handle
, **handles
= emalloc(count
* sizeof(zval
*));
180 for (handle
= zend_llist_get_first_ex(&pool
->handles
, &pos
); handle
; handle
= zend_llist_get_next_ex(&pool
->handles
, &pos
)) {
181 handles
[i
++] = *handle
;
184 /* should never happen */
186 zend_error(E_ERROR
, "number of fetched request handles do not match overall count");
190 for (i
= 0; i
< count
; ++i
) {
191 http_request_pool_detach(pool
, handles
[i
]);
196 #if HTTP_DEBUG_REQPOOLS
197 fprintf(stderr
, "Destroying %d request bodies of pool %p\n", zend_llist_count(&pool
->bodies
), pool
);
200 /* free created bodies too */
201 zend_llist_clean(&pool
->bodies
);
204 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
205 PHP_HTTP_API STATUS
_http_request_pool_send(http_request_pool
*pool TSRMLS_DC
)
207 #if HTTP_DEBUG_REQPOOLS
208 fprintf(stderr
, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool
->handles
), pool
);
211 while (http_request_pool_perform(pool
)) {
212 if (SUCCESS
!= http_request_pool_select(pool
)) {
214 http_error(HE_WARNING
, HTTP_E_SOCKET
, WSAGetLastError());
216 http_error(HE_WARNING
, HTTP_E_SOCKET
, strerror(errno
));
222 #if HTTP_DEBUG_REQPOOLS
223 fprintf(stderr
, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool
->handles
), pool
, pool
->unfinished
);
230 /* {{{ void http_request_pool_dtor(http_request_pool *) */
231 PHP_HTTP_API
void _http_request_pool_dtor(http_request_pool
*pool TSRMLS_DC
)
233 #if HTTP_DEBUG_REQPOOLS
234 fprintf(stderr
, "Destructing request pool %p\n", pool
);
237 pool
->unfinished
= 0;
238 zend_llist_clean(&pool
->finished
);
239 zend_llist_clean(&pool
->handles
);
240 zend_llist_clean(&pool
->bodies
);
241 curl_multi_cleanup(pool
->ch
);
245 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
246 PHP_HTTP_API STATUS
_http_request_pool_select(http_request_pool
*pool
)
250 struct timeval timeout
= {1, 0};
256 curl_multi_fdset(pool
->ch
, &R
, &W
, &E
, &MAX
);
257 return (-1 != select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) ? SUCCESS
: FAILURE
;
261 /* {{{ int http_request_pool_perform(http_request_pool *) */
262 PHP_HTTP_API
int _http_request_pool_perform(http_request_pool
*pool TSRMLS_DC
)
267 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(pool
->ch
, &pool
->unfinished
));
269 while (msg
= curl_multi_info_read(pool
->ch
, &remaining
)) {
270 if (CURLMSG_DONE
== msg
->msg
) {
272 #if HTTP_DEBUG_REQPOOLS
273 fprintf(stderr
, "Done CURL handle: %p (remaining: %d)\n", msg
->easy_handle
, remaining
);
276 zend_llist_apply_with_argument(&pool
->handles
, (llist_apply_with_arg_func_t
) http_request_pool_responsehandler
, msg
->easy_handle TSRMLS_CC
);
280 return pool
->unfinished
;
284 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
285 STATUS
_http_request_pool_requesthandler(zval
*request
, http_request_body
*body TSRMLS_DC
)
287 getObjectEx(http_request_object
, req
, request
);
288 if (SUCCESS
== http_request_object_requesthandler(req
, request
, body
)) {
289 http_request_conv(req
->ch
, &req
->response
, &req
->request
);
296 /* {{{ void http_request_pool_responsehandler(zval **) */
297 void _http_request_pool_responsehandler(zval
**req
, CURL
*ch TSRMLS_DC
)
299 getObjectEx(http_request_object
, obj
, *req
);
303 #if HTTP_DEBUG_REQPOOLS
304 fprintf(stderr
, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req
), obj
, obj
->pool
);
308 zend_objects_store_add_ref(*req TSRMLS_CC
);
309 zend_llist_add_element(&obj
->pool
->finished
, req
);
310 http_request_object_responsehandler(obj
, *req
);
317 /* {{{ static void http_request_pool_freebody(http_request_ctx **) */
318 static void http_request_pool_freebody(http_request_callback_ctx
**body
)
320 HTTP_REQUEST_CALLBACK_DATA(*body
, http_request_body
*, b
);
321 http_request_body_free(b
);
326 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
327 static int http_request_pool_compare_handles(void *h1
, void *h2
)
329 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
333 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
341 * vim600: noet sw=4 ts=4 fdm=marker
342 * vim<600: noet sw=4 ts=4