2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #include <curl/curl.h>
17 static int php_http_request_datashare_compare_handles(void *h1
, void *h2
);
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
)
21 php_http_request_datashare_t
*free_h
= NULL
;
24 free_h
= h
= emalloc(sizeof(*h
));
26 memset(h
, sizeof(*h
), 0);
28 zend_llist_init(&h
->requests
, sizeof(zval
*), ZVAL_PTR_DTOR
, 0);
30 h
->rf
= rf
? rf
: php_http_resource_factory_init(NULL
, h
->ops
->rsrc
, NULL
, NULL
);
31 TSRMLS_SET_CTX(h
->ts
);
34 if (!(h
= h
->ops
->init(h
, init_arg
))) {
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
)
46 if (from
->ops
->copy
) {
47 return from
->ops
->copy(from
, to
);
53 PHP_HTTP_API
void php_http_request_datashare_dtor(php_http_request_datashare_t
*h
)
58 zend_llist_destroy(&h
->requests
);
59 php_http_resource_factory_free(&h
->rf
);
62 PHP_HTTP_API
void php_http_request_datashare_free(php_http_request_datashare_t
**h
)
64 php_http_request_datashare_dtor(*h
);
69 PHP_HTTP_API STATUS
php_http_request_datashare_attach(php_http_request_datashare_t
*h
, zval
*request
)
71 TSRMLS_FETCH_FROM_CTX(h
->ts
);
74 php_http_request_object_t
*obj
= zend_object_store_get_object(request TSRMLS_CC
);
76 if (SUCCESS
== h
->ops
->attach(h
, obj
->request
)) {
78 zend_llist_add_element(&h
->requests
, &request
);
86 static int php_http_request_datashare_compare_handles(void *h1
, void *h2
)
88 return (Z_OBJ_HANDLE_PP((zval
**) h1
) == Z_OBJ_HANDLE_P((zval
*) h2
));
91 PHP_HTTP_API STATUS
php_http_request_datashare_detach(php_http_request_datashare_t
*h
, zval
*request
)
93 TSRMLS_FETCH_FROM_CTX(h
->ts
);
96 php_http_request_object_t
*obj
= zend_object_store_get_object(request TSRMLS_CC
);
98 if (SUCCESS
== h
->ops
->detach(h
, obj
->request
)) {
99 zend_llist_del_element(&h
->requests
, request
, php_http_request_datashare_compare_handles
);
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
)
108 if (h
->ops
->setopt
) {
109 return h
->ops
->setopt(h
, opt
, arg
);
114 static void detach(void *r
, void *h TSRMLS_DC
)
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
);
119 PHP_HTTP_API
void php_http_request_datashare_reset(php_http_request_datashare_t
*h
)
121 TSRMLS_FETCH_FROM_CTX(h
->ts
);
125 } else if (h
->ops
->detach
) {
126 zend_llist_apply_with_argument(&h
->requests
, detach
, h TSRMLS_CC
);
129 zend_llist_clean(&h
->requests
);
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)
136 PHP_HTTP_EMPTY_ARGS(__construct
);
137 PHP_HTTP_EMPTY_ARGS(__destruct
);
138 PHP_HTTP_EMPTY_ARGS(reset
);
139 PHP_HTTP_EMPTY_ARGS(count
);
141 PHP_HTTP_BEGIN_ARGS(attach
, 1)
142 PHP_HTTP_ARG_OBJ(http
\\Request
, request
, 0)
144 PHP_HTTP_BEGIN_ARGS(detach
, 1)
145 PHP_HTTP_ARG_OBJ(http
\\Request
, request
, 0)
148 static void php_http_request_datashare_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
);
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
)
161 static zend_object_handlers php_http_request_datashare_object_handlers
;
163 zend_object_value
php_http_request_datashare_object_new(zend_class_entry
*ce TSRMLS_DC
)
165 return php_http_request_datashare_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
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
)
170 zend_object_value ov
;
171 php_http_request_datashare_object_t
*o
;
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
);
180 o
->share
= php_http_request_datashare_init(NULL
, NULL
, NULL
, NULL TSRMLS_CC
);
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
;
193 void php_http_request_datashare_object_free(void *object TSRMLS_DC
)
195 php_http_request_datashare_object_t
*o
= (php_http_request_datashare_object_t
*) object
;
197 php_http_request_datashare_free(&o
->share
);
198 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
202 static void php_http_request_datashare_object_write_prop(zval
*object
, zval
*member
, zval
*value
, const zend_literal
*literal_key TSRMLS_DC
)
204 zend_property_info
*pi
;
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
);
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
;
219 if (SUCCESS
!= php_http_request_datashare_setopt(obj
->share
, opt
, &enable
)) {
224 zend_get_std_object_handlers()->write_property(object
, member
, value
, literal_key TSRMLS_CC
);
227 static zval
**php_http_request_datashare_object_get_prop_ptr(zval
*object
, zval
*member
, const zend_literal
*literal_key TSRMLS_DC
)
229 zend_property_info
*pi
;
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
;
235 return zend_get_std_object_handlers()->get_property_ptr_ptr(object
, member
, literal_key TSRMLS_CC
);
239 PHP_METHOD(HttpRequestDataShare
, __construct
)
241 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
242 zend_parse_parameters_none();
243 } end_error_handling();
246 PHP_METHOD(HttpRequestDataShare
, __destruct
)
248 php_http_request_datashare_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
250 if (SUCCESS
== zend_parse_parameters_none()) {
251 ; /* we always want to clean up */
254 php_http_request_datashare_reset(obj
->share
);
257 PHP_METHOD(HttpRequestDataShare
, count
)
259 if (SUCCESS
== zend_parse_parameters_none()) {
260 php_http_request_datashare_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
262 RETURN_LONG(zend_llist_count(&obj
->share
->requests
));
268 PHP_METHOD(HttpRequestDataShare
, attach
)
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
);
275 RETURN_SUCCESS(php_http_request_datashare_attach(obj
->share
, request
));
281 PHP_METHOD(HttpRequestDataShare
, detach
)
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
);
288 RETURN_SUCCESS(php_http_request_datashare_detach(obj
->share
, request
));
293 PHP_METHOD(HttpRequestDataShare
, reset
)
295 if (SUCCESS
== zend_parse_parameters_none()) {
296 php_http_request_datashare_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
298 php_http_request_datashare_reset(obj
->share
);
304 PHP_MINIT_FUNCTION(http_request_datashare
)
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
;
313 zend_class_implements(php_http_request_datashare_class_entry TSRMLS_CC
, 1, spl_ce_Countable
);
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
);
326 * vim600: noet sw=4 ts=4 fdm=marker
327 * vim<600: noet sw=4 ts=4