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