- release 0.18.0
[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 #include "php.h"
19
20 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
21
22 #include "php_http.h"
23 #include "php_http_std_defs.h"
24 #include "php_http_api.h"
25 #include "php_http_request_api.h"
26 #include "php_http_request_pool_api.h"
27 #include "php_http_request_object.h"
28 #include "php_http_requestpool_object.h"
29
30 #ifndef HTTP_DEBUG_REQPOOLS
31 # define HTTP_DEBUG_REQPOOLS 0
32 #endif
33
34 ZEND_EXTERN_MODULE_GLOBALS(http);
35
36 #ifndef HAVE_CURL_MULTI_STRERROR
37 # define curl_multi_strerror(dummy) "unknown error"
38 #endif
39
40 static void http_request_pool_freebody(http_request_callback_ctx **body);
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 zend_llist_init(&pool->bodies, sizeof(http_request_callback_ctx *), (llist_dtor_func_t) http_request_pool_freebody, 0);
69
70 #if HTTP_DEBUG_REQPOOLS
71 fprintf(stderr, "Initialized request pool %p\n", pool);
72 #endif
73
74 return pool;
75 }
76 /* }}} */
77
78 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
79 PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *request TSRMLS_DC)
80 {
81 getObjectEx(http_request_object, req, request);
82
83 #if HTTP_DEBUG_REQPOOLS
84 fprintf(stderr, "Attaching HttpRequest(#%d) %p to pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
85 #endif
86
87 if (req->pool) {
88 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");
89 } else {
90 http_request_callback_ctx *body = http_request_callback_data_ex(http_request_body_new(), 0);
91
92 if (SUCCESS != http_request_pool_requesthandler(request, body->data)) {
93 http_error_ex(HE_WARNING, HTTP_E_REQUEST, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
94 } else {
95 CURLMcode code = curl_multi_add_handle(pool->ch, req->ch);
96
97 if ((CURLM_OK != code) && (CURLM_CALL_MULTI_PERFORM != code)) {
98 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code));
99 } else {
100 req->pool = pool;
101
102 zend_llist_add_element(&pool->handles, &request);
103 zend_llist_add_element(&pool->bodies, &body);
104
105 ZVAL_ADDREF(request);
106
107 #if HTTP_DEBUG_REQPOOLS
108 fprintf(stderr, "> %d HttpRequests attached to pool %p\n", zend_llist_count(&pool->handles), pool);
109 #endif
110 return SUCCESS;
111 }
112 }
113 efree(body->data);
114 efree(body);
115 }
116 return FAILURE;
117 }
118 /* }}} */
119
120 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
121 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request TSRMLS_DC)
122 {
123 CURLMcode code;
124 getObjectEx(http_request_object, req, request);
125
126 #if HTTP_DEBUG_REQPOOLS
127 fprintf(stderr, "Detaching HttpRequest(#%d) %p from pool %p\n", Z_OBJ_HANDLE_P(request), req, pool);
128 #endif
129
130 if (!req->pool) {
131 /* not attached to any pool */
132 #if HTTP_DEBUG_REQPOOLS
133 fprintf(stderr, "HttpRequest object(#%d) %p is not attached to any HttpRequestPool\n", Z_OBJ_HANDLE_P(request), req);
134 #endif
135 } else if (req->pool != pool) {
136 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "HttpRequest object(#%d) is not attached to this HttpRequestPool", Z_OBJ_HANDLE_P(request));
137 } else if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->ch))) {
138 http_error_ex(HE_WARNING, HTTP_E_REQUEST_POOL, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code));
139 } else {
140 req->pool = NULL;
141 zend_llist_del_element(&pool->finished, request, http_request_pool_compare_handles);
142 zend_llist_del_element(&pool->handles, request, http_request_pool_compare_handles);
143
144 #if HTTP_DEBUG_REQPOOLS
145 fprintf(stderr, "> %d HttpRequests remaining in pool %p\n", zend_llist_count(&pool->handles), pool);
146 #endif
147
148 return SUCCESS;
149 }
150 return FAILURE;
151 }
152 /* }}} */
153
154 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
155 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
156 {
157 int count = zend_llist_count(&pool->handles);
158
159 #if HTTP_DEBUG_REQPOOLS
160 fprintf(stderr, "Detaching %d requests from pool %p\n", count, pool);
161 #endif
162
163 /*
164 * we cannot apply a function to the llist which actually detaches
165 * the curl handle *and* removes the llist element --
166 * so let's get our hands dirty
167 */
168 if (count) {
169 int i = 0;
170 zend_llist_position pos;
171 zval **handle, **handles = emalloc(count * sizeof(zval *));
172
173 for (handle = zend_llist_get_first_ex(&pool->handles, &pos); handle; handle = zend_llist_get_next_ex(&pool->handles, &pos)) {
174 handles[i++] = *handle;
175 }
176
177 /* should never happen */
178 if (i != count) {
179 zend_error(E_ERROR, "number of fetched request handles do not match overall count");
180 count = i;
181 }
182
183 for (i = 0; i < count; ++i) {
184 http_request_pool_detach(pool, handles[i]);
185 }
186 efree(handles);
187 }
188
189 #if HTTP_DEBUG_REQPOOLS
190 fprintf(stderr, "Destroying %d request bodies of pool %p\n", zend_llist_count(&pool->bodies), pool);
191 #endif
192
193 /* free created bodies too */
194 zend_llist_clean(&pool->bodies);
195 }
196
197 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
198 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
199 {
200 #if HTTP_DEBUG_REQPOOLS
201 fprintf(stderr, "Attempt to send %d requests of pool %p\n", zend_llist_count(&pool->handles), pool);
202 #endif
203
204 while (http_request_pool_perform(pool)) {
205 if (SUCCESS != http_request_pool_select(pool)) {
206 #ifdef PHP_WIN32
207 http_error(HE_WARNING, HTTP_E_SOCKET, WSAGetLastError());
208 #else
209 http_error(HE_WARNING, HTTP_E_SOCKET, strerror(errno));
210 #endif
211 return FAILURE;
212 }
213 }
214
215 #if HTTP_DEBUG_REQPOOLS
216 fprintf(stderr, "Finished sending %d HttpRequests of pool %p (still unfinished: %d)\n", zend_llist_count(&pool->handles), pool, pool->unfinished);
217 #endif
218
219 return SUCCESS;
220 }
221 /* }}} */
222
223 /* {{{ void http_request_pool_dtor(http_request_pool *) */
224 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
225 {
226 #if HTTP_DEBUG_REQPOOLS
227 fprintf(stderr, "Destructing request pool %p\n", pool);
228 #endif
229
230 pool->unfinished = 0;
231 zend_llist_clean(&pool->finished);
232 zend_llist_clean(&pool->handles);
233 zend_llist_clean(&pool->bodies);
234 curl_multi_cleanup(pool->ch);
235 }
236 /* }}} */
237
238 #ifdef PHP_WIN32
239 # define SELECT_ERROR SOCKET_ERROR
240 #else
241 # define SELECT_ERROR -1
242 #endif
243
244 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
245 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
246 {
247 int MAX;
248 fd_set R, W, E;
249 struct timeval timeout = {1, 0};
250
251 FD_ZERO(&R);
252 FD_ZERO(&W);
253 FD_ZERO(&E);
254
255 if (CURLM_OK == curl_multi_fdset(pool->ch, &R, &W, &E, &MAX)) {
256 if (MAX == -1 || SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
257 return SUCCESS;
258 }
259 }
260 return FAILURE;
261 }
262 /* }}} */
263
264 /* {{{ int http_request_pool_perform(http_request_pool *) */
265 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool TSRMLS_DC)
266 {
267 CURLMsg *msg;
268 int remaining = 0;
269
270 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
271
272 while ((msg = curl_multi_info_read(pool->ch, &remaining))) {
273 if (CURLMSG_DONE == msg->msg) {
274 if (CURLE_OK != msg->data.result) {
275 http_error(HE_WARNING, HTTP_E_REQUEST, curl_easy_strerror(msg->data.result));
276 }
277 zend_llist_apply_with_argument(&pool->handles, (llist_apply_with_arg_func_t) http_request_pool_responsehandler, msg->easy_handle TSRMLS_CC);
278 }
279 }
280
281 return pool->unfinished;
282 }
283 /* }}} */
284
285 /* {{{ STATUS http_request_pool_requesthandler(zval *, http_request_body *) */
286 STATUS _http_request_pool_requesthandler(zval *request, http_request_body *body TSRMLS_DC)
287 {
288 getObjectEx(http_request_object, req, request);
289 if (SUCCESS == http_request_object_requesthandler(req, request, body)) {
290 http_request_conv(req->ch, &req->response, &req->request);
291 return SUCCESS;
292 }
293 return FAILURE;
294 }
295 /* }}} */
296
297 /* {{{ void http_request_pool_responsehandler(zval **) */
298 void _http_request_pool_responsehandler(zval **req, CURL *ch TSRMLS_DC)
299 {
300 getObjectEx(http_request_object, obj, *req);
301
302 if (obj->ch == ch) {
303
304 #if HTTP_DEBUG_REQPOOLS
305 fprintf(stderr, "Fetching data from HttpRequest(#%d) %p of pool %p\n", Z_OBJ_HANDLE_PP(req), obj, obj->pool);
306 #endif
307
308 ZVAL_ADDREF(*req);
309 zend_llist_add_element(&obj->pool->finished, req);
310 http_request_object_responsehandler(obj, *req);
311 }
312 }
313 /* }}} */
314
315 /*#*/
316
317 /* {{{ static void http_request_pool_freebody(http_request_ctx **) */
318 static void http_request_pool_freebody(http_request_callback_ctx **body)
319 {
320 HTTP_REQUEST_CALLBACK_DATA(*body, http_request_body *, b);
321 http_request_body_free(b);
322 efree(*body);
323 }
324 /* }}} */
325
326 /* {{{ static int http_request_pool_compare_handles(void *, void *) */
327 static int http_request_pool_compare_handles(void *h1, void *h2)
328 {
329 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
330 }
331 /* }}} */
332
333 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
334
335
336 /*
337 * Local variables:
338 * tab-width: 4
339 * c-basic-offset: 4
340 * End:
341 * vim600: noet sw=4 ts=4 fdm=marker
342 * vim<600: noet sw=4 ts=4
343 */
344