- use a HashTable to stat persistent handles
[m6w6/ext-http] / http_request_pool_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_CURL
16 #include "php_http.h"
17
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
19
20 #include "php_http_api.h"
21 #include "php_http_exception_object.h"
22 #include "php_http_request_api.h"
23 #include "php_http_request_object.h"
24 #include "php_http_request_pool_api.h"
25 #include "php_http_requestpool_object.h"
26 #include "php_http_persistent_handle_api.h"
27
28 #ifndef HTTP_DEBUG_REQPOOLS
29 # define HTTP_DEBUG_REQPOOLS 0
30 #endif
31
32 #ifdef HTTP_HAVE_PERSISTENT_HANDLES
33 # define HTTP_CURL_MULTI_CTOR(ch) (SUCCESS == http_persistent_handle_acquire("http_request_pool", &(ch)))
34 # define HTTP_CURL_MULTI_DTOR(chp) http_persistent_handle_release("http_request_pool", (chp))
35 #else
36 # define HTTP_CURL_MULTI_CTOR(ch) ((ch) = curl_multi_init())
37 # define HTTP_CURL_MULTI_DTOR(chp) curl_multi_cleanup(*(chp)); *(chp) = NULL
38 #endif
39
40 static int http_request_pool_compare_handles(void *h1, void *h2);
41
42 #ifdef HTTP_HAVE_PERSISTENT_HANDLES
43 PHP_MINIT_FUNCTION(http_request_pool)
44 {
45 if (SUCCESS != http_persistent_handle_provide("http_request_pool", curl_multi_init, (http_persistent_handle_dtor) curl_multi_cleanup)) {
46 return FAILURE;
47 }
48 return SUCCESS;
49 }
50 #endif
51
52 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
53 PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool TSRMLS_DC)
54 {
55 zend_bool free_pool;
56
57 #if HTTP_DEBUG_REQPOOLS
58 fprintf(stderr, "Initializing request pool %p\n", pool);
59 #endif
60
61 if ((free_pool = (!pool))) {
62 pool = emalloc(sizeof(http_request_pool));
63 pool->ch = NULL;
64 }
65
66 if (!HTTP_CURL_MULTI_CTOR(pool->ch)) {
67 if (free_pool) {
68 efree(pool);
69 }
70 return NULL;
71 }
72
73 pool->unfinished = 0;
74 zend_llist_init(&pool->finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
75 zend_llist_init(&pool->handles, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
76
77 #if HTTP_DEBUG_REQPOOLS
78 fprintf(stderr, "Initialized request pool %p\n", pool);
79 #endif
80
81 return pool;
82 }
83 /* }}} */
84
85 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
86 PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *request TSRMLS_DC)
87 {
88 getObjectEx(http_request_object, req, request);
89
90 #if HTTP_DEBUG_REQPOOLS
91 fprintf(stderr, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
92 #endif
93
94 if (req->pool) {
95 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");
96 } else if (SUCCESS != http_request_object_requesthandler(req, request)) {
97 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not initialize HttpRequest object(#%d) for attaching to the HttpRequestPool", Z_OBJ_HANDLE_P(request));
98 } else {
99 CURLMcode code = curl_multi_add_handle(pool->ch, req->request->ch);
100
101 if ((CURLM_OK != code) && (CURLM_CALL_MULTI_PERFORM != code)) {
102 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not attach HttpRequest object(#%d) to the HttpRequestPool: %s", Z_OBJ_HANDLE_P(request), curl_multi_strerror(code));
103 } else {
104 req->pool = pool;
105
106 ZVAL_ADDREF(request);
107 zend_llist_add_element(&pool->handles, &request);
108
109 #if HTTP_DEBUG_REQPOOLS
110 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
111 #endif
112 return SUCCESS;
113 }
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 CURLMcode code;
123 getObjectEx(http_request_object, req, request);
124
125 #if HTTP_DEBUG_REQPOOLS
126 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
127 #endif
128
129 if (!req->pool) {
130 /* not attached to any pool */
131 #if HTTP_DEBUG_REQPOOLS
132 fprintf(stderr, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request), req);
133 #endif
134 } else if (req->pool != pool) {
135 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
136 } else if (req->request->_in_progress_cb) {
137 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "HttpRequest object(#%d) cannot be detached from the HttpRequestPool while executing the progress callback", Z_OBJ_HANDLE_P(request));
138 } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->request->ch))) {
139 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object(#%d) from the HttpRequestPool: %s", Z_OBJ_HANDLE_P(request), curl_multi_strerror(code));
140 } else {
141 req->pool = NULL;
142 zend_llist_del_element(&pool->finished, request, http_request_pool_compare_handles);
143 zend_llist_del_element(&pool->handles, request, http_request_pool_compare_handles);
144
145 #if HTTP_DEBUG_REQPOOLS
146 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
147 #endif
148
149 return SUCCESS;
150 }
151 return FAILURE;
152 }
153 /* }}} */
154
155 /* {{{ void http_request_pool_apply(http_request_pool *, http_request_pool_apply_func) */
156 PHP_HTTP_API void _http_request_pool_apply(http_request_pool *pool, http_request_pool_apply_func cb TSRMLS_DC)
157 {
158 int count = zend_llist_count(&pool->handles);
159
160 if (count) {
161 int i = 0;
162 zend_llist_position pos;
163 zval **handle, **handles = emalloc(count * sizeof(zval *));
164
165 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
166 handles[i++] = *handle;
167 }
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
175 for (i = 0; i < count; ++i) {
176 if (cb(pool, handles[i] TSRMLS_CC)) {
177 break;
178 }
179 }
180 efree(handles);
181 }
182 }
183 /* }}} */
184
185 /* {{{ void http_request_pool_apply_with_arg(http_request_pool *, http_request_pool_apply_with_arg_func, void *) */
186 PHP_HTTP_API void _http_request_pool_apply_with_arg(http_request_pool *pool, http_request_pool_apply_with_arg_func cb, void *arg TSRMLS_DC)
187 {
188 int count = zend_llist_count(&pool->handles);
189
190 if (count) {
191 int i = 0;
192 zend_llist_position pos;
193 zval **handle, **handles = emalloc(count * sizeof(zval *));
194
195 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
196 handles[i++] = *handle;
197 }
198
199 /* should never happen */
200 if (i != count) {
201 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
202 count = i;
203 }
204
205 for (i = 0; i < count; ++i) {
206 if (cb(pool, handles[i], arg TSRMLS_CC)) {
207 break;
208 }
209 }
210 efree(handles);
211 }
212 }
213 /* }}} */
214
215 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
216 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
217 {
218 #if HTTP_DEBUG_REQPOOLS
219 fprintf(stderr, "Detaching %d requests from pool %p\n", zend_llist_count(&pool->handles), pool);
220 #endif
221 http_request_pool_apply(pool, _http_request_pool_detach);
222 }
223 /* }}} */
224
225 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
226 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
227 {
228 #if HTTP_DEBUG_REQPOOLS
229 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
230 #endif
231
232 while (http_request_pool_perform(pool)) {
233 if (SUCCESS != http_request_pool_select(pool)) {
234 #ifdef PHP_WIN32
235 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
236 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError());
237 #else
238 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
239 #endif
240 return FAILURE;
241 }
242 }
243
244 #if HTTP_DEBUG_REQPOOLS
245 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
246 #endif
247
248 return SUCCESS;
249 }
250 /* }}} */
251
252 /* {{{ void http_request_pool_dtor(http_request_pool *) */
253 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
254 {
255 #if HTTP_DEBUG_REQPOOLS
256 fprintf(stderr, "Destructing request pool %p\n", pool);
257 #endif
258
259 pool->unfinished = 0;
260 zend_llist_clean(&pool->finished);
261 zend_llist_clean(&pool->handles);
262 HTTP_CURL_MULTI_DTOR(&pool->ch);
263 }
264 /* }}} */
265
266 #ifdef PHP_WIN32
267 # define SELECT_ERROR SOCKET_ERROR
268 #else
269 # define SELECT_ERROR -1
270 #endif
271
272 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
273 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
274 {
275 int MAX;
276 fd_set R, W, E;
277 struct timeval timeout = {1, 0};
278 #ifdef HAVE_CURL_MULTI_TIMEOUT
279 long max_tout = 1000;
280
281 if ((CURLM_OK == curl_multi_timeout(pool->ch, &max_tout)) && (max_tout != -1)) {
282 timeout.tv_sec = max_tout / 1000;
283 timeout.tv_usec = (max_tout % 1000) * 1000;
284 }
285 #endif
286
287 FD_ZERO(&R);
288 FD_ZERO(&W);
289 FD_ZERO(&E);
290
291 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
292 if (MAX == -1) {
293 http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / HTTP_MCROSEC));
294 return SUCCESS;
295 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
296 return SUCCESS;
297 }
298 }
299 return FAILURE;
300 }
301 /* }}} */
302
303 /* {{{ int http_request_pool_perform(http_request_pool *) */
304 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool TSRMLS_DC)
305 {
306 CURLMsg *msg;
307 int remaining = 0;
308
309 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
310
311 while ((msg = curl_multi_info_read(pool->ch, &remaining))) {
312 if (CURLMSG_DONE == msg->msg) {
313 if (CURLE_OK != msg->data.result) {
314 http_request *r = NULL;
315 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &r);
316 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), r?r->_error:"", r?r->url:"");
317 }
318 http_request_pool_apply_with_arg(pool, _http_request_pool_responsehandler, msg->easy_handle);
319 }
320 }
321
322 return pool->unfinished;
323 }
324 /* }}} */
325
326 /* {{{ void http_request_pool_responsehandler(http_request_pool *, zval *, void *) */
327 int _http_request_pool_responsehandler(http_request_pool *pool, zval *req, void *ch TSRMLS_DC)
328 {
329 getObjectEx(http_request_object, obj, req);
330
331 if ((!ch) || obj->request->ch == (CURL *) ch) {
332
333 #if HTTP_DEBUG_REQPOOLS
334 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_P(req), obj, obj->pool);
335 #endif
336
337 ZVAL_ADDREF(req);
338 zend_llist_add_element(&obj->pool->finished, &req);
339 http_request_object_responsehandler(obj, req);
340 return 1;
341 }
342 return 0;
343 }
344 /* }}} */
345
346 /*#*/
347
348 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
349 static int http_request_pool_compare_handles(void *h1, void *h2)
350 {
351 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
352 }
353 /* }}} */
354
355 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
356
357
358 /*
359 * Local variables:
360 * tab-width: 4
361 * c-basic-offset: 4
362 * End:
363 * vim600: noet sw=4 ts=4 fdm=marker
364 * vim<600: noet sw=4 ts=4
365 */
366