- print doesn't like commas
[m6w6/ext-http] / http_request_pool_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
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 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
24
25 #include "php_http.h"
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"
32
33 #ifndef HTTP_DEBUG_REQPOOLS
34 # define HTTP_DEBUG_REQPOOLS 0
35 #endif
36
37 ZEND_EXTERN_MODULE_GLOBALS(http);
38
39 #ifndef HAVE_CURL_MULTI_STRERROR
40 # define curl_multi_strerror(dummy) "unknown error"
41 #endif
42
43 static void http_request_pool_freebody(http_request_callback_ctx **body);
44 static int http_request_pool_compare_handles(void *h1, void *h2);
45
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)
48 {
49 zend_bool free_pool;
50 #if HTTP_DEBUG_REQPOOLS
51 fprintf(stderr, "Initializing request pool %p\n", pool);
52 #endif
53 if ((free_pool = (!pool))) {
54 pool = emalloc(sizeof(http_request_pool));
55 pool->ch = NULL;
56 }
57
58 if (!pool->ch) {
59 if (!(pool->ch = curl_multi_init())) {
60 http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initialize curl");
61 if (free_pool) {
62 efree(pool);
63 }
64 return NULL;
65 }
66 }
67
68 pool->unfinished = 0;
69 zend_llist_init(&pool->handles, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
70 zend_llist_init(&pool->bodies, sizeof(http_request_callback_ctx *), (llist_dtor_func_t) http_request_pool_freebody, 0);
71 #if HTTP_DEBUG_REQPOOLS
72 fprintf(stderr, "Initialized request pool %p\n", pool);
73 #endif
74 return pool;
75 }
76 /* }}} */
77
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)
80 {
81 getObjectEx(http_request_object, req, request);
82 #if HTTP_DEBUG_REQPOOLS
83 fprintf(stderr, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
84 #endif
85 if (req->pool) {
86 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");
87 } else {
88 http_request_callback_ctx *body = http_request_callback_data_ex(http_request_body_new(), 0);
89
90 if (SUCCESS != http_request_pool_requesthandler(request, body->data)) {
91 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
92 } else {
93 CURLMcode code = curl_multi_add_handle(pool->ch, req->ch);
94
95 if ((CURLM_OK != code) && (CURLM_CALL_MULTI_PERFORM != code)) {
96 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code));
97 } else {
98 req->pool = pool;
99
100 zend_llist_add_element(&pool->handles, &request);
101 zend_llist_add_element(&pool->bodies, &body);
102
103 zval_add_ref(&request);
104 zend_objects_store_add_ref(request TSRMLS_CC);
105
106 #if HTTP_DEBUG_REQPOOLS
107 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
108 #endif
109 return SUCCESS;
110 }
111 }
112 efree(body->data);
113 efree(body);
114 }
115 return FAILURE;
116 }
117 /* }}} */
118
119 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
120 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request TSRMLS_DC)
121 {
122 getObjectEx(http_request_object, req, request);
123 #if HTTP_DEBUG_REQPOOLS
124 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
125 #endif
126 if (!req->pool) {
127 /* not attached to any pool */
128 #if HTTP_DEBUG_REQPOOLS
129 fprintf(stderr, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request), req);
130 #endif
131 } else if (req->pool != pool) {
132 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
133 } else {
134 CURLMcode code;
135
136 req->pool = NULL;
137 zend_llist_del_element(&pool->handles, request, http_request_pool_compare_handles);
138 #if HTTP_DEBUG_REQPOOLS
139 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
140 #endif
141 if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->ch))) {
142 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code));
143 } else {
144 return SUCCESS;
145 }
146 }
147 return FAILURE;
148 }
149 /* }}} */
150
151 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
152 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
153 {
154 int count = zend_llist_count(&pool->handles);
155 #if HTTP_DEBUG_REQPOOLS
156 fprintf(stderr, "Detaching %d requests from pool %p\n", count, pool);
157 #endif
158 /*
159 * we cannot apply a function to the llist which actually detaches
160 * the curl handle *and* removes the llist element --
161 * so let's get our hands dirty
162 */
163 if (count) {
164 int i = 0;
165 zend_llist_position pos;
166 zval **handle, **handles = emalloc(count * sizeof(zval *));
167
168 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
169 handles[i++] = *handle;
170 }
171 /* should never happen */
172 if (i != count) {
173 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
174 count = i;
175 }
176 for (i = 0; i < count; ++i) {
177 http_request_pool_detach(pool, handles[i]);
178 }
179 efree(handles);
180 }
181 #if HTTP_DEBUG_REQPOOLS
182 fprintf(stderr, "Destroying %d request bodies of pool %p\n", zend_llist_count(&pool->bodies), pool);
183 #endif
184 /* free created bodies too */
185 zend_llist_clean(&pool->bodies);
186 }
187
188 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
189 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
190 {
191 #if HTTP_DEBUG_REQPOOLS
192 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
193 #endif
194 while (http_request_pool_perform(pool)) {
195 if (SUCCESS != http_request_pool_select(pool)) {
196 #ifdef PHP_WIN32
197 http_error(HE_WARNING, HTTP_E_SOCKET, WSAGetLastError());
198 #else
199 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
200 #endif
201 return FAILURE;
202 }
203 }
204 #if HTTP_DEBUG_REQPOOLS
205 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
206 {
207 int remaining = 0;
208 CURLMsg *msg;
209 /*
210 * FIXXME: populate --somehow
211 */
212 do {
213 if (msg = curl_multi_info_read(pool->ch, &remaining))
214 fprintf(stderr, "CURL: %s (%d)\n", curl_easy_strerror(msg->data.result), msg->data.result);
215 } while (remaining);
216 }
217 #endif
218 zend_llist_apply(&pool->handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
219 return SUCCESS;
220 }
221 /* }}} */
222
223 /* {{{ void http_request_pool_dtor(http_request_pool *) */
224 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
225 {
226 #if HTTP_DEBUG_REQPOOLS
227 fprintf(stderr, "Destructing request pool %p\n", pool);
228 #endif
229 pool->unfinished = 0;
230 zend_llist_clean(&pool->handles);
231 zend_llist_clean(&pool->bodies);
232 curl_multi_cleanup(pool->ch);
233 }
234 /* }}} */
235
236 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
237 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
238 {
239 int MAX;
240 fd_set R, W, E;
241 struct timeval timeout = {1, 0};
242
243 FD_ZERO(&R);
244 FD_ZERO(&W);
245 FD_ZERO(&E);
246
247 curl_multi_fdset(pool->ch, &R, &W, &E, &MAX);
248 return (-1 != select(MAX + 1, &R, &W, &E, &timeout)) ? SUCCESS : FAILURE;
249 }
250 /* }}} */
251
252 /* {{{ int http_request_pool_perform(http_request_pool *) */
253 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool)
254 {
255 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
256 return pool->unfinished;
257 }
258 /* }}} */
259
260 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
261 STATUS _http_request_pool_requesthandler(zval *request, http_request_body *body TSRMLS_DC)
262 {
263 getObjectEx(http_request_object, req, request);
264 if (SUCCESS == http_request_object_requesthandler(req, request, body)) {
265 http_request_conv(req->ch, &req->response, &req->request);
266 return SUCCESS;
267 }
268 return FAILURE;
269 }
270 /* }}} */
271
272 /* {{{ void http_request_pool_responsehandler(zval **) */
273 void _http_request_pool_responsehandler(zval **req TSRMLS_DC)
274 {
275 getObjectEx(http_request_object, obj, *req);
276 #if HTTP_DEBUG_REQPOOLS
277 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req), obj, obj->pool);
278 #endif
279 http_request_object_responsehandler(obj, *req);
280 }
281 /* }}} */
282
283 /*#*/
284
285 /* {{{ static void http_request_pool_freebody(http_request_ctx **) */
286 static void http_request_pool_freebody(http_request_callback_ctx **body)
287 {
288 HTTP_REQUEST_CALLBACK_DATA(*body, http_request_body *, b);
289 http_request_body_free(b);
290 efree(*body);
291 }
292 /* }}} */
293
294 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
295 static int http_request_pool_compare_handles(void *h1, void *h2)
296 {
297 int match = (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
298 #if HTTP_DEBUG_REQPOOLS
299 /* if(match) fprintf(stderr, "OK\n"); */
300 #endif
301 return match;
302 }
303 /* }}} */
304
305 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
306
307
308 /*
309 * Local variables:
310 * tab-width: 4
311 * c-basic-offset: 4
312 * End:
313 * vim600: noet sw=4 ts=4 fdm=marker
314 * vim<600: noet sw=4 ts=4
315 */
316