- curl folks seem to have identified the MSVC quirks regarding DST offsets
[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(#%d) for attaching to the HttpRequestPool", Z_OBJ_HANDLE_P(request));
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(#%d) to the HttpRequestPool: %s", Z_OBJ_HANDLE_P(request), 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 (req->request->_in_progress_cb) {
123 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));
124 } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->request->ch))) {
125 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));
126 } else {
127 req->pool = NULL;
128 zend_llist_del_element(&pool->finished, request, http_request_pool_compare_handles);
129 zend_llist_del_element(&pool->handles, request, http_request_pool_compare_handles);
130
131 #if HTTP_DEBUG_REQPOOLS
132 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
133 #endif
134
135 return SUCCESS;
136 }
137 return FAILURE;
138 }
139 /* }}} */
140
141 /* {{{ void http_request_pool_apply(http_request_pool *, http_request_pool_apply_func) */
142 PHP_HTTP_API void _http_request_pool_apply(http_request_pool *pool, http_request_pool_apply_func cb TSRMLS_DC)
143 {
144 int count = zend_llist_count(&pool->handles);
145
146 if (count) {
147 int i = 0;
148 zend_llist_position pos;
149 zval **handle, **handles = emalloc(count * sizeof(zval *));
150
151 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
152 handles[i++] = *handle;
153 }
154
155 /* should never happen */
156 if (i != count) {
157 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
158 count = i;
159 }
160
161 for (i = 0; i < count; ++i) {
162 if (cb(pool, handles[i] TSRMLS_CC)) {
163 break;
164 }
165 }
166 efree(handles);
167 }
168 }
169 /* }}} */
170
171 /* {{{ void http_request_pool_apply_with_arg(http_request_pool *, http_request_pool_apply_with_arg_func, void *) */
172 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)
173 {
174 int count = zend_llist_count(&pool->handles);
175
176 if (count) {
177 int i = 0;
178 zend_llist_position pos;
179 zval **handle, **handles = emalloc(count * sizeof(zval *));
180
181 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
182 handles[i++] = *handle;
183 }
184
185 /* should never happen */
186 if (i != count) {
187 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
188 count = i;
189 }
190
191 for (i = 0; i < count; ++i) {
192 if (cb(pool, handles[i], arg TSRMLS_CC)) {
193 break;
194 }
195 }
196 efree(handles);
197 }
198 }
199 /* }}} */
200
201 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
202 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
203 {
204 #if HTTP_DEBUG_REQPOOLS
205 fprintf(stderr, "Detaching %d requests from pool %p\n", zend_llist_count(&pool->handles), pool);
206 #endif
207 http_request_pool_apply(pool, _http_request_pool_detach);
208 }
209 /* }}} */
210
211 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
212 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
213 {
214 #if HTTP_DEBUG_REQPOOLS
215 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
216 #endif
217
218 while (http_request_pool_perform(pool)) {
219 if (SUCCESS != http_request_pool_select(pool)) {
220 #ifdef PHP_WIN32
221 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
222 http_error_ex(HE_WARNING, HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError());
223 #else
224 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
225 #endif
226 return FAILURE;
227 }
228 }
229
230 #if HTTP_DEBUG_REQPOOLS
231 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
232 #endif
233
234 return SUCCESS;
235 }
236 /* }}} */
237
238 /* {{{ void http_request_pool_dtor(http_request_pool *) */
239 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
240 {
241 #if HTTP_DEBUG_REQPOOLS
242 fprintf(stderr, "Destructing request pool %p\n", pool);
243 #endif
244
245 pool->unfinished = 0;
246 zend_llist_clean(&pool->finished);
247 zend_llist_clean(&pool->handles);
248 curl_multi_cleanup(pool->ch);
249 }
250 /* }}} */
251
252 #ifdef PHP_WIN32
253 # define SELECT_ERROR SOCKET_ERROR
254 #else
255 # define SELECT_ERROR -1
256 #endif
257
258 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
259 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
260 {
261 int MAX;
262 fd_set R, W, E;
263 struct timeval timeout = {1, 0};
264 #ifdef HAVE_CURL_MULTI_TIMEOUT
265 long max_tout = 1000;
266
267 if ((CURLM_OK == curl_multi_timeout(pool->ch, &max_tout)) && (max_tout != -1)) {
268 timeout.tv_sec = max_tout / 1000;
269 timeout.tv_usec = (max_tout % 1000) * 1000;
270 }
271 #endif
272
273 FD_ZERO(&R);
274 FD_ZERO(&W);
275 FD_ZERO(&E);
276
277 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
278 if (MAX == -1) {
279 http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / HTTP_MCROSEC));
280 return SUCCESS;
281 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
282 return SUCCESS;
283 }
284 }
285 return FAILURE;
286 }
287 /* }}} */
288
289 /* {{{ int http_request_pool_perform(http_request_pool *) */
290 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool TSRMLS_DC)
291 {
292 CURLMsg *msg;
293 int remaining = 0;
294
295 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
296
297 while ((msg = curl_multi_info_read(pool->ch, &remaining))) {
298 if (CURLMSG_DONE == msg->msg) {
299 if (CURLE_OK != msg->data.result) {
300 http_request *r = NULL;
301 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &r);
302 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), r?r->_error:"", r?r->url:"");
303 }
304 http_request_pool_apply_with_arg(pool, _http_request_pool_responsehandler, msg->easy_handle);
305 }
306 }
307
308 return pool->unfinished;
309 }
310 /* }}} */
311
312 /* {{{ void http_request_pool_responsehandler(http_request_pool *, zval *, void *) */
313 int _http_request_pool_responsehandler(http_request_pool *pool, zval *req, void *ch TSRMLS_DC)
314 {
315 getObjectEx(http_request_object, obj, req);
316
317 if ((!ch) || obj->request->ch == (CURL *) ch) {
318
319 #if HTTP_DEBUG_REQPOOLS
320 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_P(req), obj, obj->pool);
321 #endif
322
323 ZVAL_ADDREF(req);
324 zend_llist_add_element(&obj->pool->finished, &req);
325 http_request_object_responsehandler(obj, req);
326 return 1;
327 }
328 return 0;
329 }
330 /* }}} */
331
332 /*#*/
333
334 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
335 static int http_request_pool_compare_handles(void *h1, void *h2)
336 {
337 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
338 }
339 /* }}} */
340
341 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
342
343
344 /*
345 * Local variables:
346 * tab-width: 4
347 * c-basic-offset: 4
348 * End:
349 * vim600: noet sw=4 ts=4 fdm=marker
350 * vim<600: noet sw=4 ts=4
351 */
352