060493556807323dce2e14c4e02f5eb9ca1cc74e
[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-2010, 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 ZEND_LITERAL_KEY_DC 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 ZEND_LITERAL_KEY_DC 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 #if PHP_VERSION_ID < 50399
129 ALLOC_HASHTABLE(OBJ_PROP(o));
130 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
131 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
132 #else
133 object_properties_init(&o->zo, ce);
134 #endif
135
136 ov.handle = putObject(http_requestdatashare_object, o);
137 ov.handlers = &http_requestdatashare_object_handlers;
138
139 return ov;
140 }
141
142 void _http_requestdatashare_object_free(zend_object *object TSRMLS_DC)
143 {
144 http_requestdatashare_object *o = (http_requestdatashare_object *) object;
145
146 if (!o->share->persistent) {
147 http_request_datashare_free(&o->share);
148 }
149 freeObject(o);
150 }
151
152 static zval *_http_requestdatashare_object_read_prop(zval *object, zval *member, int type ZEND_LITERAL_KEY_DC TSRMLS_DC)
153 {
154 if (type == BP_VAR_W &&
155 #if PHP_VERSION_ID < 50399
156 zend_hash_exists(&THIS_CE->default_properties, Z_STRVAL_P(member), Z_STRLEN_P(member)+1)
157 #else
158 zend_get_property_info(THIS_CE, member, 1 TSRMLS_CC)
159 #endif
160 ) {
161 zend_error(E_ERROR, "Cannot access HttpRequestDataShare default properties by reference or array key/index");
162 return NULL;
163 }
164
165 return zend_get_std_object_handlers()->read_property(object, member, type ZEND_LITERAL_KEY_CC TSRMLS_CC);
166 }
167
168 static void _http_requestdatashare_object_write_prop(zval *object, zval *member, zval *value ZEND_LITERAL_KEY_DC TSRMLS_DC)
169 {
170 #if PHP_VERSION_ID < 50399
171 if (zend_hash_exists(&THIS_CE->default_properties, Z_STRVAL_P(member), Z_STRLEN_P(member)+1)) {
172 #else
173 if (zend_get_property_info(THIS_CE, member, 1 TSRMLS_CC)) {
174 #endif
175 int status;
176 getObjectEx(http_requestdatashare_object, obj, object);
177
178 status = http_request_datashare_set(obj->share, Z_STRVAL_P(member), Z_STRLEN_P(member), (zend_bool) i_zend_is_true(value));
179 if (SUCCESS != status) {
180 return;
181 }
182 }
183
184 zend_get_std_object_handlers()->write_property(object, member, value ZEND_LITERAL_KEY_CC TSRMLS_CC);
185 }
186
187 /* {{{ proto void HttpRequestDataShare::__destruct()
188 Clean up HttpRequestDataShare object. */
189 PHP_METHOD(HttpRequestDataShare, __destruct)
190 {
191 NO_ARGS {
192 getObject(http_requestdatashare_object, obj);
193 http_request_datashare_detach_all(obj->share);
194 }
195 }
196 /* }}} */
197
198 /* {{{ proto int HttpRequestDataShare::count()
199 Implements Countable::count(). */
200 PHP_METHOD(HttpRequestDataShare, count)
201 {
202 getObject(http_requestdatashare_object, obj);
203
204 NO_ARGS;
205
206 RETURN_LONG(zend_llist_count(HTTP_RSHARE_HANDLES(obj->share)));
207 }
208 /* }}} */
209
210 PHP_METHOD(HttpRequestDataShare, attach)
211 {
212 zval *request;
213 getObject(http_requestdatashare_object, obj);
214
215 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
216 RETURN_FALSE;
217 }
218
219 RETURN_SUCCESS(http_request_datashare_attach(obj->share, request));
220 }
221
222 PHP_METHOD(HttpRequestDataShare, detach)
223 {
224 zval *request;
225 getObject(http_requestdatashare_object, obj);
226
227 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
228 RETURN_FALSE;
229 }
230
231 RETURN_SUCCESS(http_request_datashare_detach(obj->share, request));
232 }
233
234 PHP_METHOD(HttpRequestDataShare, reset)
235 {
236 NO_ARGS {
237 getObject(http_requestdatashare_object, obj);
238 http_request_datashare_detach_all(obj->share);
239 }
240 }
241
242 PHP_METHOD(HttpRequestDataShare, factory)
243 {
244 zend_bool global = 0;
245 char *cn = NULL;
246 int cl = 0;
247 zend_object_value ov;
248
249 SET_EH_THROW_HTTP();
250 if ( SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bs", &global, &cn, &cl) &&
251 SUCCESS == http_object_new(&ov, cn, cl, _http_requestdatashare_object_new_ex, http_requestdatashare_object_ce, NULL, NULL)) {
252 RETVAL_OBJVAL(ov, 0);
253 http_requestdatashare_instantiate(return_value, global);
254 }
255 SET_EH_NORMAL();
256 }
257
258 #ifndef WONKY
259 /* {{{ proto static HttpRequestDataShare HttpRequestDataShare::singleton([bool global = false])
260 Get a single instance (differentiates between the global setting). */
261 PHP_METHOD(HttpRequestDataShare, singleton)
262 {
263 zend_bool global = 0;
264 zval *instance = *zend_std_get_static_property(THIS_CE, ZEND_STRS("instance")-1, 0 ZEND_LITERAL_NIL_CC TSRMLS_CC);
265
266 SET_EH_THROW_HTTP();
267 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
268 zval **zobj_ptr = NULL, *zobj = NULL;
269
270 if (Z_TYPE_P(instance) == IS_ARRAY) {
271 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void *) &zobj_ptr)) {
272 RETVAL_ZVAL(*zobj_ptr, 1, 0);
273 } else {
274 zobj = http_requestdatashare_instantiate(NULL, global);
275 add_index_zval(instance, global, zobj);
276 RETVAL_OBJECT(zobj, 1);
277 }
278 } else {
279 MAKE_STD_ZVAL(instance);
280 array_init(instance);
281
282 zobj = http_requestdatashare_instantiate(NULL, global);
283 add_index_zval(instance, global, zobj);
284 RETVAL_OBJECT(zobj, 1);
285
286 zend_update_static_property(THIS_CE, ZEND_STRS("instance")-1, instance TSRMLS_CC);
287 zval_ptr_dtor(&instance);
288 }
289 }
290 SET_EH_NORMAL();
291 }
292 /* }}} */
293 #endif /* !WONKY */
294
295 static inline zval *_http_requestdatashare_instantiate(zval *this_ptr, zend_bool global TSRMLS_DC)
296 {
297 if (!this_ptr) {
298 MAKE_STD_ZVAL(this_ptr);
299 Z_TYPE_P(this_ptr) = IS_OBJECT;
300 this_ptr->value.obj = http_requestdatashare_object_new_ex(http_requestdatashare_object_ce, global ? http_request_datashare_global_get() : NULL, NULL);
301 }
302 if (global) {
303 if (HTTP_G->request.datashare.cookie) {
304 zend_update_property_bool(THIS_CE, getThis(), ZEND_STRS("cookie")-1, HTTP_G->request.datashare.cookie TSRMLS_CC);
305 }
306 if (HTTP_G->request.datashare.dns) {
307 zend_update_property_bool(THIS_CE, getThis(), ZEND_STRS("dns")-1, HTTP_G->request.datashare.dns TSRMLS_CC);
308 }
309 if (HTTP_G->request.datashare.ssl) {
310 zend_update_property_bool(THIS_CE, getThis(), ZEND_STRS("ssl")-1, HTTP_G->request.datashare.ssl TSRMLS_CC);
311 }
312 if (HTTP_G->request.datashare.connect) {
313 zend_update_property_bool(THIS_CE, getThis(), ZEND_STRS("connect")-1, HTTP_G->request.datashare.connect TSRMLS_CC);
314 }
315 }
316 return this_ptr;
317 }
318
319 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
320
321 /*
322 * Local variables:
323 * tab-width: 4
324 * c-basic-offset: 4
325 * End:
326 * vim600: noet sw=4 ts=4 fdm=marker
327 * vim<600: noet sw=4 ts=4
328 */
329