fix build with old curl version
[m6w6/ext-http] / php_http_client_factory.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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #include <ext/spl/spl_observer.h>
16
17 /*
18 * array of name => php_http_client_factory_driver_t*
19 */
20 static HashTable php_http_client_factory_drivers;
21
22 PHP_HTTP_API STATUS php_http_client_factory_add_driver(const char *name_str, size_t name_len, php_http_client_factory_driver_t *driver)
23 {
24 return zend_hash_add(&php_http_client_factory_drivers, name_str, name_len + 1, (void *) driver, sizeof(php_http_client_factory_driver_t), NULL);
25 }
26
27 PHP_HTTP_API STATUS php_http_client_factory_get_driver(const char *name_str, size_t name_len, php_http_client_factory_driver_t *driver)
28 {
29 php_http_client_factory_driver_t *tmp;
30
31 if (SUCCESS == zend_hash_find(&php_http_client_factory_drivers, name_str, name_len + 1, (void *) &tmp)) {
32 *driver = *tmp;
33 return SUCCESS;
34 }
35 return FAILURE;
36 }
37
38 static zend_class_entry *php_http_client_factory_find_class_entry(zval *this_ptr, const char *for_str, size_t for_len TSRMLS_DC)
39 {
40 /* stupid non-const api */
41 char *sc = estrndup(for_str, for_len);
42 zval *cn = zend_read_property(Z_OBJCE_P(getThis()), getThis(), sc, for_len, 0 TSRMLS_CC);
43
44 efree(sc);
45 if (Z_TYPE_P(cn) == IS_STRING && Z_STRLEN_P(cn)) {
46 return zend_fetch_class(Z_STRVAL_P(cn), Z_STRLEN_P(cn), 0 TSRMLS_CC);
47 }
48
49 return NULL;
50 }
51
52 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientFactory, method, 0, req_args)
53 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientFactory, method, 0)
54 #define PHP_HTTP_REQUEST_FACTORY_ME(method, visibility) PHP_ME(HttpClientFactory, method, PHP_HTTP_ARGS(HttpClientFactory, method), visibility)
55 #define PHP_HTTP_REQUEST_FACTORY_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientFactory, method))
56 #define PHP_HTTP_REQUEST_FACTORY_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClientFactory_##al), PHP_HTTP_ARGS(HttpClientFactory, al), vis)
57
58 PHP_HTTP_BEGIN_ARGS(__construct, 0)
59 PHP_HTTP_ARG_VAL(options, 0)
60 PHP_HTTP_END_ARGS;
61 PHP_HTTP_BEGIN_ARGS(createClient, 0)
62 PHP_HTTP_ARG_ARR(options, 1, 0)
63 PHP_HTTP_END_ARGS;
64 PHP_HTTP_BEGIN_ARGS(createPool, 0)
65 PHP_HTTP_ARG_OBJ(http\\Client, client1, 1)
66 PHP_HTTP_ARG_OBJ(http\\Client, client2, 1)
67 PHP_HTTP_ARG_OBJ(http\\Client, clientN, 1)
68 PHP_HTTP_END_ARGS;
69 PHP_HTTP_BEGIN_ARGS(createDataShare, 0)
70 PHP_HTTP_ARG_OBJ(http\\Client, client1, 1)
71 PHP_HTTP_ARG_OBJ(http\\Client, client2, 1)
72 PHP_HTTP_ARG_OBJ(http\\Client, clientN, 1)
73 PHP_HTTP_END_ARGS;
74 PHP_HTTP_EMPTY_ARGS(getDriver);
75 PHP_HTTP_EMPTY_ARGS(getAvailableDrivers);
76
77 static zend_class_entry *php_http_client_factory_class_entry;
78
79 zend_class_entry *php_http_client_factory_get_class_entry(void)
80 {
81 return php_http_client_factory_class_entry;
82 }
83
84 static zend_function_entry php_http_client_factory_method_entry[] = {
85 PHP_HTTP_REQUEST_FACTORY_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
86 PHP_HTTP_REQUEST_FACTORY_ME(createClient, ZEND_ACC_PUBLIC)
87 PHP_HTTP_REQUEST_FACTORY_ME(createPool, ZEND_ACC_PUBLIC)
88 PHP_HTTP_REQUEST_FACTORY_ME(createDataShare, ZEND_ACC_PUBLIC)
89 PHP_HTTP_REQUEST_FACTORY_ME(getDriver, ZEND_ACC_PUBLIC)
90 PHP_HTTP_REQUEST_FACTORY_ME(getAvailableDrivers, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
91
92 EMPTY_FUNCTION_ENTRY
93 };
94
95 PHP_METHOD(HttpClientFactory, __construct)
96 {
97 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
98 HashTable *options = NULL;
99
100 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h", &options)) {
101 if (options) {
102 zval **val;
103 HashPosition pos;
104 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
105
106 FOREACH_HASH_KEYVAL(pos, options, key, val) {
107 if (key.type == HASH_KEY_IS_STRING) {
108 zval *newval = php_http_zsep(1, Z_TYPE_PP(val), *val);
109 zend_update_property(php_http_client_factory_class_entry, getThis(), key.str, key.len - 1, newval TSRMLS_CC);
110 zval_ptr_dtor(&newval);
111 }
112 }
113 }
114 }
115 } end_error_handling();
116 }
117
118 PHP_METHOD(HttpClientFactory, createClient)
119 {
120 zval *options = NULL;
121
122 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
123 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!", &options)) {
124 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
125 zval *zdriver;
126 zend_object_value ov;
127 zend_class_entry *class_entry = NULL;
128 php_http_client_t *req = NULL;
129 php_http_client_factory_driver_t driver;
130
131 zdriver = zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
132
133 if ((IS_STRING == Z_TYPE_P(zdriver)) && (SUCCESS == php_http_client_factory_get_driver(Z_STRVAL_P(zdriver), Z_STRLEN_P(zdriver), &driver)) && driver.client_ops) {
134 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
135 php_http_resource_factory_t *rf = NULL;
136
137 if (Z_STRLEN_P(phi)) {
138 char *name_str;
139 size_t name_len;
140 php_http_persistent_handle_factory_t *pf;
141
142 name_len = spprintf(&name_str, 0, "http_client.%s", Z_STRVAL_P(zdriver));
143
144 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
145 rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon);
146 }
147
148 efree(name_str);
149 }
150
151 req = php_http_client_init(NULL, driver.client_ops, rf, NULL TSRMLS_CC);
152 if (req) {
153 if (!(class_entry = php_http_client_factory_find_class_entry(getThis(), ZEND_STRL("clientClass") TSRMLS_CC))) {
154 class_entry = driver.client_ops->class_entry();
155 }
156
157 if (SUCCESS == php_http_new(&ov, class_entry, driver.client_ops->create_object, driver.client_ops->class_entry(), req, NULL TSRMLS_CC)) {
158 ZVAL_OBJVAL(return_value, ov, 0);
159 zend_call_method(&return_value, Z_OBJCE_P(return_value), NULL, ZEND_STRL("__construct"), NULL, !!options, options, NULL TSRMLS_CC);
160 } else {
161 php_http_client_free(&req);
162 }
163 }
164
165 zval_ptr_dtor(&phi);
166 } else {
167 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "clients are not supported by this driver");
168 }
169 } end_error_handling();
170 }
171 } end_error_handling();
172 }
173
174 PHP_METHOD(HttpClientFactory, createPool)
175 {
176 int argc = 0;
177 zval ***argv = NULL;
178
179 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
180 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &argv, &argc)) {
181 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
182 int i;
183 zval *zdriver;
184 zend_object_value ov;
185 zend_class_entry *class_entry = NULL;
186 php_http_client_pool_t *pool = NULL;
187 php_http_client_factory_driver_t driver;
188
189 zdriver = zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
190 if ((IS_STRING == Z_TYPE_P(zdriver)) && (SUCCESS == php_http_client_factory_get_driver(Z_STRVAL_P(zdriver), Z_STRLEN_P(zdriver), &driver)) && driver.client_pool_ops) {
191 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
192 php_http_resource_factory_t *rf = NULL;
193
194 if (Z_STRLEN_P(phi)) {
195 char *name_str;
196 size_t name_len;
197 php_http_persistent_handle_factory_t *pf;
198
199 name_len = spprintf(&name_str, 0, "http_client_pool.%s", Z_STRVAL_P(zdriver));
200
201 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
202 rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon);
203 }
204
205 efree(name_str);
206 }
207
208 pool = php_http_client_pool_init(NULL, driver.client_pool_ops, rf, NULL TSRMLS_CC);
209 if (pool) {
210 if (!(class_entry = php_http_client_factory_find_class_entry(getThis(), ZEND_STRL("clientPoolClass") TSRMLS_CC))) {
211 class_entry = driver.client_pool_ops->class_entry();
212 }
213
214 if (SUCCESS == php_http_new(&ov, class_entry, driver.client_pool_ops->create_object, driver.client_pool_ops->class_entry(), pool, NULL TSRMLS_CC)) {
215 ZVAL_OBJVAL(return_value, ov, 0);
216 for (i = 0; i < argc; ++i) {
217 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_client_get_class_entry() TSRMLS_CC)) {
218 php_http_client_pool_attach(pool, *(argv[i]));
219 }
220 }
221 } else {
222 php_http_client_pool_free(&pool);
223 }
224 }
225
226 zval_ptr_dtor(&phi);
227 } else {
228 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "pools are not supported by this driver");
229 }
230 } end_error_handling();
231
232 if (argv) {
233 efree(argv);
234 }
235 }
236 } end_error_handling();
237 }
238
239 PHP_METHOD(HttpClientFactory, createDataShare)
240 {
241 int argc = 0;
242 zval ***argv = NULL;
243
244 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
245 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "*", &argv, &argc)) {
246 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
247 int i;
248 zval *zdriver;
249 zend_object_value ov;
250 zend_class_entry *class_entry;
251 php_http_client_datashare_t *share = NULL;
252 php_http_client_factory_driver_t driver;
253
254 zdriver = zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
255 if ((IS_STRING == Z_TYPE_P(zdriver)) && (SUCCESS == php_http_client_factory_get_driver(Z_STRVAL_P(zdriver), Z_STRLEN_P(zdriver), &driver)) && driver.client_datashare_ops) {
256 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
257 php_http_resource_factory_t *rf = NULL;
258
259 if (Z_STRLEN_P(phi)) {
260 char *name_str;
261 size_t name_len;
262 php_http_persistent_handle_factory_t *pf;
263
264 name_len = spprintf(&name_str, 0, "http_client_datashare.%s", Z_STRVAL_P(zdriver));
265
266 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
267 rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon);
268 }
269
270 efree(name_str);
271 }
272
273 share = php_http_client_datashare_init(NULL, driver.client_datashare_ops, rf, NULL TSRMLS_CC);
274 if (share) {
275 if (!(class_entry = php_http_client_factory_find_class_entry(getThis(), ZEND_STRL("clientDataShareClass") TSRMLS_CC))) {
276 class_entry = driver.client_datashare_ops->class_entry();
277 }
278
279 if (SUCCESS == php_http_new(&ov, class_entry, driver.client_datashare_ops->create_object, driver.client_datashare_ops->class_entry(), share, NULL TSRMLS_CC)) {
280 ZVAL_OBJVAL(return_value, ov, 0);
281 for (i = 0; i < argc; ++i) {
282 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_client_get_class_entry() TSRMLS_CC)) {
283 php_http_client_datashare_attach(share, *(argv[i]));
284 }
285 }
286 } else {
287 php_http_client_datashare_free(&share);
288 }
289 }
290
291 zval_ptr_dtor(&phi);
292 } else {
293 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "datashares are not supported by this driver");
294 }
295 } end_error_handling();
296 if (argv) {
297 efree(argv);
298 }
299 }
300 } end_error_handling();
301 }
302
303 PHP_METHOD(HttpClientFactory, getDriver)
304 {
305 if (SUCCESS == zend_parse_parameters_none()) {
306 RETURN_PROP(php_http_client_factory_class_entry, "driver");
307 }
308 RETURN_FALSE;
309 }
310
311 PHP_METHOD(HttpClientFactory, getAvailableDrivers)
312 {
313 if (SUCCESS == zend_parse_parameters_none()) {
314 HashPosition pos;
315 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
316
317 array_init(return_value);
318 FOREACH_HASH_KEY(pos, &php_http_client_factory_drivers, key) {
319 add_next_index_stringl(return_value, key.str, key.len - 1, 1);
320 }
321 return;
322 }
323 RETURN_FALSE;
324 }
325
326 PHP_MINIT_FUNCTION(http_client_factory)
327 {
328 zend_hash_init(&php_http_client_factory_drivers, 0, NULL, NULL, 1);
329
330 PHP_HTTP_REGISTER_CLASS(http\\Client, Factory, http_client_factory, php_http_object_get_class_entry(), 0);
331 php_http_client_factory_class_entry->create_object = php_http_client_factory_new;
332
333 zend_declare_property_stringl(php_http_client_factory_class_entry, ZEND_STRL("driver"), ZEND_STRL("curl"), ZEND_ACC_PROTECTED TSRMLS_CC);
334 zend_declare_property_null(php_http_client_factory_class_entry, ZEND_STRL("persistentHandleId"), ZEND_ACC_PROTECTED TSRMLS_CC);
335 zend_declare_property_null(php_http_client_factory_class_entry, ZEND_STRL("clientClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
336 zend_declare_property_null(php_http_client_factory_class_entry, ZEND_STRL("clientPoolClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
337 zend_declare_property_null(php_http_client_factory_class_entry, ZEND_STRL("clientDataShareClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
338
339 return SUCCESS;
340 }
341
342 PHP_MSHUTDOWN_FUNCTION(http_client_factory)
343 {
344 zend_hash_destroy(&php_http_client_factory_drivers);
345
346 return SUCCESS;
347 }
348
349
350 /*
351 * Local variables:
352 * tab-width: 4
353 * c-basic-offset: 4
354 * End:
355 * vim600: noet sw=4 ts=4 fdm=marker
356 * vim<600: noet sw=4 ts=4
357 */
358