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