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