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