update curl info
[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 *meth_str = NULL, *url_str = NULL;
117 int meth_len, url_len;
118 zval *options = NULL;
119
120 with_error_handling(EH_THROW, php_http_exception_class_entry) {
121 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!a!", &url_str, &url_len, &meth_str, &meth_len, &options)) {
122 with_error_handling(EH_THROW, php_http_exception_class_entry) {
123 zval *zdriver, *os;
124 zend_object_value ov;
125 zend_class_entry *class_entry = NULL;
126 php_http_request_t *req = NULL;
127 php_http_request_factory_driver_t driver;
128
129 class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestClass") TSRMLS_CC);
130
131 if (!class_entry) {
132 class_entry = php_http_request_class_entry;
133 }
134
135 zdriver = zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
136
137 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) {
138 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
139 php_http_resource_factory_t *rf = NULL;
140
141 if (Z_STRLEN_P(phi)) {
142 char *name_str;
143 size_t name_len;
144 php_http_persistent_handle_factory_t *pf;
145
146 name_len = spprintf(&name_str, 0, "http_request.%s", Z_STRVAL_P(zdriver));
147
148 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
149 rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_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_str) {
169 zend_update_property_stringl(php_http_request_class_entry, return_value, ZEND_STRL("method"), meth_str, meth_len 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 rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon);
221 }
222
223 efree(name_str);
224 }
225
226 pool = php_http_request_pool_init(NULL, driver.request_pool_ops, rf, NULL TSRMLS_CC);
227 if (pool) {
228 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)) {
229 ZVAL_OBJVAL(return_value, ov, 0);
230 for (i = 0; i < argc; ++i) {
231 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_request_class_entry TSRMLS_CC)) {
232 php_http_request_pool_attach(pool, *(argv[i]));
233 }
234 }
235 } else {
236 php_http_request_pool_free(&pool);
237 }
238 }
239
240 zval_ptr_dtor(&phi);
241 } else {
242 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "pools are not supported by this driver");
243 }
244 } end_error_handling();
245 }
246 } end_error_handling();
247 }
248
249 PHP_METHOD(HttpRequestFactory, createDataShare)
250 {
251 int argc = 0;
252 zval ***argv;
253
254 with_error_handling(EH_THROW, php_http_exception_class_entry) {
255 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|*", &argv, &argc)) {
256 with_error_handling(EH_THROW, php_http_exception_class_entry) {
257 int i;
258 zval *zdriver;
259 zend_object_value ov;
260 zend_class_entry *class_entry;
261 php_http_request_datashare_t *share = NULL;
262 php_http_request_factory_driver_t driver;
263
264 if (!(class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestDataShareClass") TSRMLS_CC))) {
265 class_entry = php_http_request_datashare_class_entry;
266 }
267
268 zdriver = zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
269 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) {
270 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
271 php_http_resource_factory_t *rf = NULL;
272
273 if (Z_STRLEN_P(phi)) {
274 char *name_str;
275 size_t name_len;
276 php_http_persistent_handle_factory_t *pf;
277
278 name_len = spprintf(&name_str, 0, "http_request_datashare.%s", Z_STRVAL_P(zdriver));
279
280 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
281 rf = php_http_resource_factory_init(NULL, php_http_persistent_handle_resource_factory_ops(), pf, (void (*)(void *)) php_http_persistent_handle_abandon);
282 }
283
284 efree(name_str);
285 }
286
287 share = php_http_request_datashare_init(NULL, driver.request_datashare_ops, rf, NULL TSRMLS_CC);
288 if (share) {
289 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)) {
290 ZVAL_OBJVAL(return_value, ov, 0);
291 for (i = 0; i < argc; ++i) {
292 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_request_class_entry TSRMLS_CC)) {
293 php_http_request_datashare_attach(share, *(argv[i]));
294 }
295 }
296 } else {
297 php_http_request_datashare_free(&share);
298 }
299 }
300
301 zval_ptr_dtor(&phi);
302 } else {
303 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "datashares are not supported by this driver");
304 }
305 } end_error_handling();
306 }
307 } end_error_handling();
308 }
309
310 PHP_METHOD(HttpRequestFactory, getDriver)
311 {
312 if (SUCCESS == zend_parse_parameters_none()) {
313 RETURN_PROP(php_http_request_factory_class_entry, "driver");
314 }
315 RETURN_FALSE;
316 }
317
318 PHP_METHOD(HttpRequestFactory, getAvailableDrivers)
319 {
320 if (SUCCESS == zend_parse_parameters_none()) {
321 HashPosition pos;
322 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
323
324 array_init(return_value);
325 FOREACH_HASH_KEY(pos, &php_http_request_factory_drivers, key) {
326 add_next_index_stringl(return_value, key.str, key.len - 1, 1);
327 }
328 return;
329 }
330 RETURN_FALSE;
331 }
332
333 PHP_MINIT_FUNCTION(http_request_factory)
334 {
335 zend_hash_init(&php_http_request_factory_drivers, 0, NULL, NULL, 1);
336
337 PHP_HTTP_REGISTER_CLASS(http\\Request, Factory, http_request_factory, php_http_object_class_entry, 0);
338 php_http_request_factory_class_entry->create_object = php_http_request_factory_new;
339
340 zend_declare_property_stringl(php_http_request_factory_class_entry, ZEND_STRL("driver"), ZEND_STRL("curl"), ZEND_ACC_PROTECTED TSRMLS_CC);
341 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("persistentHandleId"), ZEND_ACC_PROTECTED TSRMLS_CC);
342 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("requestClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
343 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("requestPoolClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
344 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("requestDataShareClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
345
346 return SUCCESS;
347 }
348
349 PHP_MSHUTDOWN_FUNCTION(http_request_factory)
350 {
351 zend_hash_destroy(&php_http_request_factory_drivers);
352
353 return SUCCESS;
354 }
355
356
357 /*
358 * Local variables:
359 * tab-width: 4
360 * c-basic-offset: 4
361 * End:
362 * vim600: noet sw=4 ts=4 fdm=marker
363 * vim<600: noet sw=4 ts=4
364 */
365