batch renaming: client*curl curl*client
[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_get_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 zend_class_entry *php_http_client_factory_class_entry;
78 zend_function_entry php_http_client_factory_method_entry[] = {
79 PHP_HTTP_REQUEST_FACTORY_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
80 PHP_HTTP_REQUEST_FACTORY_ME(createClient, ZEND_ACC_PUBLIC)
81 PHP_HTTP_REQUEST_FACTORY_ME(createPool, ZEND_ACC_PUBLIC)
82 PHP_HTTP_REQUEST_FACTORY_ME(createDataShare, ZEND_ACC_PUBLIC)
83 PHP_HTTP_REQUEST_FACTORY_ME(getDriver, ZEND_ACC_PUBLIC)
84 PHP_HTTP_REQUEST_FACTORY_ME(getAvailableDrivers, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
85
86 EMPTY_FUNCTION_ENTRY
87 };
88
89 PHP_METHOD(HttpClientFactory, __construct)
90 {
91 with_error_handling(EH_THROW, php_http_exception_class_entry) {
92 HashTable *options = NULL;
93
94 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h", &options)) {
95 if (options) {
96 zval **val;
97 HashPosition pos;
98 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
99
100 FOREACH_HASH_KEYVAL(pos, options, key, val) {
101 if (key.type == HASH_KEY_IS_STRING) {
102 zval *newval = php_http_zsep(1, Z_TYPE_PP(val), *val);
103 zend_update_property(php_http_client_factory_class_entry, getThis(), key.str, key.len - 1, newval TSRMLS_CC);
104 zval_ptr_dtor(&newval);
105 }
106 }
107 }
108 }
109 } end_error_handling();
110 }
111
112 PHP_METHOD(HttpClientFactory, createClient)
113 {
114 zval *options = NULL;
115
116 with_error_handling(EH_THROW, php_http_exception_class_entry) {
117 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!", &options)) {
118 with_error_handling(EH_THROW, php_http_exception_class_entry) {
119 zval *zdriver;
120 zend_object_value ov;
121 zend_class_entry *class_entry = NULL;
122 php_http_client_t *req = NULL;
123 php_http_client_factory_driver_t driver;
124
125 zdriver = zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
126
127 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) {
128 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
129 php_http_resource_factory_t *rf = NULL;
130
131 if (Z_STRLEN_P(phi)) {
132 char *name_str;
133 size_t name_len;
134 php_http_persistent_handle_factory_t *pf;
135
136 name_len = spprintf(&name_str, 0, "http_client.%s", Z_STRVAL_P(zdriver));
137
138 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
139 rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon);
140 }
141
142 efree(name_str);
143 }
144
145 req = php_http_client_init(NULL, driver.client_ops, rf, NULL TSRMLS_CC);
146 if (req) {
147 if (!(class_entry = php_http_client_factory_get_class_entry(getThis(), ZEND_STRL("clientClass") TSRMLS_CC))) {
148 class_entry = driver.client_ops->class_entry();
149 }
150
151 if (SUCCESS == php_http_new(&ov, class_entry, driver.client_ops->create_object, driver.client_ops->class_entry(), req, NULL TSRMLS_CC)) {
152 ZVAL_OBJVAL(return_value, ov, 0);
153 zend_call_method(&return_value, Z_OBJCE_P(return_value), NULL, ZEND_STRL("__construct"), NULL, !!options, options, NULL TSRMLS_CC);
154 } else {
155 php_http_client_free(&req);
156 }
157 }
158
159 zval_ptr_dtor(&phi);
160 } else {
161 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "clients are not supported by this driver");
162 }
163 } end_error_handling();
164 }
165 } end_error_handling();
166 }
167
168 PHP_METHOD(HttpClientFactory, createPool)
169 {
170 int argc = 0;
171 zval ***argv;
172
173 with_error_handling(EH_THROW, php_http_exception_class_entry) {
174 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|*", &argv, &argc)) {
175 with_error_handling(EH_THROW, php_http_exception_class_entry) {
176 int i;
177 zval *zdriver;
178 zend_object_value ov;
179 zend_class_entry *class_entry = NULL;
180 php_http_client_pool_t *pool = NULL;
181 php_http_client_factory_driver_t driver;
182
183 zdriver = zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
184 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) {
185 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
186 php_http_resource_factory_t *rf = NULL;
187
188 if (Z_STRLEN_P(phi)) {
189 char *name_str;
190 size_t name_len;
191 php_http_persistent_handle_factory_t *pf;
192
193 name_len = spprintf(&name_str, 0, "http_client_pool.%s", Z_STRVAL_P(zdriver));
194
195 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
196 rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon);
197 }
198
199 efree(name_str);
200 }
201
202 pool = php_http_client_pool_init(NULL, driver.client_pool_ops, rf, NULL TSRMLS_CC);
203 if (pool) {
204 if (!(class_entry = php_http_client_factory_get_class_entry(getThis(), ZEND_STRL("clientPoolClass") TSRMLS_CC))) {
205 class_entry = driver.client_pool_ops->class_entry();
206 }
207
208 if (SUCCESS == php_http_new(&ov, class_entry, driver.client_pool_ops->create_object, driver.client_pool_ops->class_entry(), pool, NULL TSRMLS_CC)) {
209 ZVAL_OBJVAL(return_value, ov, 0);
210 for (i = 0; i < argc; ++i) {
211 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_client_get_class_entry() TSRMLS_CC)) {
212 php_http_client_pool_attach(pool, *(argv[i]));
213 }
214 }
215 } else {
216 php_http_client_pool_free(&pool);
217 }
218 }
219
220 zval_ptr_dtor(&phi);
221 } else {
222 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "pools are not supported by this driver");
223 }
224 } end_error_handling();
225 }
226 } end_error_handling();
227 }
228
229 PHP_METHOD(HttpClientFactory, createDataShare)
230 {
231 int argc = 0;
232 zval ***argv;
233
234 with_error_handling(EH_THROW, php_http_exception_class_entry) {
235 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|*", &argv, &argc)) {
236 with_error_handling(EH_THROW, php_http_exception_class_entry) {
237 int i;
238 zval *zdriver;
239 zend_object_value ov;
240 zend_class_entry *class_entry;
241 php_http_client_datashare_t *share = NULL;
242 php_http_client_factory_driver_t driver;
243
244 zdriver = zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
245 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) {
246 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_client_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
247 php_http_resource_factory_t *rf = NULL;
248
249 if (Z_STRLEN_P(phi)) {
250 char *name_str;
251 size_t name_len;
252 php_http_persistent_handle_factory_t *pf;
253
254 name_len = spprintf(&name_str, 0, "http_client_datashare.%s", Z_STRVAL_P(zdriver));
255
256 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
257 rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon);
258 }
259
260 efree(name_str);
261 }
262
263 share = php_http_client_datashare_init(NULL, driver.client_datashare_ops, rf, NULL TSRMLS_CC);
264 if (share) {
265 if (!(class_entry = php_http_client_factory_get_class_entry(getThis(), ZEND_STRL("clientDataShareClass") TSRMLS_CC))) {
266 class_entry = driver.client_datashare_ops->class_entry();
267 }
268
269 if (SUCCESS == php_http_new(&ov, class_entry, driver.client_datashare_ops->create_object, driver.client_datashare_ops->class_entry(), share, NULL TSRMLS_CC)) {
270 ZVAL_OBJVAL(return_value, ov, 0);
271 for (i = 0; i < argc; ++i) {
272 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_client_get_class_entry() TSRMLS_CC)) {
273 php_http_client_datashare_attach(share, *(argv[i]));
274 }
275 }
276 } else {
277 php_http_client_datashare_free(&share);
278 }
279 }
280
281 zval_ptr_dtor(&phi);
282 } else {
283 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "datashares are not supported by this driver");
284 }
285 } end_error_handling();
286 }
287 } end_error_handling();
288 }
289
290 PHP_METHOD(HttpClientFactory, getDriver)
291 {
292 if (SUCCESS == zend_parse_parameters_none()) {
293 RETURN_PROP(php_http_client_factory_class_entry, "driver");
294 }
295 RETURN_FALSE;
296 }
297
298 PHP_METHOD(HttpClientFactory, getAvailableDrivers)
299 {
300 if (SUCCESS == zend_parse_parameters_none()) {
301 HashPosition pos;
302 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
303
304 array_init(return_value);
305 FOREACH_HASH_KEY(pos, &php_http_client_factory_drivers, key) {
306 add_next_index_stringl(return_value, key.str, key.len - 1, 1);
307 }
308 return;
309 }
310 RETURN_FALSE;
311 }
312
313 PHP_MINIT_FUNCTION(http_client_factory)
314 {
315 zend_hash_init(&php_http_client_factory_drivers, 0, NULL, NULL, 1);
316
317 PHP_HTTP_REGISTER_CLASS(http\\Client, Factory, http_client_factory, php_http_object_class_entry, 0);
318 php_http_client_factory_class_entry->create_object = php_http_client_factory_new;
319
320 zend_declare_property_stringl(php_http_client_factory_class_entry, ZEND_STRL("driver"), ZEND_STRL("curl"), ZEND_ACC_PROTECTED TSRMLS_CC);
321 zend_declare_property_null(php_http_client_factory_class_entry, ZEND_STRL("persistentHandleId"), ZEND_ACC_PROTECTED TSRMLS_CC);
322 zend_declare_property_null(php_http_client_factory_class_entry, ZEND_STRL("clientClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
323 zend_declare_property_null(php_http_client_factory_class_entry, ZEND_STRL("clientPoolClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
324 zend_declare_property_null(php_http_client_factory_class_entry, ZEND_STRL("clientDataShareClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
325
326 return SUCCESS;
327 }
328
329 PHP_MSHUTDOWN_FUNCTION(http_client_factory)
330 {
331 zend_hash_destroy(&php_http_client_factory_drivers);
332
333 return SUCCESS;
334 }
335
336
337 /*
338 * Local variables:
339 * tab-width: 4
340 * c-basic-offset: 4
341 * End:
342 * vim600: noet sw=4 ts=4 fdm=marker
343 * vim<600: noet sw=4 ts=4
344 */
345