- update test files
[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-2006, 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 #ifndef WONKY
51 HTTP_BEGIN_ARGS(singleton, 0)
52 HTTP_ARG_VAL(global, 0)
53 HTTP_END_ARGS;
54 #endif
55
56
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);
63
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)
72 #ifndef WONKY
73 HTTP_RSHARE_ME(singleton, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
74 #endif
75 EMPTY_FUNCTION_ENTRY
76 };
77 static zend_object_handlers http_requestdatashare_object_handlers;
78
79 PHP_MINIT_FUNCTION(http_requestdatashare_object)
80 {
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;
85
86 #if defined(HAVE_SPL) && !defined(WONKY)
87 zend_class_implements(http_requestdatashare_object_ce TSRMLS_CC, 1, spl_ce_Countable);
88 #endif
89
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);
95
96 return SUCCESS;
97 }
98
99 zend_object_value _http_requestdatashare_object_new(zend_class_entry *ce TSRMLS_DC)
100 {
101 return http_requestdatashare_object_new_ex(ce, NULL, NULL);
102 }
103
104 zend_object_value _http_requestdatashare_object_new_ex(zend_class_entry *ce, http_request_datashare *share, http_requestdatashare_object **ptr TSRMLS_DC)
105 {
106 zend_object_value ov;
107 http_requestdatashare_object *o;
108
109 o = ecalloc(1, sizeof(http_requestdatashare_object));
110 o->zo.ce = ce;
111
112 if (share) {
113 o->share = share;
114 } else {
115 o->share = http_request_datashare_new();
116 }
117
118 if (ptr) {
119 *ptr = o;
120 }
121
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 *));
125
126 ov.handle = putObject(http_requestdatashare_object, o);
127 ov.handlers = &http_requestdatashare_object_handlers;
128
129 return ov;
130 }
131
132 void _http_requestdatashare_object_free(zend_object *object TSRMLS_DC)
133 {
134 http_requestdatashare_object *o = (http_requestdatashare_object *) object;
135
136 if (!o->share->persistent) {
137 http_request_datashare_free(&o->share);
138 }
139 freeObject(o);
140 }
141
142 static zval *_http_requestdatashare_object_read_prop(zval *object, zval *member, int type TSRMLS_DC)
143 {
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");
146 return NULL;
147 }
148
149 return zend_get_std_object_handlers()->read_property(object, member, type TSRMLS_CC);
150 }
151
152 static void _http_requestdatashare_object_write_prop(zval *object, zval *member, zval *value TSRMLS_DC)
153 {
154 if (zend_hash_exists(&OBJ_PROP_CE->default_properties, Z_STRVAL_P(member), Z_STRLEN_P(member)+1)) {
155 int status;
156 zval *orig = value;
157 getObjectEx(http_requestdatashare_object, obj, object);
158
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));
161 if (orig != value) {
162 zval_ptr_dtor(&value);
163 value = orig;
164 }
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 *
175 * Clean up HttpRequestDataShare object.
176 */
177 PHP_METHOD(HttpRequestDataShare, __destruct)
178 {
179 NO_ARGS {
180 getObject(http_requestdatashare_object, obj);
181 http_request_datashare_detach_all(obj->share);
182 }
183 }
184 /* }}} */
185
186 /* {{{ proto int HttpRequestDataShare::count()
187 *
188 * Implements Countable::count().
189 */
190 PHP_METHOD(HttpRequestDataShare, count)
191 {
192 getObject(http_requestdatashare_object, obj);
193
194 NO_ARGS;
195
196 RETURN_LONG(zend_llist_count(HTTP_RSHARE_HANDLES(obj->share)));
197 }
198 /* }}} */
199
200 PHP_METHOD(HttpRequestDataShare, attach)
201 {
202 zval *request;
203 getObject(http_requestdatashare_object, obj);
204
205 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
206 RETURN_FALSE;
207 }
208
209 RETURN_SUCCESS(http_request_datashare_attach(obj->share, request));
210 }
211
212 PHP_METHOD(HttpRequestDataShare, detach)
213 {
214 zval *request;
215 getObject(http_requestdatashare_object, obj);
216
217 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
218 RETURN_FALSE;
219 }
220
221 RETURN_SUCCESS(http_request_datashare_detach(obj->share, request));
222 }
223
224 PHP_METHOD(HttpRequestDataShare, reset)
225 {
226 NO_ARGS {
227 getObject(http_requestdatashare_object, obj);
228 http_request_datashare_detach_all(obj->share);
229 }
230 }
231
232 #ifndef WONKY
233 /* {{{ proto static HttpRequestDataShare HttpRequestDataShare::singleton([bool global = false])
234 *
235 * Get a single instance (differentiates between the global setting).
236 */
237 PHP_METHOD(HttpRequestDataShare, singleton)
238 {
239 zend_bool global = 0;
240 zval *instance = GET_STATIC_PROP(instance);
241
242 SET_EH_THROW_HTTP();
243 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
244 zval **zobj_ptr = NULL, *zobj = NULL;
245
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);
249 } else {
250 zobj = http_requestdatashare_instantiate(NULL, global);
251 add_index_zval(instance, global, zobj);
252 RETVAL_OBJECT(zobj, 1);
253 }
254 } else {
255 MAKE_STD_ZVAL(instance);
256 array_init(instance);
257
258 zobj = http_requestdatashare_instantiate(NULL, global);
259 add_index_zval(instance, global, zobj);
260 RETVAL_OBJECT(zobj, 1);
261
262 SET_STATIC_PROP(instance, instance);
263 zval_ptr_dtor(&instance);
264 }
265 }
266 SET_EH_NORMAL();
267 }
268 /* }}} */
269
270 static inline zval *_http_requestdatashare_instantiate(zval *this_ptr, zend_bool global TSRMLS_DC)
271 {
272 if (!this_ptr) {
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);
276 }
277 if (global) {
278 if (HTTP_G->request.datashare.cookie) {
279 UPD_PROP(bool, cookie, HTTP_G->request.datashare.cookie);
280 }
281 if (HTTP_G->request.datashare.dns) {
282 UPD_PROP(bool, dns, HTTP_G->request.datashare.dns);
283 }
284 if (HTTP_G->request.datashare.ssl) {
285 UPD_PROP(bool, ssl, HTTP_G->request.datashare.ssl);
286 }
287 if (HTTP_G->request.datashare.connect) {
288 UPD_PROP(bool, connect, HTTP_G->request.datashare.connect);
289 }
290 }
291 return this_ptr;
292 }
293 #endif
294
295
296
297 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
298
299 /*
300 * Local variables:
301 * tab-width: 4
302 * c-basic-offset: 4
303 * End:
304 * vim600: noet sw=4 ts=4 fdm=marker
305 * vim<600: noet sw=4 ts=4
306 */
307