- release 1.0.0RC2
[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_detach_all(http_request_pool *) */
140 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
141 {
142 int count = zend_llist_count(&pool->handles);
143
144 #if HTTP_DEBUG_REQPOOLS
145 fprintf(stderr, "Detaching %d requests from pool %p\n", count, pool);
146 #endif
147
148 /*
149 * we cannot apply a function to the llist which actually detaches
150 * the curl handle *and* removes the llist element --
151 * so let's get our hands dirty
152 */
153 if (count) {
154 int i = 0;
155 zend_llist_position pos;
156 zval **handle, **handles = emalloc(count * sizeof(zval *));
157
158 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
159 handles[i++] = *handle;
160 }
161
162 /* should never happen */
163 if (i != count) {
164 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
165 count = i;
166 }
167
168 for (i = 0; i < count; ++i) {
169 http_request_pool_detach(pool, handles[i]);
170 }
171 efree(handles);
172 }
173 }
174
175 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
176 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
177 {
178 #if HTTP_DEBUG_REQPOOLS
179 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
180 #endif
181
182 while (http_request_pool_perform(pool)) {
183 if (SUCCESS != http_request_pool_select(pool)) {
184 #ifdef PHP_WIN32
185 http_error(HE_WARNING, HTTP_E_SOCKET, WSAGetLastError());
186 #else
187 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
188 #endif
189 return FAILURE;
190 }
191 }
192
193 #if HTTP_DEBUG_REQPOOLS
194 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
195 #endif
196
197 return SUCCESS;
198 }
199 /* }}} */
200
201 /* {{{ void http_request_pool_dtor(http_request_pool *) */
202 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
203 {
204 #if HTTP_DEBUG_REQPOOLS
205 fprintf(stderr, "Destructing request pool %p\n", pool);
206 #endif
207
208 pool->unfinished = 0;
209 zend_llist_clean(&pool->finished);
210 zend_llist_clean(&pool->handles);
211 curl_multi_cleanup(pool->ch);
212 }
213 /* }}} */
214
215 #ifdef PHP_WIN32
216 # define SELECT_ERROR SOCKET_ERROR
217 #else
218 # define SELECT_ERROR -1
219 #endif
220
221 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
222 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
223 {
224 int MAX;
225 fd_set R, W, E;
226 struct timeval timeout = {1, 0};
227
228 FD_ZERO(&R);
229 FD_ZERO(&W);
230 FD_ZERO(&E);
231
232 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
233 if (MAX == -1 || SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
234 return SUCCESS;
235 }
236 }
237 return FAILURE;
238 }
239 /* }}} */
240
241 /* {{{ int http_request_pool_perform(http_request_pool *) */
242 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool TSRMLS_DC)
243 {
244 CURLMsg *msg;
245 int remaining = 0;
246
247 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
248
249 while ((msg = curl_multi_info_read(pool->ch, &remaining))) {
250 if (CURLMSG_DONE == msg->msg) {
251 if (CURLE_OK != msg->data.result) {
252 http_request_pool_try {
253 http_request *r = NULL;
254 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &r);
255 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), r?r->_error:"", r?r->url:"");
256 } http_request_pool_catch();
257 }
258 http_request_pool_try {
259 zend_llist_apply_with_argument(&pool->handles, (llist_apply_with_arg_func_t) http_request_pool_responsehandler, msg->easy_handle TSRMLS_CC);
260 } http_request_pool_catch();
261 }
262 }
263 http_request_pool_final();
264
265 return pool->unfinished;
266 }
267 /* }}} */
268
269 /* {{{ void http_request_pool_responsehandler(zval **) */
270 void _http_request_pool_responsehandler(zval **req, CURL *ch TSRMLS_DC)
271 {
272 getObjectEx(http_request_object, obj, *req);
273
274 if (obj->request->ch == ch) {
275
276 #if HTTP_DEBUG_REQPOOLS
277 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req), obj, obj->pool);
278 #endif
279
280 ZVAL_ADDREF(*req);
281 zend_llist_add_element(&obj->pool->finished, req);
282 http_request_object_responsehandler(obj, *req);
283 }
284 }
285 /* }}} */
286
287 static void move_backtrace_args(zval *from, zval *to TSRMLS_DC)
288 {
289 zval **args, **trace_0, *old_trace_0, *trace = NULL;
290
291 if ((trace = zend_read_property(zend_exception_get_default(), from, "trace", lenof("trace"), 0 TSRMLS_CC))) {
292 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void **) &trace_0)) {
293 old_trace_0 = *trace_0;
294 if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(trace_0), "args", sizeof("args"), (void **) &args)) {
295 if ((trace = zend_read_property(zend_exception_get_default(), to, "trace", lenof("trace"), 0 TSRMLS_CC))) {
296 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void **) &trace_0)) {
297 ZVAL_ADDREF(*args);
298 add_assoc_zval(*trace_0, "args", *args);
299 zend_hash_del(Z_ARRVAL_P(old_trace_0), "args", sizeof("args"));
300 }
301 }
302 }
303 }
304 }
305 }
306 /* {{{ void http_request_pool_wrap_exception(zval *, zval *) */
307 void _http_request_pool_wrap_exception(zval *old_exception, zval *new_exception TSRMLS_DC)
308 {
309 zend_class_entry *ce = HTTP_EX_CE(request_pool);
310
311 /* if old_exception is already an HttpRequestPoolException append the new one,
312 else create a new HttpRequestPoolException and append the old and new exceptions */
313 if (old_exception && Z_OBJCE_P(old_exception) == ce) {
314 zval *old_exprop, *new_exprop;
315
316 MAKE_STD_ZVAL(new_exprop);
317 array_init(new_exprop);
318 old_exprop = zend_read_property(ce, old_exception, "exceptionStack", lenof("exceptionStack"), 0 TSRMLS_CC);
319 if (Z_TYPE_P(old_exprop) == IS_ARRAY) {
320 array_copy(old_exprop, new_exprop);
321 }
322 add_next_index_zval(new_exprop, new_exception);
323 zend_update_property(ce, old_exception, "exceptionStack", lenof("exceptionStack"), new_exprop TSRMLS_CC);
324 zval_ptr_dtor(&new_exprop);
325
326 EG(exception) = old_exception;
327 } else if (new_exception && Z_OBJCE_P(new_exception) != ce){
328 zval *exval, *exprop;
329
330 MAKE_STD_ZVAL(exval);
331 object_init_ex(exval, ce);
332 MAKE_STD_ZVAL(exprop);
333 array_init(exprop);
334
335 if (old_exception) {
336 add_next_index_zval(exprop, old_exception);
337 }
338 move_backtrace_args(new_exception, exval TSRMLS_CC);
339 zend_update_property_long(ce, exval, "code", lenof("code"), HTTP_E_REQUEST_POOL TSRMLS_CC);
340 zend_update_property_string(ce, exval, "message", lenof("message"), "See exceptionStack property" TSRMLS_CC);
341 add_next_index_zval(exprop, new_exception);
342 zend_update_property(ce, exval, "exceptionStack", lenof("exceptionStack"), exprop TSRMLS_CC);
343 zval_ptr_dtor(&exprop);
344
345 EG(exception) = exval;
346 }
347 }
348 /* }}} */
349
350 /*#*/
351
352 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
353 static int http_request_pool_compare_handles(void *h1, void *h2)
354 {
355 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
356 }
357 /* }}} */
358
359 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
360
361
362 /*
363 * Local variables:
364 * tab-width: 4
365 * c-basic-offset: 4
366 * End:
367 * vim600: noet sw=4 ts=4 fdm=marker
368 * vim<600: noet sw=4 ts=4
369 */
370