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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_CURL
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
20 #include "zend_interfaces.h"
22 #include "php_http_api.h"
23 #include "php_http_exception_object.h"
24 #include "php_http_request_api.h"
25 #include "php_http_request_object.h"
26 #include "php_http_request_datashare_api.h"
27 #include "php_http_requestdatashare_object.h"
29 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequestDataShare, method, 0, req_args)
30 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpRequestDataShare, method, 0)
31 #define HTTP_RSHARE_ME(method, visibility) PHP_ME(HttpRequestDataShare, method, HTTP_ARGS(HttpRequestDataShare, method), visibility)
33 #if defined(HAVE_SPL) && !defined(WONKY)
34 /* SPL doesn't install its headers */
35 extern PHPAPI zend_class_entry
*spl_ce_Countable
;
38 HTTP_EMPTY_ARGS(__destruct
);
39 HTTP_EMPTY_ARGS(count
);
41 HTTP_BEGIN_ARGS(attach
, 1)
42 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
44 HTTP_BEGIN_ARGS(detach
, 1)
45 HTTP_ARG_OBJ(HttpRequest
, request
, 0)
48 HTTP_EMPTY_ARGS(reset
);
51 HTTP_BEGIN_ARGS(singleton
, 0)
52 HTTP_ARG_VAL(global
, 0)
57 #define http_requestdatashare_object_read_prop _http_requestdatashare_object_read_prop
58 static zval
*_http_requestdatashare_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
);
59 #define http_requestdatashare_object_write_prop _http_requestdatashare_object_write_prop
60 static void _http_requestdatashare_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
);
61 #define http_requestdatashare_instantiate(t, g) _http_requestdatashare_instantiate((t), (g) TSRMLS_CC)
62 static inline zval
*_http_requestdatashare_instantiate(zval
*this_ptr
, zend_bool global TSRMLS_DC
);
64 #define OBJ_PROP_CE http_requestdatashare_object_ce
65 zend_class_entry
*http_requestdatashare_object_ce
;
66 zend_function_entry http_requestdatashare_object_fe
[] = {
67 HTTP_RSHARE_ME(__destruct
, ZEND_ACC_PUBLIC
|ZEND_ACC_DTOR
)
68 HTTP_RSHARE_ME(count
, ZEND_ACC_PUBLIC
)
69 HTTP_RSHARE_ME(attach
, ZEND_ACC_PUBLIC
)
70 HTTP_RSHARE_ME(detach
, ZEND_ACC_PUBLIC
)
71 HTTP_RSHARE_ME(reset
, ZEND_ACC_PUBLIC
)
73 HTTP_RSHARE_ME(singleton
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
77 static zend_object_handlers http_requestdatashare_object_handlers
;
79 PHP_MINIT_FUNCTION(http_requestdatashare_object
)
81 HTTP_REGISTER_CLASS_EX(HttpRequestDataShare
, http_requestdatashare_object
, NULL
, 0);
82 http_requestdatashare_object_handlers
.clone_obj
= NULL
;
83 http_requestdatashare_object_handlers
.read_property
= http_requestdatashare_object_read_prop
;
84 http_requestdatashare_object_handlers
.write_property
= http_requestdatashare_object_write_prop
;
86 #if defined(HAVE_SPL) && !defined(WONKY)
87 zend_class_implements(http_requestdatashare_object_ce TSRMLS_CC
, 1, spl_ce_Countable
);
90 DCL_STATIC_PROP_N(PRIVATE
, instance
);
91 DCL_PROP(PUBLIC
, bool, cookie
, 0);
92 DCL_PROP(PUBLIC
, bool, dns
, 0);
93 DCL_PROP(PUBLIC
, bool, ssl
, 0);
94 DCL_PROP(PUBLIC
, bool, connect
, 0);
99 zend_object_value
_http_requestdatashare_object_new(zend_class_entry
*ce TSRMLS_DC
)
101 return http_requestdatashare_object_new_ex(ce
, NULL
, NULL
);
104 zend_object_value
_http_requestdatashare_object_new_ex(zend_class_entry
*ce
, http_request_datashare
*share
, http_requestdatashare_object
**ptr TSRMLS_DC
)
106 zend_object_value ov
;
107 http_requestdatashare_object
*o
;
109 o
= ecalloc(1, sizeof(http_requestdatashare_object
));
115 o
->share
= http_request_datashare_new();
122 ALLOC_HASHTABLE(OBJ_PROP(o
));
123 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
124 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
126 ov
.handle
= putObject(http_requestdatashare_object
, o
);
127 ov
.handlers
= &http_requestdatashare_object_handlers
;
132 void _http_requestdatashare_object_free(zend_object
*object TSRMLS_DC
)
134 http_requestdatashare_object
*o
= (http_requestdatashare_object
*) object
;
136 if (!o
->share
->persistent
) {
137 http_request_datashare_free(&o
->share
);
142 static zval
*_http_requestdatashare_object_read_prop(zval
*object
, zval
*member
, int type TSRMLS_DC
)
144 if (type
== BP_VAR_W
&& zend_hash_exists(&OBJ_PROP_CE
->default_properties
, Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1)) {
145 zend_error(E_ERROR
, "Cannot access HttpRequestDataShare default properties by reference or array key/index");
149 return zend_get_std_object_handlers()->read_property(object
, member
, type TSRMLS_CC
);
152 static void _http_requestdatashare_object_write_prop(zval
*object
, zval
*member
, zval
*value TSRMLS_DC
)
154 if (zend_hash_exists(&OBJ_PROP_CE
->default_properties
, Z_STRVAL_P(member
), Z_STRLEN_P(member
)+1)) {
157 getObjectEx(http_requestdatashare_object
, obj
, object
);
159 SEPARATE_ZVAL_IF_NOT_REF(&value
);
160 status
= http_request_datashare_set(obj
->share
, Z_STRVAL_P(member
), Z_STRLEN_P(member
), (zend_bool
) zval_is_true(value
));
162 zval_ptr_dtor(&value
);
165 if (SUCCESS
!= status
) {
170 zend_get_std_object_handlers()->write_property(object
, member
, value TSRMLS_CC
);
173 /* {{{ proto void HttpRequestDataShare::__destruct()
175 * Clean up HttpRequestDataShare object.
177 PHP_METHOD(HttpRequestDataShare
, __destruct
)
180 getObject(http_requestdatashare_object
, obj
);
181 http_request_datashare_detach_all(obj
->share
);
186 /* {{{ proto int HttpRequestDataShare::count()
188 * Implements Countable::count().
190 PHP_METHOD(HttpRequestDataShare
, count
)
192 getObject(http_requestdatashare_object
, obj
);
196 RETURN_LONG(zend_llist_count(HTTP_RSHARE_HANDLES(obj
->share
)));
200 PHP_METHOD(HttpRequestDataShare
, attach
)
203 getObject(http_requestdatashare_object
, obj
);
205 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
209 RETURN_SUCCESS(http_request_datashare_attach(obj
->share
, request
));
212 PHP_METHOD(HttpRequestDataShare
, detach
)
215 getObject(http_requestdatashare_object
, obj
);
217 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &request
, http_request_object_ce
)) {
221 RETURN_SUCCESS(http_request_datashare_detach(obj
->share
, request
));
224 PHP_METHOD(HttpRequestDataShare
, reset
)
227 getObject(http_requestdatashare_object
, obj
);
228 http_request_datashare_detach_all(obj
->share
);
233 /* {{{ proto static HttpRequestDataShare HttpRequestDataShare::singleton([bool global = false])
235 * Get a single instance (differentiates between the global setting).
237 PHP_METHOD(HttpRequestDataShare
, singleton
)
239 zend_bool global
= 0;
240 zval
*instance
= GET_STATIC_PROP(instance
);
243 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &global
)) {
244 zval
**zobj_ptr
= NULL
, *zobj
= NULL
;
246 if (Z_TYPE_P(instance
) == IS_ARRAY
) {
247 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(instance
), global
, (void *) &zobj_ptr
)) {
248 RETVAL_ZVAL(*zobj_ptr
, 1, 0);
250 zobj
= http_requestdatashare_instantiate(NULL
, global
);
251 add_index_zval(instance
, global
, zobj
);
252 RETVAL_OBJECT(zobj
, 1);
255 MAKE_STD_ZVAL(instance
);
256 array_init(instance
);
258 zobj
= http_requestdatashare_instantiate(NULL
, global
);
259 add_index_zval(instance
, global
, zobj
);
260 RETVAL_OBJECT(zobj
, 1);
262 SET_STATIC_PROP(instance
, instance
);
263 zval_ptr_dtor(&instance
);
270 static inline zval
*_http_requestdatashare_instantiate(zval
*this_ptr
, zend_bool global TSRMLS_DC
)
273 MAKE_STD_ZVAL(this_ptr
);
274 Z_TYPE_P(this_ptr
) = IS_OBJECT
;
275 this_ptr
->value
.obj
= http_requestdatashare_object_new_ex(http_requestdatashare_object_ce
, global
? http_request_datashare_global_get() : NULL
, NULL
);
278 if (HTTP_G
->request
.datashare
.cookie
) {
279 UPD_PROP(bool, cookie
, HTTP_G
->request
.datashare
.cookie
);
281 if (HTTP_G
->request
.datashare
.dns
) {
282 UPD_PROP(bool, dns
, HTTP_G
->request
.datashare
.dns
);
284 if (HTTP_G
->request
.datashare
.ssl
) {
285 UPD_PROP(bool, ssl
, HTTP_G
->request
.datashare
.ssl
);
287 if (HTTP_G
->request
.datashare
.connect
) {
288 UPD_PROP(bool, connect
, HTTP_G
->request
.datashare
.connect
);
297 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
304 * vim600: noet sw=4 ts=4 fdm=marker
305 * vim<600: noet sw=4 ts=4