e93a078c0b7b1ac6cccc8dcc054b1888e253c17d
[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
27 #ifndef HTTP_DEBUG_REQPOOLS
28 # define HTTP_DEBUG_REQPOOLS 0
29 #endif
30
31 #ifndef HAVE_CURL_MULTI_STRERROR
32 # define curl_multi_strerror(dummy) "unknown error"
33 #endif
34
35 static int http_request_pool_compare_handles(void *h1, void *h2);
36
37 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
38 PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool TSRMLS_DC)
39 {
40 zend_bool free_pool;
41
42 #if HTTP_DEBUG_REQPOOLS
43 fprintf(stderr, "Initializing request pool %p\n", pool);
44 #endif
45
46 if ((free_pool = (!pool))) {
47 pool = emalloc(sizeof(http_request_pool));
48 pool->ch = NULL;
49 }
50
51 HTTP_CHECK_CURL_INIT(pool->ch, curl_multi_init(), ;);
52 if (!pool->ch) {
53 if (free_pool) {
54 efree(pool);
55 }
56 return NULL;
57 }
58
59 pool->unfinished = 0;
60 zend_llist_init(&pool->finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
61 zend_llist_init(&pool->handles, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
62
63 #if HTTP_DEBUG_REQPOOLS
64 fprintf(stderr, "Initialized request pool %p\n", pool);
65 #endif
66
67 return pool;
68 }
69 /* }}} */
70
71 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
72 PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *request TSRMLS_DC)
73 {
74 getObjectEx(http_request_object, req, request);
75
76 #if HTTP_DEBUG_REQPOOLS
77 fprintf(stderr, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
78 #endif
79
80 if (req->pool) {
81 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");
82 } else if (SUCCESS != http_request_object_requesthandler(req, request)) {
83 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
84 } else {
85 CURLMcode code = curl_multi_add_handle(pool->ch, req->request->ch);
86
87 if ((CURLM_OK != code) && (CURLM_CALL_MULTI_PERFORM != code)) {
88 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code));
89 } else {
90 req->pool = pool;
91
92 ZVAL_ADDREF(request);
93 zend_llist_add_element(&pool->handles, &request);
94
95 #if HTTP_DEBUG_REQPOOLS
96 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
97 #endif
98 return SUCCESS;
99 }
100 }
101 return FAILURE;
102 }
103 /* }}} */
104
105 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
106 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request TSRMLS_DC)
107 {
108 CURLMcode code;
109 getObjectEx(http_request_object, req, request);
110
111 #if HTTP_DEBUG_REQPOOLS
112 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
113 #endif
114
115 if (!req->pool) {
116 /* not attached to any pool */
117 #if HTTP_DEBUG_REQPOOLS
118 fprintf(stderr, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request), req);
119 #endif
120 } else if (req->pool != pool) {
121 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
122 } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->request->ch))) {
123 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code));
124 } else {
125 req->pool = NULL;
126 zend_llist_del_element(&pool->finished, request, http_request_pool_compare_handles);
127 zend_llist_del_element(&pool->handles, request, http_request_pool_compare_handles);
128
129 #if HTTP_DEBUG_REQPOOLS
130 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
131 #endif
132
133 return SUCCESS;
134 }
135 return FAILURE;
136 }
137 /* }}} */
138
139 /* {{{ void http_request_pool_apply(http_request_pool *, http_request_pool_apply_func) */
140 PHP_HTTP_API void _http_request_pool_apply(http_request_pool *pool, http_request_pool_apply_func cb TSRMLS_DC)
141 {
142 int count = zend_llist_count(&pool->handles);
143
144 if (count) {
145 int i = 0;
146 zend_llist_position pos;
147 zval **handle, **handles = emalloc(count * sizeof(zval *));
148
149 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
150 handles[i++] = *handle;
151 }
152
153 /* should never happen */
154 if (i != count) {
155 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
156 count = i;
157 }
158
159 for (i = 0; i < count; ++i) {
160 if (cb(pool, handles[i] TSRMLS_CC)) {
161 break;
162 }
163 }
164 efree(handles);
165 }
166 }
167 /* }}} */
168
169 /* {{{ void http_request_pool_apply_with_arg(http_request_pool *, http_request_pool_apply_with_arg_func, void *) */
170 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)
171 {
172 int count = zend_llist_count(&pool->handles);
173
174 if (count) {
175 int i = 0;
176 zend_llist_position pos;
177 zval **handle, **handles = emalloc(count * sizeof(zval *));
178
179 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
180 handles[i++] = *handle;
181 }
182
183 /* should never happen */
184 if (i != count) {
185 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
186 count = i;
187 }
188
189 for (i = 0; i < count; ++i) {
190 if (cb(pool, handles[i], arg TSRMLS_CC)) {
191 break;
192 }
193 }
194 efree(handles);
195 }
196 }
197 /* }}} */
198
199 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
200 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
201 {
202 #if HTTP_DEBUG_REQPOOLS
203 fprintf(stderr, "Detaching %d requests from pool %p\n", zend_llist_count(&pool->handles), pool);
204 #endif
205 http_request_pool_apply(pool, _http_request_pool_detach);
206 }
207 /* }}} */
208
209 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
210 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
211 {
212 #if HTTP_DEBUG_REQPOOLS
213 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
214 #endif
215
216 while (http_request_pool_perform(pool, 0)) {
217 if (SUCCESS != http_request_pool_select(pool)) {
218 #ifdef PHP_WIN32
219 http_error(HE_WARNING, HTTP_E_SOCKET, WSAGetLastError());
220 #else
221 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
222 #endif
223 return FAILURE;
224 }
225 }
226
227 #if HTTP_DEBUG_REQPOOLS
228 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
229 #endif
230
231 return SUCCESS;
232 }
233 /* }}} */
234
235 /* {{{ void http_request_pool_dtor(http_request_pool *) */
236 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
237 {
238 #if HTTP_DEBUG_REQPOOLS
239 fprintf(stderr, "Destructing request pool %p\n", pool);
240 #endif
241
242 pool->unfinished = 0;
243 zend_llist_clean(&pool->finished);
244 zend_llist_clean(&pool->handles);
245 curl_multi_cleanup(pool->ch);
246 }
247 /* }}} */
248
249 #ifdef PHP_WIN32
250 # define SELECT_ERROR SOCKET_ERROR
251 #else
252 # define SELECT_ERROR -1
253 #endif
254
255 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
256 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
257 {
258 int MAX;
259 fd_set R, W, E;
260 struct timeval timeout = {1, 0};
261
262 FD_ZERO(&R);
263 FD_ZERO(&W);
264 FD_ZERO(&E);
265
266 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
267 if (MAX == -1 || SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
268 return SUCCESS;
269 }
270 }
271 return FAILURE;
272 }
273 /* }}} */
274
275 /* {{{ int http_request_pool_perform(http_request_pool *) */
276 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool, int once TSRMLS_DC)
277 {
278 CURLMsg *msg;
279 int remaining = 0;
280
281 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
282
283 while ((msg = curl_multi_info_read(pool->ch, &remaining))) {
284 if (CURLMSG_DONE == msg->msg) {
285 if (CURLE_OK != msg->data.result) {
286 http_request_pool_try {
287 http_request *r = NULL;
288 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &r);
289 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), r?r->_error:"", r?r->url:"");
290 } http_request_pool_catch();
291 }
292 http_request_pool_try {
293 http_request_pool_apply_with_arg(pool, _http_request_pool_responsehandler, msg->easy_handle);
294 } http_request_pool_catch();
295 }
296 }
297 if (once || !pool->unfinished) {
298 http_request_pool_final();
299 }
300
301 return pool->unfinished;
302 }
303 /* }}} */
304
305 /* {{{ void http_request_pool_responsehandler(http_request_pool *, zval *, void *) */
306 int _http_request_pool_responsehandler(http_request_pool *pool, zval *req, void *ch TSRMLS_DC)
307 {
308 getObjectEx(http_request_object, obj, req);
309
310 if ((!ch) || obj->request->ch == (CURL *) ch) {
311
312 #if HTTP_DEBUG_REQPOOLS
313 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_P(req), obj, obj->pool);
314 #endif
315
316 ZVAL_ADDREF(req);
317 zend_llist_add_element(&obj->pool->finished, &req);
318 http_request_object_responsehandler(obj, req);
319 return 1;
320 }
321 return 0;
322 }
323 /* }}} */
324
325 /*#*/
326
327 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
328 static int http_request_pool_compare_handles(void *h1, void *h2)
329 {
330 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
331 }
332 /* }}} */
333
334 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
335
336
337 /*
338 * Local variables:
339 * tab-width: 4
340 * c-basic-offset: 4
341 * End:
342 * vim600: noet sw=4 ts=4 fdm=marker
343 * vim<600: noet sw=4 ts=4
344 */
345