- we need a bit more space for the footer too in http_gzencode()
[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 HTTP_CHECK_CURL_INIT(pool->ch, curl_multi_init(), ;);
59 if (!pool->ch) {
60 if (free_pool) {
61 efree(pool);
62 }
63 return NULL;
64 }
65
66 pool->unfinished = 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);
69 #if HTTP_DEBUG_REQPOOLS
70 fprintf(stderr, "Initialized request pool %p\n", pool);
71 #endif
72 return pool;
73 }
74 /* }}} */
75
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)
78 {
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);
82 #endif
83 if (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");
85 } else {
86 http_request_callback_ctx *body = http_request_callback_data_ex(http_request_body_new(), 0);
87
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");
90 } else {
91 CURLMcode code = curl_multi_add_handle(pool->ch, req->ch);
92
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));
95 } else {
96 req->pool = pool;
97
98 zend_llist_add_element(&pool->handles, &request);
99 zend_llist_add_element(&pool->bodies, &body);
100
101 zval_add_ref(&request);
102 zend_objects_store_add_ref(request TSRMLS_CC);
103
104 #if HTTP_DEBUG_REQPOOLS
105 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
106 #endif
107 return SUCCESS;
108 }
109 }
110 efree(body->data);
111 efree(body);
112 }
113 return FAILURE;
114 }
115 /* }}} */
116
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)
119 {
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);
123 #endif
124 if (!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);
128 #endif
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));
131 } else {
132 CURLMcode code;
133
134 req->pool = NULL;
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);
138 #endif
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));
141 } else {
142 return SUCCESS;
143 }
144 }
145 return FAILURE;
146 }
147 /* }}} */
148
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)
151 {
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);
155 #endif
156 /*
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
160 */
161 if (count) {
162 int i = 0;
163 zend_llist_position pos;
164 zval **handle, **handles = emalloc(count * sizeof(zval *));
165
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;
168 }
169 /* should never happen */
170 if (i != count) {
171 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
172 count = i;
173 }
174 for (i = 0; i < count; ++i) {
175 http_request_pool_detach(pool, handles[i]);
176 }
177 efree(handles);
178 }
179 #if HTTP_DEBUG_REQPOOLS
180 fprintf(stderr, "Destroying %d request bodies of pool %p\n", zend_llist_count(&pool->bodies), pool);
181 #endif
182 /* free created bodies too */
183 zend_llist_clean(&pool->bodies);
184 }
185
186 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
187 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
188 {
189 #if HTTP_DEBUG_REQPOOLS
190 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
191 #endif
192 while (http_request_pool_perform(pool)) {
193 if (SUCCESS != http_request_pool_select(pool)) {
194 #ifdef PHP_WIN32
195 http_error(HE_WARNING, HTTP_E_SOCKET, WSAGetLastError());
196 #else
197 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
198 #endif
199 return FAILURE;
200 }
201 }
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);
204 {
205 int remaining = 0;
206 CURLMsg *msg;
207 /*
208 * FIXXME: populate --somehow
209 */
210 do {
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);
213 } while (remaining);
214 }
215 #endif
216 zend_llist_apply(&pool->handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
217 return SUCCESS;
218 }
219 /* }}} */
220
221 /* {{{ void http_request_pool_dtor(http_request_pool *) */
222 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
223 {
224 #if HTTP_DEBUG_REQPOOLS
225 fprintf(stderr, "Destructing request pool %p\n", pool);
226 #endif
227 pool->unfinished = 0;
228 zend_llist_clean(&pool->handles);
229 zend_llist_clean(&pool->bodies);
230 curl_multi_cleanup(pool->ch);
231 }
232 /* }}} */
233
234 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
235 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
236 {
237 int MAX;
238 fd_set R, W, E;
239 struct timeval timeout = {1, 0};
240
241 FD_ZERO(&R);
242 FD_ZERO(&W);
243 FD_ZERO(&E);
244
245 curl_multi_fdset(pool->ch, &R, &W, &E, &MAX);
246 return (-1 != select(MAX + 1, &R, &W, &E, &timeout)) ? SUCCESS : FAILURE;
247 }
248 /* }}} */
249
250 /* {{{ int http_request_pool_perform(http_request_pool *) */
251 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool)
252 {
253 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
254 return pool->unfinished;
255 }
256 /* }}} */
257
258 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
259 STATUS _http_request_pool_requesthandler(zval *request, http_request_body *body TSRMLS_DC)
260 {
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);
264 return SUCCESS;
265 }
266 return FAILURE;
267 }
268 /* }}} */
269
270 /* {{{ void http_request_pool_responsehandler(zval **) */
271 void _http_request_pool_responsehandler(zval **req TSRMLS_DC)
272 {
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);
276 #endif
277 http_request_object_responsehandler(obj, *req);
278 }
279 /* }}} */
280
281 /*#*/
282
283 /* {{{ static void http_request_pool_freebody(http_request_ctx **) */
284 static void http_request_pool_freebody(http_request_callback_ctx **body)
285 {
286 HTTP_REQUEST_CALLBACK_DATA(*body, http_request_body *, b);
287 http_request_body_free(b);
288 efree(*body);
289 }
290 /* }}} */
291
292 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
293 static int http_request_pool_compare_handles(void *h1, void *h2)
294 {
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"); */
298 #endif
299 return match;
300 }
301 /* }}} */
302
303 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
304
305
306 /*
307 * Local variables:
308 * tab-width: 4
309 * c-basic-offset: 4
310 * End:
311 * vim600: noet sw=4 ts=4 fdm=marker
312 * vim<600: noet sw=4 ts=4
313 */
314