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 <ext/spl/spl_observer.h>
16 #include <ext/standard/php_array.h>
18 PHP_HTTP_API php_http_client_t
*php_http_client_init(php_http_client_t
*h
, php_http_client_ops_t
*ops
, php_http_resource_factory_t
*rf
, void *init_arg TSRMLS_DC
)
20 php_http_client_t
*free_h
= NULL
;
23 free_h
= h
= emalloc(sizeof(*h
));
25 memset(h
, 0, sizeof(*h
));
30 } else if (ops
->rsrc
) {
31 h
->rf
= php_http_resource_factory_init(NULL
, h
->ops
->rsrc
, h
, NULL
);
33 h
->request
.buffer
= php_http_buffer_init(NULL
);
34 h
->request
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
35 h
->request
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
37 h
->response
.buffer
= php_http_buffer_init(NULL
);
38 h
->response
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
39 h
->response
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
41 TSRMLS_SET_CTX(h
->ts
);
44 if (!(h
= h
->ops
->init(h
, init_arg
))) {
45 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Could not initialize request");
48 php_http_client_free(&free_h
);
56 PHP_HTTP_API
void php_http_client_dtor(php_http_client_t
*h
)
62 php_http_resource_factory_free(&h
->rf
);
64 php_http_message_parser_free(&h
->request
.parser
);
65 php_http_message_free(&h
->request
.message
);
66 php_http_buffer_free(&h
->request
.buffer
);
68 php_http_message_parser_free(&h
->response
.parser
);
69 php_http_message_free(&h
->response
.message
);
70 php_http_buffer_free(&h
->response
.buffer
);
73 PHP_HTTP_API
void php_http_client_free(php_http_client_t
**h
)
76 php_http_client_dtor(*h
);
82 PHP_HTTP_API php_http_client_t
*php_http_client_copy(php_http_client_t
*from
, php_http_client_t
*to
)
84 if (!from
->ops
->copy
) {
87 TSRMLS_FETCH_FROM_CTX(from
->ts
);
90 to
= ecalloc(1, sizeof(*to
));
95 php_http_resource_factory_addref(from
->rf
);
97 } else if (to
->ops
->rsrc
){
98 to
->rf
= php_http_resource_factory_init(NULL
, to
->ops
->rsrc
, to
, NULL
);
101 to
->request
.buffer
= php_http_buffer_init(NULL
);
102 to
->request
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
103 to
->request
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
105 to
->response
.buffer
= php_http_buffer_init(NULL
);
106 to
->response
.parser
= php_http_message_parser_init(NULL TSRMLS_CC
);
107 to
->response
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
109 TSRMLS_SET_CTX(to
->ts
);
111 return to
->ops
->copy(from
, to
);
115 PHP_HTTP_API STATUS
php_http_client_exec(php_http_client_t
*h
, php_http_message_t
*msg
)
118 return h
->ops
->exec(h
, msg
);
123 PHP_HTTP_API STATUS
php_http_client_reset(php_http_client_t
*h
)
126 return h
->ops
->reset(h
);
131 PHP_HTTP_API STATUS
php_http_client_setopt(php_http_client_t
*h
, php_http_client_setopt_opt_t opt
, void *arg
)
133 if (h
->ops
->setopt
) {
134 return h
->ops
->setopt(h
, opt
, arg
);
139 PHP_HTTP_API STATUS
php_http_client_getopt(php_http_client_t
*h
, php_http_client_getopt_opt_t opt
, void *arg
)
141 if (h
->ops
->getopt
) {
142 return h
->ops
->getopt(h
, opt
, arg
);
147 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClient, method, 0, req_args)
148 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClient, method, 0)
149 #define PHP_HTTP_CLIENT_ME(method, visibility) PHP_ME(HttpClient, method, PHP_HTTP_ARGS(HttpClient, method), visibility)
150 #define PHP_HTTP_CLIENT_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClient, method))
151 #define PHP_HTTP_CLIENT_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClient_##al), PHP_HTTP_ARGS(HttpClient, al), vis)
153 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
154 PHP_HTTP_ARG_ARR(options
, 1, 0)
157 PHP_HTTP_EMPTY_ARGS(getOptions
);
158 PHP_HTTP_BEGIN_ARGS(setOptions
, 0)
159 PHP_HTTP_ARG_ARR(options
, 1, 0)
162 PHP_HTTP_EMPTY_ARGS(getSslOptions
);
163 PHP_HTTP_BEGIN_ARGS(setSslOptions
, 0)
164 PHP_HTTP_ARG_ARR(ssl_options
, 1, 0)
167 PHP_HTTP_BEGIN_ARGS(addSslOptions
, 0)
168 PHP_HTTP_ARG_ARR(ssl_options
, 1, 0)
171 PHP_HTTP_EMPTY_ARGS(getCookies
);
172 PHP_HTTP_BEGIN_ARGS(setCookies
, 0)
173 PHP_HTTP_ARG_VAL(cookies
, 0)
176 PHP_HTTP_BEGIN_ARGS(addCookies
, 1)
177 PHP_HTTP_ARG_VAL(cookies
, 0)
180 PHP_HTTP_EMPTY_ARGS(enableCookies
);
181 PHP_HTTP_BEGIN_ARGS(resetCookies
, 0)
182 PHP_HTTP_ARG_VAL(session_only
, 0)
184 PHP_HTTP_EMPTY_ARGS(flushCookies
);
186 PHP_HTTP_EMPTY_ARGS(getResponseMessageClass
);
187 PHP_HTTP_BEGIN_ARGS(setResponseMessageClass
, 1)
188 PHP_HTTP_ARG_VAL(message_class_name
, 0)
191 PHP_HTTP_EMPTY_ARGS(getResponseMessage
);
192 PHP_HTTP_EMPTY_ARGS(getRequestMessage
);
193 PHP_HTTP_EMPTY_ARGS(getHistory
);
194 PHP_HTTP_EMPTY_ARGS(clearHistory
);
196 PHP_HTTP_BEGIN_ARGS(setRequest
, 1)
197 PHP_HTTP_ARG_OBJ(http
\\Client
\\Request
, request
, 1)
199 PHP_HTTP_EMPTY_ARGS(getRequest
);
201 PHP_HTTP_EMPTY_ARGS(getObservers
);
202 PHP_HTTP_BEGIN_ARGS(attach
, 1)
203 PHP_HTTP_ARG_OBJ(SplObserver
, observer
, 0)
205 PHP_HTTP_BEGIN_ARGS(detach
, 1)
206 PHP_HTTP_ARG_OBJ(SplObserver
, observer
, 0)
208 PHP_HTTP_EMPTY_ARGS(notify
);
209 PHP_HTTP_EMPTY_ARGS(getProgress
);
210 PHP_HTTP_BEGIN_ARGS(getTransferInfo
, 0)
211 PHP_HTTP_ARG_VAL(name
, 0)
214 PHP_HTTP_BEGIN_ARGS(request
, 2)
215 PHP_HTTP_ARG_VAL(method
, 0)
216 PHP_HTTP_ARG_VAL(url
, 0)
217 PHP_HTTP_ARG_ARR(headers
, 1, 0)
218 PHP_HTTP_ARG_VAL(body
, 0)
219 PHP_HTTP_ARG_ARR(options
, 1, 0)
222 static zend_class_entry
*php_http_client_class_entry
;
224 zend_class_entry
*php_http_client_get_class_entry(void)
226 return php_http_client_class_entry
;
229 static zend_function_entry php_http_client_method_entry
[] = {
230 PHP_HTTP_CLIENT_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
231 PHP_HTTP_CLIENT_ME(getObservers
, ZEND_ACC_PUBLIC
)
232 PHP_HTTP_CLIENT_ME(notify
, ZEND_ACC_PUBLIC
)
233 PHP_HTTP_CLIENT_ME(attach
, ZEND_ACC_PUBLIC
)
234 PHP_HTTP_CLIENT_ME(detach
, ZEND_ACC_PUBLIC
)
235 PHP_HTTP_CLIENT_ME(getProgress
, ZEND_ACC_PUBLIC
)
236 PHP_HTTP_CLIENT_ME(getTransferInfo
, ZEND_ACC_PUBLIC
)
238 PHP_HTTP_CLIENT_ME(setOptions
, ZEND_ACC_PUBLIC
)
239 PHP_HTTP_CLIENT_ME(getOptions
, ZEND_ACC_PUBLIC
)
240 PHP_HTTP_CLIENT_ME(setSslOptions
, ZEND_ACC_PUBLIC
)
241 PHP_HTTP_CLIENT_ME(getSslOptions
, ZEND_ACC_PUBLIC
)
242 PHP_HTTP_CLIENT_ME(addSslOptions
, ZEND_ACC_PUBLIC
)
244 PHP_HTTP_CLIENT_ME(addCookies
, ZEND_ACC_PUBLIC
)
245 PHP_HTTP_CLIENT_ME(getCookies
, ZEND_ACC_PUBLIC
)
246 PHP_HTTP_CLIENT_ME(setCookies
, ZEND_ACC_PUBLIC
)
248 PHP_HTTP_CLIENT_ME(enableCookies
, ZEND_ACC_PUBLIC
)
249 PHP_HTTP_CLIENT_ME(resetCookies
, ZEND_ACC_PUBLIC
)
250 PHP_HTTP_CLIENT_ME(flushCookies
, ZEND_ACC_PUBLIC
)
252 PHP_HTTP_CLIENT_ME(setRequest
, ZEND_ACC_PUBLIC
)
253 PHP_HTTP_CLIENT_ME(getRequest
, ZEND_ACC_PUBLIC
)
255 PHP_HTTP_CLIENT_ME(getResponseMessage
, ZEND_ACC_PUBLIC
)
256 PHP_HTTP_CLIENT_ME(getRequestMessage
, ZEND_ACC_PUBLIC
)
257 PHP_HTTP_CLIENT_ME(getHistory
, ZEND_ACC_PUBLIC
)
258 PHP_HTTP_CLIENT_ME(clearHistory
, ZEND_ACC_PUBLIC
)
260 PHP_HTTP_CLIENT_ME(getResponseMessageClass
, ZEND_ACC_PUBLIC
)
261 PHP_HTTP_CLIENT_ME(setResponseMessageClass
, ZEND_ACC_PUBLIC
)
263 PHP_HTTP_CLIENT_ME(request
, ZEND_ACC_PUBLIC
)
268 static zend_object_handlers php_http_client_object_handlers
;
270 zend_object_handlers
*php_http_client_get_object_handlers(void)
272 return &php_http_client_object_handlers
;
275 static php_http_client_ops_t php_http_client_user_ops
= {
284 (php_http_new_t
) php_http_client_object_new_ex
,
285 php_http_client_get_class_entry
288 zend_object_value
php_http_client_object_new(zend_class_entry
*ce TSRMLS_DC
)
290 return php_http_client_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
293 zend_object_value
php_http_client_object_new_ex(zend_class_entry
*ce
, php_http_client_t
*r
, php_http_client_object_t
**ptr TSRMLS_DC
)
295 zend_object_value ov
;
296 php_http_client_object_t
*o
;
298 o
= ecalloc(1, sizeof(*o
));
299 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
300 object_properties_init((zend_object
*) o
, ce
);
302 ov
.handle
= zend_objects_store_put(o
, NULL
, php_http_client_object_free
, NULL TSRMLS_CC
);
303 ov
.handlers
= &php_http_client_object_handlers
;
305 if (!(o
->client
= r
)) {
306 o
->client
= php_http_client_init(NULL
, &php_http_client_user_ops
, NULL
, &ov TSRMLS_CC
);
316 zend_object_value
php_http_client_object_clone(zval
*this_ptr TSRMLS_DC
)
318 zend_object_value new_ov
;
319 php_http_client_object_t
*new_obj
, *old_obj
= zend_object_store_get_object(this_ptr TSRMLS_CC
);
321 new_ov
= php_http_client_object_new_ex(old_obj
->zo
.ce
, php_http_client_copy(old_obj
->client
, NULL
), &new_obj TSRMLS_CC
);
322 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
327 void php_http_client_object_free(void *object TSRMLS_DC
)
329 php_http_client_object_t
*o
= (php_http_client_object_t
*) object
;
331 php_http_client_free(&o
->client
);
332 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
336 static inline zend_object_value
php_http_client_object_message(zval
*this_ptr
, php_http_message_t
*msg TSRMLS_DC
)
338 zend_object_value ov
;
339 zval
*zcn
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("responseMessageClass"), 0 TSRMLS_CC
);
340 zend_class_entry
*class_entry
;
343 && (class_entry
= zend_fetch_class(Z_STRVAL_P(zcn
), Z_STRLEN_P(zcn
), 0 TSRMLS_CC
))
344 && (SUCCESS
== php_http_new(&ov
, class_entry
, (php_http_new_t
) php_http_message_object_new_ex
, php_http_client_response_get_class_entry(), msg
, NULL TSRMLS_CC
))) {
347 return php_http_message_object_new_ex(php_http_client_response_get_class_entry(), msg
, NULL TSRMLS_CC
);
351 STATUS
php_http_client_object_handle_request(zval
*zclient
, zval
**zreq TSRMLS_DC
)
353 php_http_client_object_t
*obj
= zend_object_store_get_object(zclient TSRMLS_CC
);
354 php_http_client_progress_t
*progress
;
358 /* do we have a valid request? */
360 /* remember the request */
361 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("request"), *zreq TSRMLS_CC
);
363 /* maybe a request is already set */
364 *zreq
= zend_read_property(php_http_client_class_entry
, zclient
, ZEND_STRL("request"), 0 TSRMLS_CC
);
366 if (Z_TYPE_PP(zreq
) != IS_OBJECT
|| !instanceof_function(Z_OBJCE_PP(zreq
), php_http_client_request_get_class_entry() TSRMLS_CC
)) {
367 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "The client does not have a valid request set");
372 /* reset request handle */
373 php_http_client_reset(obj
->client
);
375 /* reset transfer info */
376 zend_update_property_null(php_http_client_class_entry
, zclient
, ZEND_STRL("transferInfo") TSRMLS_CC
);
379 /* set client options */
380 zend_hash_init(&options
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
381 zoptions
= zend_read_property(php_http_client_class_entry
, zclient
, ZEND_STRL("options"), 0 TSRMLS_CC
);
382 if (Z_TYPE_P(zoptions
) == IS_ARRAY
&& zend_hash_num_elements(Z_ARRVAL_P(zoptions
))) {
383 php_array_merge(&options
, Z_ARRVAL_P(zoptions
), 1 TSRMLS_CC
);
385 zoptions
= zend_read_property(php_http_client_request_get_class_entry(), *zreq
, ZEND_STRL("options"), 0 TSRMLS_CC
);
386 if (Z_TYPE_P(zoptions
) == IS_ARRAY
&& zend_hash_num_elements(Z_ARRVAL_P(zoptions
))) {
387 php_array_merge(&options
, Z_ARRVAL_P(zoptions
), 1 TSRMLS_CC
);
389 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_SETTINGS
, &options
);
390 zend_hash_destroy(&options
);
392 /* set progress callback */
393 if (SUCCESS
== php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, &progress
)) {
394 if (!progress
->callback
) {
395 php_http_client_progress_callback_t
*callback
= emalloc(sizeof(*callback
));
397 callback
->type
= PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER
;
398 MAKE_STD_ZVAL(callback
->func
.user
);
399 array_init(callback
->func
.user
);
401 add_next_index_zval(callback
->func
.user
, zclient
);
402 add_next_index_stringl(callback
->func
.user
, ZEND_STRL("notify"), 1);
404 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK
, callback
);
406 progress
->state
.info
= "start";
407 php_http_client_progress_notify(progress TSRMLS_CC
);
408 progress
->state
.started
= 1;
415 STATUS
php_http_client_object_handle_response(zval
*zclient TSRMLS_DC
)
417 php_http_client_object_t
*obj
= zend_object_store_get_object(zclient TSRMLS_CC
);
418 php_http_client_progress_t
*progress
;
419 php_http_message_t
*msg
;
422 /* always fetch info */
425 php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
, Z_ARRVAL_P(info
));
426 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("transferInfo"), info TSRMLS_CC
);
427 zval_ptr_dtor(&info
);
429 if ((msg
= obj
->client
->response
.message
)) {
430 if (i_zend_is_true(zend_read_property(php_http_client_class_entry
, zclient
, ZEND_STRL("recordHistory"), 0 TSRMLS_CC
))) {
431 zval
*new_hist
, *old_hist
= zend_read_property(php_http_client_class_entry
, zclient
, ZEND_STRL("history"), 0 TSRMLS_CC
);
432 php_http_message_t
*zipped
= php_http_message_zip(obj
->client
->response
.message
, obj
->client
->request
.message
);
433 zend_object_value ov
= php_http_client_object_message(zclient
, zipped TSRMLS_CC
);
435 MAKE_STD_ZVAL(new_hist
);
436 ZVAL_OBJVAL(new_hist
, ov
, 0);
438 if (Z_TYPE_P(old_hist
) == IS_OBJECT
) {
439 php_http_message_object_prepend(new_hist
, old_hist
, 1 TSRMLS_CC
);
442 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("history"), new_hist TSRMLS_CC
);
443 zval_ptr_dtor(&new_hist
);
446 /* update response info */
447 if (PHP_HTTP_MESSAGE_TYPE(RESPONSE
, msg
)) {
450 MAKE_STD_ZVAL(message
);
451 ZVAL_OBJVAL(message
, php_http_client_object_message(zclient
, msg TSRMLS_CC
), 0);
452 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("responseMessage"), message TSRMLS_CC
);
453 zval_ptr_dtor(&message
);
455 obj
->client
->response
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
457 zend_update_property_null(php_http_client_class_entry
, zclient
, ZEND_STRL("responseMessage") TSRMLS_CC
);
460 zend_update_property_null(php_http_client_class_entry
, zclient
, ZEND_STRL("responseMessage") TSRMLS_CC
);
463 if ((msg
= obj
->client
->request
.message
)) {
464 if (PHP_HTTP_MESSAGE_TYPE(REQUEST
, msg
)) {
467 /* update the actual request message */
468 MAKE_STD_ZVAL(message
);
469 ZVAL_OBJVAL(message
, php_http_message_object_new_ex(php_http_message_get_class_entry(), msg
, NULL TSRMLS_CC
), 0);
470 zend_update_property(php_http_client_class_entry
, zclient
, ZEND_STRL("requestMessage"), message TSRMLS_CC
);
471 zval_ptr_dtor(&message
);
472 obj
->client
->request
.message
= php_http_message_init(NULL
, 0 TSRMLS_CC
);
476 if (SUCCESS
== php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, &progress
)) {
477 progress
->state
.info
= "finished";
478 progress
->state
.finished
= 1;
479 php_http_client_progress_notify(progress TSRMLS_CC
);
481 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK
, NULL
);
486 void php_http_client_options_set(zval
*this_ptr
, zval
*opts TSRMLS_DC
)
488 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
491 zend_class_entry
*this_ce
= Z_OBJCE_P(getThis());
492 zend_bool is_client
= instanceof_function(this_ce
, php_http_client_class_entry TSRMLS_CC
);
494 MAKE_STD_ZVAL(new_opts
);
495 array_init(new_opts
);
497 if (!opts
|| !zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
498 zend_update_property(this_ce
, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC
);
499 zval_ptr_dtor(&new_opts
);
501 zval
*old_opts
, *add_opts
, **opt
;
503 MAKE_STD_ZVAL(add_opts
);
504 array_init(add_opts
);
505 /* some options need extra attention -- thus cannot use array_merge() directly */
506 FOREACH_KEYVAL(pos
, opts
, key
, opt
) {
507 if (key
.type
== HASH_KEY_IS_STRING
) {
508 #define KEYMATCH(k, s) ((sizeof(s)==k.len) && !strcasecmp(k.str, s))
509 if (Z_TYPE_PP(opt
) == IS_ARRAY
&& (KEYMATCH(key
, "ssl") || KEYMATCH(key
, "cookies"))) {
510 php_http_client_options_set_subr(getThis(), key
.str
, key
.len
, *opt
, 0 TSRMLS_CC
);
511 } else if (is_client
&& (KEYMATCH(key
, "recordHistory") || KEYMATCH(key
, "responseMessageClass"))) {
512 zend_update_property(this_ce
, getThis(), key
.str
, key
.len
-1, *opt TSRMLS_CC
);
513 } else if (Z_TYPE_PP(opt
) == IS_NULL
) {
514 old_opts
= zend_read_property(this_ce
, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC
);
515 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
516 zend_symtable_del(Z_ARRVAL_P(old_opts
), key
.str
, key
.len
);
520 add_assoc_zval_ex(add_opts
, key
.str
, key
.len
, *opt
);
525 old_opts
= zend_read_property(this_ce
, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC
);
526 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
527 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL_P(new_opts
));
529 array_join(Z_ARRVAL_P(add_opts
), Z_ARRVAL_P(new_opts
), 0, 0);
530 zend_update_property(this_ce
, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC
);
531 zval_ptr_dtor(&new_opts
);
532 zval_ptr_dtor(&add_opts
);
536 void php_http_client_options_set_subr(zval
*this_ptr
, char *key
, size_t len
, zval
*opts
, int overwrite TSRMLS_DC
)
538 if (overwrite
|| (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
)))) {
539 zend_class_entry
*this_ce
= Z_OBJCE_P(getThis());
540 zval
*old_opts
, *new_opts
, **entry
= NULL
;
542 MAKE_STD_ZVAL(new_opts
);
543 array_init(new_opts
);
544 old_opts
= zend_read_property(this_ce
, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC
);
545 if (Z_TYPE_P(old_opts
) == IS_ARRAY
) {
546 array_copy(Z_ARRVAL_P(old_opts
), Z_ARRVAL_P(new_opts
));
550 if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
552 zend_symtable_update(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &opts
, sizeof(zval
*), NULL
);
554 zend_symtable_del(Z_ARRVAL_P(new_opts
), key
, len
);
556 } else if (opts
&& zend_hash_num_elements(Z_ARRVAL_P(opts
))) {
557 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &entry
)) {
558 array_join(Z_ARRVAL_P(opts
), Z_ARRVAL_PP(entry
), 0, 0);
561 zend_symtable_update(Z_ARRVAL_P(new_opts
), key
, len
, (void *) &opts
, sizeof(zval
*), NULL
);
565 zend_update_property(this_ce
, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC
);
566 zval_ptr_dtor(&new_opts
);
570 void php_http_client_options_get_subr(zval
*this_ptr
, char *key
, size_t len
, zval
*return_value TSRMLS_DC
)
572 zend_class_entry
*this_ce
= Z_OBJCE_P(getThis());
573 zval
**options
, *opts
= zend_read_property(this_ce
, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC
);
575 if ((Z_TYPE_P(opts
) == IS_ARRAY
) && (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(opts
), key
, len
, (void *) &options
))) {
576 RETVAL_ZVAL(*options
, 1, 0);
580 PHP_METHOD(HttpClient
, __construct
)
582 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
583 zval
*os
, *opts
= NULL
;
586 object_init_ex(os
, spl_ce_SplObjectStorage
);
587 zend_update_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), os TSRMLS_CC
);
590 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
591 php_http_client_options_set(getThis(), opts TSRMLS_CC
);
594 } end_error_handling();
597 PHP_METHOD(HttpClient
, getObservers
)
599 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
600 if (SUCCESS
== zend_parse_parameters_none()) {
601 RETVAL_PROP(php_http_client_class_entry
, "observers");
603 } end_error_handling();
606 static int notify(zend_object_iterator
*iter
, void *puser TSRMLS_DC
)
608 zval
**observer
= NULL
;
610 iter
->funcs
->get_current_data(iter
, &observer TSRMLS_CC
);
614 zend_call_method_with_1_params(observer
, NULL
, NULL
, "update", &retval
, puser
);
615 zval_ptr_dtor(&retval
);
621 PHP_METHOD(HttpClient
, notify
)
623 if (SUCCESS
== zend_parse_parameters_none()) {
624 zval
*observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC
);
626 if (Z_TYPE_P(observers
) == IS_OBJECT
) {
627 Z_ADDREF_P(getThis());
628 spl_iterator_apply(observers
, notify
, getThis() TSRMLS_CC
);
629 zval_ptr_dtor(&getThis());
633 RETVAL_ZVAL(getThis(), 1, 0);
636 PHP_METHOD(HttpClient
, attach
)
640 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &observer
, spl_ce_SplObserver
)) {
641 zval
*retval
, *observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC
);
642 zend_call_method_with_1_params(&observers
, NULL
, NULL
, "attach", &retval
, observer
);
643 zval_ptr_dtor(&retval
);
646 RETVAL_ZVAL(getThis(), 1, 0);
649 PHP_METHOD(HttpClient
, detach
)
653 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &observer
, spl_ce_SplObserver
)) {
654 zval
*retval
, *observers
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC
);
655 zend_call_method_with_1_params(&observers
, NULL
, NULL
, "detach", &retval
, observer
);
656 zval_ptr_dtor(&retval
);
659 RETVAL_ZVAL(getThis(), 1, 0);
662 PHP_METHOD(HttpClient
, getProgress
)
664 if (SUCCESS
== zend_parse_parameters_none()) {
665 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
666 php_http_client_progress_t
*progress
= NULL
;
668 if (SUCCESS
== php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, &progress
)) {
669 object_init(return_value
);
670 add_property_bool(return_value
, "started", progress
->state
.started
);
671 add_property_bool(return_value
, "finished", progress
->state
.finished
);
672 add_property_string(return_value
, "info", STR_PTR(progress
->state
.info
), 1);
673 add_property_double(return_value
, "dltotal", progress
->state
.dl
.total
);
674 add_property_double(return_value
, "dlnow", progress
->state
.dl
.now
);
675 add_property_double(return_value
, "ultotal", progress
->state
.ul
.total
);
676 add_property_double(return_value
, "ulnow", progress
->state
.ul
.now
);
681 PHP_METHOD(HttpClient
, getTransferInfo
)
683 char *info_name
= NULL
;
686 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
687 zval
**infop
, *temp
= NULL
, *info
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("transferInfo"), 0 TSRMLS_CC
);
689 /* request completed? */
690 if (Z_TYPE_P(info
) != IS_ARRAY
) {
691 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
695 php_http_client_getopt(obj
->client
, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
, Z_ARRVAL_P(temp
));
699 if (info_len
&& info_name
) {
700 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(info
), php_http_pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void *) &infop
)) {
701 RETVAL_ZVAL(*infop
, 1, 0);
703 php_http_error(HE_NOTICE
, PHP_HTTP_E_INVALID_PARAM
, "Could not find transfer info named %s", info_name
);
707 RETVAL_ZVAL(info
, 1, 0);
711 zval_ptr_dtor(&temp
);
718 PHP_METHOD(HttpClient
, setOptions
)
722 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
723 php_http_client_options_set(getThis(), opts TSRMLS_CC
);
725 RETVAL_ZVAL(getThis(), 1, 0);
729 PHP_METHOD(HttpClient
, getOptions
)
731 if (SUCCESS
== zend_parse_parameters_none()) {
732 RETURN_PROP(php_http_client_class_entry
, "options");
737 PHP_METHOD(HttpClient
, setSslOptions
)
741 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
742 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts
, 1 TSRMLS_CC
);
744 RETVAL_ZVAL(getThis(), 1, 0);
748 PHP_METHOD(HttpClient
, addSslOptions
)
752 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
753 php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts
, 0 TSRMLS_CC
);
755 RETVAL_ZVAL(getThis(), 1, 0);
759 PHP_METHOD(HttpClient
, getSslOptions
)
761 if (SUCCESS
== zend_parse_parameters_none()) {
762 php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC
);
766 PHP_METHOD(HttpClient
, setCookies
)
770 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
771 php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts
, 1 TSRMLS_CC
);
773 RETVAL_ZVAL(getThis(), 1, 0);
777 PHP_METHOD(HttpClient
, addCookies
)
781 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a!/", &opts
)) {
782 php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts
, 0 TSRMLS_CC
);
784 RETVAL_ZVAL(getThis(), 1, 0);
788 PHP_METHOD(HttpClient
, getCookies
)
790 if (SUCCESS
== zend_parse_parameters_none()) {
791 php_http_client_options_get_subr(getThis(), ZEND_STRS("cookies"), return_value TSRMLS_CC
);
795 PHP_METHOD(HttpClient
, enableCookies
)
797 if (SUCCESS
== zend_parse_parameters_none()){
798 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
800 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_COOKIES_ENABLE
, NULL
);
802 RETVAL_ZVAL(getThis(), 1, 0);
805 PHP_METHOD(HttpClient
, resetCookies
)
807 zend_bool session_only
= 0;
809 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &session_only
)) {
810 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
812 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_COOKIES_RESET_SESSION
, NULL
);
814 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_COOKIES_RESET
, NULL
);
817 RETVAL_ZVAL(getThis(), 1, 0);
820 PHP_METHOD(HttpClient
, flushCookies
)
822 if (SUCCESS
== zend_parse_parameters_none()) {
823 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
825 php_http_client_setopt(obj
->client
, PHP_HTTP_CLIENT_OPT_COOKIES_FLUSH
, NULL
);
827 RETVAL_ZVAL(getThis(), 1, 0);
830 PHP_METHOD(HttpClient
, getResponseMessage
)
832 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
833 if (SUCCESS
== zend_parse_parameters_none()) {
834 zval
*message
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC
);
836 if (Z_TYPE_P(message
) == IS_OBJECT
) {
837 RETVAL_OBJECT(message
, 1);
839 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpClient does not contain a response message");
842 } end_error_handling();
845 PHP_METHOD(HttpClient
, getRequestMessage
)
847 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
848 if (SUCCESS
== zend_parse_parameters_none()) {
849 zval
*message
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("requestMessage"), 0 TSRMLS_CC
);
851 if (Z_TYPE_P(message
) == IS_OBJECT
) {
852 RETVAL_OBJECT(message
, 1);
854 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "HttpClient does not contain a request message");
857 } end_error_handling();
860 PHP_METHOD(HttpClient
, getHistory
)
862 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
863 if (SUCCESS
== zend_parse_parameters_none()) {
864 zval
*hist
= zend_read_property(php_http_client_class_entry
, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC
);
866 if (Z_TYPE_P(hist
) == IS_OBJECT
) {
867 RETVAL_OBJECT(hist
, 1);
869 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "The history is empty");
872 } end_error_handling();
875 PHP_METHOD(HttpClient
, clearHistory
)
877 if (SUCCESS
== zend_parse_parameters_none()) {
878 zend_update_property_null(php_http_client_class_entry
, getThis(), ZEND_STRL("history") TSRMLS_CC
);
880 RETVAL_ZVAL(getThis(), 1, 0);
883 PHP_METHOD(HttpClient
, getResponseMessageClass
)
885 if (SUCCESS
== zend_parse_parameters_none()) {
886 RETURN_PROP(php_http_client_class_entry
, "responseMessageClass");
891 PHP_METHOD(HttpClient
, setResponseMessageClass
)
896 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &cn
, &cl
)) {
897 zend_update_property_stringl(php_http_client_class_entry
, getThis(), ZEND_STRL("responseMessageClass"), cn
, cl TSRMLS_CC
);
899 RETVAL_ZVAL(getThis(), 1, 0);
902 PHP_METHOD(HttpClient
, setRequest
)
906 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zreq
, php_http_client_request_get_class_entry())) {
907 zend_update_property(php_http_client_class_entry
, getThis(), ZEND_STRL("request"), zreq TSRMLS_CC
);
909 RETURN_ZVAL(getThis(), 1, 0);
912 PHP_METHOD(HttpClient
, getRequest
)
914 if (SUCCESS
== zend_parse_parameters_none()) {
915 RETURN_PROP(php_http_client_class_entry
, "request");
919 PHP_METHOD(HttpClient
, request
)
921 char *meth_str
, *url_str
;
922 int meth_len
, url_len
;
923 zval
*zheader
, *zbody
, *zoptions
;
925 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
926 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a!z!a!/", &meth_str
, &meth_len
, &url_str
, &url_len
, &zheader
, &zbody
, &zoptions
)) {
927 php_http_message_object_t
*msg_obj
;
928 zend_object_value ov
;
929 zval
*req
, *res
= NULL
;
931 php_http_new(&ov
, php_http_client_request_get_class_entry(), (php_http_new_t
) php_http_message_object_new_ex
, NULL
, NULL
, (void *) &msg_obj TSRMLS_CC
);
933 ZVAL_OBJVAL(req
, ov
, 0);
935 msg_obj
->message
= php_http_message_init(NULL
, PHP_HTTP_REQUEST TSRMLS_CC
);
936 PHP_HTTP_INFO(msg_obj
->message
).request
.url
= estrndup(url_str
, url_len
);
937 PHP_HTTP_INFO(msg_obj
->message
).request
.method
= estrndup(meth_str
, meth_len
);
940 array_copy(Z_ARRVAL_P(zheader
), &msg_obj
->message
->hdrs
);
944 php_http_message_object_set_body(msg_obj
, zbody TSRMLS_CC
);
948 php_http_client_options_set(req
, zoptions TSRMLS_CC
);
951 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL
, "send", &res
, req
);
952 RETVAL_ZVAL(res
, 0, 1);
955 } end_error_handling();
958 PHP_METHOD(HttpClient
, send
)
964 with_error_handling(EH_THROW
, php_http_exception_get_class_entry()) {
965 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|O!", &zreq
, php_http_client_request_get_class_entry())) {
966 if (SUCCESS
== php_http_client_object_handle_request(getThis(), &zreq TSRMLS_CC
)) {
967 php_http_client_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
968 php_http_message_object_t
*req
= zend_object_store_get_object(zreq TSRMLS_CC
);
970 php_http_client_exec(obj
->client
, req
->message
);
972 if (SUCCESS
== php_http_client_object_handle_response(getThis() TSRMLS_CC
)) {
973 RETVAL_PROP(php_http_client_class_entry
, "responseMessage");
975 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Failed to handle response");
978 php_http_error(HE_WARNING
, PHP_HTTP_E_CLIENT
, "Failed to handle request");
981 } end_error_handling();
984 PHP_MINIT_FUNCTION(http_client
)
986 PHP_HTTP_REGISTER_CLASS(http
\\Client
, AbstractClient
, http_client
, php_http_object_get_class_entry(), ZEND_ACC_EXPLICIT_ABSTRACT_CLASS
);
987 php_http_client_class_entry
->create_object
= php_http_client_object_new
;
988 memcpy(&php_http_client_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
989 php_http_client_object_handlers
.clone_obj
= php_http_client_object_clone
;
991 zend_class_implements(php_http_client_class_entry TSRMLS_CC
, 2, spl_ce_SplSubject
, php_http_client_interface_get_class_entry());
993 zend_declare_property_string(php_http_client_class_entry
, ZEND_STRL("responseMessageClass"), "", ZEND_ACC_PRIVATE TSRMLS_CC
);
994 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("observers"), ZEND_ACC_PRIVATE TSRMLS_CC
);
995 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("transferInfo"), ZEND_ACC_PRIVATE TSRMLS_CC
);
996 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("responseMessage"), ZEND_ACC_PRIVATE TSRMLS_CC
);
997 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("requestMessage"), ZEND_ACC_PRIVATE TSRMLS_CC
);
998 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("history"), ZEND_ACC_PRIVATE TSRMLS_CC
);
1000 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1001 zend_declare_property_null(php_http_client_class_entry
, ZEND_STRL("request"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1003 zend_declare_property_bool(php_http_client_class_entry
, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
1013 * vim600: noet sw=4 ts=4 fdm=marker
1014 * vim<600: noet sw=4 ts=4