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