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 static void http_request_pool_freebody(http_request_body
**body
);
40 static int http_request_pool_compare_handles(void *h1
, void *h2
);
42 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
43 PHP_HTTP_API http_request_pool
*_http_request_pool_init(http_request_pool
*pool TSRMLS_DC
)
46 #if HTTP_DEBUG_REQPOOLS
47 fprintf(stderr
, "Initializing request pool %p\n", pool
);
49 if ((free_pool
= (!pool
))) {
50 pool
= emalloc(sizeof(http_request_pool
));
55 if (!(pool
->ch
= curl_multi_init())) {
56 http_error(HE_WARNING
, HTTP_E_REQUEST
, "Could not initialize curl");
65 zend_llist_init(&pool
->handles
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
66 zend_llist_init(&pool
->bodies
, sizeof(http_request_body
*), (llist_dtor_func_t
) http_request_pool_freebody
, 0);
67 #if HTTP_DEBUG_REQPOOLS
68 fprintf(stderr
, "Initialized request pool %p\n", pool
);
74 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
75 PHP_HTTP_API STATUS
_http_request_pool_attach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
77 getObjectEx(http_request_object
, req
, request
);
78 #if HTTP_DEBUG_REQPOOLS
79 fprintf(stderr
, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request
), req
, pool
);
82 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");
84 http_request_body
*body
= http_request_body_new();
86 if (SUCCESS
!= http_request_pool_requesthandler(request
, body
)) {
87 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
89 CURLMcode code
= curl_multi_add_handle(pool
->ch
, req
->ch
);
91 if ((CURLM_OK
!= code
) && (CURLM_CALL_MULTI_PERFORM
!= code
)) {
92 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_POOL
, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code
));
96 zend_llist_add_element(&pool
->handles
, &request
);
97 zend_llist_add_element(&pool
->bodies
, &body
);
99 zval_add_ref(&request
);
100 zend_objects_store_add_ref(request TSRMLS_CC
);
102 #if HTTP_DEBUG_REQPOOLS
103 fprintf(stderr
, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool
->handles
), pool
);
114 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
115 PHP_HTTP_API STATUS
_http_request_pool_detach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
117 getObjectEx(http_request_object
, req
, request
);
118 #if HTTP_DEBUG_REQPOOLS
119 fprintf(stderr
, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request
), req
, pool
);
122 /* not attached to any pool */
123 #if HTTP_DEBUG_REQPOOLS
124 fprintf(stderr
, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request
), req
);
126 } else if (req
->pool
!= pool
) {
127 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request
));
132 zend_llist_del_element(&pool
->handles
, request
, http_request_pool_compare_handles
);
133 #if HTTP_DEBUG_REQPOOLS
134 fprintf(stderr
, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool
->handles
), pool
);
136 if (CURLM_OK
!= (code
= curl_multi_remove_handle(pool
->ch
, req
->ch
))) {
137 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_POOL
, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code
));
146 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
147 PHP_HTTP_API
void _http_request_pool_detach_all(http_request_pool
*pool TSRMLS_DC
)
149 int count
= zend_llist_count(&pool
->handles
);
150 #if HTTP_DEBUG_REQPOOLS
151 fprintf(stderr
, "Detaching %d requests from pool %p\n", count
, pool
);
154 * we cannot apply a function to the llist which actually detaches
155 * the curl handle *and* removes the llist element --
156 * so let's get our hands dirty
160 zend_llist_position pos
;
161 zval
**handle
, **handles
= emalloc(count
* sizeof(zval
*));
163 for (handle
= zend_llist_get_first_ex(&pool
->handles
, &pos
); handle
; handle
= zend_llist_get_next_ex(&pool
->handles
, &pos
)) {
164 handles
[i
++] = *handle
;
166 for (i
= 0; i
< count
; ++i
) {
167 http_request_pool_detach(pool
, handles
[i
]);
174 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
175 PHP_HTTP_API STATUS
_http_request_pool_send(http_request_pool
*pool TSRMLS_DC
)
177 #if HTTP_DEBUG_REQPOOLS
178 fprintf(stderr
, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool
->handles
), pool
);
180 while (http_request_pool_perform(pool
)) {
181 #if HTTP_DEBUG_REQPOOLS
182 fprintf(stderr
, "> %d unfinished requests of pool %p remaining\n", pool
->unfinished
, pool
);
184 if (SUCCESS
!= http_request_pool_select(pool
)) {
185 http_error(HE_WARNING
, HTTP_E_SOCKET
, "Socket error");
189 #if HTTP_DEBUG_REQPOOLS
190 fprintf(stderr
, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool
->handles
), pool
, pool
->unfinished
);
192 zend_llist_apply(&pool
->handles
, (llist_apply_func_t
) http_request_pool_responsehandler TSRMLS_CC
);
197 /* {{{ void http_request_pool_dtor(http_request_pool *) */
198 PHP_HTTP_API
void _http_request_pool_dtor(http_request_pool
*pool TSRMLS_DC
)
200 #if HTTP_DEBUG_REQPOOLS
201 fprintf(stderr
, "Destructing request pool %p\n", pool
);
203 pool
->unfinished
= 0;
204 zend_llist_clean(&pool
->handles
);
205 zend_llist_clean(&pool
->bodies
);
206 curl_multi_cleanup(pool
->ch
);
210 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
211 PHP_HTTP_API STATUS
_http_request_pool_select(http_request_pool
*pool
)
215 struct timeval timeout
= {1, 0};
221 curl_multi_fdset(pool
->ch
, &R
, &W
, &E
, &MAX
);
222 return (-1 != select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) ? SUCCESS
: FAILURE
;
226 /* {{{ int http_request_pool_perform(http_request_pool *) */
227 PHP_HTTP_API
int _http_request_pool_perform(http_request_pool
*pool
)
229 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(pool
->ch
, &pool
->unfinished
));
230 return pool
->unfinished
;
234 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
235 STATUS
_http_request_pool_requesthandler(zval
*request
, http_request_body
*body TSRMLS_DC
)
237 getObjectEx(http_request_object
, req
, request
);
238 if (SUCCESS
== http_request_object_requesthandler(req
, request
, body
)) {
239 http_request_conv(req
->ch
, &req
->response
, &req
->request
);
246 /* {{{ void http_request_pool_responsehandler(zval **) */
247 void _http_request_pool_responsehandler(zval
**req TSRMLS_DC
)
249 getObjectEx(http_request_object
, obj
, *req
);
250 #if HTTP_DEBUG_REQPOOLS
251 fprintf(stderr
, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req
), obj
, obj
->pool
);
253 http_request_object_responsehandler(obj
, *req
);
259 /* {{{ static void http_request_pool_freebody(http_request_body **) */
260 static void http_request_pool_freebody(http_request_body
**body
)
263 http_request_body_free(*body
);
267 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
268 static int http_request_pool_compare_handles(void *h1
, void *h2
)
270 int match
= (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
271 #if HTTP_DEBUG_REQPOOLS
272 /* if(match) fprintf(stderr, "OK\n"); */
278 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
286 * vim600: noet sw=4 ts=4 fdm=marker
287 * vim<600: noet sw=4 ts=4