2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #include <ext/spl/spl_observer.h>
18 * array of name => php_http_request_factory_driver_t*
20 static HashTable php_http_request_factory_drivers
;
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
)
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
);
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
)
29 php_http_request_factory_driver_t
*tmp
;
31 if (SUCCESS
== zend_hash_find(&php_http_request_factory_drivers
, name_str
, name_len
+ 1, (void *) &tmp
)) {
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
)
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
);
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
);
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)
58 PHP_HTTP_BEGIN_ARGS(__construct
, 1)
59 PHP_HTTP_ARG_VAL(options
, 0)
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)
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)
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)
76 PHP_HTTP_EMPTY_ARGS(getDriver
);
77 PHP_HTTP_EMPTY_ARGS(getAvailableDrivers
);
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
)
91 PHP_METHOD(HttpRequestFactory
, __construct
)
93 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
94 HashTable
*options
= NULL
;
96 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|h", &options
)) {
100 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
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
);
111 } end_error_handling();
114 PHP_METHOD(HttpRequestFactory
, createRequest
)
116 char *url_str
= NULL
;
119 zval
*options
= NULL
;
121 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
122 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!la!", &url_str
, &url_len
, &meth
, &options
)) {
123 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
125 zend_object_value ov
;
126 zend_class_entry
*class_entry
= NULL
;
127 php_http_request_t
*req
= NULL
;
128 php_http_request_factory_driver_t driver
;
130 class_entry
= php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestClass") TSRMLS_CC
);
133 class_entry
= php_http_request_class_entry
;
136 zdriver
= zend_read_property(php_http_request_factory_class_entry
, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC
);
138 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
) {
139 zval
*phi
= php_http_zsep(1, IS_STRING
, zend_read_property(php_http_request_factory_class_entry
, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC
));
140 php_http_resource_factory_t
*rf
= NULL
;
142 if (Z_STRLEN_P(phi
)) {
145 php_http_persistent_handle_factory_t
*pf
;
147 name_len
= spprintf(&name_str
, 0, "http_request.%s", Z_STRVAL_P(zdriver
));
149 if ((pf
= php_http_persistent_handle_concede(NULL
, name_str
, name_len
, Z_STRVAL_P(phi
), Z_STRLEN_P(phi
) TSRMLS_CC
))) {
150 rf
= php_http_resource_factory_init(NULL
, php_http_persistent_handle_resource_factory_ops(), pf
, (void (*)(void *)) php_http_persistent_handle_abandon
);
156 req
= php_http_request_init(NULL
, driver
.request_ops
, rf
, NULL TSRMLS_CC
);
158 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
)) {
159 ZVAL_OBJVAL(return_value
, ov
, 0);
162 object_init_ex(os
, spl_ce_SplObjectStorage
);
163 zend_update_property(php_http_request_class_entry
, return_value
, ZEND_STRL("observers"), os TSRMLS_CC
);
167 zend_update_property_stringl(php_http_request_class_entry
, return_value
, ZEND_STRL("url"), url_str
, url_len TSRMLS_CC
);
170 zend_update_property_long(php_http_request_class_entry
, return_value
, ZEND_STRL("method"), meth TSRMLS_CC
);
173 zend_call_method_with_1_params(&return_value
, Z_OBJCE_P(return_value
), NULL
, "setoptions", NULL
, options
);
176 php_http_request_free(&req
);
182 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_FACTORY
, "requests are not supported by this driver");
184 } end_error_handling();
186 } end_error_handling();
189 PHP_METHOD(HttpRequestFactory
, createPool
)
194 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
195 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|*", &argv
, &argc
)) {
196 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
199 zend_object_value ov
;
200 zend_class_entry
*class_entry
= NULL
;
201 php_http_request_pool_t
*pool
= NULL
;
202 php_http_request_factory_driver_t driver
;
204 if (!(class_entry
= php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestPoolClass") TSRMLS_CC
))) {
205 class_entry
= php_http_request_pool_class_entry
;
208 zdriver
= zend_read_property(php_http_request_factory_class_entry
, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC
);
209 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
) {
210 zval
*phi
= php_http_zsep(1, IS_STRING
, zend_read_property(php_http_request_factory_class_entry
, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC
));
211 php_http_resource_factory_t
*rf
= NULL
;
213 if (Z_STRLEN_P(phi
)) {
216 php_http_persistent_handle_factory_t
*pf
;
218 name_len
= spprintf(&name_str
, 0, "http_request_pool.%s", Z_STRVAL_P(zdriver
));
220 if ((pf
= php_http_persistent_handle_concede(NULL
, name_str
, name_len
, Z_STRVAL_P(phi
), Z_STRLEN_P(phi
) TSRMLS_CC
))) {
221 rf
= php_http_resource_factory_init(NULL
, php_http_persistent_handle_resource_factory_ops(), pf
, (void (*)(void *)) php_http_persistent_handle_abandon
);
227 pool
= php_http_request_pool_init(NULL
, driver
.request_pool_ops
, rf
, NULL TSRMLS_CC
);
229 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
)) {
230 ZVAL_OBJVAL(return_value
, ov
, 0);
231 for (i
= 0; i
< argc
; ++i
) {
232 if (Z_TYPE_PP(argv
[i
]) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(argv
[i
]), php_http_request_class_entry TSRMLS_CC
)) {
233 php_http_request_pool_attach(pool
, *(argv
[i
]));
237 php_http_request_pool_free(&pool
);
243 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_FACTORY
, "pools are not supported by this driver");
245 } end_error_handling();
247 } end_error_handling();
250 PHP_METHOD(HttpRequestFactory
, createDataShare
)
255 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
256 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|*", &argv
, &argc
)) {
257 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
260 zend_object_value ov
;
261 zend_class_entry
*class_entry
;
262 php_http_request_datashare_t
*share
= NULL
;
263 php_http_request_factory_driver_t driver
;
265 if (!(class_entry
= php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestDataShareClass") TSRMLS_CC
))) {
266 class_entry
= php_http_request_datashare_class_entry
;
269 zdriver
= zend_read_property(php_http_request_factory_class_entry
, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC
);
270 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
) {
271 zval
*phi
= php_http_zsep(1, IS_STRING
, zend_read_property(php_http_request_factory_class_entry
, getThis(), ZEND_STRL("persistentHandleId"), 0 TSRMLS_CC
));
272 php_http_resource_factory_t
*rf
= NULL
;
274 if (Z_STRLEN_P(phi
)) {
277 php_http_persistent_handle_factory_t
*pf
;
279 name_len
= spprintf(&name_str
, 0, "http_request_datashare.%s", Z_STRVAL_P(zdriver
));
281 if ((pf
= php_http_persistent_handle_concede(NULL
, name_str
, name_len
, Z_STRVAL_P(phi
), Z_STRLEN_P(phi
) TSRMLS_CC
))) {
282 rf
= php_http_resource_factory_init(NULL
, php_http_persistent_handle_resource_factory_ops(), pf
, (void (*)(void *)) php_http_persistent_handle_abandon
);
288 share
= php_http_request_datashare_init(NULL
, driver
.request_datashare_ops
, rf
, NULL TSRMLS_CC
);
290 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
)) {
291 ZVAL_OBJVAL(return_value
, ov
, 0);
292 for (i
= 0; i
< argc
; ++i
) {
293 if (Z_TYPE_PP(argv
[i
]) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(argv
[i
]), php_http_request_class_entry TSRMLS_CC
)) {
294 php_http_request_datashare_attach(share
, *(argv
[i
]));
298 php_http_request_datashare_free(&share
);
304 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_FACTORY
, "datashares are not supported by this driver");
306 } end_error_handling();
308 } end_error_handling();
311 PHP_METHOD(HttpRequestFactory
, getDriver
)
313 if (SUCCESS
== zend_parse_parameters_none()) {
314 RETURN_PROP(php_http_request_factory_class_entry
, "driver");
319 PHP_METHOD(HttpRequestFactory
, getAvailableDrivers
)
321 if (SUCCESS
== zend_parse_parameters_none()) {
323 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
325 array_init(return_value
);
326 FOREACH_HASH_KEY(pos
, &php_http_request_factory_drivers
, key
) {
327 add_next_index_stringl(return_value
, key
.str
, key
.len
- 1, 1);
334 PHP_MINIT_FUNCTION(http_request_factory
)
336 zend_hash_init(&php_http_request_factory_drivers
, 0, NULL
, NULL
, 1);
338 PHP_HTTP_REGISTER_CLASS(http
\\Request
, Factory
, http_request_factory
, php_http_object_class_entry
, 0);
339 php_http_request_factory_class_entry
->create_object
= php_http_request_factory_new
;
341 zend_declare_property_stringl(php_http_request_factory_class_entry
, ZEND_STRL("driver"), ZEND_STRL("curl"), ZEND_ACC_PROTECTED TSRMLS_CC
);
342 zend_declare_property_null(php_http_request_factory_class_entry
, ZEND_STRL("persistentHandleId"), ZEND_ACC_PROTECTED TSRMLS_CC
);
343 zend_declare_property_null(php_http_request_factory_class_entry
, ZEND_STRL("requestClass"), ZEND_ACC_PROTECTED TSRMLS_CC
);
344 zend_declare_property_null(php_http_request_factory_class_entry
, ZEND_STRL("requestPoolClass"), ZEND_ACC_PROTECTED TSRMLS_CC
);
345 zend_declare_property_null(php_http_request_factory_class_entry
, ZEND_STRL("requestDataShareClass"), ZEND_ACC_PROTECTED TSRMLS_CC
);
350 PHP_MSHUTDOWN_FUNCTION(http_request_factory
)
352 zend_hash_destroy(&php_http_request_factory_drivers
);
363 * vim600: noet sw=4 ts=4 fdm=marker
364 * vim<600: noet sw=4 ts=4