- gcc didn't like those changes
[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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #define HTTP_WANT_CURL
20 #include "php_http.h"
21
22 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
23
24 #include "php_http_api.h"
25 #include "php_http_exception_object.h"
26 #include "php_http_request_api.h"
27 #include "php_http_request_object.h"
28 #include "php_http_request_pool_api.h"
29 #include "php_http_requestpool_object.h"
30
31 #ifndef HTTP_DEBUG_REQPOOLS
32 # define HTTP_DEBUG_REQPOOLS 0
33 #endif
34
35 ZEND_EXTERN_MODULE_GLOBALS(http);
36
37 #ifndef HAVE_CURL_MULTI_STRERROR
38 # define curl_multi_strerror(dummy) "unknown error"
39 #endif
40
41 static int http_request_pool_compare_handles(void *h1, void *h2);
42
43 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
44 PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool TSRMLS_DC)
45 {
46 zend_bool free_pool;
47
48 #if HTTP_DEBUG_REQPOOLS
49 fprintf(stderr, "Initializing request pool %p\n", pool);
50 #endif
51
52 if ((free_pool = (!pool))) {
53 pool = emalloc(sizeof(http_request_pool));
54 pool->ch = NULL;
55 }
56
57 HTTP_CHECK_CURL_INIT(pool->ch, curl_multi_init(), ;);
58 if (!pool->ch) {
59 if (free_pool) {
60 efree(pool);
61 }
62 return NULL;
63 }
64
65 pool->unfinished = 0;
66 zend_llist_init(&pool->finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
67 zend_llist_init(&pool->handles, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
68
69 #if HTTP_DEBUG_REQPOOLS
70 fprintf(stderr, "Initialized request pool %p\n", pool);
71 #endif
72
73 return pool;
74 }
75 /* }}} */
76
77 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
78 PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *request TSRMLS_DC)
79 {
80 getObjectEx(http_request_object, req, request);
81
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
86 if (req->pool) {
87 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");
88 } else if (SUCCESS != http_request_object_requesthandler(req, request)) {
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->request->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 ZVAL_ADDREF(request);
99 zend_llist_add_element(&pool->handles, &request);
100
101 #if HTTP_DEBUG_REQPOOLS
102 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
103 #endif
104 return SUCCESS;
105 }
106 }
107 return FAILURE;
108 }
109 /* }}} */
110
111 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
112 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request TSRMLS_DC)
113 {
114 CURLMcode code;
115 getObjectEx(http_request_object, req, request);
116
117 #if HTTP_DEBUG_REQPOOLS
118 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
119 #endif
120
121 if (!req->pool) {
122 /* not attached to any pool */
123 #if HTTP_DEBUG_REQPOOLS
124 fprintf(stderr, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request), req);
125 #endif
126 } else if (req->pool != pool) {
127 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
128 } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->request->ch))) {
129 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code));
130 } else {
131 req->pool = NULL;
132 zend_llist_del_element(&pool->finished, request, http_request_pool_compare_handles);
133 zend_llist_del_element(&pool->handles, request, http_request_pool_compare_handles);
134
135 #if HTTP_DEBUG_REQPOOLS
136 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
137 #endif
138
139 return SUCCESS;
140 }
141 return FAILURE;
142 }
143 /* }}} */
144
145 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
146 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
147 {
148 int count = zend_llist_count(&pool->handles);
149
150 #if HTTP_DEBUG_REQPOOLS
151 fprintf(stderr, "Detaching %d requests from pool %p\n", count, pool);
152 #endif
153
154 /*
155 * we cannot apply a function to the llist which actually detaches
156 * the curl handle *and* removes the llist element --
157 * so let's get our hands dirty
158 */
159 if (count) {
160 int i = 0;
161 zend_llist_position pos;
162 zval **handle, **handles = emalloc(count * sizeof(zval *));
163
164 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
165 handles[i++] = *handle;
166 }
167
168 /* should never happen */
169 if (i != count) {
170 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
171 count = i;
172 }
173
174 for (i = 0; i < count; ++i) {
175 http_request_pool_detach(pool, handles[i]);
176 }
177 efree(handles);
178 }
179 }
180
181 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
182 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
183 {
184 #if HTTP_DEBUG_REQPOOLS
185 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
186 #endif
187
188 while (http_request_pool_perform(pool)) {
189 if (SUCCESS != http_request_pool_select(pool)) {
190 #ifdef PHP_WIN32
191 http_error(HE_WARNING, HTTP_E_SOCKET, WSAGetLastError());
192 #else
193 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
194 #endif
195 return FAILURE;
196 }
197 }
198
199 #if HTTP_DEBUG_REQPOOLS
200 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
201 #endif
202
203 return SUCCESS;
204 }
205 /* }}} */
206
207 /* {{{ void http_request_pool_dtor(http_request_pool *) */
208 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
209 {
210 #if HTTP_DEBUG_REQPOOLS
211 fprintf(stderr, "Destructing request pool %p\n", pool);
212 #endif
213
214 pool->unfinished = 0;
215 zend_llist_clean(&pool->finished);
216 zend_llist_clean(&pool->handles);
217 curl_multi_cleanup(pool->ch);
218 }
219 /* }}} */
220
221 #ifdef PHP_WIN32
222 # define SELECT_ERROR SOCKET_ERROR
223 #else
224 # define SELECT_ERROR -1
225 #endif
226
227 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
228 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
229 {
230 int MAX;
231 fd_set R, W, E;
232 struct timeval timeout = {1, 0};
233
234 FD_ZERO(&R);
235 FD_ZERO(&W);
236 FD_ZERO(&E);
237
238 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
239 if (MAX == -1 || SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
240 return SUCCESS;
241 }
242 }
243 return FAILURE;
244 }
245 /* }}} */
246
247 /* {{{ int http_request_pool_perform(http_request_pool *) */
248 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool TSRMLS_DC)
249 {
250 CURLMsg *msg;
251 int remaining = 0;
252
253 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
254
255 while ((msg = curl_multi_info_read(pool->ch, &remaining))) {
256 if (CURLMSG_DONE == msg->msg) {
257 if (CURLE_OK != msg->data.result) {
258 http_request_pool_try {
259 http_request *r = NULL;
260 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &r);
261 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), r?r->_error:"", r?r->url:"");
262 } http_request_pool_catch();
263 }
264 http_request_pool_try {
265 zend_llist_apply_with_argument(&pool->handles, (llist_apply_with_arg_func_t) http_request_pool_responsehandler, msg->easy_handle TSRMLS_CC);
266 } http_request_pool_catch();
267 }
268 }
269 http_request_pool_final();
270
271 return pool->unfinished;
272 }
273 /* }}} */
274
275 /* {{{ void http_request_pool_responsehandler(zval **) */
276 void _http_request_pool_responsehandler(zval **req, CURL *ch TSRMLS_DC)
277 {
278 getObjectEx(http_request_object, obj, *req);
279
280 if (obj->request->ch == ch) {
281
282 #if HTTP_DEBUG_REQPOOLS
283 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req), obj, obj->pool);
284 #endif
285
286 ZVAL_ADDREF(*req);
287 zend_llist_add_element(&obj->pool->finished, req);
288 http_request_object_responsehandler(obj, *req);
289 }
290 }
291 /* }}} */
292
293 static void move_backtrace_args(zval *from, zval *to TSRMLS_DC)
294 {
295 zval **args, **trace_0, *old_trace_0, *trace = NULL;
296
297 if ((trace = zend_read_property(zend_exception_get_default(), from, "trace", lenof("trace"), 0 TSRMLS_CC))) {
298 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void **) &trace_0)) {
299 old_trace_0 = *trace_0;
300 if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(trace_0), "args", sizeof("args"), (void **) &args)) {
301 if ((trace = zend_read_property(zend_exception_get_default(), to, "trace", lenof("trace"), 0 TSRMLS_CC))) {
302 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void **) &trace_0)) {
303 ZVAL_ADDREF(*args);
304 add_assoc_zval(*trace_0, "args", *args);
305 zend_hash_del(Z_ARRVAL_P(old_trace_0), "args", sizeof("args"));
306 }
307 }
308 }
309 }
310 }
311 }
312 /* {{{ void http_request_pool_wrap_exception(zval *, zval *) */
313 void _http_request_pool_wrap_exception(zval *old_exception, zval *new_exception TSRMLS_DC)
314 {
315 zend_class_entry *ce = HTTP_EX_CE(request_pool);
316
317 /* if old_exception is already an HttpRequestPoolException append the new one,
318 else create a new HttpRequestPoolException and append the old and new exceptions */
319 if (old_exception && Z_OBJCE_P(old_exception) == ce) {
320 zval *old_exprop, *new_exprop;
321
322 MAKE_STD_ZVAL(new_exprop);
323 array_init(new_exprop);
324 old_exprop = zend_read_property(ce, old_exception, "exceptionStack", lenof("exceptionStack"), 0 TSRMLS_CC);
325 if (Z_TYPE_P(old_exprop) == IS_ARRAY) {
326 array_copy(old_exprop, new_exprop);
327 }
328 add_next_index_zval(new_exprop, new_exception);
329 zend_update_property(ce, old_exception, "exceptionStack", lenof("exceptionStack"), new_exprop TSRMLS_CC);
330 zval_ptr_dtor(&new_exprop);
331
332 EG(exception) = old_exception;
333 } else if (new_exception && Z_OBJCE_P(new_exception) != ce){
334 zval *exval, *exprop;
335
336 MAKE_STD_ZVAL(exval);
337 object_init_ex(exval, ce);
338 MAKE_STD_ZVAL(exprop);
339 array_init(exprop);
340
341 if (old_exception) {
342 add_next_index_zval(exprop, old_exception);
343 }
344 move_backtrace_args(new_exception, exval TSRMLS_CC);
345 zend_update_property_long(ce, exval, "code", lenof("code"), HTTP_E_REQUEST_POOL TSRMLS_CC);
346 zend_update_property_string(ce, exval, "message", lenof("message"), "See exceptionStack property" TSRMLS_CC);
347 add_next_index_zval(exprop, new_exception);
348 zend_update_property(ce, exval, "exceptionStack", lenof("exceptionStack"), exprop TSRMLS_CC);
349 zval_ptr_dtor(&exprop);
350
351 EG(exception) = exval;
352 }
353 }
354 /* }}} */
355
356 /*#*/
357
358 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
359 static int http_request_pool_compare_handles(void *h1, void *h2)
360 {
361 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
362 }
363 /* }}} */
364
365 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
366
367
368 /*
369 * Local variables:
370 * tab-width: 4
371 * c-basic-offset: 4
372 * End:
373 * vim600: noet sw=4 ts=4 fdm=marker
374 * vim<600: noet sw=4 ts=4
375 */
376