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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
20 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
23 #include "php_http_std_defs.h"
24 #include "php_http_api.h"
25 #include "php_http_request_api.h"
26 #include "php_http_request_pool_api.h"
27 #include "php_http_request_object.h"
28 #include "php_http_requestpool_object.h"
30 #ifndef HTTP_DEBUG_REQPOOLS
31 # define HTTP_DEBUG_REQPOOLS 0
34 ZEND_EXTERN_MODULE_GLOBALS(http
);
36 #ifndef HAVE_CURL_MULTI_STRERROR
37 # define curl_multi_strerror(dummy) "unknown error"
40 static void http_request_pool_freebody(http_request_callback_ctx
**body
);
41 static int http_request_pool_compare_handles(void *h1
, void *h2
);
43 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
44 PHP_HTTP_API http_request_pool
*_http_request_pool_init(http_request_pool
*pool TSRMLS_DC
)
48 #if HTTP_DEBUG_REQPOOLS
49 fprintf(stderr
, "Initializing request pool %p\n", pool
);
52 if ((free_pool
= (!pool
))) {
53 pool
= emalloc(sizeof(http_request_pool
));
57 HTTP_CHECK_CURL_INIT(pool
->ch
, curl_multi_init(), ;);
66 zend_llist_init(&pool
->finished
, sizeof(zval
*), (llist_dtor_func_t
) ZVAL_PTR_DTOR
, 0);
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);
70 #if HTTP_DEBUG_REQPOOLS
71 fprintf(stderr
, "Initialized request pool %p\n", pool
);
78 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
79 PHP_HTTP_API STATUS
_http_request_pool_attach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
81 getObjectEx(http_request_object
, req
, request
);
83 #if HTTP_DEBUG_REQPOOLS
84 fprintf(stderr
, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request
), req
, pool
);
88 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");
90 http_request_callback_ctx
*body
= http_request_callback_data_ex(http_request_body_new(), 0);
92 if (SUCCESS
!= http_request_pool_requesthandler(request
, body
->data
)) {
93 http_error_ex(HE_WARNING
, HTTP_E_REQUEST
, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
95 CURLMcode code
= curl_multi_add_handle(pool
->ch
, req
->ch
);
97 if ((CURLM_OK
!= code
) && (CURLM_CALL_MULTI_PERFORM
!= code
)) {
98 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_POOL
, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code
));
102 zend_llist_add_element(&pool
->handles
, &request
);
103 zend_llist_add_element(&pool
->bodies
, &body
);
105 ZVAL_ADDREF(request
);
106 Z_OBJ_ADDREF_P(request
);
108 #if HTTP_DEBUG_REQPOOLS
109 fprintf(stderr
, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool
->handles
), pool
);
121 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
122 PHP_HTTP_API STATUS
_http_request_pool_detach(http_request_pool
*pool
, zval
*request TSRMLS_DC
)
124 getObjectEx(http_request_object
, req
, request
);
126 #if HTTP_DEBUG_REQPOOLS
127 fprintf(stderr
, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request
), req
, pool
);
131 /* not attached to any pool */
132 #if HTTP_DEBUG_REQPOOLS
133 fprintf(stderr
, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request
), req
);
135 } else if (req
->pool
!= pool
) {
136 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request
));
141 zend_llist_del_element(&pool
->handles
, request
, http_request_pool_compare_handles
);
142 zend_llist_del_element(&pool
->finished
, request
, http_request_pool_compare_handles
);
144 #if HTTP_DEBUG_REQPOOLS
145 fprintf(stderr
, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool
->handles
), pool
);
148 if (CURLM_OK
!= (code
= curl_multi_remove_handle(pool
->ch
, req
->ch
))) {
149 http_error_ex(HE_WARNING
, HTTP_E_REQUEST_POOL
, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code
));
158 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
159 PHP_HTTP_API
void _http_request_pool_detach_all(http_request_pool
*pool TSRMLS_DC
)
161 int count
= zend_llist_count(&pool
->handles
);
163 #if HTTP_DEBUG_REQPOOLS
164 fprintf(stderr
, "Detaching %d requests from pool %p\n", count
, pool
);
168 * we cannot apply a function to the llist which actually detaches
169 * the curl handle *and* removes the llist element --
170 * so let's get our hands dirty
174 zend_llist_position pos
;
175 zval
**handle
, **handles
= emalloc(count
* sizeof(zval
*));
177 for (handle
= zend_llist_get_first_ex(&pool
->handles
, &pos
); handle
; handle
= zend_llist_get_next_ex(&pool
->handles
, &pos
)) {
178 handles
[i
++] = *handle
;
181 /* should never happen */
183 zend_error(E_ERROR
, "number of fetched request handles do not match overall count");
187 for (i
= 0; i
< count
; ++i
) {
188 http_request_pool_detach(pool
, handles
[i
]);
193 #if HTTP_DEBUG_REQPOOLS
194 fprintf(stderr
, "Destroying %d request bodies of pool %p\n", zend_llist_count(&pool
->bodies
), pool
);
197 /* free created bodies too */
198 zend_llist_clean(&pool
->bodies
);
201 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
202 PHP_HTTP_API STATUS
_http_request_pool_send(http_request_pool
*pool TSRMLS_DC
)
204 #if HTTP_DEBUG_REQPOOLS
205 fprintf(stderr
, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool
->handles
), pool
);
208 while (http_request_pool_perform(pool
)) {
209 if (SUCCESS
!= http_request_pool_select(pool
)) {
211 http_error(HE_WARNING
, HTTP_E_SOCKET
, WSAGetLastError());
213 http_error(HE_WARNING
, HTTP_E_SOCKET
, strerror(errno
));
219 #if HTTP_DEBUG_REQPOOLS
220 fprintf(stderr
, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool
->handles
), pool
, pool
->unfinished
);
227 /* {{{ void http_request_pool_dtor(http_request_pool *) */
228 PHP_HTTP_API
void _http_request_pool_dtor(http_request_pool
*pool TSRMLS_DC
)
230 #if HTTP_DEBUG_REQPOOLS
231 fprintf(stderr
, "Destructing request pool %p\n", pool
);
234 pool
->unfinished
= 0;
235 zend_llist_clean(&pool
->finished
);
236 zend_llist_clean(&pool
->handles
);
237 zend_llist_clean(&pool
->bodies
);
238 curl_multi_cleanup(pool
->ch
);
242 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
243 PHP_HTTP_API STATUS
_http_request_pool_select(http_request_pool
*pool
)
247 struct timeval timeout
= {1, 0};
253 curl_multi_fdset(pool
->ch
, &R
, &W
, &E
, &MAX
);
254 return (-1 != select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) ? SUCCESS
: FAILURE
;
258 /* {{{ int http_request_pool_perform(http_request_pool *) */
259 PHP_HTTP_API
int _http_request_pool_perform(http_request_pool
*pool TSRMLS_DC
)
264 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(pool
->ch
, &pool
->unfinished
));
266 while (msg
= curl_multi_info_read(pool
->ch
, &remaining
)) {
267 if (CURLMSG_DONE
== msg
->msg
) {
269 #if HTTP_DEBUG_REQPOOLS
270 fprintf(stderr
, "Done CURL handle: %p (remaining: %d)\n", msg
->easy_handle
, remaining
);
273 zend_llist_apply_with_argument(&pool
->handles
, (llist_apply_with_arg_func_t
) http_request_pool_responsehandler
, msg
->easy_handle TSRMLS_CC
);
277 return pool
->unfinished
;
281 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
282 STATUS
_http_request_pool_requesthandler(zval
*request
, http_request_body
*body TSRMLS_DC
)
284 getObjectEx(http_request_object
, req
, request
);
285 if (SUCCESS
== http_request_object_requesthandler(req
, request
, body
)) {
286 http_request_conv(req
->ch
, &req
->response
, &req
->request
);
293 /* {{{ void http_request_pool_responsehandler(zval **) */
294 void _http_request_pool_responsehandler(zval
**req
, CURL
*ch TSRMLS_DC
)
296 getObjectEx(http_request_object
, obj
, *req
);
300 #if HTTP_DEBUG_REQPOOLS
301 fprintf(stderr
, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req
), obj
, obj
->pool
);
305 Z_OBJ_ADDREF_PP(req
);
306 zend_llist_add_element(&obj
->pool
->finished
, req
);
307 http_request_object_responsehandler(obj
, *req
);
314 /* {{{ static void http_request_pool_freebody(http_request_ctx **) */
315 static void http_request_pool_freebody(http_request_callback_ctx
**body
)
317 HTTP_REQUEST_CALLBACK_DATA(*body
, http_request_body
*, b
);
318 http_request_body_free(b
);
323 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
324 static int http_request_pool_compare_handles(void *h1
, void *h2
)
326 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
330 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
338 * vim600: noet sw=4 ts=4 fdm=marker
339 * vim<600: noet sw=4 ts=4