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