remove that awkward and error prone global request datashare
[m6w6/ext-http] / php_http_request_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_request_factory_driver_t*
19 */
20 static HashTable php_http_request_factory_drivers;
21
22 PHP_HTTP_API STATUS php_http_request_factory_add_driver(const char *name_str, size_t name_len, php_http_request_factory_driver_t *driver)
23 {
24 return zend_hash_add(&php_http_request_factory_drivers, name_str, name_len + 1, (void *) driver, sizeof(php_http_request_factory_driver_t), NULL);
25 }
26
27 PHP_HTTP_API STATUS php_http_request_factory_get_driver(const char *name_str, size_t name_len, php_http_request_factory_driver_t *driver)
28 {
29 php_http_request_factory_driver_t *tmp;
30
31 if (SUCCESS == zend_hash_find(&php_http_request_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_request_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(HttpRequestFactory, method, 0, req_args)
53 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequestFactory, method, 0)
54 #define PHP_HTTP_REQUEST_FACTORY_ME(method, visibility) PHP_ME(HttpRequestFactory, method, PHP_HTTP_ARGS(HttpRequestFactory, method), visibility)
55 #define PHP_HTTP_REQUEST_FACTORY_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpRequestFactory, method))
56 #define PHP_HTTP_REQUEST_FACTORY_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpRequestFactory_##al), PHP_HTTP_ARGS(HttpRequestFactory, al), vis)
57
58 PHP_HTTP_BEGIN_ARGS(__construct, 1)
59 PHP_HTTP_ARG_VAL(options, 0)
60 PHP_HTTP_END_ARGS;
61 PHP_HTTP_BEGIN_ARGS(createRequest, 0)
62 PHP_HTTP_ARG_VAL(url, 0)
63 PHP_HTTP_ARG_VAL(method, 0)
64 PHP_HTTP_ARG_VAL(options, 0)
65 PHP_HTTP_END_ARGS;
66 PHP_HTTP_BEGIN_ARGS(createPool, 0)
67 PHP_HTTP_ARG_OBJ(http\\Request, request1, 1)
68 PHP_HTTP_ARG_OBJ(http\\Request, request2, 1)
69 PHP_HTTP_ARG_OBJ(http\\Request, requestN, 1)
70 PHP_HTTP_END_ARGS;
71 PHP_HTTP_BEGIN_ARGS(createDataShare, 0)
72 PHP_HTTP_ARG_OBJ(http\\Request, request1, 1)
73 PHP_HTTP_ARG_OBJ(http\\Request, request2, 1)
74 PHP_HTTP_ARG_OBJ(http\\Request, requestN, 1)
75 PHP_HTTP_END_ARGS;
76 PHP_HTTP_EMPTY_ARGS(getDriver);
77 PHP_HTTP_EMPTY_ARGS(getAvailableDrivers);
78
79 zend_class_entry *php_http_request_factory_class_entry;
80 zend_function_entry php_http_request_factory_method_entry[] = {
81 PHP_HTTP_REQUEST_FACTORY_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
82 PHP_HTTP_REQUEST_FACTORY_ME(createRequest, ZEND_ACC_PUBLIC)
83 PHP_HTTP_REQUEST_FACTORY_ME(createPool, ZEND_ACC_PUBLIC)
84 PHP_HTTP_REQUEST_FACTORY_ME(createDataShare, ZEND_ACC_PUBLIC)
85 PHP_HTTP_REQUEST_FACTORY_ME(getDriver, ZEND_ACC_PUBLIC)
86 PHP_HTTP_REQUEST_FACTORY_ME(getAvailableDrivers, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
87
88 EMPTY_FUNCTION_ENTRY
89 };
90
91 PHP_METHOD(HttpRequestFactory, __construct)
92 {
93 with_error_handling(EH_THROW, php_http_exception_class_entry) {
94 HashTable *options = NULL;
95
96 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h", &options)) {
97 if (options) {
98 zval **val;
99 HashPosition pos;
100 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
101
102 FOREACH_HASH_KEYVAL(pos, options, key, val) {
103 if (key.type == HASH_KEY_IS_STRING) {
104 zval *newval = php_http_zsep(1, Z_TYPE_PP(val), *val);
105 zend_update_property(php_http_request_factory_class_entry, getThis(), key.str, key.len - 1, newval TSRMLS_CC);
106 zval_ptr_dtor(&newval);
107 }
108 }
109 }
110 }
111 } end_error_handling();
112 }
113
114 PHP_METHOD(HttpRequestFactory, createRequest)
115 {
116 char *url_str = NULL;
117 int url_len;
118 long meth = -1;
119 zval *options = NULL;
120
121 with_error_handling(EH_THROW, php_http_exception_class_entry) {
122 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!la!", &url_str, &url_len, &meth, &options)) {
123 with_error_handling(EH_THROW, php_http_exception_class_entry) {
124 zval *zdriver, *os;
125 zend_object_value ov;
126 zend_class_entry *class_entry = NULL;
127 php_http_request_t *req = NULL;
128 php_http_request_factory_driver_t driver;
129
130 class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestClass") TSRMLS_CC);
131
132 if (!class_entry) {
133 class_entry = php_http_request_class_entry;
134 }
135
136 zdriver = zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
137
138 if ((IS_STRING == Z_TYPE_P(zdriver)) && (SUCCESS == php_http_request_factory_get_driver(Z_STRVAL_P(zdriver), Z_STRLEN_P(zdriver), &driver)) && driver.request_ops) {
139 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
140 php_http_resource_factory_t *rf = NULL;
141
142 if (Z_STRLEN_P(phi)) {
143 char *name_str;
144 size_t name_len;
145 php_http_persistent_handle_factory_t *pf;
146
147 name_len = spprintf(&name_str, 0, "http_request.%s", Z_STRVAL_P(zdriver));
148
149 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
150 php_http_resource_factory_ops_t ops = {
151 (php_http_resource_factory_handle_ctor_t) php_http_persistent_handle_acquire,
152 (php_http_resource_factory_handle_copy_t) php_http_persistent_handle_accrete,
153 (php_http_resource_factory_handle_dtor_t) php_http_persistent_handle_release
154 };
155
156 rf = php_http_resource_factory_init(NULL, &ops, pf, (void (*)(void *)) php_http_persistent_handle_abandon);
157 }
158
159 efree(name_str);
160 }
161
162 req = php_http_request_init(NULL, driver.request_ops, rf, NULL TSRMLS_CC);
163 if (req) {
164 if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_request_object_new_ex, php_http_request_class_entry, req, NULL TSRMLS_CC)) {
165 ZVAL_OBJVAL(return_value, ov, 0);
166
167 MAKE_STD_ZVAL(os);
168 object_init_ex(os, spl_ce_SplObjectStorage);
169 zend_update_property(php_http_request_class_entry, return_value, ZEND_STRL("observers"), os TSRMLS_CC);
170 zval_ptr_dtor(&os);
171
172 if (url_str) {
173 zend_update_property_stringl(php_http_request_class_entry, return_value, ZEND_STRL("url"), url_str, url_len TSRMLS_CC);
174 }
175 if (meth > 0) {
176 zend_update_property_long(php_http_request_class_entry, return_value, ZEND_STRL("method"), meth TSRMLS_CC);
177 }
178 if (options) {
179 zend_call_method_with_1_params(&return_value, Z_OBJCE_P(return_value), NULL, "setoptions", NULL, options);
180 }
181 } else {
182 php_http_request_free(&req);
183 }
184 }
185
186 zval_ptr_dtor(&phi);
187 } else {
188 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "requests are not supported by this driver");
189 }
190 } end_error_handling();
191 }
192 } end_error_handling();
193 }
194
195 PHP_METHOD(HttpRequestFactory, createPool)
196 {
197 int argc = 0;
198 zval ***argv;
199
200 with_error_handling(EH_THROW, php_http_exception_class_entry) {
201 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|*", &argv, &argc)) {
202 with_error_handling(EH_THROW, php_http_exception_class_entry) {
203 int i;
204 zval *zdriver;
205 zend_object_value ov;
206 zend_class_entry *class_entry = NULL;
207 php_http_request_pool_t *pool = NULL;
208 php_http_request_factory_driver_t driver;
209
210 if (!(class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestPoolClass") TSRMLS_CC))) {
211 class_entry = php_http_request_pool_class_entry;
212 }
213
214 zdriver = zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
215 if ((IS_STRING == Z_TYPE_P(zdriver)) && (SUCCESS == php_http_request_factory_get_driver(Z_STRVAL_P(zdriver), Z_STRLEN_P(zdriver), &driver)) && driver.request_pool_ops) {
216 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
217 php_http_resource_factory_t *rf = NULL;
218
219 if (Z_STRLEN_P(phi)) {
220 char *name_str;
221 size_t name_len;
222 php_http_persistent_handle_factory_t *pf;
223
224 name_len = spprintf(&name_str, 0, "http_request_pool.%s", Z_STRVAL_P(zdriver));
225
226 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
227 php_http_resource_factory_ops_t ops = {
228 (php_http_resource_factory_handle_ctor_t) php_http_persistent_handle_acquire,
229 (php_http_resource_factory_handle_copy_t) php_http_persistent_handle_accrete,
230 (php_http_resource_factory_handle_dtor_t) php_http_persistent_handle_release
231 };
232
233 rf = php_http_resource_factory_init(NULL, &ops, pf, (void (*)(void *)) php_http_persistent_handle_abandon);
234 }
235
236 efree(name_str);
237 }
238
239 pool = php_http_request_pool_init(NULL, driver.request_pool_ops, rf, NULL TSRMLS_CC);
240 if (pool) {
241 if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_request_pool_object_new_ex, php_http_request_pool_class_entry, pool, NULL TSRMLS_CC)) {
242 ZVAL_OBJVAL(return_value, ov, 0);
243 for (i = 0; i < argc; ++i) {
244 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_request_class_entry TSRMLS_CC)) {
245 php_http_request_pool_attach(pool, *(argv[i]));
246 }
247 }
248 } else {
249 php_http_request_pool_free(&pool);
250 }
251 }
252
253 zval_ptr_dtor(&phi);
254 } else {
255 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "pools are not supported by this driver");
256 }
257 } end_error_handling();
258 }
259 } end_error_handling();
260 }
261
262 PHP_METHOD(HttpRequestFactory, createDataShare)
263 {
264 int argc = 0;
265 zval ***argv;
266
267 with_error_handling(EH_THROW, php_http_exception_class_entry) {
268 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|*", &argv, &argc)) {
269 with_error_handling(EH_THROW, php_http_exception_class_entry) {
270 int i;
271 zval *zdriver;
272 zend_object_value ov;
273 zend_class_entry *class_entry;
274 php_http_request_datashare_t *share = NULL;
275 php_http_request_factory_driver_t driver;
276
277 if (!(class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestDataShareClass") TSRMLS_CC))) {
278 class_entry = php_http_request_datashare_class_entry;
279 }
280
281 zdriver = zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
282 if ((IS_STRING == Z_TYPE_P(zdriver)) && (SUCCESS == php_http_request_factory_get_driver(Z_STRVAL_P(zdriver), Z_STRLEN_P(zdriver), &driver)) && driver.request_datashare_ops) {
283 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
284 php_http_resource_factory_t *rf = NULL;
285
286 if (Z_STRLEN_P(phi)) {
287 char *name_str;
288 size_t name_len;
289 php_http_persistent_handle_factory_t *pf;
290
291 name_len = spprintf(&name_str, 0, "http_request_datashare.%s", Z_STRVAL_P(zdriver));
292
293 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
294 php_http_resource_factory_ops_t ops = {
295 (php_http_resource_factory_handle_ctor_t) php_http_persistent_handle_acquire,
296 (php_http_resource_factory_handle_copy_t) php_http_persistent_handle_accrete,
297 (php_http_resource_factory_handle_dtor_t) php_http_persistent_handle_release
298 };
299
300 rf = php_http_resource_factory_init(NULL, &ops, pf, (void (*)(void *)) php_http_persistent_handle_abandon);
301 }
302
303 efree(name_str);
304 }
305
306 share = php_http_request_datashare_init(NULL, driver.request_datashare_ops, rf, NULL TSRMLS_CC);
307 if (share) {
308 if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_request_datashare_object_new_ex, php_http_request_datashare_class_entry, share, NULL TSRMLS_CC)) {
309 ZVAL_OBJVAL(return_value, ov, 0);
310 for (i = 0; i < argc; ++i) {
311 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_request_class_entry TSRMLS_CC)) {
312 php_http_request_datashare_attach(share, *(argv[i]));
313 }
314 }
315 } else {
316 php_http_request_datashare_free(&share);
317 }
318 }
319
320 zval_ptr_dtor(&phi);
321 } else {
322 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "datashares are not supported by this driver");
323 }
324 } end_error_handling();
325 }
326 } end_error_handling();
327 }
328
329 PHP_METHOD(HttpRequestFactory, getDriver)
330 {
331 if (SUCCESS == zend_parse_parameters_none()) {
332 RETURN_PROP(php_http_request_factory_class_entry, "driver");
333 }
334 RETURN_FALSE;
335 }
336
337 PHP_METHOD(HttpRequestFactory, getAvailableDrivers)
338 {
339 if (SUCCESS == zend_parse_parameters_none()) {
340 HashPosition pos;
341 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
342
343 array_init(return_value);
344 FOREACH_HASH_KEY(pos, &php_http_request_factory_drivers, key) {
345 add_next_index_stringl(return_value, key.str, key.len - 1, 1);
346 }
347 return;
348 }
349 RETURN_FALSE;
350 }
351
352 PHP_MINIT_FUNCTION(http_request_factory)
353 {
354 zend_hash_init(&php_http_request_factory_drivers, 0, NULL, NULL, 1);
355
356 PHP_HTTP_REGISTER_CLASS(http\\Request, Factory, http_request_factory, php_http_object_class_entry, 0);
357 php_http_request_factory_class_entry->create_object = php_http_request_factory_new;
358
359 zend_declare_property_stringl(php_http_request_factory_class_entry, ZEND_STRL("driver"), ZEND_STRL("curl"), ZEND_ACC_PROTECTED TSRMLS_CC);
360 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("persistentHandleId"), ZEND_ACC_PROTECTED TSRMLS_CC);
361 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("requestClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
362 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("requestPoolClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
363 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("requestDataShareClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
364
365 return SUCCESS;
366 }
367
368 PHP_MSHUTDOWN_FUNCTION(http_request_factory)
369 {
370 zend_hash_destroy(&php_http_request_factory_drivers);
371
372 return SUCCESS;
373 }
374
375
376 /*
377 * Local variables:
378 * tab-width: 4
379 * c-basic-offset: 4
380 * End:
381 * vim600: noet sw=4 ts=4 fdm=marker
382 * vim<600: noet sw=4 ts=4
383 */
384