less intrusive fix
[m6w6/ext-http] / php_http_request_datashare.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 #include <curl/curl.h>
16
17 static int php_http_request_datashare_compare_handles(void *h1, void *h2);
18
19 PHP_HTTP_API php_http_request_datashare_t *php_http_request_datashare_init(php_http_request_datashare_t *h, php_http_request_datashare_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC)
20 {
21 php_http_request_datashare_t *free_h = NULL;
22
23 if (!h) {
24 free_h = h = emalloc(sizeof(*h));
25 }
26 memset(h, sizeof(*h), 0);
27
28 zend_llist_init(&h->requests, sizeof(zval *), ZVAL_PTR_DTOR, 0);
29 h->ops = ops;
30 h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, NULL, NULL);
31 TSRMLS_SET_CTX(h->ts);
32
33 if (h->ops->init) {
34 if (!(h = h->ops->init(h, init_arg))) {
35 if (free_h) {
36 efree(free_h);
37 }
38 }
39 }
40
41 return h;
42 }
43
44 PHP_HTTP_API php_http_request_datashare_t *php_http_request_datashare_copy(php_http_request_datashare_t *from, php_http_request_datashare_t *to)
45 {
46 if (from->ops->copy) {
47 return from->ops->copy(from, to);
48 }
49
50 return NULL;
51 }
52
53 PHP_HTTP_API void php_http_request_datashare_dtor(php_http_request_datashare_t *h)
54 {
55 if (h->ops->dtor) {
56 h->ops->dtor(h);
57 }
58 zend_llist_destroy(&h->requests);
59 php_http_resource_factory_free(&h->rf);
60 }
61
62 PHP_HTTP_API void php_http_request_datashare_free(php_http_request_datashare_t **h)
63 {
64 php_http_request_datashare_dtor(*h);
65 efree(*h);
66 *h = NULL;
67 }
68
69 PHP_HTTP_API STATUS php_http_request_datashare_attach(php_http_request_datashare_t *h, zval *request)
70 {
71 TSRMLS_FETCH_FROM_CTX(h->ts);
72
73 if (h->ops->attach) {
74 php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC);
75
76 if (SUCCESS == h->ops->attach(h, obj->request)) {
77 Z_ADDREF_P(request);
78 zend_llist_add_element(&h->requests, &request);
79 return SUCCESS;
80 }
81 }
82
83 return FAILURE;
84 }
85
86 static int php_http_request_datashare_compare_handles(void *h1, void *h2)
87 {
88 return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2));
89 }
90
91 PHP_HTTP_API STATUS php_http_request_datashare_detach(php_http_request_datashare_t *h, zval *request)
92 {
93 TSRMLS_FETCH_FROM_CTX(h->ts);
94
95 if (h->ops->detach) {
96 php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC);
97
98 if (SUCCESS == h->ops->detach(h, obj->request)) {
99 zend_llist_del_element(&h->requests, request, php_http_request_datashare_compare_handles);
100 return SUCCESS;
101 }
102 }
103 return FAILURE;
104 }
105
106 PHP_HTTP_API STATUS php_http_request_datashare_setopt(php_http_request_datashare_t *h, php_http_request_datashare_setopt_opt_t opt, void *arg)
107 {
108 if (h->ops->setopt) {
109 return h->ops->setopt(h, opt, arg);
110 }
111 return FAILURE;
112 }
113
114 static void detach(void *r, void *h TSRMLS_DC)
115 {
116 ((php_http_request_datashare_t *) h)->ops->detach(h, ((php_http_request_object_t *) zend_object_store_get_object(*((zval **) r) TSRMLS_CC))->request);
117 }
118
119 PHP_HTTP_API void php_http_request_datashare_reset(php_http_request_datashare_t *h)
120 {
121 TSRMLS_FETCH_FROM_CTX(h->ts);
122
123 if (h->ops->reset) {
124 h->ops->reset(h);
125 } else if (h->ops->detach) {
126 zend_llist_apply_with_argument(&h->requests, detach, h TSRMLS_CC);
127 }
128
129 zend_llist_clean(&h->requests);
130 }
131
132 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpRequestDataShare, method, 0, req_args)
133 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequestDataShare, method, 0)
134 #define PHP_HTTP_RSHARE_ME(method, visibility) PHP_ME(HttpRequestDataShare, method, PHP_HTTP_ARGS(HttpRequestDataShare, method), visibility)
135
136 PHP_HTTP_EMPTY_ARGS(__construct);
137 PHP_HTTP_EMPTY_ARGS(__destruct);
138 PHP_HTTP_EMPTY_ARGS(reset);
139 PHP_HTTP_EMPTY_ARGS(count);
140
141 PHP_HTTP_BEGIN_ARGS(attach, 1)
142 PHP_HTTP_ARG_OBJ(http\\Request, request, 0)
143 PHP_HTTP_END_ARGS;
144 PHP_HTTP_BEGIN_ARGS(detach, 1)
145 PHP_HTTP_ARG_OBJ(http\\Request, request, 0)
146 PHP_HTTP_END_ARGS;
147
148 static void php_http_request_datashare_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC);
149
150 #define php_http_request_datashare_class_entry php_http_request_datashare_class_entry
151 zend_class_entry *php_http_request_datashare_class_entry;
152 zend_function_entry php_http_request_datashare_method_entry[] = {
153 PHP_HTTP_RSHARE_ME(__construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
154 PHP_HTTP_RSHARE_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
155 PHP_HTTP_RSHARE_ME(count, ZEND_ACC_PUBLIC)
156 PHP_HTTP_RSHARE_ME(attach, ZEND_ACC_PUBLIC)
157 PHP_HTTP_RSHARE_ME(detach, ZEND_ACC_PUBLIC)
158 PHP_HTTP_RSHARE_ME(reset, ZEND_ACC_PUBLIC)
159 EMPTY_FUNCTION_ENTRY
160 };
161 static zend_object_handlers php_http_request_datashare_object_handlers;
162
163 zend_object_value php_http_request_datashare_object_new(zend_class_entry *ce TSRMLS_DC)
164 {
165 return php_http_request_datashare_object_new_ex(ce, NULL, NULL TSRMLS_CC);
166 }
167
168 zend_object_value php_http_request_datashare_object_new_ex(zend_class_entry *ce, php_http_request_datashare_t *share, php_http_request_datashare_object_t **ptr TSRMLS_DC)
169 {
170 zend_object_value ov;
171 php_http_request_datashare_object_t *o;
172
173 o = ecalloc(1, sizeof(*o));
174 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
175 object_properties_init((zend_object *) o, ce);
176
177 if (share) {
178 o->share = share;
179 } else {
180 o->share = php_http_request_datashare_init(NULL, NULL, NULL, NULL TSRMLS_CC);
181 }
182
183 if (ptr) {
184 *ptr = o;
185 }
186
187 ov.handle = zend_objects_store_put(o, NULL, php_http_request_datashare_object_free, NULL TSRMLS_CC);
188 ov.handlers = &php_http_request_datashare_object_handlers;
189
190 return ov;
191 }
192
193 void php_http_request_datashare_object_free(void *object TSRMLS_DC)
194 {
195 php_http_request_datashare_object_t *o = (php_http_request_datashare_object_t *) object;
196
197 php_http_request_datashare_free(&o->share);
198 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
199 efree(o);
200 }
201
202 static void php_http_request_datashare_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC)
203 {
204 zend_property_info *pi;
205
206 if ((pi = zend_get_property_info(php_http_request_datashare_class_entry, member, 1 TSRMLS_CC))) {
207 zend_bool enable = i_zend_is_true(value);
208 php_http_request_datashare_setopt_opt_t opt;
209 php_http_request_datashare_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
210
211 if (!strcmp(pi->name, "cookie")) {
212 opt = PHP_HTTP_REQUEST_DATASHARE_OPT_COOKIES;
213 } else if (!strcmp(pi->name, "dns")) {
214 opt = PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER;
215 } else {
216 return;
217 }
218
219 if (SUCCESS != php_http_request_datashare_setopt(obj->share, opt, &enable)) {
220 return;
221 }
222 }
223
224 zend_get_std_object_handlers()->write_property(object, member, value, literal_key TSRMLS_CC);
225 }
226
227 static zval **php_http_request_datashare_object_get_prop_ptr(zval *object, zval *member, const zend_literal *literal_key TSRMLS_DC)
228 {
229 zend_property_info *pi;
230
231 if ((pi = zend_get_property_info(php_http_request_datashare_class_entry, member, 1 TSRMLS_CC))) {
232 return &php_http_property_proxy_init(NULL, object, member, NULL TSRMLS_CC)->myself;
233 }
234
235 return zend_get_std_object_handlers()->get_property_ptr_ptr(object, member, literal_key TSRMLS_CC);
236 }
237
238
239 PHP_METHOD(HttpRequestDataShare, __construct)
240 {
241 with_error_handling(EH_THROW, php_http_exception_class_entry) {
242 zend_parse_parameters_none();
243 } end_error_handling();
244 }
245
246 PHP_METHOD(HttpRequestDataShare, __destruct)
247 {
248 php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
249
250 if (SUCCESS == zend_parse_parameters_none()) {
251 ; /* we always want to clean up */
252 }
253
254 php_http_request_datashare_reset(obj->share);
255 }
256
257 PHP_METHOD(HttpRequestDataShare, count)
258 {
259 if (SUCCESS == zend_parse_parameters_none()) {
260 php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
261
262 RETURN_LONG(zend_llist_count(&obj->share->requests));
263 }
264 RETURN_FALSE;
265 }
266
267
268 PHP_METHOD(HttpRequestDataShare, attach)
269 {
270 zval *request;
271
272 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) {
273 php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
274
275 RETURN_SUCCESS(php_http_request_datashare_attach(obj->share, request));
276 }
277 RETURN_FALSE;
278
279 }
280
281 PHP_METHOD(HttpRequestDataShare, detach)
282 {
283 zval *request;
284
285 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) {
286 php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
287
288 RETURN_SUCCESS(php_http_request_datashare_detach(obj->share, request));
289 }
290 RETURN_FALSE;
291 }
292
293 PHP_METHOD(HttpRequestDataShare, reset)
294 {
295 if (SUCCESS == zend_parse_parameters_none()) {
296 php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
297
298 php_http_request_datashare_reset(obj->share);
299 RETURN_TRUE;
300 }
301 RETURN_FALSE;
302 }
303
304 PHP_MINIT_FUNCTION(http_request_datashare)
305 {
306 PHP_HTTP_REGISTER_CLASS(http\\Request, DataShare, http_request_datashare, php_http_object_class_entry, 0);
307 php_http_request_datashare_class_entry->create_object = php_http_request_datashare_object_new;
308 memcpy(&php_http_request_datashare_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
309 php_http_request_datashare_object_handlers.clone_obj = NULL;
310 php_http_request_datashare_object_handlers.write_property = php_http_request_datashare_object_write_prop;
311 php_http_request_datashare_object_handlers.get_property_ptr_ptr = php_http_request_datashare_object_get_prop_ptr;
312
313 zend_class_implements(php_http_request_datashare_class_entry TSRMLS_CC, 1, spl_ce_Countable);
314
315 zend_declare_property_bool(php_http_request_datashare_class_entry, ZEND_STRL("cookie"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
316 zend_declare_property_bool(php_http_request_datashare_class_entry, ZEND_STRL("dns"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
317
318 return SUCCESS;
319 }
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