282fda9ed2df25b2becac2c64ccd09e617642fb0
[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_body **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_body *), (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_body *body = http_request_body_new();
89
90 if (SUCCESS != http_request_pool_requesthandler(request, body)) {
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);
113 }
114 return FAILURE;
115 }
116 /* }}} */
117
118 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
119 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request TSRMLS_DC)
120 {
121 getObjectEx(http_request_object, req, request);
122 #if HTTP_DEBUG_REQPOOLS
123 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
124 #endif
125 if (!req->pool) {
126 /* not attached to any pool */
127 #if HTTP_DEBUG_REQPOOLS
128 fprintf(stderr, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request), req);
129 #endif
130 } else if (req->pool != pool) {
131 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
132 } else {
133 CURLMcode code;
134
135 req->pool = NULL;
136 zend_llist_del_element(&pool->handles, request, http_request_pool_compare_handles);
137 #if HTTP_DEBUG_REQPOOLS
138 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
139 #endif
140 if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->ch))) {
141 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code));
142 } else {
143 return SUCCESS;
144 }
145 }
146 return FAILURE;
147 }
148 /* }}} */
149
150 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
151 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
152 {
153 int count = zend_llist_count(&pool->handles);
154 #if HTTP_DEBUG_REQPOOLS
155 fprintf(stderr, "Detaching %d requests from pool %p\n", count, pool);
156 #endif
157 /*
158 * we cannot apply a function to the llist which actually detaches
159 * the curl handle *and* removes the llist element --
160 * so let's get our hands dirty
161 */
162 if (count) {
163 int i = 0;
164 zend_llist_position pos;
165 zval **handle, **handles = emalloc(count * sizeof(zval *));
166
167 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
168 handles[i++] = *handle;
169 }
170 for (i = 0; i < count; ++i) {
171 http_request_pool_detach(pool, handles[i]);
172 }
173 efree(handles);
174 }
175 }
176
177
178 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
179 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
180 {
181 #if HTTP_DEBUG_REQPOOLS
182 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
183 #endif
184 while (http_request_pool_perform(pool)) {
185 #if HTTP_DEBUG_REQPOOLS
186 fprintf(stderr, "> %d unfinished requests of pool %p remaining\n", pool->unfinished, pool);
187 #endif
188 if (SUCCESS != http_request_pool_select(pool)) {
189 #ifdef PHP_WIN32
190 http_error(HE_WARNING, HTTP_E_SOCKET, WSAGetLastError());
191 #else
192 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
193 #endif
194 return FAILURE;
195 }
196 }
197 #if HTTP_DEBUG_REQPOOLS
198 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
199 #endif
200 zend_llist_apply(&pool->handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
201 return SUCCESS;
202 }
203 /* }}} */
204
205 /* {{{ void http_request_pool_dtor(http_request_pool *) */
206 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
207 {
208 #if HTTP_DEBUG_REQPOOLS
209 fprintf(stderr, "Destructing request pool %p\n", pool);
210 #endif
211 pool->unfinished = 0;
212 zend_llist_clean(&pool->handles);
213 zend_llist_clean(&pool->bodies);
214 curl_multi_cleanup(pool->ch);
215 }
216 /* }}} */
217
218 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
219 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
220 {
221 int MAX;
222 fd_set R, W, E;
223 struct timeval timeout = {1, 0};
224
225 FD_ZERO(&R);
226 FD_ZERO(&W);
227 FD_ZERO(&E);
228
229 curl_multi_fdset(pool->ch, &R, &W, &E, &MAX);
230 return (-1 != select(MAX + 1, &R, &W, &E, &timeout)) ? SUCCESS : FAILURE;
231 }
232 /* }}} */
233
234 /* {{{ int http_request_pool_perform(http_request_pool *) */
235 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool)
236 {
237 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
238 return pool->unfinished;
239 }
240 /* }}} */
241
242 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
243 STATUS _http_request_pool_requesthandler(zval *request, http_request_body *body TSRMLS_DC)
244 {
245 getObjectEx(http_request_object, req, request);
246 if (SUCCESS == http_request_object_requesthandler(req, request, body)) {
247 http_request_conv(req->ch, &req->response, &req->request);
248 return SUCCESS;
249 }
250 return FAILURE;
251 }
252 /* }}} */
253
254 /* {{{ void http_request_pool_responsehandler(zval **) */
255 void _http_request_pool_responsehandler(zval **req TSRMLS_DC)
256 {
257 getObjectEx(http_request_object, obj, *req);
258 #if HTTP_DEBUG_REQPOOLS
259 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req), obj, obj->pool);
260 #endif
261 http_request_object_responsehandler(obj, *req);
262 }
263 /* }}} */
264
265 /*#*/
266
267 /* {{{ static void http_request_pool_freebody(http_request_body **) */
268 static void http_request_pool_freebody(http_request_body **body)
269 {
270 TSRMLS_FETCH();
271 http_request_body_free(*body);
272 }
273 /* }}} */
274
275 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
276 static int http_request_pool_compare_handles(void *h1, void *h2)
277 {
278 int match = (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
279 #if HTTP_DEBUG_REQPOOLS
280 /* if(match) fprintf(stderr, "OK\n"); */
281 #endif
282 return match;
283 }
284 /* }}} */
285
286 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
287
288
289 /*
290 * Local variables:
291 * tab-width: 4
292 * c-basic-offset: 4
293 * End:
294 * vim600: noet sw=4 ts=4 fdm=marker
295 * vim<600: noet sw=4 ts=4
296 */
297