* use the resource factory
[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(persistentHandle, 0)
54 PHP_HTTP_END_ARGS;
55 PHP_HTTP_BEGIN_ARGS(createPool, 0)
56 PHP_HTTP_ARG_VAL(persistentHandle, 0)
57 PHP_HTTP_END_ARGS;
58 PHP_HTTP_BEGIN_ARGS(createDataShare, 0)
59 PHP_HTTP_ARG_VAL(persistentHandle, 0)
60 PHP_HTTP_END_ARGS;
61 PHP_HTTP_EMPTY_ARGS(getGlobalDataShareInstance);
62 PHP_HTTP_EMPTY_ARGS(getDriver);
63 PHP_HTTP_EMPTY_ARGS(getAvailableDrivers);
64
65 zend_class_entry *php_http_request_factory_class_entry;
66 zend_function_entry php_http_request_factory_method_entry[] = {
67 PHP_HTTP_REQUEST_FACTORY_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
68 PHP_HTTP_REQUEST_FACTORY_ME(createRequest, ZEND_ACC_PUBLIC)
69 PHP_HTTP_REQUEST_FACTORY_ME(createPool, ZEND_ACC_PUBLIC)
70 PHP_HTTP_REQUEST_FACTORY_ME(createDataShare, ZEND_ACC_PUBLIC)
71 PHP_HTTP_REQUEST_FACTORY_ME(getGlobalDataShareInstance, ZEND_ACC_PUBLIC)
72 PHP_HTTP_REQUEST_FACTORY_ME(getDriver, ZEND_ACC_PUBLIC)
73 PHP_HTTP_REQUEST_FACTORY_ME(getAvailableDrivers, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
74
75 EMPTY_FUNCTION_ENTRY
76 };
77
78 PHP_METHOD(HttpRequestFactory, __construct)
79 {
80 with_error_handling(EH_THROW, php_http_exception_class_entry) {
81 HashTable *options = NULL;
82
83 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h", &options)) {
84 if (options) {
85 zval **val;
86 HashPosition pos;
87 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
88
89 FOREACH_HASH_KEYVAL(pos, options, key, val) {
90 if (key.type == HASH_KEY_IS_STRING) {
91 zval *newval = php_http_zsep(1, Z_TYPE_PP(val), *val);
92 zend_update_property(php_http_request_factory_class_entry, getThis(), key.str, key.len - 1, newval);
93 zval_ptr_dtor(&newval);
94 }
95 }
96 }
97 }
98 } end_error_handling();
99 }
100
101 PHP_METHOD(HttpRequestFactory, createRequest)
102 {
103 char *url_str = NULL;
104 int url_len;
105 long meth = -1;
106 zval *options = NULL;
107
108 with_error_handling(EH_THROW, php_http_exception_class_entry) {
109 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!la!", &url_str, &url_len, &meth, &options)) {
110 with_error_handling(EH_THROW, php_http_exception_class_entry) {
111 zval *zdriver, *os;
112 zend_object_value ov;
113 zend_class_entry *class_entry = NULL;
114 php_http_request_t *req = NULL;
115 php_http_request_factory_driver_t driver;
116
117 class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestClass") TSRMLS_CC);
118
119 if (!class_entry) {
120 class_entry = php_http_request_class_entry;
121 }
122
123 zdriver = zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
124
125 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) {
126 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
127 php_http_resource_factory_t *rf = NULL;
128
129 if (Z_STRLEN_P(phi)) {
130 char *name_str;
131 size_t name_len;
132 php_http_persistent_handle_factory_t *pf;
133
134 name_len = spprintf(&name_str, 0, "http_request.%s", Z_STRVAL_P(zdriver));
135
136 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
137 php_http_resource_factory_ops_t ops = {
138 php_http_persistent_handle_acquire,
139 php_http_persistent_handle_accrete,
140 php_http_persistent_handle_release
141 };
142
143 rf = php_http_resource_factory_init(NULL, &ops, pf, php_http_persistent_handle_abandon TSRMLS_CC);
144 }
145
146 efree(name_str);
147 }
148
149 req = php_http_request_init(NULL, driver.request_ops, rf, NULL TSRMLS_CC);
150 if (req) {
151 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)) {
152 ZVAL_OBJVAL(return_value, ov, 0);
153
154 MAKE_STD_ZVAL(os);
155 object_init_ex(os, spl_ce_SplObjectStorage);
156 zend_update_property(php_http_request_class_entry, return_value, ZEND_STRL("observers"), os TSRMLS_CC);
157 zval_ptr_dtor(&os);
158
159 if (url_str) {
160 zend_update_property_stringl(php_http_request_class_entry, return_value, ZEND_STRL("url"), url_str, url_len TSRMLS_CC);
161 }
162 if (meth > 0) {
163 zend_update_property_long(php_http_request_class_entry, return_value, ZEND_STRL("method"), meth TSRMLS_CC);
164 }
165 if (options) {
166 zend_call_method_with_1_params(&return_value, Z_OBJCE_P(return_value), NULL, "setoptions", NULL, options);
167 }
168 } else {
169 php_http_request_free(&req);
170 }
171 }
172
173 zval_ptr_dtor(&phi);
174 } else {
175 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "requests are not supported by this driver");
176 }
177 } end_error_handling();
178 }
179 } end_error_handling();
180 }
181
182 PHP_METHOD(HttpRequestFactory, createPool)
183 {
184 int argc = 0;
185 zval ***argv;
186
187 with_error_handling(EH_THROW, php_http_exception_class_entry) {
188 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|*", &argv, &argc)) {
189 with_error_handling(EH_THROW, php_http_exception_class_entry) {
190 int i;
191 zval *zdriver;
192 zend_object_value ov;
193 zend_class_entry *class_entry = NULL;
194 php_http_request_pool_t *pool = NULL;
195 php_http_request_factory_driver_t driver;
196
197 if (!(class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestPoolClass") TSRMLS_CC))) {
198 class_entry = php_http_request_pool_class_entry;
199 }
200
201 zdriver = zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC);
202 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) {
203 zval *phi = php_http_zsep(1, IS_STRING, zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC));
204 php_http_resource_factory_t *rf = NULL;
205
206 if (Z_STRLEN_P(phi)) {
207 char *name_str;
208 size_t name_len;
209 php_http_persistent_handle_factory_t *pf;
210
211 name_len = spprintf(&name_str, 0, "http_request_pool.%s", Z_STRVAL_P(zdriver));
212
213 if ((pf = php_http_persistent_handle_concede(NULL , name_str, name_len, Z_STRVAL_P(phi), Z_STRLEN_P(phi) TSRMLS_CC))) {
214 php_http_resource_factory_ops_t ops = {
215 php_http_persistent_handle_acquire,
216 php_http_persistent_handle_accrete,
217 php_http_persistent_handle_release
218 };
219
220 rf = php_http_resource_factory_init(NULL, &ops, pf, php_http_persistent_handle_abandon TSRMLS_CC);
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 php_http_resource_factory_ops_t ops = {
282 php_http_persistent_handle_acquire,
283 php_http_persistent_handle_accrete,
284 php_http_persistent_handle_release
285 };
286
287 rf = php_http_resource_factory_init(NULL, &ops, pf, php_http_persistent_handle_abandon TSRMLS_CC);
288 }
289
290 efree(name_str);
291 }
292
293 share = php_http_request_datashare_init(NULL, driver.request_datashare_ops, rf, NULL, 0 TSRMLS_CC);
294 if (share) {
295 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)) {
296 ZVAL_OBJVAL(return_value, ov, 0);
297 for (i = 0; i < argc; ++i) {
298 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_request_class_entry TSRMLS_CC)) {
299 php_http_request_datashare_attach(share, *(argv[i]));
300 }
301 }
302 } else {
303 php_http_request_datashare_free(&share);
304 }
305 }
306
307 zval_ptr_dtor(&phi);
308 } else {
309 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "datashares are not supported by this driver");
310 }
311 } end_error_handling();
312 }
313 } end_error_handling();
314 }
315
316 PHP_METHOD(HttpRequestFactory, getGlobalDataShareInstance)
317 {
318 with_error_handling(EH_THROW, php_http_exception_class_entry) {
319 if (SUCCESS == zend_parse_parameters_none()) {
320 with_error_handling(EH_THROW, php_http_exception_class_entry) {
321 zval *instance = *zend_std_get_static_property(php_http_request_datashare_class_entry, ZEND_STRL("instance"), 0, NULL TSRMLS_CC);
322
323 if (Z_TYPE_P(instance) != IS_OBJECT) {
324 zval *zdriver;
325 zend_object_value ov;
326 zend_class_entry *class_entry;
327 php_http_request_datashare_t *share;
328
329 if (!(class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestDataShareClass") TSRMLS_CC))) {
330 class_entry = php_http_request_datashare_class_entry;
331 }
332
333 if ((zdriver = zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC))
334 && (IS_STRING == Z_TYPE_P(zdriver))
335 && (share = php_http_request_datashare_global_get(Z_STRVAL_P(zdriver), Z_STRLEN_P(zdriver) TSRMLS_CC))
336 && (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))
337 ) {
338 MAKE_STD_ZVAL(instance);
339 ZVAL_OBJVAL(instance, ov, 0);
340 zend_update_static_property(php_http_request_datashare_class_entry, ZEND_STRL("instance"), instance TSRMLS_CC);
341
342 if (PHP_HTTP_G->request_datashare.cookie) {
343 zend_update_property_bool(php_http_request_datashare_class_entry, instance, ZEND_STRL("cookie"), PHP_HTTP_G->request_datashare.cookie TSRMLS_CC);
344 }
345 if (PHP_HTTP_G->request_datashare.dns) {
346 zend_update_property_bool(php_http_request_datashare_class_entry, instance, ZEND_STRL("dns"), PHP_HTTP_G->request_datashare.dns TSRMLS_CC);
347 }
348 }
349 }
350
351 RETVAL_ZVAL(instance, 1, 0);
352 } end_error_handling();
353 }
354 } end_error_handling();
355 }
356
357
358 PHP_METHOD(HttpRequestFactory, getDriver)
359 {
360 if (SUCCESS == zend_parse_parameters_none()) {
361 RETURN_PROP(php_http_request_factory_class_entry, "driver");
362 }
363 RETURN_FALSE;
364 }
365
366 PHP_METHOD(HttpRequestFactory, getAvailableDrivers)
367 {
368 if (SUCCESS == zend_parse_parameters_none()) {
369 HashPosition pos;
370 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
371
372 array_init(return_value);
373 FOREACH_HASH_KEY(pos, &php_http_request_factory_drivers, key) {
374 add_next_index_stringl(return_value, key.str, key.len - 1, 1);
375 }
376 return;
377 }
378 RETURN_FALSE;
379 }
380
381 PHP_MINIT_FUNCTION(http_request_factory)
382 {
383 zend_hash_init(&php_http_request_factory_drivers, 0, NULL, NULL, 1);
384
385 PHP_HTTP_REGISTER_CLASS(http\\request, Factory, http_request_factory, php_http_object_class_entry, 0);
386 php_http_request_factory_class_entry->create_object = php_http_request_factory_new;
387
388 zend_declare_property_stringl(php_http_request_factory_class_entry, ZEND_STRL("driver"), ZEND_STRL("curl"), ZEND_ACC_PROTECTED TSRMLS_CC);
389 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("persistentHandleId"), ZEND_ACC_PROTECTED TSRMLS_CC);
390 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("requestClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
391 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("requestPoolClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
392 zend_declare_property_null(php_http_request_factory_class_entry, ZEND_STRL("requestDataShareClass"), ZEND_ACC_PROTECTED TSRMLS_CC);
393
394 return SUCCESS;
395 }
396
397 PHP_MSHUTDOWN_FUNCTION(http_request_factory)
398 {
399 zend_hash_destroy(&php_http_request_factory_drivers);
400
401 return SUCCESS;
402 }
403