2a297d680f0f4a71a8757c473f515d1c31d7f791
[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 void http_request_pool_freebody(http_request_callback_ctx **body);
42 static int http_request_pool_compare_handles(void *h1, void *h2);
43
44 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
45 PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool TSRMLS_DC)
46 {
47 zend_bool free_pool;
48
49 #if HTTP_DEBUG_REQPOOLS
50 fprintf(stderr, "Initializing request pool %p\n", pool);
51 #endif
52
53 if ((free_pool = (!pool))) {
54 pool = emalloc(sizeof(http_request_pool));
55 pool->ch = NULL;
56 }
57
58 HTTP_CHECK_CURL_INIT(pool->ch, curl_multi_init(), ;);
59 if (!pool->ch) {
60 if (free_pool) {
61 efree(pool);
62 }
63 return NULL;
64 }
65
66 pool->unfinished = 0;
67 zend_llist_init(&pool->finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
68 zend_llist_init(&pool->handles, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
69 zend_llist_init(&pool->bodies, sizeof(http_request_callback_ctx *), (llist_dtor_func_t) http_request_pool_freebody, 0);
70
71 #if HTTP_DEBUG_REQPOOLS
72 fprintf(stderr, "Initialized request pool %p\n", pool);
73 #endif
74
75 return pool;
76 }
77 /* }}} */
78
79 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
80 PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *request TSRMLS_DC)
81 {
82 getObjectEx(http_request_object, req, request);
83
84 #if HTTP_DEBUG_REQPOOLS
85 fprintf(stderr, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
86 #endif
87
88 if (req->pool) {
89 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");
90 } else {
91 http_request_callback_ctx *body = http_request_callback_data_ex(http_request_body_new(), 0);
92
93 if (SUCCESS != http_request_pool_requesthandler(request, body->data)) {
94 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
95 } else {
96 CURLMcode code = curl_multi_add_handle(pool->ch, req->ch);
97
98 if ((CURLM_OK != code) && (CURLM_CALL_MULTI_PERFORM != code)) {
99 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code));
100 } else {
101 req->pool = pool;
102
103 zend_llist_add_element(&pool->handles, &request);
104 zend_llist_add_element(&pool->bodies, &body);
105
106 ZVAL_ADDREF(request);
107
108 #if HTTP_DEBUG_REQPOOLS
109 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
110 #endif
111 return SUCCESS;
112 }
113 }
114 efree(body->data);
115 efree(body);
116 }
117 return FAILURE;
118 }
119 /* }}} */
120
121 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
122 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request TSRMLS_DC)
123 {
124 CURLMcode code;
125 getObjectEx(http_request_object, req, request);
126
127 #if HTTP_DEBUG_REQPOOLS
128 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
129 #endif
130
131 if (!req->pool) {
132 /* not attached to any pool */
133 #if HTTP_DEBUG_REQPOOLS
134 fprintf(stderr, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request), req);
135 #endif
136 } else if (req->pool != pool) {
137 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
138 } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->ch))) {
139 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object from the HttpRequestPool: %s", 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_detach_all(http_request_pool *) */
156 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
157 {
158 int count = zend_llist_count(&pool->handles);
159
160 #if HTTP_DEBUG_REQPOOLS
161 fprintf(stderr, "Detaching %d requests from pool %p\n", count, pool);
162 #endif
163
164 /*
165 * we cannot apply a function to the llist which actually detaches
166 * the curl handle *and* removes the llist element --
167 * so let's get our hands dirty
168 */
169 if (count) {
170 int i = 0;
171 zend_llist_position pos;
172 zval **handle, **handles = emalloc(count * sizeof(zval *));
173
174 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
175 handles[i++] = *handle;
176 }
177
178 /* should never happen */
179 if (i != count) {
180 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
181 count = i;
182 }
183
184 for (i = 0; i < count; ++i) {
185 http_request_pool_detach(pool, handles[i]);
186 }
187 efree(handles);
188 }
189
190 #if HTTP_DEBUG_REQPOOLS
191 fprintf(stderr, "Destroying %d request bodies of pool %p\n", zend_llist_count(&pool->bodies), pool);
192 #endif
193
194 /* free created bodies too */
195 zend_llist_clean(&pool->bodies);
196 }
197
198 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
199 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
200 {
201 #if HTTP_DEBUG_REQPOOLS
202 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
203 #endif
204
205 while (http_request_pool_perform(pool)) {
206 if (SUCCESS != http_request_pool_select(pool)) {
207 #ifdef PHP_WIN32
208 http_error(HE_WARNING, HTTP_E_SOCKET, WSAGetLastError());
209 #else
210 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
211 #endif
212 return FAILURE;
213 }
214 }
215
216 #if HTTP_DEBUG_REQPOOLS
217 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
218 #endif
219
220 return SUCCESS;
221 }
222 /* }}} */
223
224 /* {{{ void http_request_pool_dtor(http_request_pool *) */
225 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
226 {
227 #if HTTP_DEBUG_REQPOOLS
228 fprintf(stderr, "Destructing request pool %p\n", pool);
229 #endif
230
231 pool->unfinished = 0;
232 zend_llist_clean(&pool->finished);
233 zend_llist_clean(&pool->handles);
234 zend_llist_clean(&pool->bodies);
235 curl_multi_cleanup(pool->ch);
236 }
237 /* }}} */
238
239 #ifdef PHP_WIN32
240 # define SELECT_ERROR SOCKET_ERROR
241 #else
242 # define SELECT_ERROR -1
243 #endif
244
245 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
246 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
247 {
248 int MAX;
249 fd_set R, W, E;
250 struct timeval timeout = {1, 0};
251
252 FD_ZERO(&R);
253 FD_ZERO(&W);
254 FD_ZERO(&E);
255
256 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
257 if (MAX == -1 || SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
258 return SUCCESS;
259 }
260 }
261 return FAILURE;
262 }
263 /* }}} */
264
265 /* {{{ int http_request_pool_perform(http_request_pool *) */
266 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool TSRMLS_DC)
267 {
268 CURLMsg *msg;
269 int remaining = 0;
270
271 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
272
273 while ((msg = curl_multi_info_read(pool->ch, &remaining))) {
274 if (CURLMSG_DONE == msg->msg) {
275 if (CURLE_OK != msg->data.result) {
276 http_request_pool_try {
277 http_error(HE_WARNING, HTTP_E_REQUEST, curl_easy_strerror(msg->data.result));
278 } http_request_pool_catch();
279 }
280 http_request_pool_try {
281 zend_llist_apply_with_argument(&pool->handles, (llist_apply_with_arg_func_t) http_request_pool_responsehandler, msg->easy_handle TSRMLS_CC);
282 } http_request_pool_catch();
283 }
284 }
285 http_request_pool_final();
286
287 return pool->unfinished;
288 }
289 /* }}} */
290
291 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
292 STATUS _http_request_pool_requesthandler(zval *request, http_request_body *body TSRMLS_DC)
293 {
294 getObjectEx(http_request_object, req, request);
295 if (SUCCESS == http_request_object_requesthandler(req, request, body)) {
296 http_request_conv(req->ch, &req->response, &req->request);
297 return SUCCESS;
298 }
299 return FAILURE;
300 }
301 /* }}} */
302
303 /* {{{ void http_request_pool_responsehandler(zval **) */
304 void _http_request_pool_responsehandler(zval **req, CURL *ch TSRMLS_DC)
305 {
306 getObjectEx(http_request_object, obj, *req);
307
308 if (obj->ch == ch) {
309
310 #if HTTP_DEBUG_REQPOOLS
311 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req), obj, obj->pool);
312 #endif
313
314 ZVAL_ADDREF(*req);
315 zend_llist_add_element(&obj->pool->finished, req);
316 http_request_object_responsehandler(obj, *req);
317 }
318 }
319 /* }}} */
320
321 static void move_backtrace_args(zval *from, zval *to TSRMLS_DC)
322 {
323 zval **args, **trace_0, *old_trace_0, *trace = NULL;
324
325 if ((trace = zend_read_property(zend_exception_get_default(), from, "trace", lenof("trace"), 0 TSRMLS_CC))) {
326 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void **) &trace_0)) {
327 old_trace_0 = *trace_0;
328 if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(trace_0), "args", sizeof("args"), (void **) &args)) {
329 if ((trace = zend_read_property(zend_exception_get_default(), to, "trace", lenof("trace"), 0 TSRMLS_CC))) {
330 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(trace), 0, (void **) &trace_0)) {
331 ZVAL_ADDREF(*args);
332 add_assoc_zval(*trace_0, "args", *args);
333 zend_hash_del(Z_ARRVAL_P(old_trace_0), "args", sizeof("args"));
334 }
335 }
336 }
337 }
338 }
339 }
340 /* {{{ void http_request_pool_wrap_exception(zval *, zval *) */
341 void _http_request_pool_wrap_exception(zval *old_exception, zval *new_exception TSRMLS_DC)
342 {
343 zend_class_entry *ce = HTTP_EX_CE(request_pool);
344
345 /* if old_exception is already an HttpRequestPoolException append the new one,
346 else create a new HttpRequestPoolException and append the old and new exceptions */
347 if (old_exception && Z_OBJCE_P(old_exception) == ce) {
348 zval *exprop;
349
350 exprop = zend_read_property(ce, old_exception, "exceptionStack", lenof("exceptionStack"), 0 TSRMLS_CC);
351 SEP_PROP(&exprop);
352 convert_to_array(exprop);
353
354 add_next_index_zval(exprop, new_exception);
355 zend_update_property(ce, old_exception, "exceptionStack", lenof("exceptionStack"), exprop TSRMLS_CC);
356
357 EG(exception) = old_exception;
358 } else if (new_exception && Z_OBJCE_P(new_exception) != ce){
359 zval *exval, *exprop;
360
361 MAKE_STD_ZVAL(exval);
362 object_init_ex(exval, ce);
363 MAKE_STD_ZVAL(exprop);
364 array_init(exprop);
365
366 if (old_exception) {
367 add_next_index_zval(exprop, old_exception);
368 }
369 move_backtrace_args(new_exception, exval TSRMLS_CC);
370 zend_update_property_long(ce, exval, "code", lenof("code"), HTTP_E_REQUEST_POOL TSRMLS_CC);
371 zend_update_property_string(ce, exval, "message", lenof("message"), "See exceptionStack property" TSRMLS_CC);
372 add_next_index_zval(exprop, new_exception);
373 zend_update_property(ce, exval, "exceptionStack", lenof("exceptionStack"), exprop TSRMLS_CC);
374 zval_ptr_dtor(&exprop);
375
376 EG(exception) = exval;
377 }
378 }
379 /* }}} */
380
381 /*#*/
382
383 /* {{{ static void http_request_pool_freebody(http_request_ctx **) */
384 static void http_request_pool_freebody(http_request_callback_ctx **body)
385 {
386 HTTP_REQUEST_CALLBACK_DATA(*body, http_request_body *, b);
387 http_request_body_free(&b);
388 efree(*body);
389 *body = NULL;
390 }
391 /* }}} */
392
393 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
394 static int http_request_pool_compare_handles(void *h1, void *h2)
395 {
396 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
397 }
398 /* }}} */
399
400 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
401
402
403 /*
404 * Local variables:
405 * tab-width: 4
406 * c-basic-offset: 4
407 * End:
408 * vim600: noet sw=4 ts=4 fdm=marker
409 * vim<600: noet sw=4 ts=4
410 */
411