fixed another new 5_3 incompatibility (bug #15065)
[m6w6/ext-http] / http_requestdatashare_object.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-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_CURL
16 #include "php_http.h"
17
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
19
20 #include "zend_interfaces.h"
21
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"
28
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)
32
33 #if defined(HAVE_SPL) && !defined(WONKY)
34 /* SPL doesn't install its headers */
35 extern PHPAPI zend_class_entry *spl_ce_Countable;
36 #endif
37
38 HTTP_EMPTY_ARGS(__destruct);
39 HTTP_EMPTY_ARGS(count);
40
41 HTTP_BEGIN_ARGS(attach, 1)
42 HTTP_ARG_OBJ(HttpRequest, request, 0)
43 HTTP_END_ARGS;
44 HTTP_BEGIN_ARGS(detach, 1)
45 HTTP_ARG_OBJ(HttpRequest, request, 0)
46 HTTP_END_ARGS;
47
48 HTTP_EMPTY_ARGS(reset);
49
50 HTTP_BEGIN_ARGS(factory, 0)
51 HTTP_ARG_VAL(global, 0)
52 HTTP_ARG_VAL(class_name, 0)
53 HTTP_END_ARGS;
54
55 #ifndef WONKY
56 HTTP_BEGIN_ARGS(singleton, 0)
57 HTTP_ARG_VAL(global, 0)
58 HTTP_END_ARGS;
59 #endif
60
61
62 #define http_requestdatashare_object_read_prop _http_requestdatashare_object_read_prop
63 static zval *_http_requestdatashare_object_read_prop(zval *object, zval *member, int type TSRMLS_DC);
64 #define http_requestdatashare_object_write_prop _http_requestdatashare_object_write_prop
65 static void _http_requestdatashare_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC);
66 #define http_requestdatashare_instantiate(t, g) _http_requestdatashare_instantiate((t), (g) TSRMLS_CC)
67 static inline zval *_http_requestdatashare_instantiate(zval *this_ptr, zend_bool global TSRMLS_DC);
68
69 #define THIS_CE http_requestdatashare_object_ce
70 zend_class_entry *http_requestdatashare_object_ce;
71 zend_function_entry http_requestdatashare_object_fe[] = {
72 HTTP_RSHARE_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
73 HTTP_RSHARE_ME(count, ZEND_ACC_PUBLIC)
74 HTTP_RSHARE_ME(attach, ZEND_ACC_PUBLIC)
75 HTTP_RSHARE_ME(detach, ZEND_ACC_PUBLIC)
76 HTTP_RSHARE_ME(reset, ZEND_ACC_PUBLIC)
77 HTTP_RSHARE_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
78 #ifndef WONKY
79 HTTP_RSHARE_ME(singleton, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
80 #endif
81 EMPTY_FUNCTION_ENTRY
82 };
83 static zend_object_handlers http_requestdatashare_object_handlers;
84
85 PHP_MINIT_FUNCTION(http_requestdatashare_object)
86 {
87 HTTP_REGISTER_CLASS_EX(HttpRequestDataShare, http_requestdatashare_object, NULL, 0);
88 http_requestdatashare_object_handlers.clone_obj = NULL;
89 http_requestdatashare_object_handlers.read_property = http_requestdatashare_object_read_prop;
90 http_requestdatashare_object_handlers.write_property = http_requestdatashare_object_write_prop;
91
92 #if defined(HAVE_SPL) && !defined(WONKY)
93 zend_class_implements(http_requestdatashare_object_ce TSRMLS_CC, 1, spl_ce_Countable);
94 #endif
95
96 zend_declare_property_null(THIS_CE, ZEND_STRS("instance")-1, (ZEND_ACC_STATIC|ZEND_ACC_PRIVATE) TSRMLS_CC);
97 zend_declare_property_bool(THIS_CE, ZEND_STRS("cookie")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
98 zend_declare_property_bool(THIS_CE, ZEND_STRS("dns")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
99 zend_declare_property_bool(THIS_CE, ZEND_STRS("ssl")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
100 zend_declare_property_bool(THIS_CE, ZEND_STRS("connect")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
101
102 return SUCCESS;
103 }
104
105 zend_object_value _http_requestdatashare_object_new(zend_class_entry *ce TSRMLS_DC)
106 {
107 return http_requestdatashare_object_new_ex(ce, NULL, NULL);
108 }
109
110 zend_object_value _http_requestdatashare_object_new_ex(zend_class_entry *ce, http_request_datashare *share, http_requestdatashare_object **ptr TSRMLS_DC)
111 {
112 zend_object_value ov;
113 http_requestdatashare_object *o;
114
115 o = ecalloc(1, sizeof(http_requestdatashare_object));
116 o->zo.ce = ce;
117
118 if (share) {
119 o->share = share;
120 } else {
121 o->share = http_request_datashare_new();
122 }
123
124 if (ptr) {
125 *ptr = o;
126 }
127
128 ALLOC_HASHTABLE(OBJ_PROP(o));
129 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
130 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
131
132 ov.handle = putObject(http_requestdatashare_object, o);
133 ov.handlers = &http_requestdatashare_object_handlers;
134
135 return ov;
136 }
137
138 void _http_requestdatashare_object_free(zend_object *object TSRMLS_DC)
139 {
140 http_requestdatashare_object *o = (http_requestdatashare_object *) object;
141
142 if (!o->share->persistent) {
143 http_request_datashare_free(&o->share);
144 }
145 freeObject(o);
146 }
147
148 static zval *_http_requestdatashare_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
149 {
150 if (type == BP_VAR_W && zend_hash_exists(&THIS_CE->default_properties, Z_STRVAL_P(member), Z_STRLEN_P(member)+1)) {
151 zend_error(E_ERROR, "Cannot access HttpRequestDataShare default properties by reference or array key/index");
152 return NULL;
153 }
154
155 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
156 }
157
158 static void _http_requestdatashare_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC)
159 {
160 if (zend_hash_exists(&THIS_CE->default_properties, Z_STRVAL_P(member), Z_STRLEN_P(member)+1)) {
161 int status;
162 getObjectEx(http_requestdatashare_object, obj, object);
163
164 status = http_request_datashare_set(obj->share, Z_STRVAL_P(member), Z_STRLEN_P(member), (zend_bool) i_zend_is_true(value));
165 if (SUCCESS != status) {
166 return;
167 }
168 }
169
170 zend_get_std_object_handlers()->write_property(object, member, value TSRMLS_CC);
171 }
172
173 /* {{{ proto void HttpRequestDataShare::__destruct()
174 Clean up HttpRequestDataShare object. */
175 PHP_METHOD(HttpRequestDataShare, __destruct)
176 {
177 NO_ARGS {
178 getObject(http_requestdatashare_object, obj);
179 http_request_datashare_detach_all(obj->share);
180 }
181 }
182 /* }}} */
183
184 /* {{{ proto int HttpRequestDataShare::count()
185 Implements Countable::count(). */
186 PHP_METHOD(HttpRequestDataShare, count)
187 {
188 getObject(http_requestdatashare_object, obj);
189
190 NO_ARGS;
191
192 RETURN_LONG(zend_llist_count(HTTP_RSHARE_HANDLES(obj->share)));
193 }
194 /* }}} */
195
196 PHP_METHOD(HttpRequestDataShare, attach)
197 {
198 zval *request;
199 getObject(http_requestdatashare_object, obj);
200
201 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
202 RETURN_FALSE;
203 }
204
205 RETURN_SUCCESS(http_request_datashare_attach(obj->share, request));
206 }
207
208 PHP_METHOD(HttpRequestDataShare, detach)
209 {
210 zval *request;
211 getObject(http_requestdatashare_object, obj);
212
213 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
214 RETURN_FALSE;
215 }
216
217 RETURN_SUCCESS(http_request_datashare_detach(obj->share, request));
218 }
219
220 PHP_METHOD(HttpRequestDataShare, reset)
221 {
222 NO_ARGS {
223 getObject(http_requestdatashare_object, obj);
224 http_request_datashare_detach_all(obj->share);
225 }
226 }
227
228 PHP_METHOD(HttpRequestDataShare, factory)
229 {
230 zend_bool global = 0;
231 char *cn = NULL;
232 int cl = 0;
233 zend_object_value ov;
234
235 SET_EH_THROW_HTTP();
236 if ( SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bs", &global, &cn, &cl) &&
237 SUCCESS == http_object_new(&ov, cn, cl, _http_requestdatashare_object_new_ex, http_requestdatashare_object_ce, NULL, NULL)) {
238 RETVAL_OBJVAL(ov, 0);
239 http_requestdatashare_instantiate(return_value, global);
240 }
241 SET_EH_NORMAL();
242 }
243
244 #ifndef WONKY
245 /* {{{ proto static HttpRequestDataShare HttpRequestDataShare::singleton([bool global = false])
246 Get a single instance (differentiates between the global setting). */
247 PHP_METHOD(HttpRequestDataShare, singleton)
248 {
249 zend_bool global = 0;
250 zval *instance = *zend_std_get_static_property(THIS_CE, ZEND_STRS("instance")-1, 0 TSRMLS_CC);
251
252 SET_EH_THROW_HTTP();
253 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
254 zval **zobj_ptr = NULL, *zobj = NULL;
255
256 if (Z_TYPE_P(instance) == IS_ARRAY) {
257 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void *) &zobj_ptr)) {
258 RETVAL_ZVAL(*zobj_ptr, 1, 0);
259 } else {
260 zobj = http_requestdatashare_instantiate(NULL, global);
261 add_index_zval(instance, global, zobj);
262 RETVAL_OBJECT(zobj, 1);
263 }
264 } else {
265 MAKE_STD_ZVAL(instance);
266 array_init(instance);
267
268 zobj = http_requestdatashare_instantiate(NULL, global);
269 add_index_zval(instance, global, zobj);
270 RETVAL_OBJECT(zobj, 1);
271
272 zend_update_static_property(THIS_CE, ZEND_STRS("instance")-1, instance TSRMLS_CC);
273 zval_ptr_dtor(&instance);
274 }
275 }
276 SET_EH_NORMAL();
277 }
278 /* }}} */
279 #endif /* !WONKY */
280
281 static inline zval *_http_requestdatashare_instantiate(zval *this_ptr, zend_bool global TSRMLS_DC)
282 {
283 if (!this_ptr) {
284 MAKE_STD_ZVAL(this_ptr);
285 Z_TYPE_P(this_ptr) = IS_OBJECT;
286 this_ptr->value.obj = http_requestdatashare_object_new_ex(http_requestdatashare_object_ce, global ? http_request_datashare_global_get() : NULL, NULL);
287 }
288 if (global) {
289 if (HTTP_G->request.datashare.cookie) {
290 zend_update_property_bool(THIS_CE, getThis(), ZEND_STRS("cookie")-1, HTTP_G->request.datashare.cookie TSRMLS_CC);
291 }
292 if (HTTP_G->request.datashare.dns) {
293 zend_update_property_bool(THIS_CE, getThis(), ZEND_STRS("dns")-1, HTTP_G->request.datashare.dns TSRMLS_CC);
294 }
295 if (HTTP_G->request.datashare.ssl) {
296 zend_update_property_bool(THIS_CE, getThis(), ZEND_STRS("ssl")-1, HTTP_G->request.datashare.ssl TSRMLS_CC);
297 }
298 if (HTTP_G->request.datashare.connect) {
299 zend_update_property_bool(THIS_CE, getThis(), ZEND_STRS("connect")-1, HTTP_G->request.datashare.connect TSRMLS_CC);
300 }
301 }
302 return this_ptr;
303 }
304
305 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
306
307 /*
308 * Local variables:
309 * tab-width: 4
310 * c-basic-offset: 4
311 * End:
312 * vim600: noet sw=4 ts=4 fdm=marker
313 * vim<600: noet sw=4 ts=4
314 */
315