use the params parser for query strings
[m6w6/ext-http] / php_http_property_proxy.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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #ifndef PHP_HTTP_PPDBG
16 # define PHP_HTTP_PPDBG 0
17 #endif
18
19 PHP_HTTP_API php_http_property_proxy_t *php_http_property_proxy_init(php_http_property_proxy_t *proxy, zval *object, zval *member, zval *parent TSRMLS_DC)
20 {
21 if (!proxy) {
22 proxy = emalloc(sizeof(*proxy));
23 }
24 memset(proxy, 0, sizeof(*proxy));
25
26 MAKE_STD_ZVAL(proxy->myself);
27 ZVAL_OBJVAL(proxy->myself, php_http_property_proxy_object_new_ex(php_http_property_proxy_get_class_entry(), proxy, NULL TSRMLS_CC), 0);
28 Z_ADDREF_P(object);
29 proxy->object = object;
30 proxy->member = php_http_ztyp(IS_STRING, member);
31 if (parent) {
32 Z_ADDREF_P(parent);
33 proxy->parent = parent;
34 }
35
36 #if PHP_HTTP_PPDBG
37 fprintf(stderr, "proxy_init: %s\n", Z_STRVAL_P(proxy->member));
38 #endif
39 return proxy;
40 }
41
42 PHP_HTTP_API void php_http_property_proxy_dtor(php_http_property_proxy_t *proxy)
43 {
44 zval_ptr_dtor(&proxy->object);
45 zval_ptr_dtor(&proxy->member);
46 zval_ptr_dtor(&proxy->myself);
47 if (proxy->parent) {
48 zval_ptr_dtor(&proxy->parent);
49 }
50 }
51
52 PHP_HTTP_API void php_http_property_proxy_free(php_http_property_proxy_t **proxy)
53 {
54 if (*proxy) {
55 php_http_property_proxy_dtor(*proxy);
56 efree(*proxy);
57 *proxy = NULL;
58 }
59 }
60
61
62 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpPropertyProxy, method, 0, req_args)
63 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpPropertyProxy, method, 0)
64 #define PHP_HTTP_PP_ME(method, visibility) PHP_ME(HttpPropertyProxy, method, PHP_HTTP_ARGS(HttpPropertyProxy, method), visibility)
65
66 PHP_HTTP_EMPTY_ARGS(__construct);
67
68 static zend_class_entry *php_http_property_proxy_class_entry;
69
70 zend_class_entry *php_http_property_proxy_get_class_entry(void)
71 {
72 return php_http_property_proxy_class_entry;
73 }
74
75 static zend_function_entry php_http_property_proxy_method_entry[] = {
76 PHP_HTTP_PP_ME(__construct, ZEND_ACC_FINAL|ZEND_ACC_PRIVATE)
77 EMPTY_FUNCTION_ENTRY
78 };
79
80 static zend_object_handlers php_http_property_proxy_object_handlers;
81
82 zend_object_value php_http_property_proxy_object_new(zend_class_entry *ce TSRMLS_DC)
83 {
84 return php_http_property_proxy_object_new_ex(ce, NULL, NULL TSRMLS_CC);
85 }
86
87 zend_object_value php_http_property_proxy_object_new_ex(zend_class_entry *ce, php_http_property_proxy_t *proxy, php_http_property_proxy_object_t **ptr TSRMLS_DC)
88 {
89 zend_object_value ov;
90 php_http_property_proxy_object_t *o;
91
92 o = ecalloc(1, sizeof(*o));
93 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
94 object_properties_init((zend_object *) o, ce);
95
96 if (ptr) {
97 *ptr = o;
98 }
99 o->proxy = proxy;
100
101 ov.handle = zend_objects_store_put(o, NULL, php_http_property_proxy_object_free, NULL TSRMLS_CC);
102 ov.handlers = &php_http_property_proxy_object_handlers;
103
104 return ov;
105 }
106
107 void php_http_property_proxy_object_free(void *object TSRMLS_DC)
108 {
109 php_http_property_proxy_object_t *o = object;
110
111 if (o->proxy) {
112 php_http_property_proxy_free(&o->proxy);
113 }
114 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
115 efree(o);
116 }
117
118 static zval *php_http_property_proxy_object_get(zval *object TSRMLS_DC)
119 {
120 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
121
122 #if PHP_HTTP_PPDBG
123 fprintf(stderr, "proxy_get: %s\n", Z_STRVAL_P(obj->proxy->member));
124 #endif
125 if (Z_TYPE_P(obj->proxy->object) == IS_OBJECT) {
126 return zend_read_property(Z_OBJCE_P(obj->proxy->object), obj->proxy->object, Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member), 0 TSRMLS_CC);
127 } else {
128 zval **data = NULL;
129
130 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(obj->proxy->object), Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member)+1, (void *) &data)) {
131 Z_ADDREF_PP(data);
132 } else {
133 zval *unset;
134
135 MAKE_STD_ZVAL(unset);
136 ZVAL_NULL(unset);
137 zend_symtable_update(Z_ARRVAL_P(obj->proxy->object), Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member)+1, (void *) &unset, sizeof(zval *), (void *) &data);
138 Z_ADDREF_PP(data);
139 }
140
141 return *data;
142 }
143
144 return NULL;
145 }
146
147 static void php_http_property_proxy_object_set(zval **object, zval *value TSRMLS_DC)
148 {
149 php_http_property_proxy_object_t *obj = zend_object_store_get_object(*object TSRMLS_CC);
150 zval *target = obj->proxy->parent ? php_http_property_proxy_object_get(obj->proxy->parent TSRMLS_CC) : obj->proxy->object;
151
152 #if PHP_HTTP_PPDBG
153 fprintf(stderr, "proxy_set: %s\n", Z_STRVAL_P(obj->proxy->member));
154 #endif
155 Z_ADDREF_P(value);
156 if (Z_TYPE_P(target) == IS_OBJECT) {
157 zend_update_property(Z_OBJCE_P(target), target, Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member), value TSRMLS_CC);
158 } else {
159 zend_symtable_update(Z_ARRVAL_P(target), Z_STRVAL_P(obj->proxy->member), Z_STRLEN_P(obj->proxy->member)+1, (void *) &value, sizeof(zval *), NULL);
160 }
161 /* propagate */
162 if (obj->proxy->parent) {
163 php_http_property_proxy_object_set(&obj->proxy->parent, target TSRMLS_CC);
164 zval_ptr_dtor(&target);
165 }
166 }
167
168 static STATUS php_http_property_proxy_object_cast(zval *object, zval *return_value, int type TSRMLS_DC)
169 {
170 zval *old_value, *new_value;
171
172 old_value = php_http_property_proxy_object_get(object TSRMLS_CC);
173 new_value = php_http_ztyp(type, old_value);
174
175 if (old_value != new_value) {
176 zval_ptr_dtor(&old_value);
177 }
178
179 RETVAL_ZVAL(new_value, 0, 0);
180
181 return SUCCESS;
182 }
183
184 static zval *php_http_property_proxy_object_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
185 {
186 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
187 zval *retval = NULL, *property;
188
189 #if PHP_HTTP_PPDBG
190 zval *ocpy = php_http_ztyp(IS_STRING, offset);
191 fprintf(stderr, "read_dimension: %s.%s (%d)\n", Z_STRVAL_P(obj->proxy->member), Z_STRVAL_P(ocpy), type);
192 zval_ptr_dtor(&ocpy);
193 #endif
194
195 property = php_http_property_proxy_object_get(object TSRMLS_CC);
196 if (type != BP_VAR_R) {
197 Z_ADDREF_P(property);
198 retval = php_http_property_proxy_init(NULL, property, offset, obj->proxy->myself TSRMLS_CC)->myself;
199 } else if (Z_TYPE_P(property) == IS_ARRAY) {
200 zval **data = NULL;
201
202 if (Z_TYPE_P(offset) == IS_LONG) {
203 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(property), Z_LVAL_P(offset), (void *) &data)) {
204 retval = *data;
205 }
206 } else {
207 offset = php_http_ztyp(IS_STRING, offset);
208 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(property), Z_STRVAL_P(offset), Z_STRLEN_P(offset), (void *) &data)) {
209 retval = *data;
210 }
211 zval_ptr_dtor(&offset);
212 }
213
214 if (data) {
215 Z_ADDREF_PP(data);
216 }
217 }
218 zval_ptr_dtor(&property);
219
220 return retval;
221 }
222
223 static void php_http_property_proxy_object_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC)
224 {
225 zval *property = php_http_property_proxy_object_get(object TSRMLS_CC);
226
227 #if PHP_HTTP_PPDBG
228 php_http_property_proxy_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
229 zval *ocpy = offset ? php_http_ztyp(IS_STRING, offset) : offset;
230 fprintf(stderr, "write_dimension: %s.%s (%d)\n", Z_STRVAL_P(obj->proxy->member), ocpy?Z_STRVAL_P(ocpy):"", Z_TYPE_P(property));
231 if(ocpy)zval_ptr_dtor(&ocpy);
232 #endif
233 switch (Z_TYPE_P(property)) {
234 case IS_NULL:
235 array_init(property);
236 /* no break */
237 case IS_ARRAY:
238 Z_ADDREF_P(value);
239 if (!offset) {
240 add_next_index_zval(property, value);
241 } else if (Z_TYPE_P(offset) == IS_LONG) {
242 add_index_zval(property, Z_LVAL_P(offset), value);
243 } else {
244 offset = php_http_ztyp(IS_STRING, offset);
245 add_assoc_zval_ex(property, Z_STRVAL_P(offset), Z_STRLEN_P(offset) + 1, value);
246 zval_ptr_dtor(&offset);
247 }
248 php_http_property_proxy_object_set(&object, property TSRMLS_CC);
249 zval_ptr_dtor(&property);
250 break;
251
252 default:
253 zval_ptr_dtor(&property);
254 break;
255 }
256 }
257
258 PHP_METHOD(HttpPropertyProxy, __construct)
259 {
260 }
261
262 PHP_MINIT_FUNCTION(http_property_proxy)
263 {
264 PHP_HTTP_REGISTER_CLASS(http\\Object, PropertyProxy, http_property_proxy, NULL, ZEND_ACC_FINAL_CLASS);
265 php_http_property_proxy_class_entry->create_object = php_http_property_proxy_object_new;
266 memcpy(&php_http_property_proxy_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
267 php_http_property_proxy_object_handlers.set = php_http_property_proxy_object_set;
268 php_http_property_proxy_object_handlers.get = php_http_property_proxy_object_get;
269 php_http_property_proxy_object_handlers.cast_object = php_http_property_proxy_object_cast;
270 php_http_property_proxy_object_handlers.read_dimension = php_http_property_proxy_object_read_dimension;
271 php_http_property_proxy_object_handlers.write_dimension = php_http_property_proxy_object_write_dimension;
272
273 return SUCCESS;
274 }
275
276
277 /*
278 * Local variables:
279 * tab-width: 4
280 * c-basic-offset: 4
281 * End:
282 * vim600: noet sw=4 ts=4 fdm=marker
283 * vim<600: noet sw=4 ts=4
284 */
285