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
)
50 #if HTTP_DEBUG_REQPOOLS
51 fprintf(stderr
, "Initializing request pool %p\n", pool
);
53 if ((free_pool
= (!pool
))) {
54 pool
= emalloc(sizeof(http_request_pool
));
58 HTTP_CHECK_CURL_INIT(pool
->ch
, curl_multi_init(), ;);
67 zend_llist_init(&pool
->handles
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
68 zend_llist_init(&pool
->bodies
, sizeof(http_request_callback_ctx
*), (llist_dtor_func_t
) http_request_pool_freebody
, 0);
69 #if HTTP_DEBUG_REQPOOLS
70 fprintf(stderr
, "Initialized request pool %p\n", pool
);
76 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
77 PHP_HTTP_API STATUS
_http_request_pool_attach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
79 getObjectEx(http_request_object
, req
, request
);
80 #if HTTP_DEBUG_REQPOOLS
81 fprintf(stderr
, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request
), req
, pool
);
84 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");
86 http_request_callback_ctx
*body
= http_request_callback_data_ex(http_request_body_new(), 0);
88 if (SUCCESS
!= http_request_pool_requesthandler(request
, body
->data
)) {
89 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
91 CURLMcode code
= curl_multi_add_handle(pool
->ch
, req
->ch
);
93 if ((CURLM_OK
!= code
) && (CURLM_CALL_MULTI_PERFORM
!= code
)) {
94 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_POOL
, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code
));
98 zend_llist_add_element(&pool
->handles
, &request
);
99 zend_llist_add_element(&pool
->bodies
, &body
);
101 zval_add_ref(&request
);
102 zend_objects_store_add_ref(request TSRMLS_CC
);
104 #if HTTP_DEBUG_REQPOOLS
105 fprintf(stderr
, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool
->handles
), pool
);
117 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
118 PHP_HTTP_API STATUS
_http_request_pool_detach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
120 getObjectEx(http_request_object
, req
, request
);
121 #if HTTP_DEBUG_REQPOOLS
122 fprintf(stderr
, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request
), req
, pool
);
125 /* not attached to any pool */
126 #if HTTP_DEBUG_REQPOOLS
127 fprintf(stderr
, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request
), req
);
129 } else if (req
->pool
!= pool
) {
130 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request
));
135 zend_llist_del_element(&pool
->handles
, request
, http_request_pool_compare_handles
);
136 #if HTTP_DEBUG_REQPOOLS
137 fprintf(stderr
, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool
->handles
), pool
);
139 if (CURLM_OK
!= (code
= curl_multi_remove_handle(pool
->ch
, req
->ch
))) {
140 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_POOL
, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code
));
149 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
150 PHP_HTTP_API
void _http_request_pool_detach_all(http_request_pool
*pool TSRMLS_DC
)
152 int count
= zend_llist_count(&pool
->handles
);
153 #if HTTP_DEBUG_REQPOOLS
154 fprintf(stderr
, "Detaching %d requests from pool %p\n", count
, pool
);
157 * we cannot apply a function to the llist which actually detaches
158 * the curl handle *and* removes the llist element --
159 * so let's get our hands dirty
163 zend_llist_position pos
;
164 zval
**handle
, **handles
= emalloc(count
* sizeof(zval
*));
166 for (handle
= zend_llist_get_first_ex(&pool
->handles
, &pos
); handle
; handle
= zend_llist_get_next_ex(&pool
->handles
, &pos
)) {
167 handles
[i
++] = *handle
;
169 /* should never happen */
171 zend_error(E_ERROR
, "number of fetched request handles do not match overall count");
174 for (i
= 0; i
< count
; ++i
) {
175 http_request_pool_detach(pool
, handles
[i
]);
179 #if HTTP_DEBUG_REQPOOLS
180 fprintf(stderr
, "Destroying %d request bodies of pool %p\n", zend_llist_count(&pool
->bodies
), pool
);
182 /* free created bodies too */
183 zend_llist_clean(&pool
->bodies
);
186 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
187 PHP_HTTP_API STATUS
_http_request_pool_send(http_request_pool
*pool TSRMLS_DC
)
189 #if HTTP_DEBUG_REQPOOLS
190 fprintf(stderr
, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool
->handles
), pool
);
192 while (http_request_pool_perform(pool
)) {
193 if (SUCCESS
!= http_request_pool_select(pool
)) {
195 http_error(HE_WARNING
, HTTP_E_SOCKET
, WSAGetLastError());
197 http_error(HE_WARNING
, HTTP_E_SOCKET
, strerror(errno
));
202 #if HTTP_DEBUG_REQPOOLS
203 fprintf(stderr
, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool
->handles
), pool
, pool
->unfinished
);
208 * FIXXME: populate --somehow
211 if (msg
= curl_multi_info_read(pool
->ch
, &remaining
))
212 fprintf(stderr
, "CURL: %s (%d)\n", curl_easy_strerror(msg
->data
.result
), msg
->data
.result
);
216 zend_llist_apply(&pool
->handles
, (llist_apply_func_t
) http_request_pool_responsehandler TSRMLS_CC
);
221 /* {{{ void http_request_pool_dtor(http_request_pool *) */
222 PHP_HTTP_API
void _http_request_pool_dtor(http_request_pool
*pool TSRMLS_DC
)
224 #if HTTP_DEBUG_REQPOOLS
225 fprintf(stderr
, "Destructing request pool %p\n", pool
);
227 pool
->unfinished
= 0;
228 zend_llist_clean(&pool
->handles
);
229 zend_llist_clean(&pool
->bodies
);
230 curl_multi_cleanup(pool
->ch
);
234 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
235 PHP_HTTP_API STATUS
_http_request_pool_select(http_request_pool
*pool
)
239 struct timeval timeout
= {1, 0};
245 curl_multi_fdset(pool
->ch
, &R
, &W
, &E
, &MAX
);
246 return (-1 != select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) ? SUCCESS
: FAILURE
;
250 /* {{{ int http_request_pool_perform(http_request_pool *) */
251 PHP_HTTP_API
int _http_request_pool_perform(http_request_pool
*pool
)
253 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(pool
->ch
, &pool
->unfinished
));
254 return pool
->unfinished
;
258 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
259 STATUS
_http_request_pool_requesthandler(zval
*request
, http_request_body
*body TSRMLS_DC
)
261 getObjectEx(http_request_object
, req
, request
);
262 if (SUCCESS
== http_request_object_requesthandler(req
, request
, body
)) {
263 http_request_conv(req
->ch
, &req
->response
, &req
->request
);
270 /* {{{ void http_request_pool_responsehandler(zval **) */
271 void _http_request_pool_responsehandler(zval
**req TSRMLS_DC
)
273 getObjectEx(http_request_object
, obj
, *req
);
274 #if HTTP_DEBUG_REQPOOLS
275 fprintf(stderr
, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req
), obj
, obj
->pool
);
277 http_request_object_responsehandler(obj
, *req
);
283 /* {{{ static void http_request_pool_freebody(http_request_ctx **) */
284 static void http_request_pool_freebody(http_request_callback_ctx
**body
)
286 HTTP_REQUEST_CALLBACK_DATA(*body
, http_request_body
*, b
);
287 http_request_body_free(b
);
292 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
293 static int http_request_pool_compare_handles(void *h1
, void *h2
)
295 int match
= (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
296 #if HTTP_DEBUG_REQPOOLS
297 /* if(match) fprintf(stderr, "OK\n"); */
303 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
311 * vim600: noet sw=4 ts=4 fdm=marker
312 * vim<600: noet sw=4 ts=4