d8aae7c004f8c924a7bfb962ec7855f49cad760a
[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
51 #if HTTP_DEBUG_REQPOOLS
52 fprintf(stderr, "Initializing request pool %p\n", pool);
53 #endif
54
55 if ((free_pool = (!pool))) {
56 pool = emalloc(sizeof(http_request_pool));
57 pool->ch = NULL;
58 }
59
60 HTTP_CHECK_CURL_INIT(pool->ch, curl_multi_init(), ;);
61 if (!pool->ch) {
62 if (free_pool) {
63 efree(pool);
64 }
65 return NULL;
66 }
67
68 pool->unfinished = 0;
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);
72
73 #if HTTP_DEBUG_REQPOOLS
74 fprintf(stderr, "Initialized request pool %p\n", pool);
75 #endif
76
77 return pool;
78 }
79 /* }}} */
80
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)
83 {
84 getObjectEx(http_request_object, req, request);
85
86 #if HTTP_DEBUG_REQPOOLS
87 fprintf(stderr, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
88 #endif
89
90 if (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");
92 } else {
93 http_request_callback_ctx *body = http_request_callback_data_ex(http_request_body_new(), 0);
94
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");
97 } else {
98 CURLMcode code = curl_multi_add_handle(pool->ch, req->ch);
99
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));
102 } else {
103 req->pool = pool;
104
105 zend_llist_add_element(&pool->handles, &request);
106 zend_llist_add_element(&pool->bodies, &body);
107
108 zval_add_ref(&request);
109 Z_OBJ_ADDREF_P(request);
110
111 #if HTTP_DEBUG_REQPOOLS
112 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
113 #endif
114 return SUCCESS;
115 }
116 }
117 efree(body->data);
118 efree(body);
119 }
120 return FAILURE;
121 }
122 /* }}} */
123
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)
126 {
127 getObjectEx(http_request_object, req, request);
128
129 #if HTTP_DEBUG_REQPOOLS
130 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
131 #endif
132
133 if (!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);
137 #endif
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));
140 } else {
141 CURLMcode code;
142
143 req->pool = NULL;
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);
146
147 #if HTTP_DEBUG_REQPOOLS
148 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
149 #endif
150
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));
153 } else {
154 return SUCCESS;
155 }
156 }
157 return FAILURE;
158 }
159 /* }}} */
160
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)
163 {
164 int count = zend_llist_count(&pool->handles);
165
166 #if HTTP_DEBUG_REQPOOLS
167 fprintf(stderr, "Detaching %d requests from pool %p\n", count, pool);
168 #endif
169
170 /*
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
174 */
175 if (count) {
176 int i = 0;
177 zend_llist_position pos;
178 zval **handle, **handles = emalloc(count * sizeof(zval *));
179
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;
182 }
183
184 /* should never happen */
185 if (i != count) {
186 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
187 count = i;
188 }
189
190 for (i = 0; i < count; ++i) {
191 http_request_pool_detach(pool, handles[i]);
192 }
193 efree(handles);
194 }
195
196 #if HTTP_DEBUG_REQPOOLS
197 fprintf(stderr, "Destroying %d request bodies of pool %p\n", zend_llist_count(&pool->bodies), pool);
198 #endif
199
200 /* free created bodies too */
201 zend_llist_clean(&pool->bodies);
202 }
203
204 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
205 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
206 {
207 #if HTTP_DEBUG_REQPOOLS
208 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
209 #endif
210
211 while (http_request_pool_perform(pool)) {
212 if (SUCCESS != http_request_pool_select(pool)) {
213 #ifdef PHP_WIN32
214 http_error(HE_WARNING, HTTP_E_SOCKET, WSAGetLastError());
215 #else
216 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
217 #endif
218 return FAILURE;
219 }
220 }
221
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);
224 #endif
225
226 return SUCCESS;
227 }
228 /* }}} */
229
230 /* {{{ void http_request_pool_dtor(http_request_pool *) */
231 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
232 {
233 #if HTTP_DEBUG_REQPOOLS
234 fprintf(stderr, "Destructing request pool %p\n", pool);
235 #endif
236
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);
242 }
243 /* }}} */
244
245 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
246 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
247 {
248 int MAX;
249 fd_set R, W, E;
250 struct timeval timeout = {1, 0};
251
252 FD_ZERO(&R);
253 FD_ZERO(&W);
254 FD_ZERO(&E);
255
256 curl_multi_fdset(pool->ch, &R, &W, &E, &MAX);
257 return (-1 != select(MAX + 1, &R, &W, &E, &timeout)) ? SUCCESS : FAILURE;
258 }
259 /* }}} */
260
261 /* {{{ int http_request_pool_perform(http_request_pool *) */
262 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool TSRMLS_DC)
263 {
264 CURLMsg *msg;
265 int remaining = 0;
266
267 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
268
269 while (msg = curl_multi_info_read(pool->ch, &remaining)) {
270 if (CURLMSG_DONE == msg->msg) {
271
272 #if HTTP_DEBUG_REQPOOLS
273 fprintf(stderr, "Done CURL handle: %p (remaining: %d)\n", msg->easy_handle, remaining);
274 #endif
275
276 zend_llist_apply_with_argument(&pool->handles, (llist_apply_with_arg_func_t) http_request_pool_responsehandler, msg->easy_handle TSRMLS_CC);
277 }
278 }
279
280 return pool->unfinished;
281 }
282 /* }}} */
283
284 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
285 STATUS _http_request_pool_requesthandler(zval *request, http_request_body *body TSRMLS_DC)
286 {
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);
290 return SUCCESS;
291 }
292 return FAILURE;
293 }
294 /* }}} */
295
296 /* {{{ void http_request_pool_responsehandler(zval **) */
297 void _http_request_pool_responsehandler(zval **req, CURL *ch TSRMLS_DC)
298 {
299 getObjectEx(http_request_object, obj, *req);
300
301 if (obj->ch == ch) {
302
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);
305 #endif
306
307 zval_add_ref(req);
308 Z_OBJ_ADDREF_PP(req);
309 zend_llist_add_element(&obj->pool->finished, req);
310 http_request_object_responsehandler(obj, *req);
311 }
312 }
313 /* }}} */
314
315 /*#*/
316
317 /* {{{ static void http_request_pool_freebody(http_request_ctx **) */
318 static void http_request_pool_freebody(http_request_callback_ctx **body)
319 {
320 HTTP_REQUEST_CALLBACK_DATA(*body, http_request_body *, b);
321 http_request_body_free(b);
322 efree(*body);
323 }
324 /* }}} */
325
326 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
327 static int http_request_pool_compare_handles(void *h1, void *h2)
328 {
329 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
330 }
331 /* }}} */
332
333 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
334
335
336 /*
337 * Local variables:
338 * tab-width: 4
339 * c-basic-offset: 4
340 * End:
341 * vim600: noet sw=4 ts=4 fdm=marker
342 * vim<600: noet sw=4 ts=4
343 */
344