From: Michael Wallner Date: Mon, 26 Mar 2012 17:00:58 +0000 (+0000) Subject: add current state of refactoring X-Git-Tag: RELEASE_2_1_0_RC3~10^2^2~145^2~12 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=6cc24086f57aa1e8f4d6a2c12361635e7c24f60f;hp=880540695ec6784e5570a15533467f7dff98d4f1 add current state of refactoring --- diff --git a/config9.m4 b/config9.m4 index be7bdf9..c318e16 100644 --- a/config9.m4 +++ b/config9.m4 @@ -416,11 +416,16 @@ dnl ---- php_http_persistent_handle.c \ php_http_property_proxy.c \ php_http_querystring.c \ - php_http_request.c \ - php_http_request_factory.c \ php_http_curl.c \ - php_http_request_pool.c \ - php_http_request_datashare.c \ + php_http_client_interface.c \ + php_http_client.c \ + php_http_client_pool.c \ + php_http_client_datashare.c \ + php_http_client_curl.c \ + php_http_client_pool_curl.c \ + php_http_client_datashare_curl.c \ + php_http_client_request.c \ + php_http_client_response.c \ php_http_strlist.c \ php_http_url.c \ php_http_version.c \ diff --git a/php_http.c b/php_http.c index 202317c..f4b7394 100644 --- a/php_http.c +++ b/php_http.c @@ -140,13 +140,18 @@ PHP_MINIT_FUNCTION(http) || SUCCESS != PHP_MINIT_CALL(http_message_body) || SUCCESS != PHP_MINIT_CALL(http_property_proxy) || SUCCESS != PHP_MINIT_CALL(http_querystring) - || SUCCESS != PHP_MINIT_CALL(http_request_factory) - || SUCCESS != PHP_MINIT_CALL(http_request) + || SUCCESS != PHP_MINIT_CALL(http_client_interface) + || SUCCESS != PHP_MINIT_CALL(http_client) + || SUCCESS != PHP_MINIT_CALL(http_client_request) + || SUCCESS != PHP_MINIT_CALL(http_client_response) + || SUCCESS != PHP_MINIT_CALL(http_client_datashare) + || SUCCESS != PHP_MINIT_CALL(http_client_pool) #if PHP_HTTP_HAVE_CURL || SUCCESS != PHP_MINIT_CALL(http_curl) #endif - || SUCCESS != PHP_MINIT_CALL(http_request_datashare) - || SUCCESS != PHP_MINIT_CALL(http_request_pool) + || SUCCESS != PHP_MINIT_CALL(http_client_curl) + || SUCCESS != PHP_MINIT_CALL(http_client_pool_curl) + || SUCCESS != PHP_MINIT_CALL(http_client_datashare_curl) || SUCCESS != PHP_MINIT_CALL(http_url) || SUCCESS != PHP_MINIT_CALL(http_env) || SUCCESS != PHP_MINIT_CALL(http_env_request) @@ -170,7 +175,6 @@ PHP_MSHUTDOWN_FUNCTION(http) #if PHP_HTTP_HAVE_CURL || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl) #endif - || SUCCESS != PHP_MSHUTDOWN_CALL(http_request_factory) || SUCCESS != PHP_MSHUTDOWN_CALL(http_persistent_handle) ) { return FAILURE; @@ -183,8 +187,8 @@ PHP_RINIT_FUNCTION(http) { if (0 || SUCCESS != PHP_RINIT_CALL(http_env) -#if PHP_HTTP_HAVE_CURL - || SUCCESS != PHP_RINIT_CALL(http_curl) +#if PHP_HTTP_HAVE_CURL && PHP_HTTP_HAVE_EVENT + || SUCCESS != PHP_RINIT_CALL(http_client_pool_curl) #endif ) { return FAILURE; diff --git a/php_http_api.h b/php_http_api.h index 23e63cd..bc3a2f5 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -95,11 +95,15 @@ typedef int STATUS; #include "php_http_persistent_handle.h" #include "php_http_property_proxy.h" #include "php_http_querystring.h" -#include "php_http_request.h" -#include "php_http_request_datashare.h" -#include "php_http_request_factory.h" -#include "php_http_request_pool.h" -#include "php_http_serf.h" +#include "php_http_client_interface.h" +#include "php_http_client.h" +#include "php_http_client_request.h" +#include "php_http_client_response.h" +#include "php_http_client_curl.h" +#include "php_http_client_pool.h" +#include "php_http_client_pool_curl.h" +#include "php_http_client_datashare.h" +#include "php_http_client_datashare_curl.h" #include "php_http_url.h" #include "php_http_version.h" diff --git a/php_http_client.c b/php_http_client.c new file mode 100644 index 0000000..5d77693 --- /dev/null +++ b/php_http_client.c @@ -0,0 +1,1057 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" + +#include + +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) +{ + php_http_client_t *free_h = NULL; + + if (!h) { + free_h = h = emalloc(sizeof(*h)); + } + memset(h, 0, sizeof(*h)); + + h->ops = ops; + h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, h, NULL); + h->buffer = php_http_buffer_init(NULL); + h->parser = php_http_message_parser_init(NULL TSRMLS_CC); + h->message = php_http_message_init(NULL, 0 TSRMLS_CC); + + TSRMLS_SET_CTX(h->ts); + + if (h->ops->init) { + if (!(h = h->ops->init(h, init_arg))) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Could not initialize request"); + if (free_h) { + h->ops->dtor = NULL; + php_http_client_free(&free_h); + } + } + } + + return h; +} + +PHP_HTTP_API void php_http_client_dtor(php_http_client_t *h) +{ + if (h->ops->dtor) { + h->ops->dtor(h); + } + + php_http_resource_factory_free(&h->rf); + + php_http_message_parser_free(&h->parser); + php_http_message_free(&h->message); + php_http_buffer_free(&h->buffer); +} + +PHP_HTTP_API void php_http_client_free(php_http_client_t **h) +{ + if (*h) { + php_http_client_dtor(*h); + efree(*h); + *h = NULL; + } +} + +PHP_HTTP_API php_http_client_t *php_http_client_copy(php_http_client_t *from, php_http_client_t *to) +{ + if (!from->ops->copy) { + return NULL; + } else { + TSRMLS_FETCH_FROM_CTX(from->ts); + + if (!to) { + to = ecalloc(1, sizeof(*to)); + } + + to->ops = from->ops; + if (from->rf) { + php_http_resource_factory_addref(from->rf); + to->rf = from->rf; + } else { + to->rf = php_http_resource_factory_init(NULL, to->ops->rsrc, to, NULL); + } + to->buffer = php_http_buffer_init(NULL); + to->parser = php_http_message_parser_init(NULL TSRMLS_CC); + to->message = php_http_message_init(NULL, 0 TSRMLS_CC); + + TSRMLS_SET_CTX(to->ts); + + return to->ops->copy(from, to); + } +} + +PHP_HTTP_API STATUS php_http_client_exec(php_http_client_t *h, php_http_message_t *msg) +{ + if (h->ops->exec) { + return h->ops->exec(h, msg); + } + return FAILURE; +} + +PHP_HTTP_API STATUS php_http_client_reset(php_http_client_t *h) +{ + if (h->ops->reset) { + return h->ops->reset(h); + } + return FAILURE; +} + +PHP_HTTP_API STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg) +{ + if (h->ops->setopt) { + return h->ops->setopt(h, opt, arg); + } + return FAILURE; +} + +PHP_HTTP_API STATUS php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg) +{ + if (h->ops->getopt) { + return h->ops->getopt(h, opt, arg); + } + return FAILURE; +} + +#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClient, method, 0, req_args) +#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClient, method, 0) +#define PHP_HTTP_CLIENT_ME(method, visibility) PHP_ME(HttpClient, method, PHP_HTTP_ARGS(HttpClient, method), visibility) +#define PHP_HTTP_CLIENT_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClient, method)) +#define PHP_HTTP_CLIENT_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClient_##al), PHP_HTTP_ARGS(HttpClient, al), vis) + +PHP_HTTP_EMPTY_ARGS(getOptions); +PHP_HTTP_BEGIN_ARGS(setOptions, 0) + PHP_HTTP_ARG_VAL(options, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(getSslOptions); +PHP_HTTP_BEGIN_ARGS(setSslOptions, 0) + PHP_HTTP_ARG_VAL(ssl_options, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_BEGIN_ARGS(addSslOptions, 0) + PHP_HTTP_ARG_VAL(ssl_optins, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(getHeaders); +PHP_HTTP_BEGIN_ARGS(setHeaders, 0) + PHP_HTTP_ARG_VAL(headers, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_BEGIN_ARGS(addHeaders, 1) + PHP_HTTP_ARG_VAL(headers, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(getCookies); +PHP_HTTP_BEGIN_ARGS(setCookies, 0) + PHP_HTTP_ARG_VAL(cookies, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_BEGIN_ARGS(addCookies, 1) + PHP_HTTP_ARG_VAL(cookies, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(enableCookies); +PHP_HTTP_BEGIN_ARGS(resetCookies, 0) + PHP_HTTP_ARG_VAL(session_only, 0) +PHP_HTTP_END_ARGS; +PHP_HTTP_EMPTY_ARGS(flushCookies); + +PHP_HTTP_EMPTY_ARGS(getMessageClass); +PHP_HTTP_BEGIN_ARGS(setMessageClass, 1) + PHP_HTTP_ARG_VAL(message_class_name, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(getResponseMessage); +PHP_HTTP_EMPTY_ARGS(getRequestMessage); +PHP_HTTP_EMPTY_ARGS(getHistory); +PHP_HTTP_EMPTY_ARGS(clearHistory); +PHP_HTTP_BEGIN_ARGS(send, 1) + PHP_HTTP_ARG_VAL(request, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(getObservers); +PHP_HTTP_BEGIN_ARGS(attach, 1) + PHP_HTTP_ARG_OBJ(SplObserver, observer, 0) +PHP_HTTP_END_ARGS; +PHP_HTTP_BEGIN_ARGS(detach, 1) + PHP_HTTP_ARG_OBJ(SplObserver, observer, 0) +PHP_HTTP_END_ARGS; +PHP_HTTP_EMPTY_ARGS(notify); +PHP_HTTP_EMPTY_ARGS(getProgress); +PHP_HTTP_BEGIN_ARGS(getTransferInfo, 0) + PHP_HTTP_ARG_VAL(name, 0) +PHP_HTTP_END_ARGS; + + +zend_class_entry *php_http_client_class_entry; +zend_function_entry php_http_client_method_entry[] = { + PHP_HTTP_CLIENT_ME(getObservers, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(notify, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(attach, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(detach, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(getProgress, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(getTransferInfo, ZEND_ACC_PUBLIC) + + PHP_HTTP_CLIENT_ME(setOptions, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(getOptions, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(setSslOptions, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(getSslOptions, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(addSslOptions, ZEND_ACC_PUBLIC) + + PHP_HTTP_CLIENT_ME(addCookies, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(getCookies, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(setCookies, ZEND_ACC_PUBLIC) + + PHP_HTTP_CLIENT_ME(enableCookies, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(resetCookies, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(flushCookies, ZEND_ACC_PUBLIC) + + PHP_HTTP_CLIENT_ME(send, ZEND_ACC_PUBLIC) + + PHP_HTTP_CLIENT_ME(getResponseMessage, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(getRequestMessage, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(getHistory, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(clearHistory, ZEND_ACC_PUBLIC) + + PHP_HTTP_CLIENT_ME(getMessageClass, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_ME(setMessageClass, ZEND_ACC_PUBLIC) + + EMPTY_FUNCTION_ENTRY +}; + +static zend_object_handlers php_http_client_object_handlers; + +zend_object_handlers *php_http_client_get_object_handlers(void) +{ + return &php_http_client_object_handlers; +} + +zend_object_value php_http_client_object_new(zend_class_entry *ce TSRMLS_DC) +{ + return php_http_client_object_new_ex(ce, NULL, NULL TSRMLS_CC); +} + +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) +{ + zend_object_value ov; + php_http_client_object_t *o; + + o = ecalloc(1, sizeof(php_http_client_object_t)); + zend_object_std_init((zend_object *) o, ce TSRMLS_CC); + object_properties_init((zend_object *) o, ce); + + if (!(o->client = r)) { + o->client = php_http_client_init(NULL, NULL, NULL, NULL TSRMLS_CC); + } + + if (ptr) { + *ptr = o; + } + + ov.handle = zend_objects_store_put(o, NULL, php_http_client_object_free, NULL TSRMLS_CC); + ov.handlers = &php_http_client_object_handlers; + + return ov; +} + +zend_object_value php_http_client_object_clone(zval *this_ptr TSRMLS_DC) +{ + zend_object_value new_ov; + php_http_client_object_t *new_obj, *old_obj = zend_object_store_get_object(this_ptr TSRMLS_CC); + + new_ov = php_http_client_object_new_ex(old_obj->zo.ce, php_http_client_copy(old_obj->client, NULL), &new_obj TSRMLS_CC); + zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC); + + return new_ov; +} + +void php_http_client_object_free(void *object TSRMLS_DC) +{ + php_http_client_object_t *o = (php_http_client_object_t *) object; + + php_http_client_free(&o->client); + zend_object_std_dtor((zend_object *) o TSRMLS_CC); + efree(o); +} + +static inline void php_http_client_object_check_request_content_type(zval *this_ptr TSRMLS_DC) +{ + zval *ctype = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("contentType"), 0 TSRMLS_CC); + + if (Z_STRLEN_P(ctype)) { + zval **headers, *opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + + if ( (Z_TYPE_P(opts) == IS_ARRAY) && + (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), ZEND_STRS("headers"), (void *) &headers)) && + (Z_TYPE_PP(headers) == IS_ARRAY)) { + zval **ct_header; + + /* only override if not already set */ + if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), ZEND_STRS("Content-Type"), (void *) &ct_header))) { + add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + } else + /* or not a string, zero length string or a string of spaces */ + if ((Z_TYPE_PP(ct_header) != IS_STRING) || !Z_STRLEN_PP(ct_header)) { + add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + } else { + int i, only_space = 1; + + /* check for spaces only */ + for (i = 0; i < Z_STRLEN_PP(ct_header); ++i) { + if (!PHP_HTTP_IS_CTYPE(space, Z_STRVAL_PP(ct_header)[i])) { + only_space = 0; + break; + } + } + if (only_space) { + add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + } + } + } else { + zval *headers; + + MAKE_STD_ZVAL(headers); + array_init(headers); + add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers); + zval_ptr_dtor(&headers); + } + } +} + +static inline zend_object_value php_http_client_object_message(zval *this_ptr, php_http_message_t *msg TSRMLS_DC) +{ + zend_object_value ov; + zval *zcn = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("messageClass"), 0 TSRMLS_CC); + zend_class_entry *class_entry; + + if (Z_STRLEN_P(zcn) + && (class_entry = zend_fetch_class(Z_STRVAL_P(zcn), Z_STRLEN_P(zcn), 0 TSRMLS_CC)) + && (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_message_object_new_ex, php_http_client_response_class_entry, msg, NULL TSRMLS_CC))) { + return ov; + } else { + return php_http_message_object_new_ex(php_http_client_response_class_entry, msg, NULL TSRMLS_CC); + } +} + +STATUS php_http_client_object_handle_request(zval *zclient, zval *zreq TSRMLS_DC) +{ + php_http_client_object_t *obj = zend_object_store_get_object(zclient TSRMLS_CC); + php_http_client_progress_t *progress; + zval *zoptions; + + /* reset request handle */ + php_http_client_reset(obj->client); + + /* reset transfer info */ + zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("info") TSRMLS_CC); + + /* set request options */ + zoptions = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("options"), 0 TSRMLS_CC); + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, Z_ARRVAL_P(zoptions)); + + /* set progress callback */ + if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) { + if (!progress->callback) { + php_http_client_progress_callback_t *callback = emalloc(sizeof(*callback)); + + callback->type = PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER; + callback->pass_state = 0; + MAKE_STD_ZVAL(callback->func.user); + array_init(callback->func.user); + Z_ADDREF_P(zclient); + add_next_index_zval(callback->func.user, zclient); + add_next_index_stringl(callback->func.user, ZEND_STRL("notify"), 1); + + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK, callback); + } + progress->state.info = "start"; + php_http_client_progress_notify(progress TSRMLS_CC); + progress->state.started = 1; + } + + return SUCCESS; +} + +STATUS php_http_client_object_requesthandler(php_http_client_object_t *obj, zval *this_ptr, char **meth, char **url, php_http_message_body_t **body TSRMLS_DC) +{ + zval *zoptions; + php_http_client_progress_t *progress; + + /* reset request handle */ + php_http_client_reset(obj->client); + /* reset transfer info */ + zend_update_property_null(php_http_client_class_entry, getThis(), ZEND_STRL("info") TSRMLS_CC); + + if (meth) { + *meth = Z_STRVAL_P(zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("method"), 0 TSRMLS_CC)); + } + + if (url) { + php_url *tmp, qdu = {NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL}; + zval *zurl = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("url"), 0 TSRMLS_CC); + zval *zqdata = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("queryData"), 0 TSRMLS_CC); + + if (Z_STRLEN_P(zqdata)) { + qdu.query = Z_STRVAL_P(zqdata); + } + php_http_url(0, tmp = php_url_parse(Z_STRVAL_P(zurl)), &qdu, NULL, url, NULL TSRMLS_CC); + php_url_free(tmp); + } + + if (body) { + zval *zbody = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("requestBody"), 0 TSRMLS_CC); + + if (Z_TYPE_P(zbody) == IS_OBJECT) { + *body = ((php_http_message_body_object_t *)zend_object_store_get_object(zbody TSRMLS_CC))->body; + if (*body) { + php_stream_rewind(php_http_message_body_stream(*body)); + } + } + } + + php_http_client_object_check_request_content_type(getThis() TSRMLS_CC); + zoptions = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, Z_ARRVAL_P(zoptions)); + + if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) { + if (!progress->callback) { + php_http_client_progress_callback_t *callback = emalloc(sizeof(*callback)); + + callback->type = PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER; + callback->pass_state = 0; + MAKE_STD_ZVAL(callback->func.user); + array_init(callback->func.user); + Z_ADDREF_P(getThis()); + add_next_index_zval(callback->func.user, getThis()); + add_next_index_stringl(callback->func.user, ZEND_STRL("notify"), 1); + + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK, callback); + } + progress->state.info = "start"; + php_http_client_progress_notify(progress TSRMLS_CC); + progress->state.started = 1; + } + return SUCCESS; +} + +static inline void empty_response(zval *this_ptr TSRMLS_DC) +{ + zend_update_property_null(php_http_client_class_entry, getThis(), ZEND_STRL("responseMessage") TSRMLS_CC); +} + +STATUS php_http_client_object_handle_response(zval *zclient TSRMLS_DC) +{ + php_http_client_object_t *obj = zend_object_store_get_object(zclient TSRMLS_CC); + php_http_client_progress_t *progress; + php_http_message_t *msg; + zval *info; + + /* always fetch info */ + MAKE_STD_ZVAL(info); + array_init(info); + php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, Z_ARRVAL_P(info)); + zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("transferInfo"), info TSRMLS_CC); + zval_ptr_dtor(&info); + + if ((msg = obj->client->message)) { + /* update history */ + if (i_zend_is_true(zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) { + zval *new_hist, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0 TSRMLS_CC); + zend_object_value ov = php_http_client_object_message(zclient, php_http_message_copy(msg, NULL) TSRMLS_CC); + + MAKE_STD_ZVAL(new_hist); + ZVAL_OBJVAL(new_hist, ov, 0); + + if (Z_TYPE_P(old_hist) == IS_OBJECT) { + php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC); + } + + zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), new_hist TSRMLS_CC); + zval_ptr_dtor(&new_hist); + } + + /* update response info */ + if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, msg)) { + zval *message; + + MAKE_STD_ZVAL(message); + ZVAL_OBJVAL(message, php_http_client_object_message(zclient, msg TSRMLS_CC), 0); + zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("responseMessage"), message TSRMLS_CC); + zval_ptr_dtor(&message); + + obj->client->message = php_http_message_init(NULL, 0 TSRMLS_CC); + msg = msg->parent; + } else { + zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("responseMessage") TSRMLS_CC); + } + } else { + zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("responseMessage") TSRMLS_CC); + } + + /* there might be a 100-Continue response in between */ + while (msg && !PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { + msg = msg->parent; + } + + if (PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { + zval *message; + + /* update the actual request message */ + MAKE_STD_ZVAL(message); + ZVAL_OBJVAL(message, php_http_client_object_message(zclient, php_http_message_copy_ex(msg, NULL, 0) TSRMLS_CC), 0); + zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("requestMessage"), message TSRMLS_CC); + zval_ptr_dtor(&message); + } + + if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) { + progress->state.info = "finished"; + progress->state.finished = 1; + php_http_client_progress_notify(progress TSRMLS_CC); + } + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK, NULL); + + return SUCCESS; +} + +STATUS php_http_client_object_responsehandler(php_http_client_object_t *obj, zval *this_ptr TSRMLS_DC) +{ + STATUS ret = SUCCESS; + zval *info; + php_http_message_t *msg; + php_http_client_progress_t *progress; + + /* always fetch info */ + MAKE_STD_ZVAL(info); + array_init(info); + php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, Z_ARRVAL_P(info)); + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("transferInfo"), info TSRMLS_CC); + zval_ptr_dtor(&info); + + if ((msg = obj->client->message)) { + /* update history */ + if (i_zend_is_true(zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) { + zval *new_hist, *old_hist = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC); + zend_object_value ov = php_http_client_object_message(getThis(), php_http_message_copy(msg, NULL) TSRMLS_CC); + + MAKE_STD_ZVAL(new_hist); + ZVAL_OBJVAL(new_hist, ov, 0); + + if (Z_TYPE_P(old_hist) == IS_OBJECT) { + php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC); + } + + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), new_hist TSRMLS_CC); + zval_ptr_dtor(&new_hist); + } + + /* update response info */ + if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, msg)) { + zval *message; + + MAKE_STD_ZVAL(message); + ZVAL_OBJVAL(message, php_http_client_object_message(getThis(), msg TSRMLS_CC), 0); + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("responseMessage"), message TSRMLS_CC); + zval_ptr_dtor(&message); + + obj->client->message = php_http_message_init(NULL, 0 TSRMLS_CC); + msg = msg->parent; + } else { + empty_response(getThis() TSRMLS_CC); + } + } else { + /* update properties with empty values */ + empty_response(getThis() TSRMLS_CC); + } + + while (msg && !PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { + msg = msg->parent; + } + if (PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { + zval *message; + + MAKE_STD_ZVAL(message); + ZVAL_OBJVAL(message, php_http_client_object_message(getThis(), php_http_message_copy_ex(msg, NULL, 0) TSRMLS_CC), 0); + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("requestMessage"), message TSRMLS_CC); + zval_ptr_dtor(&message); + } + + if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) { + progress->state.info = "finished"; + progress->state.finished = 1; + php_http_client_progress_notify(progress TSRMLS_CC); + } + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK, NULL); + + return ret; +} + +static int apply_pretty_key(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) +{ + zval **zpp = pDest, *arr = va_arg(args, zval *); + + if (hash_key->arKey && hash_key->nKeyLength > 1) { + char *tmp = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 0); + + Z_ADDREF_PP(zpp); + add_assoc_zval_ex(arr, tmp, hash_key->nKeyLength, *zpp); + efree(tmp); + } + return ZEND_HASH_APPLY_KEEP; +} + +static inline void php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys) +{ + zval *old_opts, *new_opts, *opts = NULL, **entry = NULL; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) { + MAKE_STD_ZVAL(new_opts); + array_init(new_opts); + old_opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + if (Z_TYPE_P(old_opts) == IS_ARRAY) { + array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts)); + } + + if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) { + if (overwrite) { + zend_hash_clean(Z_ARRVAL_PP(entry)); + } + if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) { + if (overwrite) { + array_copy(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry)); + } else { + array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, prettify_keys ? ARRAY_JOIN_PRETTIFY : 0); + } + } + } else if (opts) { + if (prettify_keys) { + zval *tmp; + + MAKE_STD_ZVAL(tmp); + array_init_size(tmp, zend_hash_num_elements(Z_ARRVAL_P(opts))); + zend_hash_apply_with_arguments(Z_ARRVAL_P(opts) TSRMLS_CC, apply_pretty_key, 1, tmp); + opts = tmp; + } else { + Z_ADDREF_P(opts); + } + add_assoc_zval_ex(new_opts, key, len, opts); + } + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC); + zval_ptr_dtor(&new_opts); + } + + RETVAL_ZVAL(getThis(), 1, 0); +} + +static inline void php_http_client_object_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len) +{ + if (SUCCESS == zend_parse_parameters_none()) { + zval *opts, **options; + + opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + array_init(return_value); + + if ( (Z_TYPE_P(opts) == IS_ARRAY) && + (SUCCESS == zend_symtable_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) { + convert_to_array(*options); + array_copy(Z_ARRVAL_PP(options), Z_ARRVAL_P(return_value)); + } + } +} + + +PHP_METHOD(HttpClient, getObservers) +{ + with_error_handling(EH_THROW, php_http_exception_class_entry) { + if (SUCCESS == zend_parse_parameters_none()) { + RETVAL_PROP(php_http_client_class_entry, "observers"); + } + } end_error_handling(); +} + +static int notify(zend_object_iterator *iter, void *puser TSRMLS_DC) +{ + zval **observer = NULL; + + iter->funcs->get_current_data(iter, &observer TSRMLS_CC); + if (observer) { + zval *retval; + + zend_call_method_with_1_params(observer, NULL, NULL, "update", &retval, puser); + zval_ptr_dtor(&retval); + return SUCCESS; + } + return FAILURE; +} + +PHP_METHOD(HttpClient, notify) +{ + if (SUCCESS == zend_parse_parameters_none()) { + zval *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC); + + if (Z_TYPE_P(observers) == IS_OBJECT) { + Z_ADDREF_P(getThis()); + spl_iterator_apply(observers, notify, getThis() TSRMLS_CC); + zval_ptr_dtor(&getThis()); + } + } + + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClient, attach) +{ + zval *observer; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) { + zval *retval, *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC); + zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer); + zval_ptr_dtor(&retval); + } + + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClient, detach) +{ + zval *observer; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) { + zval *retval, *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC); + zend_call_method_with_1_params(&observers, NULL, NULL, "detach", &retval, observer); + zval_ptr_dtor(&retval); + } + + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClient, getProgress) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_client_progress_t *progress; + + php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress); + object_init(return_value); + add_property_bool(return_value, "started", progress->state.started); + add_property_bool(return_value, "finished", progress->state.finished); + add_property_string(return_value, "info", STR_PTR(progress->state.info), 1); + add_property_double(return_value, "dltotal", progress->state.dl.total); + add_property_double(return_value, "dlnow", progress->state.dl.now); + add_property_double(return_value, "ultotal", progress->state.ul.total); + add_property_double(return_value, "ulnow", progress->state.ul.now); + } +} + +PHP_METHOD(HttpClient, getTransferInfo) +{ + char *info_name = NULL; + int info_len = 0; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) { + zval **infop, *temp = NULL, *info = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("transferInfo"), 0 TSRMLS_CC); + + /* request completed? */ + if (Z_TYPE_P(info) != IS_ARRAY) { + php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + MAKE_STD_ZVAL(temp); + array_init(temp); + php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, Z_ARRVAL_P(temp)); + info = temp; + } + + if (info_len && info_name) { + if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(info), php_http_pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) { + RETVAL_ZVAL(*infop, 1, 0); + } else { + php_http_error(HE_NOTICE, PHP_HTTP_E_INVALID_PARAM, "Could not find transfer info named %s", info_name); + RETVAL_FALSE; + } + } else { + RETVAL_ZVAL(info, 1, 0); + } + + if (temp) { + zval_ptr_dtor(&temp); + } + return; + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClient, setOptions) +{ + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + HashPosition pos; + zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) { + RETURN_FALSE; + } + + MAKE_STD_ZVAL(new_opts); + array_init(new_opts); + + if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) { + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC); + zval_ptr_dtor(&new_opts); + } else { + MAKE_STD_ZVAL(add_opts); + array_init(add_opts); + /* some options need extra attention -- thus cannot use array_merge() directly */ + FOREACH_KEYVAL(pos, opts, key, opt) { + if (key.type == HASH_KEY_IS_STRING) { +#define KEYMATCH(k, s) ((sizeof(s)==k.len) && !strcasecmp(k.str, s)) + if (KEYMATCH(key, "ssl")) { + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt); + } else if (KEYMATCH(key, "cookies")) { + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt); + } else if (KEYMATCH(key, "recordHistory")) { + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("recordHistory"), *opt TSRMLS_CC); + } else if (KEYMATCH(key, "messageClass")) { + zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmessageclass", NULL, *opt); + } else if (Z_TYPE_PP(opt) == IS_NULL) { + old_opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + if (Z_TYPE_P(old_opts) == IS_ARRAY) { + zend_symtable_del(Z_ARRVAL_P(old_opts), key.str, key.len); + } + } else { + Z_ADDREF_P(*opt); + add_assoc_zval_ex(add_opts, key.str, key.len, *opt); + } + } + } + + old_opts = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + if (Z_TYPE_P(old_opts) == IS_ARRAY) { + array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts)); + } + array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0); + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC); + zval_ptr_dtor(&new_opts); + zval_ptr_dtor(&add_opts); + } + + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClient, getOptions) +{ + if (SUCCESS == zend_parse_parameters_none()) { + RETURN_PROP(php_http_client_class_entry, "options"); + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClient, setSslOptions) +{ + php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("ssl"), 1, 0); +} + +PHP_METHOD(HttpClient, addSslOptions) +{ + php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("ssl"), 0, 0); +} + +PHP_METHOD(HttpClient, getSslOptions) +{ + php_http_client_object_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("ssl")); +} + +PHP_METHOD(HttpClient, setCookies) +{ + php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("cookies"), 1, 0); +} + +PHP_METHOD(HttpClient, addCookies) +{ + php_http_client_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("cookies"), 0, 0); +} + +PHP_METHOD(HttpClient, getCookies) +{ + php_http_client_object_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("cookies")); +} + +PHP_METHOD(HttpClient, enableCookies) +{ + if (SUCCESS == zend_parse_parameters_none()){ + php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_COOKIES_ENABLE, NULL); + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClient, resetCookies) +{ + zend_bool session_only = 0; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &session_only)) { + php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + if (session_only) { + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_COOKIES_RESET_SESSION, NULL); + } else { + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_COOKIES_RESET, NULL); + } + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClient, flushCookies) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_COOKIES_FLUSH, NULL); + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClient, getResponseMessage) +{ + with_error_handling(EH_THROW, php_http_exception_class_entry) { + if (SUCCESS == zend_parse_parameters_none()) { + zval *message = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC); + + if (Z_TYPE_P(message) == IS_OBJECT) { + RETVAL_OBJECT(message, 1); + } else { + php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HttpClient does not contain a response message"); + } + } + } end_error_handling(); +} + +PHP_METHOD(HttpClient, getRequestMessage) +{ + with_error_handling(EH_THROW, php_http_exception_class_entry) { + if (SUCCESS == zend_parse_parameters_none()) { + zval *message = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("requestMessage"), 0 TSRMLS_CC); + + if (Z_TYPE_P(message) == IS_OBJECT) { + RETVAL_OBJECT(message, 1); + } else { + php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HttpClient does not contain a request message"); + } + } + } end_error_handling(); +} + +PHP_METHOD(HttpClient, getHistory) +{ + with_error_handling(EH_THROW, php_http_exception_class_entry) { + if (SUCCESS == zend_parse_parameters_none()) { + zval *hist = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC); + + if (Z_TYPE_P(hist) == IS_OBJECT) { + RETVAL_OBJECT(hist, 1); + } else { + php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "The history is empty"); + } + } + } end_error_handling(); +} + +PHP_METHOD(HttpClient, clearHistory) +{ + if (SUCCESS == zend_parse_parameters_none()) { + zend_update_property_null(php_http_client_class_entry, getThis(), ZEND_STRL("history") TSRMLS_CC); + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClient, getMessageClass) +{ + if (SUCCESS == zend_parse_parameters_none()) { + RETURN_PROP(php_http_client_class_entry, "messageClass"); + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClient, setMessageClass) +{ + char *cn; + int cl; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &cn, &cl)) { + zend_update_property_stringl(php_http_client_class_entry, getThis(), ZEND_STRL("messageClass"), cn, cl TSRMLS_CC); + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClient, send) +{ + zval *zreq; + + RETVAL_FALSE; + + with_error_handling(EH_THROW, php_http_exception_class_entry) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zreq, php_http_client_request_class_entry)) { + if (SUCCESS == php_http_client_object_handle_request(getThis(), zreq TSRMLS_CC)) { + php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_message_object_t *req = zend_object_store_get_object(zreq TSRMLS_CC); + + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("request"), zreq TSRMLS_CC); + php_http_client_exec(obj->client, req->message); + + if (SUCCESS == php_http_client_object_handle_response(getThis() TSRMLS_CC)) { + RETVAL_PROP(php_http_client_class_entry, "responseMessage"); + } else { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to handle response"); + } + } else { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to handle request"); + } + } + } end_error_handling(); +} + +PHP_MINIT_FUNCTION(http_client) +{ + PHP_HTTP_REGISTER_CLASS(http\\Client, AbstractClient, http_client, php_http_object_class_entry, ZEND_ACC_ABSTRACT); + php_http_client_class_entry->create_object = php_http_object_new;//php_http_client_object_new; + memcpy(&php_http_client_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + php_http_client_object_handlers.clone_obj = php_http_client_object_clone; + + zend_class_implements(php_http_client_class_entry TSRMLS_CC, 2, spl_ce_SplSubject, php_http_client_interface_class_entry); + + zend_declare_property_string(php_http_client_class_entry, ZEND_STRL("messageClass"), "", ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("options"), ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("transferInfo"), ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("responseMessage"), ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("requestMessage"), ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("history"), ZEND_ACC_PRIVATE TSRMLS_CC); + + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("request"), ZEND_ACC_PROTECTED TSRMLS_CC); + + zend_declare_property_bool(php_http_client_class_entry, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); + + return SUCCESS; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client.h b/php_http_client.h new file mode 100644 index 0000000..24067a9 --- /dev/null +++ b/php_http_client.h @@ -0,0 +1,220 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_HTTP_CLIENT_H +#define PHP_HTTP_CLIENT_H + +#include "php_http_message_body.h" +#include "php_http_message_parser.h" + +typedef struct php_http_client_progress_state { + struct { + double now; + double total; + } ul; + struct { + double now; + double total; + } dl; + const char *info; + unsigned started:1; + unsigned finished:1; +} php_http_client_progress_state_t; + +#define PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER 0 +#define PHP_HTTP_CLIENT_PROGRESS_CALLBACK_INTERN 1 +typedef struct php_http_client_progress_callback { + union { + zval *user; + void (*intern)(php_http_client_progress_state_t* TSRMLS_DC); + } func; + unsigned type:1; + unsigned pass_state:1; +} php_http_client_progress_callback_t; + +typedef struct php_http_client_progress { + php_http_client_progress_state_t state; + php_http_client_progress_callback_t *callback; + unsigned in_cb:1; +} php_http_client_progress_t; + +static inline void php_http_client_progress_dtor(php_http_client_progress_t *progress TSRMLS_DC) +{ + if (progress->callback) { + if (progress->callback->type == PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER) { + zval_ptr_dtor(&progress->callback->func.user); + } + efree(progress->callback); + } + memset(progress, 0, sizeof(*progress)); +} + +static inline void php_http_client_progress_notify(php_http_client_progress_t *progress TSRMLS_DC) +{ + if (progress->callback) { + zval retval; + + INIT_PZVAL(&retval); + ZVAL_NULL(&retval); + + with_error_handling(EH_NORMAL, NULL) { + switch (progress->callback->type) { + case PHP_HTTP_CLIENT_PROGRESS_CALLBACK_USER: + if (progress->callback->pass_state) { + zval *param; + + MAKE_STD_ZVAL(param); + array_init(param); + add_assoc_bool(param, "started", progress->state.started); + add_assoc_bool(param, "finished", progress->state.finished); + add_assoc_string(param, "info", estrdup(progress->state.info), 0); + add_assoc_double(param, "dltotal", progress->state.dl.total); + add_assoc_double(param, "dlnow", progress->state.dl.now); + add_assoc_double(param, "ultotal", progress->state.ul.total); + add_assoc_double(param, "ulnow", progress->state.ul.now); + + progress->in_cb = 1; + call_user_function(EG(function_table), NULL, progress->callback->func.user, &retval, 1, ¶m TSRMLS_CC); + progress->in_cb = 0; + + zval_ptr_dtor(¶m); + } else { + progress->in_cb = 1; + call_user_function(EG(function_table), NULL, progress->callback->func.user, &retval, 0, NULL TSRMLS_CC); + progress->in_cb = 0; + } + break; + case PHP_HTTP_CLIENT_PROGRESS_CALLBACK_INTERN: + progress->callback->func.intern(progress->callback->pass_state ? &progress->state : NULL TSRMLS_CC); + break; + default: + break; + } + } end_error_handling(); + + zval_dtor(&retval); + } +} + +typedef enum php_http_client_setopt_opt { + PHP_HTTP_CLIENT_OPT_SETTINGS, /* HashTable* */ + PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK, /* php_http_client_progress_callback_t* */ + PHP_HTTP_CLIENT_OPT_COOKIES_ENABLE, /* - */ + PHP_HTTP_CLIENT_OPT_COOKIES_RESET, /* - */ + PHP_HTTP_CLIENT_OPT_COOKIES_RESET_SESSION, /* - */ + PHP_HTTP_CLIENT_OPT_COOKIES_FLUSH, /* - */ +} php_http_client_setopt_opt_t; + +typedef enum php_http_client_getopt_opt { + PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, /* php_http_client_progress_t** */ + PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, /* HashTable* */ +} php_http_client_getopt_opt_t; + +typedef struct php_http_client *(*php_http_client_init_func_t)(struct php_http_client *h, void *arg); +typedef struct php_http_client *(*php_http_client_copy_func_t)(struct php_http_client *from, struct php_http_client *to); +typedef void (*php_http_client_dtor_func_t)(struct php_http_client *h); +typedef STATUS (*php_http_client_exec_func_t)(struct php_http_client *h, php_http_message_t *msg); +typedef STATUS (*php_http_client_reset_func_t)(struct php_http_client *h); +typedef STATUS (*php_http_client_setopt_func_t)(struct php_http_client *h, php_http_client_setopt_opt_t opt, void *arg); +typedef STATUS (*php_http_client_getopt_func_t)(struct php_http_client *h, php_http_client_getopt_opt_t opt, void *arg); + +typedef struct php_http_client_ops { + php_http_resource_factory_ops_t *rsrc; + php_http_client_init_func_t init; + php_http_client_copy_func_t copy; + php_http_client_dtor_func_t dtor; + php_http_client_reset_func_t reset; + php_http_client_exec_func_t exec; + php_http_client_setopt_func_t setopt; + php_http_client_getopt_func_t getopt; +} php_http_client_ops_t; + +typedef struct php_http_client { + void *ctx; + php_http_resource_factory_t *rf; + php_http_client_ops_t *ops; + php_http_message_parser_t *parser; + php_http_message_t *message; + php_http_buffer_t *buffer; +#ifdef ZTS + void ***ts; +#endif +} php_http_client_t; + +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); +PHP_HTTP_API php_http_client_t *php_http_client_copy(php_http_client_t *from, php_http_client_t *to); +PHP_HTTP_API STATUS php_http_client_exec(php_http_client_t *h, php_http_message_t *msg); +PHP_HTTP_API STATUS php_http_client_reset(php_http_client_t *h); +PHP_HTTP_API STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg); +PHP_HTTP_API STATUS php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg); +PHP_HTTP_API void php_http_client_dtor(php_http_client_t *h); +PHP_HTTP_API void php_http_client_free(php_http_client_t **h); + +typedef struct php_http_client_object { + zend_object zo; + php_http_client_t *client; +} php_http_client_object_t; + +extern zend_class_entry *php_http_client_class_entry; +extern zend_function_entry php_http_client_method_entry[]; + +extern zend_object_value php_http_client_object_new(zend_class_entry *ce TSRMLS_DC); +extern 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); +extern zend_object_value php_http_client_object_clone(zval *zobject TSRMLS_DC); +extern void php_http_client_object_free(void *object TSRMLS_DC); + +extern zend_object_handlers *php_http_client_get_object_handlers(void); + +extern STATUS php_http_client_object_requesthandler(php_http_client_object_t *obj, zval *this_ptr, char **meth, char **url, php_http_message_body_t **body TSRMLS_DC); +extern STATUS php_http_client_object_responsehandler(php_http_client_object_t *obj, zval *this_ptr TSRMLS_DC); + +PHP_METHOD(HttpClient, __construct); +PHP_METHOD(HttpClient, getObservers); +PHP_METHOD(HttpClient, notify); +PHP_METHOD(HttpClient, attach); +PHP_METHOD(HttpClient, detach); +PHP_METHOD(HttpClient, getProgress); +PHP_METHOD(HttpClient, getTransferInfo); +PHP_METHOD(HttpClient, setOptions); +PHP_METHOD(HttpClient, getOptions); +PHP_METHOD(HttpClient, addSslOptions); +PHP_METHOD(HttpClient, setSslOptions); +PHP_METHOD(HttpClient, getSslOptions); +PHP_METHOD(HttpClient, addCookies); +PHP_METHOD(HttpClient, getCookies); +PHP_METHOD(HttpClient, setCookies); +PHP_METHOD(HttpClient, enableCookies); +PHP_METHOD(HttpClient, resetCookies); +PHP_METHOD(HttpClient, flushCookies); +PHP_METHOD(HttpClient, send); +PHP_METHOD(HttpClient, getResponseMessage); +PHP_METHOD(HttpClient, getRawResponseMessage); +PHP_METHOD(HttpClient, getRequestMessage); +PHP_METHOD(HttpClient, getRawRequestMessage); +PHP_METHOD(HttpClient, getHistory); +PHP_METHOD(HttpClient, clearHistory); +PHP_METHOD(HttpClient, getMessageClass); +PHP_METHOD(HttpClient, setMessageClass); + +extern PHP_MINIT_FUNCTION(http_client); + +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_curl.c b/php_http_client_curl.c new file mode 100644 index 0000000..f16349e --- /dev/null +++ b/php_http_client_curl.c @@ -0,0 +1,1365 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" + +#define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \ + { \ + char *K = #OPTION; \ + PHP_HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \ + } +#define PHP_HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \ + if (!strcasecmp(key.str, keyname)) { \ + zval *copy = cache_option(&curl->options.cache, keyname, strlen(keyname)+1, 0, php_http_ztyp(IS_STRING, *param)); \ + if (obdc) { \ + if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(copy) TSRMLS_CC)) { \ + return FAILURE; \ + } \ + } \ + curl_easy_setopt(ch, optname, Z_STRVAL_P(copy)); \ + zval_ptr_dtor(©); \ + continue; \ + } +#define PHP_HTTP_CURL_OPT_LONG(OPTION, ldiff) \ + { \ + char *K = #OPTION; \ + PHP_HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \ + } +#define PHP_HTTP_CURL_OPT_LONG_EX(keyname, optname) \ + if (!strcasecmp(key.str, keyname)) { \ + zval *copy = php_http_ztyp(IS_LONG, *param); \ + curl_easy_setopt(ch, optname, Z_LVAL_P(copy)); \ + zval_ptr_dtor(©); \ + continue; \ + } + + +/* resource_factory ops */ + +static void *php_http_curl_ctor(void *opaque TSRMLS_DC) +{ + void *ch; + + if ((ch = curl_easy_init())) { + get_storage(ch); + return ch; + } + return NULL; +} + +static void *php_http_curl_copy(void *opaque, void *handle TSRMLS_DC) +{ + void *ch; + + if ((ch = curl_easy_duphandle(handle))) { + curl_easy_reset(ch); + get_storage(ch); + return ch; + } + return NULL; +} + +static void php_http_curl_dtor(void *opaque, void *handle TSRMLS_DC) +{ + php_http_client_curl_storage_t *st = get_storage(handle); + + curl_easy_cleanup(handle); + + if (st) { + if (st->url) { + pefree(st->url, 1); + } + if (st->cookiestore) { + pefree(st->cookiestore, 1); + } + pefree(st, 1); + } +} + +/* callbacks */ + +static size_t php_http_client_curl_read_callback(void *data, size_t len, size_t n, void *ctx) +{ + php_http_message_body_t *body = ctx; + + if (body) { + TSRMLS_FETCH_FROM_CTX(body->ts); + return php_stream_read(php_http_message_body_stream(body), data, len * n); + } + return 0; +} + +static int php_http_client_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow) +{ + php_http_client_t *h = ctx; + php_http_client_curl_t *curl = h->ctx; + TSRMLS_FETCH_FROM_CTX(h->ts); + + curl->progress.state.dl.total = dltotal; + curl->progress.state.dl.now = dlnow; + curl->progress.state.ul.total = ultotal; + curl->progress.state.ul.now = ulnow; + + php_http_client_progress_notify(&curl->progress TSRMLS_CC); + + return 0; +} + +static curlioerr php_http_client_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx) +{ + php_http_message_body_t *body = ctx; + + if (cmd != CURLIOCMD_RESTARTREAD) { + return CURLIOE_UNKNOWNCMD; + } + + if (body) { + TSRMLS_FETCH_FROM_CTX(body->ts); + + if (SUCCESS == php_stream_rewind(php_http_message_body_stream(body))) { + return CURLIOE_OK; + } + } + + return CURLIOE_FAILRESTART; +} + +static int php_http_client_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx) +{ + php_http_client_t *h = ctx; + php_http_client_curl_t *curl = h->ctx; + unsigned flags = 0; + TSRMLS_FETCH_FROM_CTX(h->ts); + + /* catch progress */ + switch (type) { + case CURLINFO_TEXT: + if (php_memnstr(data, ZEND_STRL("About to connect"), data + length)) { + curl->progress.state.info = "resolve"; + } else if (php_memnstr(data, ZEND_STRL("Trying"), data + length)) { + curl->progress.state.info = "connect"; + } else if (php_memnstr(data, ZEND_STRL("Connected"), data + length)) { + curl->progress.state.info = "connected"; + } else if (php_memnstr(data, ZEND_STRL("left intact"), data + length)) { + curl->progress.state.info = "not disconnected"; + } else if (php_memnstr(data, ZEND_STRL("closed"), data + length)) { + curl->progress.state.info = "disconnected"; + } else if (php_memnstr(data, ZEND_STRL("Issue another request"), data + length)) { + curl->progress.state.info = "redirect"; + } + php_http_client_progress_notify(&curl->progress TSRMLS_CC); + break; + case CURLINFO_HEADER_OUT: + case CURLINFO_DATA_OUT: + case CURLINFO_SSL_DATA_OUT: + curl->progress.state.info = "send"; + break; + case CURLINFO_HEADER_IN: + case CURLINFO_DATA_IN: + case CURLINFO_SSL_DATA_IN: + curl->progress.state.info = "receive"; + break; + default: + break; + } + /* process data */ + switch (type) { + case CURLINFO_HEADER_IN: + case CURLINFO_DATA_IN: + case CURLINFO_HEADER_OUT: + case CURLINFO_DATA_OUT: + php_http_buffer_append(h->buffer, data, length); + + if (curl->options.redirects) { + flags |= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS; + } + + if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(h->parser, h->buffer, flags, &h->message)) { + return -1; + } + break; + default: + break; + } + +#if 0 + /* debug */ + _dpf(type, data, length); +#endif + + return 0; +} + +static int php_http_client_curl_dummy_callback(char *data, size_t n, size_t l, void *s) +{ + return n*l; +} + +static inline zval *cache_option(HashTable *cache, char *key, size_t keylen, ulong h, zval *opt) +{ + Z_ADDREF_P(opt); + + if (h) { + zend_hash_quick_update(cache, key, keylen, h, &opt, sizeof(zval *), NULL); + } else { + zend_hash_update(cache, key, keylen, &opt, sizeof(zval *), NULL); + } + + return opt; +} + +static inline zval *get_option(HashTable *cache, HashTable *options, char *key, size_t keylen, int type) +{ + if (options) { + zval **zoption; + ulong h = zend_hash_func(key, keylen); + + if (SUCCESS == zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) { + zval *option = php_http_ztyp(type, *zoption); + + if (cache) { + zval *cached = cache_option(cache, key, keylen, h, option); + + zval_ptr_dtor(&option); + return cached; + } + return option; + } + } + + return NULL; +} + +static STATUS set_options(php_http_client_t *h, HashTable *options) +{ + zval *zoption; + int range_req = 0; + php_http_client_curl_t *curl = h->ctx; + CURL *ch = curl->handle; + TSRMLS_FETCH_FROM_CTX(h->ts); + + /* proxy */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyhost"), IS_STRING))) { + curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption)); + /* type */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytype"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_PROXYTYPE, Z_LVAL_P(zoption)); + } + /* port */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyport"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption)); + } + /* user:pass */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauth"), IS_STRING)) && Z_STRLEN_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption)); + } + /* auth method */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauthtype"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption)); + } + /* tunnel */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytunnel"), IS_BOOL)) && Z_BVAL_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 1L); + } + } +#if PHP_HTTP_CURL_VERSION(7,19,4) + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("noproxy"), IS_STRING))) { + curl_easy_setopt(ch, CURLOPT_NOPROXY, Z_STRVAL_P(zoption)); + } +#endif + + /* dns */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_cache_timeout"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, Z_LVAL_P(zoption)); + } + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ipresolve"), IS_LONG)) && Z_LVAL_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_IPRESOLVE, Z_LVAL_P(zoption)); + } +#if PHP_HTTP_CURL_VERSION(7,21,3) + if (curl->options.resolve) { + curl_slist_free_all(curl->options.resolve); + curl->options.resolve = NULL; + } + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resolve"), IS_ARRAY))) { + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + HashPosition pos; + zval **data; + + FOREACH_KEYVAL(pos, zoption, key, data) { + zval *cpy = php_http_ztyp(IS_STRING, *data); + + curl->options.resolve = curl_slist_append(curl->options.resolve, Z_STRVAL_P(cpy)); + + zval_ptr_dtor(&cpy); + } + } +#endif +#if PHP_HTTP_CURL_VERSION(7,24,0) + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_servers"), IS_STRING)) && Z_STRLEN_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, Z_STRVAL_P(zoption)); + } +#endif + + /* limits */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_limit"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, Z_LVAL_P(zoption)); + } + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_time"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, Z_LVAL_P(zoption)); + } + /* LSF weirdance + if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption)); + } + if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption)); + } + */ + /* crashes + if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption)); + } */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("fresh_connect"), IS_BOOL)) && Z_BVAL_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L); + } + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("forbid_reuse"), IS_BOOL)) && Z_BVAL_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 1L); + } + + /* outgoing interface */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("interface"), IS_STRING))) { + curl_easy_setopt(ch, CURLOPT_INTERFACE, Z_STRVAL_P(zoption)); + + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("portrange"), IS_ARRAY))) { + zval **prs, **pre; + + zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption)); + if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) { + zend_hash_move_forward(Z_ARRVAL_P(zoption)); + if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) { + zval *prs_cpy = php_http_ztyp(IS_LONG, *prs); + zval *pre_cpy = php_http_ztyp(IS_LONG, *pre); + + if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) { + curl_easy_setopt(ch, CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy))); + curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L); + } + zval_ptr_dtor(&prs_cpy); + zval_ptr_dtor(&pre_cpy); + } + } + } + } + + /* another port */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("port"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption)); + } + + /* RFC4007 zone_id */ +#if PHP_HTTP_CURL_VERSION(7,19,0) + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("address_scope"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, Z_LVAL_P(zoption)); + } +#endif + + /* auth */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauth"), IS_STRING)) && Z_STRLEN_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption)); + } + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauthtype"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption)); + } + + /* redirects, defaults to 0 */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("redirect"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L); + curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(zoption)); + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("unrestrictedauth"), IS_BOOL))) { + curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption)); + } + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("postredir"), IS_BOOL))) { +#if PHP_HTTP_CURL_VERSION(7,19,1) + curl_easy_setopt(ch, CURLOPT_POSTREDIR, Z_BVAL_P(zoption) ? 1L : 0L); +#else + curl_easy_setopt(ch, CURLOPT_POST301, Z_BVAL_P(zoption) ? 1L : 0L); +#endif + } + } else { + curl->options.redirects = 0; + } + + /* retries, defaults to 0 */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrycount"), IS_LONG))) { + curl->options.retry.count = Z_LVAL_P(zoption); + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrydelay"), IS_DOUBLE))) { + curl->options.retry.delay = Z_DVAL_P(zoption); + } else { + curl->options.retry.delay = 0; + } + } else { + curl->options.retry.count = 0; + } + + /* referer */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("referer"), IS_STRING)) && Z_STRLEN_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption)); + } + + /* useragent, default "PECL::HTTP/version (PHP/version)" */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("useragent"), IS_STRING))) { + /* allow to send no user agent, not even default one */ + if (Z_STRLEN_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption)); + } else { + curl_easy_setopt(ch, CURLOPT_USERAGENT, NULL); + } + } + + /* resume */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resume"), IS_LONG)) && (Z_LVAL_P(zoption) > 0)) { + range_req = 1; + curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(zoption)); + } + /* or range of kind array(array(0,499), array(100,1499)) */ + else if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("range"), IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) { + HashPosition pos1, pos2; + zval **rr, **rb, **re; + php_http_buffer_t rs; + + php_http_buffer_init(&rs); + FOREACH_VAL(pos1, zoption, rr) { + if (Z_TYPE_PP(rr) == IS_ARRAY) { + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2); + if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) { + zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2); + if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) { + if ( ((Z_TYPE_PP(rb) == IS_LONG) || ((Z_TYPE_PP(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(rb), Z_STRLEN_PP(rb), NULL, NULL, 1))) && + ((Z_TYPE_PP(re) == IS_LONG) || ((Z_TYPE_PP(re) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(re), Z_STRLEN_PP(re), NULL, NULL, 1)))) { + zval *rbl = php_http_ztyp(IS_LONG, *rb); + zval *rel = php_http_ztyp(IS_LONG, *re); + + if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) { + php_http_buffer_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel)); + } + zval_ptr_dtor(&rbl); + zval_ptr_dtor(&rel); + } + } + } + } + } + + if (PHP_HTTP_BUFFER_LEN(&rs)) { + zval *cached_range; + + range_req = 1; + /* ditch last comma */ + PHP_HTTP_BUFFER_VAL(&rs)[PHP_HTTP_BUFFER_LEN(&rs)-- -1] = '\0'; + /* cache string */ + MAKE_STD_ZVAL(cached_range); + ZVAL_STRINGL(cached_range, PHP_HTTP_BUFFER_VAL(&rs), PHP_HTTP_BUFFER_LEN(&rs), 0); + curl_easy_setopt(ch, CURLOPT_RANGE, Z_STRVAL_P(cache_option(&curl->options.cache, ZEND_STRS("range"), 0, cached_range))); + zval_ptr_dtor(&cached_range); + } + } + + /* initialize headers */ + if (curl->options.headers) { + curl_slist_free_all(curl->options.headers); + curl->options.headers = NULL; + } + /* etag */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("etag"), IS_STRING)) && Z_STRLEN_P(zoption)) { + zend_bool is_quoted = !((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')); + php_http_buffer_t header; + + php_http_buffer_init(&header); + php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", range_req?"If-Match":"If-None-Match", Z_STRVAL_P(zoption)); + php_http_buffer_fix(&header); + curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header)); + php_http_buffer_dtor(&header); + } + /* compression */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("compress"), IS_BOOL)) && Z_LVAL_P(zoption)) { + curl->options.headers = curl_slist_append(curl->options.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5"); + } + curl_easy_setopt(ch, CURLOPT_HTTPHEADER, curl->options.headers); + + /* lastmodified */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("lastmodified"), IS_LONG))) { + if (Z_LVAL_P(zoption)) { + if (Z_LVAL_P(zoption) > 0) { + curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(zoption)); + } else { + curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(zoption)); + } + curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE)); + } else { + curl_easy_setopt(ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE); + } + } + + /* cookies, array('name' => 'value') */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookies"), IS_ARRAY))) { + php_http_buffer_dtor(&curl->options.cookies); + if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) { + zval *urlenc_cookies = NULL; + /* check whether cookies should not be urlencoded; default is to urlencode them */ + if ((!(urlenc_cookies = get_option(&curl->options.cache, options, ZEND_STRS("encodecookies"), IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) { + if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(zoption), &curl->options.cookies, ZEND_STRS(";"), ZEND_STRS("="), NULL, 0 TSRMLS_CC)) { + php_http_buffer_fix(&curl->options.cookies); + curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data); + } + } else { + HashPosition pos; + php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0); + zval **cookie_val; + + FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) { + if (cookie_key.type == HASH_KEY_IS_STRING) { + zval *val = php_http_ztyp(IS_STRING, *cookie_val); + php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val)); + zval_ptr_dtor(&val); + } + } + + php_http_buffer_fix(&curl->options.cookies); + if (PHP_HTTP_BUFFER_LEN(&curl->options.cookies)) { + curl_easy_setopt(ch, CURLOPT_COOKIE, PHP_HTTP_BUFFER_VAL(&curl->options.cookies)); + } + } + } + } + + /* don't load session cookies from cookiestore */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiesession"), IS_BOOL)) && Z_BVAL_P(zoption)) { + curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 1L); + } + + /* cookiestore, read initial cookies from that file and store cookies back into that file */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiestore"), IS_STRING))) { + php_http_client_curl_storage_t *storage = get_storage(curl->handle); + + if (Z_STRLEN_P(zoption)) { + if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(zoption) TSRMLS_CC)) { + return FAILURE; + } + } + if (storage->cookiestore) { + pefree(storage->cookiestore, 1); + } + storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1); + curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore); + curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore); + } + + /* maxfilesize */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("maxfilesize"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption)); + } + + /* http protocol */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("protocol"), IS_LONG))) { + curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption)); + } + + /* timeout, defaults to 0 */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("timeout"), IS_DOUBLE))) { + curl_easy_setopt(ch, CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000)); + } + /* connecttimeout, defaults to 0 */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("connecttimeout"), IS_DOUBLE))) { + curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000)); + } + + /* ssl */ + if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ssl"), IS_ARRAY))) { + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + zval **param; + HashPosition pos; + + FOREACH_KEYVAL(pos, zoption, key, param) { + if (key.type == HASH_KEY_IS_STRING) { + PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1); + PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0); + PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0); + + PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0); + PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0); + PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0); + + PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0); + PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0); + + PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1); + PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1); + PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0); + + PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1); + PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1); + PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1); + PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1); +#if PHP_HTTP_CURL_VERSION(7,19,0) + PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT, -3, 1); + #if defined(PHP_HTTP_HAVE_OPENSSL) + PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE, -3, 1); + #endif +#endif +#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) + PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO, -3); +#endif + } + } + } + return SUCCESS; +} + +static STATUS get_info(CURL *ch, HashTable *info) +{ + char *c; + long l; + double d; + struct curl_slist *s, *p; + zval *subarray, array; + INIT_PZVAL_ARRAY(&array, info); + + /* BEGIN::CURLINFO */ + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &c)) { + add_assoc_string_ex(&array, "effective_url", sizeof("effective_url"), c ? c : "", 1); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) { + add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) { + add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) { + add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) { + add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) { + add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) { + add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) { + add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) { + add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) { + add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) { + add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) { + add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) { + add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) { + add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { + add_assoc_double_ex(&array, "content_length_download", sizeof("content_length_download"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_UPLOAD, &d)) { + add_assoc_double_ex(&array, "content_length_upload", sizeof("content_length_upload"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) { + add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) { + add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) { + add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) { + add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) { + add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) { + add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) { + add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) { + add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) { + add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) { + MAKE_STD_ZVAL(subarray); + array_init(subarray); + for (p = s; p; p = p->next) { + if (p->data) { + add_next_index_string(subarray, p->data, 1); + } + } + add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray); + curl_slist_free_all(s); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_COOKIELIST, &s)) { + MAKE_STD_ZVAL(subarray); + array_init(subarray); + for (p = s; p; p = p->next) { + if (p->data) { + add_next_index_string(subarray, p->data, 1); + } + } + add_assoc_zval_ex(&array, "cookies", sizeof("cookies"), subarray); + curl_slist_free_all(s); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) { + add_assoc_string_ex(&array, "redirect_url", sizeof("redirect_url"), c ? c : "", 1); + } +#if PHP_HTTP_CURL_VERSION(7,19,0) + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) { + add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1); + } +#endif +#if PHP_HTTP_CURL_VERSION(7,19,0) + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_APPCONNECT_TIME, &d)) { + add_assoc_double_ex(&array, "appconnect_time", sizeof("appconnect_time"), d); + } +#endif +#if PHP_HTTP_CURL_VERSION(7,19,4) + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONDITION_UNMET, &l)) { + add_assoc_long_ex(&array, "condition_unmet", sizeof("condition_unmet"), l); + } +#endif +#if PHP_HTTP_CURL_VERSION(7,21,0) + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_PORT, &l)) { + add_assoc_long_ex(&array, "primary_port", sizeof("primary_port"), l); + } +#endif +#if PHP_HTTP_CURL_VERSION(7,21,0) + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_IP, &c)) { + add_assoc_string_ex(&array, "local_ip", sizeof("local_ip"), c ? c : "", 1); + } +#endif +#if PHP_HTTP_CURL_VERSION(7,21,0) + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_PORT, &l)) { + add_assoc_long_ex(&array, "local_port", sizeof("local_port"), l); + } +#endif + + /* END::CURLINFO */ + +#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) + { + int i; + zval *ci_array; + struct curl_certinfo *ci; + char *colon, *keyname; + + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) { + MAKE_STD_ZVAL(ci_array); + array_init(ci_array); + + for (i = 0; i < ci->num_of_certs; ++i) { + s = ci->certinfo[i]; + + MAKE_STD_ZVAL(subarray); + array_init(subarray); + for (p = s; p; p = p->next) { + if (p->data) { + if ((colon = strchr(p->data, ':'))) { + keyname = estrndup(p->data, colon - p->data); + add_assoc_string_ex(subarray, keyname, colon - p->data + 1, colon + 1, 1); + efree(keyname); + } else { + add_next_index_string(subarray, p->data, 1); + } + } + } + add_next_index_zval(ci_array, subarray); + } + add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array); + } + } +#endif + add_assoc_string_ex(&array, "error", sizeof("error"), get_storage(ch)->errorbuffer, 1); + + return SUCCESS; +} + + +/* request handler ops */ + +static STATUS php_http_client_curl_reset(php_http_client_t *h); + +static php_http_client_t *php_http_client_curl_init(php_http_client_t *h, void *handle) +{ + php_http_client_curl_t *ctx; + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "could not initialize curl handle"); + return NULL; + } + + ctx = ecalloc(1, sizeof(*ctx)); + ctx->handle = handle; + php_http_buffer_init(&ctx->options.cookies); + zend_hash_init(&ctx->options.cache, 0, NULL, ZVAL_PTR_DTOR, 0); + h->ctx = ctx; + +#if defined(ZTS) + curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L); +#endif + curl_easy_setopt(handle, CURLOPT_HEADER, 0L); + curl_easy_setopt(handle, CURLOPT_FILETIME, 1L); + curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L); + curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L); + curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, NULL); + curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, php_http_client_curl_dummy_callback); + curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, php_http_client_curl_raw_callback); + curl_easy_setopt(handle, CURLOPT_READFUNCTION, php_http_client_curl_read_callback); + curl_easy_setopt(handle, CURLOPT_IOCTLFUNCTION, php_http_client_curl_ioctl_callback); + curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, php_http_client_curl_progress_callback); + curl_easy_setopt(handle, CURLOPT_DEBUGDATA, h); + curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, h); + + php_http_client_curl_reset(h); + + return h; +} + +static php_http_client_t *php_http_client_curl_copy(php_http_client_t *from, php_http_client_t *to) +{ + php_http_client_curl_t *ctx = from->ctx; + void *copy; + TSRMLS_FETCH_FROM_CTX(from->ts); + + if (!(copy = php_http_resource_factory_handle_copy(from->rf, ctx->handle TSRMLS_CC))) { + return NULL; + } + + if (to) { + return php_http_client_curl_init(to, copy); + } else { + return php_http_client_init(NULL, from->ops, from->rf, copy TSRMLS_CC); + } +} + +static void php_http_client_curl_dtor(php_http_client_t *h) +{ + php_http_client_curl_t *ctx = h->ctx; + TSRMLS_FETCH_FROM_CTX(h->ts); + + curl_easy_setopt(ctx->handle, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(ctx->handle, CURLOPT_PROGRESSFUNCTION, NULL); + curl_easy_setopt(ctx->handle, CURLOPT_VERBOSE, 0L); + curl_easy_setopt(ctx->handle, CURLOPT_DEBUGFUNCTION, NULL); + + php_http_resource_factory_handle_dtor(h->rf, ctx->handle TSRMLS_CC); + + php_http_buffer_dtor(&ctx->options.cookies); + zend_hash_destroy(&ctx->options.cache); + + if (ctx->options.headers) { + curl_slist_free_all(ctx->options.headers); + ctx->options.headers = NULL; + } + php_http_client_progress_dtor(&ctx->progress TSRMLS_CC); + + efree(ctx); + h->ctx = NULL; +} +static STATUS php_http_client_curl_reset(php_http_client_t *h) +{ + CURL *ch = ((php_http_client_curl_t *) h->ctx)->handle; + php_http_client_curl_storage_t *st; + + if ((st = get_storage(ch))) { + if (st->url) { + pefree(st->url, 1); + st->url = NULL; + } + if (st->cookiestore) { + pefree(st->cookiestore, 1); + st->cookiestore = NULL; + } + st->errorbuffer[0] = '\0'; + } + + curl_easy_setopt(ch, CURLOPT_URL, NULL); +#if PHP_HTTP_CURL_VERSION(7,19,4) + curl_easy_setopt(ch, CURLOPT_NOPROXY, NULL); +#endif + curl_easy_setopt(ch, CURLOPT_PROXY, NULL); + curl_easy_setopt(ch, CURLOPT_PROXYPORT, 0L); + curl_easy_setopt(ch, CURLOPT_PROXYTYPE, 0L); + /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */ +#if PHP_HTTP_CURL_VERSION(7,19,1) + curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, NULL); + curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, NULL); +#endif + curl_easy_setopt(ch, CURLOPT_PROXYAUTH, 0L); + curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 0L); + curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, 60L); + curl_easy_setopt(ch, CURLOPT_IPRESOLVE, 0); +#if PHP_HTTP_CURL_VERSION(7,21,3) + curl_easy_setopt(ch, CURLOPT_RESOLVE, NULL); +#endif +#if PHP_HTTP_CURL_VERSION(7,24,0) + curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, NULL); +#endif + curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, 0L); + curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, 0L); + /* LFS weirdance + curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0); + curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0); + */ + /* crashes + curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, 5L); */ + curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 0L); + curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 0L); + curl_easy_setopt(ch, CURLOPT_INTERFACE, NULL); + curl_easy_setopt(ch, CURLOPT_PORT, 0L); +#if PHP_HTTP_CURL_VERSION(7,19,0) + curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, 0L); +#endif + curl_easy_setopt(ch, CURLOPT_LOCALPORT, 0L); + curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, 0L); + /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */ +#if PHP_HTTP_CURL_VERSION(7,19,1) + curl_easy_setopt(ch, CURLOPT_USERNAME, NULL); + curl_easy_setopt(ch, CURLOPT_PASSWORD, NULL); +#endif + curl_easy_setopt(ch, CURLOPT_HTTPAUTH, 0L); + curl_easy_setopt(ch, CURLOPT_ENCODING, NULL); + /* we do this ourself anyway */ + curl_easy_setopt(ch, CURLOPT_HTTP_CONTENT_DECODING, 0L); + curl_easy_setopt(ch, CURLOPT_HTTP_TRANSFER_DECODING, 0L); + curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0L); +#if PHP_HTTP_CURL_VERSION(7,19,1) + curl_easy_setopt(ch, CURLOPT_POSTREDIR, 0L); +#else + curl_easy_setopt(ch, CURLOPT_POST301, 0L); +#endif + curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, 0L); + curl_easy_setopt(ch, CURLOPT_REFERER, NULL); + curl_easy_setopt(ch, CURLOPT_USERAGENT, "PECL::HTTP/" PHP_HTTP_EXT_VERSION " (PHP/" PHP_VERSION ")"); + curl_easy_setopt(ch, CURLOPT_HTTPHEADER, NULL); + curl_easy_setopt(ch, CURLOPT_COOKIE, NULL); + curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 0L); + /* these options would enable curl's cookie engine by default which we don't want + curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL); + curl_easy_setopt(ch, CURLOPT_COOKIEJAR, NULL); */ + curl_easy_setopt(ch, CURLOPT_COOKIELIST, NULL); + curl_easy_setopt(ch, CURLOPT_RANGE, NULL); + curl_easy_setopt(ch, CURLOPT_RESUME_FROM, 0L); + curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, 0L); + curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0L); + curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0L); + curl_easy_setopt(ch, CURLOPT_TIMEOUT, 0L); + curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, 3); + curl_easy_setopt(ch, CURLOPT_SSLCERT, NULL); + curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, NULL); + curl_easy_setopt(ch, CURLOPT_SSLCERTPASSWD, NULL); + curl_easy_setopt(ch, CURLOPT_SSLKEY, NULL); + curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, NULL); + curl_easy_setopt(ch, CURLOPT_SSLKEYPASSWD, NULL); + curl_easy_setopt(ch, CURLOPT_SSLENGINE, NULL); + curl_easy_setopt(ch, CURLOPT_SSLVERSION, 0L); + curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L); + curl_easy_setopt(ch, CURLOPT_SSL_CIPHER_LIST, NULL); +#if PHP_HTTP_CURL_VERSION(7,19,0) + curl_easy_setopt(ch, CURLOPT_ISSUERCERT, NULL); +#if defined(PHP_HTTP_HAVE_OPENSSL) + curl_easy_setopt(ch, CURLOPT_CRLFILE, NULL); +#endif +#endif +#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) + curl_easy_setopt(ch, CURLOPT_CERTINFO, NULL); +#endif +#ifdef PHP_HTTP_CURL_CAINFO + curl_easy_setopt(ch, CURLOPT_CAINFO, PHP_HTTP_CURL_CAINFO); +#else + curl_easy_setopt(ch, CURLOPT_CAINFO, NULL); +#endif + curl_easy_setopt(ch, CURLOPT_CAPATH, NULL); + curl_easy_setopt(ch, CURLOPT_RANDOM_FILE, NULL); + curl_easy_setopt(ch, CURLOPT_EGDSOCKET, NULL); + curl_easy_setopt(ch, CURLOPT_POSTFIELDS, NULL); + curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, 0L); + curl_easy_setopt(ch, CURLOPT_HTTPPOST, NULL); + curl_easy_setopt(ch, CURLOPT_IOCTLDATA, NULL); + curl_easy_setopt(ch, CURLOPT_READDATA, NULL); + curl_easy_setopt(ch, CURLOPT_INFILESIZE, 0L); + curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE); + curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, NULL); + curl_easy_setopt(ch, CURLOPT_NOBODY, 0L); + curl_easy_setopt(ch, CURLOPT_POST, 0L); + curl_easy_setopt(ch, CURLOPT_UPLOAD, 0L); + curl_easy_setopt(ch, CURLOPT_HTTPGET, 1L); + + return SUCCESS; +} + +static STATUS php_http_client_curl_exec(php_http_client_t *h, php_http_message_t *msg) +{ + uint tries = 0; + CURLcode result; + php_http_client_curl_t *curl = h->ctx; + php_http_client_curl_storage_t *storage = get_storage(curl->handle); + TSRMLS_FETCH_FROM_CTX(h->ts); + + /* request url */ + if (!PHP_HTTP_INFO(msg).request.url) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Cannot request empty URL"); + } + storage->errorbuffer[0] = '\0'; + if (storage->url) { + pefree(storage->url, 1); + } + storage->url = pestrdup(PHP_HTTP_INFO(msg).request.url, 1); + curl_easy_setopt(curl->handle, CURLOPT_URL, storage->url); + + /* request method */ + switch (php_http_select_str(PHP_HTTP_INFO(msg).request.method, 4, "GET", "HEAD", "POST", "PUT")) { + case 0: + curl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1L); + break; + + case 1: + curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L); + break; + + case 2: + curl_easy_setopt(curl->handle, CURLOPT_POST, 1L); + break; + + case 3: + curl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1L); + break; + + default: { + if (PHP_HTTP_INFO(msg).request.method) { + curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, PHP_HTTP_INFO(msg).request.method); + } else { + php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD, "Cannot use empty request method"); + return FAILURE; + } + break; + } + } + + /* request headers */ + if (zend_hash_num_elements(&msg->hdrs)) { + php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0); + zval **header_val; + HashPosition pos; + php_http_buffer_t header; + + php_http_buffer_init(&header); + FOREACH_HASH_KEYVAL(pos, &msg->hdrs, header_key, header_val) { + if (header_key.type == HASH_KEY_IS_STRING) { + zval *header_cpy = php_http_ztyp(IS_STRING, *header_val); + + php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy)); + php_http_buffer_fix(&header); + curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header)); + php_http_buffer_reset(&header); + + zval_ptr_dtor(&header_cpy); + } + } + php_http_buffer_dtor(&header); + curl_easy_setopt(curl->handle, CURLOPT_HTTPHEADER, curl->options.headers); + } + + /* attach request body */ + { + /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the + * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.« + * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the + * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method + * does not allow a request body. + */ + size_t body_size = php_http_message_body_size(&msg->body); + + curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, &msg->body); + curl_easy_setopt(curl->handle, CURLOPT_READDATA, &msg->body); + curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size); + curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size); + } + +retry: + if (CURLE_OK != (result = curl_easy_perform(curl->handle))) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "%s; %s (%s)", curl_easy_strerror(result), storage->errorbuffer, storage->url); + + if (EG(exception)) { + add_property_long(EG(exception), "curlCode", result); + } + + if (curl->options.retry.count > tries++) { + switch (result) { + case CURLE_COULDNT_RESOLVE_PROXY: + case CURLE_COULDNT_RESOLVE_HOST: + case CURLE_COULDNT_CONNECT: + case CURLE_WRITE_ERROR: + case CURLE_READ_ERROR: + case CURLE_OPERATION_TIMEDOUT: + case CURLE_SSL_CONNECT_ERROR: + case CURLE_GOT_NOTHING: + case CURLE_SSL_ENGINE_SETFAILED: + case CURLE_SEND_ERROR: + case CURLE_RECV_ERROR: + case CURLE_SSL_ENGINE_INITFAILED: + case CURLE_LOGIN_DENIED: + if (curl->options.retry.delay >= PHP_HTTP_DIFFSEC) { + php_http_sleep(curl->options.retry.delay); + } + goto retry; + default: + break; + } + } else { + return FAILURE; + } + } + + return SUCCESS; +} + +static STATUS php_http_client_curl_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg) +{ + php_http_client_curl_t *curl = h->ctx; + TSRMLS_FETCH_FROM_CTX(h->ts); + + switch (opt) { + case PHP_HTTP_CLIENT_OPT_SETTINGS: + return set_options(h, arg); + break; + + case PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK: + if (curl->progress.in_cb) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Cannot change progress callback while executing it"); + return FAILURE; + } + if (curl->progress.callback) { + php_http_client_progress_dtor(&curl->progress TSRMLS_CC); + } + curl->progress.callback = arg; + break; + + case PHP_HTTP_CLIENT_OPT_COOKIES_ENABLE: + /* are cookies already enabled anyway? */ + if (!get_storage(curl->handle)->cookiestore) { + if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIEFILE, "")) { + return FAILURE; + } + } + break; + + case PHP_HTTP_CLIENT_OPT_COOKIES_RESET: + if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "ALL")) { + return FAILURE; + } + break; + + case PHP_HTTP_CLIENT_OPT_COOKIES_RESET_SESSION: + if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "SESS")) { + return FAILURE; + } + break; + + case PHP_HTTP_CLIENT_OPT_COOKIES_FLUSH: + if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "FLUSH")) { + return FAILURE; + } + break; + + default: + return FAILURE; + } + + return SUCCESS; +} + +static STATUS php_http_client_curl_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg) +{ + php_http_client_curl_t *curl = h->ctx; + + switch (opt) { + case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO: + *((php_http_client_progress_t **) arg) = &curl->progress; + break; + + case PHP_HTTP_CLIENT_OPT_TRANSFER_INFO: + get_info(curl->handle, arg); + break; + + default: + return FAILURE; + } + + return SUCCESS; +} + +static php_http_resource_factory_ops_t php_http_client_curl_resource_factory_ops = { + php_http_curl_ctor, + php_http_curl_copy, + php_http_curl_dtor +}; + +static php_http_client_ops_t php_http_client_curl_ops = { + &php_http_client_curl_resource_factory_ops, + php_http_client_curl_init, + php_http_client_curl_copy, + php_http_client_curl_dtor, + php_http_client_curl_reset, + php_http_client_curl_exec, + php_http_client_curl_setopt, + php_http_client_curl_getopt +}; + +PHP_HTTP_API php_http_client_ops_t *php_http_client_curl_get_ops(void) +{ + return &php_http_client_curl_ops; +} + + +#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientCURL, method, 0, req_args) +#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientCURL, method, 0) +#define PHP_HTTP_CURL_ME(method, visibility) PHP_ME(HttpClientCURL, method, PHP_HTTP_ARGS(HttpClientCURL, method), visibility) +#define PHP_HTTP_CURL_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientCURL, method)) +#define PHP_HTTP_CURL_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClientCURL_##al), PHP_HTTP_ARGS(HttpClientCURL, al), vis) + + +zend_class_entry *php_http_client_curl_class_entry; +zend_function_entry php_http_client_curl_method_entry[] = { + EMPTY_FUNCTION_ENTRY +}; + +zend_object_value php_http_client_curl_object_new(zend_class_entry *ce TSRMLS_DC) +{ + return php_http_client_curl_object_new_ex(ce, NULL, NULL TSRMLS_CC); +} + +zend_object_value php_http_client_curl_object_new_ex(zend_class_entry *ce, php_http_client_t *r, php_http_client_object_t **ptr TSRMLS_DC) +{ + zend_object_value ov; + php_http_client_object_t *o; + + o = ecalloc(1, sizeof(php_http_client_object_t)); + zend_object_std_init((zend_object *) o, ce TSRMLS_CC); + object_properties_init((zend_object *) o, ce); + + if (!(o->client = r)) { + o->client = php_http_client_init(NULL, &php_http_client_curl_ops, NULL, NULL TSRMLS_CC); + } + + if (ptr) { + *ptr = o; + } + + ov.handle = zend_objects_store_put(o, NULL, php_http_client_object_free, NULL TSRMLS_CC); + ov.handlers = php_http_client_get_object_handlers(); + + return ov; +} + + +PHP_MINIT_FUNCTION(http_client_curl) +{ + if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_client.curl"), &php_http_client_curl_resource_factory_ops, NULL, NULL)) { + return FAILURE; + } + + PHP_HTTP_REGISTER_CLASS(http\\Client, CURL, http_client_curl, php_http_client_class_entry, 0); + php_http_client_curl_class_entry->create_object = php_http_client_curl_object_new; + + /* + * HTTP Protocol Version Constants + */ + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("HTTP_VERSION_1_0"), CURL_HTTP_VERSION_1_0 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("HTTP_VERSION_1_1"), CURL_HTTP_VERSION_1_1 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("HTTP_VERSION_NONE"), CURL_HTTP_VERSION_NONE TSRMLS_CC); /* to be removed */ + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("HTTP_VERSION_ANY"), CURL_HTTP_VERSION_NONE TSRMLS_CC); + + /* + * SSL Version Constants + */ + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("SSL_VERSION_TLSv1"), CURL_SSLVERSION_TLSv1 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("SSL_VERSION_SSLv2"), CURL_SSLVERSION_SSLv2 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("SSL_VERSION_SSLv3"), CURL_SSLVERSION_SSLv3 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("SSL_VERSION_ANY"), CURL_SSLVERSION_DEFAULT TSRMLS_CC); + + /* + * DNS IPvX resolving + */ + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("IPRESOLVE_V4"), CURL_IPRESOLVE_V4 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("IPRESOLVE_V6"), CURL_IPRESOLVE_V6 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("IPRESOLVE_ANY"), CURL_IPRESOLVE_WHATEVER TSRMLS_CC); + + /* + * Auth Constants + */ + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("AUTH_BASIC"), CURLAUTH_BASIC TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("AUTH_DIGEST"), CURLAUTH_DIGEST TSRMLS_CC); +#if PHP_HTTP_CURL_VERSION(7,19,3) + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("AUTH_DIGEST_IE"), CURLAUTH_DIGEST_IE TSRMLS_CC); +#endif + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("AUTH_NTLM"), CURLAUTH_NTLM TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("AUTH_GSSNEG"), CURLAUTH_GSSNEGOTIATE TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("AUTH_ANY"), CURLAUTH_ANY TSRMLS_CC); + + /* + * Proxy Type Constants + */ + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("PROXY_SOCKS4"), CURLPROXY_SOCKS4 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("PROXY_SOCKS4A"), CURLPROXY_SOCKS5 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("PROXY_SOCKS5_HOSTNAME"), CURLPROXY_SOCKS5 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("PROXY_SOCKS5"), CURLPROXY_SOCKS5 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("PROXY_HTTP"), CURLPROXY_HTTP TSRMLS_CC); +# if PHP_HTTP_CURL_VERSION(7,19,4) + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("PROXY_HTTP_1_0"), CURLPROXY_HTTP_1_0 TSRMLS_CC); +# endif + + /* + * Post Redirection Constants + */ +#if PHP_HTTP_CURL_VERSION(7,19,1) + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("POSTREDIR_301"), CURL_REDIR_POST_301 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("POSTREDIR_302"), CURL_REDIR_POST_302 TSRMLS_CC); + zend_declare_class_constant_long(php_http_client_curl_class_entry, ZEND_STRL("POSTREDIR_ALL"), CURL_REDIR_POST_ALL TSRMLS_CC); +#endif + + return SUCCESS; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/php_http_client_curl.h b/php_http_client_curl.h new file mode 100644 index 0000000..059ab93 --- /dev/null +++ b/php_http_client_curl.h @@ -0,0 +1,81 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_HTTP_CLIENT_CURL_H +#define PHP_HTTP_CLIENT_CURL_H + +#if PHP_HTTP_HAVE_CURL + +PHP_HTTP_API php_http_client_ops_t *php_http_client_curl_get_ops(void); + +typedef struct php_http_client_curl { + CURL *handle; + + struct { + HashTable cache; + + struct curl_slist *headers; + struct curl_slist *resolve; + php_http_buffer_t cookies; + + long redirects; + + struct { + uint count; + double delay; + } retry; + + } options; + + php_http_client_progress_t progress; + +} php_http_client_curl_t; + +typedef struct php_http_client_curl_storage { + char *url; + char *cookiestore; + char errorbuffer[0x100]; +} php_http_client_curl_storage_t; + +static inline php_http_client_curl_storage_t *get_storage(CURL *ch) { + php_http_client_curl_storage_t *st = NULL; + + curl_easy_getinfo(ch, CURLINFO_PRIVATE, &st); + + if (!st) { + st = pecalloc(1, sizeof(*st), 1); + curl_easy_setopt(ch, CURLOPT_PRIVATE, st); + curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer); + } + + return st; +} + +extern zend_class_entry *php_http_client_curl_class_entry; +extern zend_function_entry php_http_client_curl_method_entry[]; + +zend_object_value php_http_client_curl_object_new(zend_class_entry *ce TSRMLS_DC); +zend_object_value php_http_client_curl_object_new_ex(zend_class_entry *ce, php_http_client_t *r, php_http_client_object_t **ptr TSRMLS_DC); + + +#endif /* PHP_HTTP_HAVE_CURL */ +#endif /* PHP_HTTP_CLIENT_CURL_H */ + + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/php_http_client_datashare.c b/php_http_client_datashare.c new file mode 100644 index 0000000..6cbef6b --- /dev/null +++ b/php_http_client_datashare.c @@ -0,0 +1,327 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" +#include "php_http_client_datashare.h" + +static int php_http_client_datashare_compare_handles(void *h1, void *h2); + +PHP_HTTP_API php_http_client_datashare_t *php_http_client_datashare_init(php_http_client_datashare_t *h, php_http_client_datashare_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC) +{ + php_http_client_datashare_t *free_h = NULL; + + if (!h) { + free_h = h = emalloc(sizeof(*h)); + } + memset(h, sizeof(*h), 0); + + zend_llist_init(&h->clients, sizeof(zval *), ZVAL_PTR_DTOR, 0); + h->ops = ops; + h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, NULL, NULL); + TSRMLS_SET_CTX(h->ts); + + if (h->ops->init) { + if (!(h = h->ops->init(h, init_arg))) { + if (free_h) { + efree(free_h); + } + } + } + + return h; +} + +PHP_HTTP_API php_http_client_datashare_t *php_http_client_datashare_copy(php_http_client_datashare_t *from, php_http_client_datashare_t *to) +{ + if (from->ops->copy) { + return from->ops->copy(from, to); + } + + return NULL; +} + +PHP_HTTP_API void php_http_client_datashare_dtor(php_http_client_datashare_t *h) +{ + if (h->ops->dtor) { + h->ops->dtor(h); + } + zend_llist_destroy(&h->clients); + php_http_resource_factory_free(&h->rf); +} + +PHP_HTTP_API void php_http_client_datashare_free(php_http_client_datashare_t **h) +{ + php_http_client_datashare_dtor(*h); + efree(*h); + *h = NULL; +} + +PHP_HTTP_API STATUS php_http_client_datashare_attach(php_http_client_datashare_t *h, zval *client) +{ + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (h->ops->attach) { + php_http_client_object_t *obj = zend_object_store_get_object(client TSRMLS_CC); + + if (SUCCESS == h->ops->attach(h, obj->client)) { + Z_ADDREF_P(client); + zend_llist_add_element(&h->clients, &client); + return SUCCESS; + } + } + + return FAILURE; +} + +static int php_http_client_datashare_compare_handles(void *h1, void *h2) +{ + return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2)); +} + +PHP_HTTP_API STATUS php_http_client_datashare_detach(php_http_client_datashare_t *h, zval *client) +{ + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (h->ops->detach) { + php_http_client_object_t *obj = zend_object_store_get_object(client TSRMLS_CC); + + if (SUCCESS == h->ops->detach(h, obj->client)) { + zend_llist_del_element(&h->clients, client, php_http_client_datashare_compare_handles); + return SUCCESS; + } + } + return FAILURE; +} + +PHP_HTTP_API STATUS php_http_client_datashare_setopt(php_http_client_datashare_t *h, php_http_client_datashare_setopt_opt_t opt, void *arg) +{ + if (h->ops->setopt) { + return h->ops->setopt(h, opt, arg); + } + return FAILURE; +} + +static void detach(void *r, void *h TSRMLS_DC) +{ + ((php_http_client_datashare_t *) h)->ops->detach(h, ((php_http_client_object_t *) zend_object_store_get_object(*((zval **) r) TSRMLS_CC))->client); +} + +PHP_HTTP_API void php_http_client_datashare_reset(php_http_client_datashare_t *h) +{ + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (h->ops->reset) { + h->ops->reset(h); + } else if (h->ops->detach) { + zend_llist_apply_with_argument(&h->clients, detach, h TSRMLS_CC); + } + + zend_llist_clean(&h->clients); +} + +#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientDataShare, method, 0, req_args) +#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientDataShare, method, 0) +#define PHP_HTTP_RSHARE_ME(method, visibility) PHP_ME(HttpClientDataShare, method, PHP_HTTP_ARGS(HttpClientDataShare, method), visibility) + +PHP_HTTP_EMPTY_ARGS(__destruct); +PHP_HTTP_EMPTY_ARGS(reset); +PHP_HTTP_EMPTY_ARGS(count); + +PHP_HTTP_BEGIN_ARGS(attach, 1) + PHP_HTTP_ARG_OBJ(http\\Client, client, 0) +PHP_HTTP_END_ARGS; +PHP_HTTP_BEGIN_ARGS(detach, 1) + PHP_HTTP_ARG_OBJ(http\\Client, client, 0) +PHP_HTTP_END_ARGS; + +static void php_http_client_datashare_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC); + +zend_class_entry *php_http_client_datashare_class_entry; +zend_function_entry php_http_client_datashare_method_entry[] = { + PHP_HTTP_RSHARE_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) + PHP_HTTP_RSHARE_ME(count, ZEND_ACC_PUBLIC) + PHP_HTTP_RSHARE_ME(attach, ZEND_ACC_PUBLIC) + PHP_HTTP_RSHARE_ME(detach, ZEND_ACC_PUBLIC) + PHP_HTTP_RSHARE_ME(reset, ZEND_ACC_PUBLIC) + EMPTY_FUNCTION_ENTRY +}; + +static zend_object_handlers php_http_client_datashare_object_handlers; + +zend_object_handlers *php_http_client_datashare_get_object_handlers(void) +{ + return &php_http_client_datashare_object_handlers; +} + +zend_object_value php_http_client_datashare_object_new(zend_class_entry *ce TSRMLS_DC) +{ + return php_http_client_datashare_object_new_ex(ce, NULL, NULL TSRMLS_CC); +} + +zend_object_value php_http_client_datashare_object_new_ex(zend_class_entry *ce, php_http_client_datashare_t *share, php_http_client_datashare_object_t **ptr TSRMLS_DC) +{ + zend_object_value ov; + php_http_client_datashare_object_t *o; + + o = ecalloc(1, sizeof(*o)); + zend_object_std_init((zend_object *) o, ce TSRMLS_CC); + object_properties_init((zend_object *) o, ce); + + if (share) { + o->share = share; + } else { + o->share = php_http_client_datashare_init(NULL, NULL, NULL, NULL TSRMLS_CC); + } + + if (ptr) { + *ptr = o; + } + + ov.handle = zend_objects_store_put(o, NULL, php_http_client_datashare_object_free, NULL TSRMLS_CC); + ov.handlers = &php_http_client_datashare_object_handlers; + + return ov; +} + +void php_http_client_datashare_object_free(void *object TSRMLS_DC) +{ + php_http_client_datashare_object_t *o = (php_http_client_datashare_object_t *) object; + + php_http_client_datashare_free(&o->share); + zend_object_std_dtor((zend_object *) o TSRMLS_CC); + efree(o); +} + +static void php_http_client_datashare_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC) +{ + zend_property_info *pi; + + if ((pi = zend_get_property_info(php_http_client_datashare_class_entry, member, 1 TSRMLS_CC))) { + zend_bool enable = i_zend_is_true(value); + php_http_client_datashare_setopt_opt_t opt; + php_http_client_datashare_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); + + if (!strcmp(pi->name, "cookie")) { + opt = PHP_HTTP_CLIENT_DATASHARE_OPT_COOKIES; + } else if (!strcmp(pi->name, "dns")) { + opt = PHP_HTTP_CLIENT_DATASHARE_OPT_RESOLVER; + } else if (!strcmp(pi->name, "ssl")) { + opt = PHP_HTTP_CLIENT_DATASHARE_OPT_SSLSESSIONS; + } else { + return; + } + + if (SUCCESS != php_http_client_datashare_setopt(obj->share, opt, &enable)) { + return; + } + } + + zend_get_std_object_handlers()->write_property(object, member, value, literal_key TSRMLS_CC); +} + +static zval **php_http_client_datashare_object_get_prop_ptr(zval *object, zval *member, const zend_literal *literal_key TSRMLS_DC) +{ + zend_property_info *pi; + + if ((pi = zend_get_property_info(php_http_client_datashare_class_entry, member, 1 TSRMLS_CC))) { + return &php_http_property_proxy_init(NULL, object, member, NULL TSRMLS_CC)->myself; + } + + return zend_get_std_object_handlers()->get_property_ptr_ptr(object, member, literal_key TSRMLS_CC); +} + + +PHP_METHOD(HttpClientDataShare, __destruct) +{ + php_http_client_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + /* FIXME: move to php_http_client_datashare_dtor */ + if (SUCCESS == zend_parse_parameters_none()) { + ; /* we always want to clean up */ + } + + php_http_client_datashare_reset(obj->share); +} + +PHP_METHOD(HttpClientDataShare, count) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + RETURN_LONG(zend_llist_count(&obj->share->clients)); + } + RETURN_FALSE; +} + + +PHP_METHOD(HttpClientDataShare, attach) +{ + zval *client; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &client, php_http_client_class_entry)) { + php_http_client_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + RETURN_SUCCESS(php_http_client_datashare_attach(obj->share, client)); + } + RETURN_FALSE; + +} + +PHP_METHOD(HttpClientDataShare, detach) +{ + zval *client; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &client, php_http_client_class_entry)) { + php_http_client_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + RETURN_SUCCESS(php_http_client_datashare_detach(obj->share, client)); + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientDataShare, reset) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + php_http_client_datashare_reset(obj->share); + RETURN_TRUE; + } + RETURN_FALSE; +} + +PHP_MINIT_FUNCTION(http_client_datashare) +{ + PHP_HTTP_REGISTER_CLASS(http\\Client\\DataShare, AbstractDataShare, http_client_datashare, php_http_object_class_entry, 0); + php_http_client_datashare_class_entry->create_object = php_http_object_new;//php_http_client_datashare_object_new; + memcpy(&php_http_client_datashare_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + php_http_client_datashare_object_handlers.clone_obj = NULL; + php_http_client_datashare_object_handlers.write_property = php_http_client_datashare_object_write_prop; + php_http_client_datashare_object_handlers.get_property_ptr_ptr = php_http_client_datashare_object_get_prop_ptr; + + zend_class_implements(php_http_client_datashare_class_entry TSRMLS_CC, 1, spl_ce_Countable); + + zend_declare_property_bool(php_http_client_datashare_class_entry, ZEND_STRL("cookie"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); + zend_declare_property_bool(php_http_client_datashare_class_entry, ZEND_STRL("dns"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); + zend_declare_property_bool(php_http_client_datashare_class_entry, ZEND_STRL("ssl"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); + + return SUCCESS; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_datashare.h b/php_http_client_datashare.h new file mode 100644 index 0000000..a68c00a --- /dev/null +++ b/php_http_client_datashare.h @@ -0,0 +1,92 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_HTTP_CLIENT_DATASHARE_H +#define PHP_HTTP_CLIENT_DATASHARE_H + +typedef enum php_http_client_datashare_setopt_opt { + PHP_HTTP_CLIENT_DATASHARE_OPT_COOKIES, + PHP_HTTP_CLIENT_DATASHARE_OPT_RESOLVER, + PHP_HTTP_CLIENT_DATASHARE_OPT_SSLSESSIONS, +} php_http_client_datashare_setopt_opt_t; + +typedef struct php_http_client_datashare *(*php_http_client_datashare_init_func_t)(struct php_http_client_datashare *h, void *init_arg); +typedef struct php_http_client_datashare *(*php_http_client_datashare_copy_func_t)(struct php_http_client_datashare *from, struct php_http_client_datashare *to); +typedef void (*php_http_client_datashare_dtor_func_t)(struct php_http_client_datashare *h); +typedef void (*php_http_client_datashare_reset_func_t)(struct php_http_client_datashare *h); +typedef STATUS (*php_http_client_datashare_attach_func_t)(struct php_http_client_datashare *h, php_http_client_t *client); +typedef STATUS (*php_http_client_datashare_detach_func_t)(struct php_http_client_datashare *h, php_http_client_t *client); +typedef STATUS (*php_http_client_datashare_setopt_func_t)(struct php_http_client_datashare *h, php_http_client_datashare_setopt_opt_t opt, void *arg); + +typedef struct php_http_client_datashare_ops { + php_http_resource_factory_ops_t *rsrc; + php_http_client_datashare_init_func_t init; + php_http_client_datashare_copy_func_t copy; + php_http_client_datashare_dtor_func_t dtor; + php_http_client_datashare_reset_func_t reset; + php_http_client_datashare_attach_func_t attach; + php_http_client_datashare_detach_func_t detach; + php_http_client_datashare_setopt_func_t setopt; +} php_http_client_datashare_ops_t; + +typedef struct php_http_client_datashare { + void *ctx; + php_http_resource_factory_t *rf; + php_http_client_datashare_ops_t *ops; + zend_llist clients; +#ifdef ZTS + void ***ts; +#endif +} php_http_client_datashare_t; + +extern PHP_MINIT_FUNCTION(http_client_datashare); + +PHP_HTTP_API php_http_client_datashare_t *php_http_client_datashare_init(php_http_client_datashare_t *h, php_http_client_datashare_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC); +PHP_HTTP_API php_http_client_datashare_t *php_http_client_datashare_copy(php_http_client_datashare_t *from, php_http_client_datashare_t *to); +PHP_HTTP_API void php_http_client_datashare_dtor(php_http_client_datashare_t *h); +PHP_HTTP_API void php_http_client_datashare_free(php_http_client_datashare_t **h); +PHP_HTTP_API STATUS php_http_client_datashare_attach(php_http_client_datashare_t *h, zval *client); +PHP_HTTP_API STATUS php_http_client_datashare_detach(php_http_client_datashare_t *h, zval *client); +PHP_HTTP_API STATUS php_http_client_datashare_setopt(php_http_client_datashare_t *h, php_http_client_datashare_setopt_opt_t opt, void *arg); +PHP_HTTP_API void php_http_client_datashare_reset(php_http_client_datashare_t *h); + +typedef struct php_http_client_datashare_object { + zend_object zo; + php_http_client_datashare_t *share; +} php_http_client_datashare_object_t; + +extern zend_class_entry *php_http_client_datashare_class_entry; +extern zend_function_entry php_http_client_datashare_method_entry[]; + +extern zend_object_value php_http_client_datashare_object_new(zend_class_entry *ce TSRMLS_DC); +extern zend_object_value php_http_client_datashare_object_new_ex(zend_class_entry *ce, php_http_client_datashare_t *share, php_http_client_datashare_object_t **ptr TSRMLS_DC); +extern void php_http_client_datashare_object_free(void *object TSRMLS_DC); + +extern zend_object_handlers *php_http_client_datashare_get_object_handlers(void); + +PHP_METHOD(HttpClientDataShare, __destruct); +PHP_METHOD(HttpClientDataShare, count); +PHP_METHOD(HttpClientDataShare, attach); +PHP_METHOD(HttpClientDataShare, detach); +PHP_METHOD(HttpClientDataShare, reset); + +#endif /* PHP_HTTP_CLIENT_DATASHARE_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_datashare_curl.c b/php_http_client_datashare_curl.c new file mode 100644 index 0000000..8c62a02 --- /dev/null +++ b/php_http_client_datashare_curl.c @@ -0,0 +1,216 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" + +#if PHP_HTTP_HAVE_CURL + +typedef struct php_http_client_datashare_curl { + CURLSH *handle; +} php_http_client_datashare_curl_t; + + +static void *php_http_curlsh_ctor(void *opaque TSRMLS_DC) +{ + return curl_share_init(); +} + +static void php_http_curlsh_dtor(void *opaque, void *handle TSRMLS_DC) +{ + curl_share_cleanup(handle); +} + + +/* datashare handler ops */ + +static php_http_client_datashare_t *php_http_client_datashare_curl_init(php_http_client_datashare_t *h, void *handle) +{ + php_http_client_datashare_curl_t *curl; + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_DATASHARE, "could not initialize curl share handle"); + return NULL; + } + + curl = ecalloc(1, sizeof(*curl)); + curl->handle = handle; + h->ctx = curl; + + return h; +} + +static void php_http_client_datashare_curl_dtor(php_http_client_datashare_t *h) +{ + php_http_client_datashare_curl_t *curl = h->ctx; + TSRMLS_FETCH_FROM_CTX(h->ts); + + php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC); + + efree(curl); + h->ctx = NULL; +} + +static STATUS php_http_client_datashare_curl_attach(php_http_client_datashare_t *h, php_http_client_t *r) +{ + CURLcode rc; + php_http_client_datashare_curl_t *curl = h->ctx; + php_http_client_curl_t *recurl = r->ctx; + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, curl->handle))) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_DATASHARE, "Could not attach request to the datashare: %s", curl_easy_strerror(rc)); + return FAILURE; + } + return SUCCESS; +} + +static STATUS php_http_client_datashare_curl_detach(php_http_client_datashare_t *h, php_http_client_t *r) +{ + CURLcode rc; + php_http_client_curl_t *recurl = r->ctx; + TSRMLS_FETCH_FROM_CTX(h->ts); + + + if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, NULL))) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_DATASHARE, "Could not detach request from the datashare: %s", curl_share_strerror(rc)); + return FAILURE; + } + return SUCCESS; +} + +static STATUS php_http_client_datashare_curl_setopt(php_http_client_datashare_t *h, php_http_client_datashare_setopt_opt_t opt, void *arg) +{ + CURLSHcode rc; + php_http_client_datashare_curl_t *curl = h->ctx; + + switch (opt) { + case PHP_HTTP_CLIENT_DATASHARE_OPT_COOKIES: + if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE))) { + TSRMLS_FETCH_FROM_CTX(h->ts); + + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_DATASHARE, "Could not %s sharing of cookie data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc)); + return FAILURE; + } + break; + + case PHP_HTTP_CLIENT_DATASHARE_OPT_RESOLVER: + if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_DNS))) { + TSRMLS_FETCH_FROM_CTX(h->ts); + + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_DATASHARE, "Could not %s sharing of resolver data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc)); + return FAILURE; + } + break; + +#if PHP_HTTP_CURL_VERSION(7,23,0) + case PHP_HTTP_CLIENT_DATASHARE_OPT_SSLSESSIONS: + if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_SSL_SESSION))) { + TSRMLS_FETCH_FROM_CTX(h->ts); + + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_DATASHARE, "Could not %s sharing of SSL session data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc)); + return FAILURE; + } + break; +#endif + + default: + return FAILURE; + } + + return SUCCESS; +} + +static php_http_resource_factory_ops_t php_http_curlsh_resource_factory_ops = { + php_http_curlsh_ctor, + NULL, + php_http_curlsh_dtor +}; + +static php_http_client_datashare_ops_t php_http_client_datashare_curl_ops = { + &php_http_curlsh_resource_factory_ops, + php_http_client_datashare_curl_init, + NULL /* copy */, + php_http_client_datashare_curl_dtor, + NULL /*reset */, + php_http_client_datashare_curl_attach, + php_http_client_datashare_curl_detach, + php_http_client_datashare_curl_setopt, +}; + +PHP_HTTP_API php_http_client_datashare_ops_t *php_http_client_datashare_curl_get_ops(void) +{ + return &php_http_client_datashare_curl_ops; +} + +#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientDataShare, method, 0, req_args) +#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientDataShare, method, 0) +#define PHP_HTTP_RSHARE_ME(method, visibility) PHP_ME(HttpClientDataShare, method, PHP_HTTP_ARGS(HttpClientDataShare, method), visibility) + +zend_class_entry *php_http_client_datashare_curl_class_entry; +zend_function_entry php_http_client_datashare_curl_method_entry[] = { + EMPTY_FUNCTION_ENTRY +}; + +zend_object_value php_http_client_datashare_curl_object_new(zend_class_entry *ce TSRMLS_DC) +{ + return php_http_client_datashare_curl_object_new_ex(ce, NULL, NULL TSRMLS_CC); +} + +zend_object_value php_http_client_datashare_curl_object_new_ex(zend_class_entry *ce, php_http_client_datashare_t *share, php_http_client_datashare_object_t **ptr TSRMLS_DC) +{ + zend_object_value ov; + php_http_client_datashare_object_t *o; + + o = ecalloc(1, sizeof(*o)); + zend_object_std_init((zend_object *) o, ce TSRMLS_CC); + object_properties_init((zend_object *) o, ce); + + if (share) { + o->share = share; + } else { + o->share = php_http_client_datashare_init(NULL, &php_http_client_datashare_curl_ops, NULL, NULL TSRMLS_CC); + } + + if (ptr) { + *ptr = o; + } + + ov.handle = zend_objects_store_put(o, NULL, php_http_client_datashare_object_free, NULL TSRMLS_CC); + ov.handlers = php_http_client_datashare_get_object_handlers(); + + return ov; +} + + +PHP_MINIT_FUNCTION(http_client_datashare_curl) +{ + if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_client_datashare.curl"), &php_http_curlsh_resource_factory_ops, NULL, NULL)) { + return FAILURE; + } + + PHP_HTTP_REGISTER_CLASS(http\\Client\\DataShare, CURL, http_client_datashare_curl, php_http_client_datashare_class_entry, 0); + php_http_client_datashare_curl_class_entry->create_object = php_http_client_datashare_curl_object_new; + return SUCCESS; +} + +#endif /* PHP_HTTP_HAVE_CURL */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_datashare_curl.h b/php_http_client_datashare_curl.h new file mode 100644 index 0000000..74859d0 --- /dev/null +++ b/php_http_client_datashare_curl.h @@ -0,0 +1,37 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_HTTP_CLIENT_DATASHARE_CURL_H +#define PHP_HTTP_CLIENT_DATASHARE_CURL_H + +#if PHP_HTTP_HAVE_CURL + +PHP_HTTP_API php_http_client_datashare_ops_t *php_http_client_datashare_curl_get_ops(void); + +extern zend_object_value php_http_client_datashare_curl_object_new(zend_class_entry *ce TSRMLS_DC); +extern zend_object_value php_http_client_datashare_curl_object_new_ex(zend_class_entry *ce, php_http_client_datashare_t *share, php_http_client_datashare_object_t **ptr TSRMLS_DC); + +PHP_MINIT_FUNCTION(http_client_datashare_curl); + +#endif /* PHP_HTTP_HAVE_CURL */ + +#endif /* PHP_HTTP_CLIENT_DATASHARE_CURL_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_interface.c b/php_http_client_interface.c new file mode 100644 index 0000000..6912508 --- /dev/null +++ b/php_http_client_interface.c @@ -0,0 +1,46 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" +#include "php_http_client.h" + +#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClient, method, 0, req_args) +#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClient, method, 0) +#define PHP_HTTP_CLIENT_ME(method, visibility) PHP_ME(HttpClient, method, PHP_HTTP_ARGS(HttpClient, method), visibility) + +PHP_HTTP_BEGIN_ARGS(send, 1) + PHP_HTTP_ARG_VAL(request, 0) +PHP_HTTP_END_ARGS; + +zend_class_entry *php_http_client_interface_class_entry; + +zend_function_entry php_http_client_interface_method_entry[] = { + PHP_HTTP_CLIENT_ME(send, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT) + {NULL, NULL, NULL} +}; + +PHP_MINIT_FUNCTION(http_client_interface) +{ + PHP_HTTP_REGISTER_INTERFACE(http, Client, http_client_interface, ZEND_ACC_INTERFACE); + + return SUCCESS; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_interface.h b/php_http_client_interface.h new file mode 100644 index 0000000..797945a --- /dev/null +++ b/php_http_client_interface.h @@ -0,0 +1,30 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_HTTP_CLIENT_H + +extern zend_class_entry *php_http_client_interface_class_entry; +extern zend_function_entry php_http_client_interface_method_entry[]; + +PHP_MINIT_FUNCTION(http_client_interface); + +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_pool.c b/php_http_client_pool.c new file mode 100644 index 0000000..f0b6ca6 --- /dev/null +++ b/php_http_client_pool.c @@ -0,0 +1,577 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" + +PHP_HTTP_API php_http_client_pool_t *php_http_client_pool_init(php_http_client_pool_t *h, php_http_client_pool_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC) +{ + php_http_client_pool_t *free_h = NULL; + + if (!h) { + free_h = h = emalloc(sizeof(*h)); + } + memset(h, 0, sizeof(*h)); + + h->ops = ops; + h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, NULL, NULL); + zend_llist_init(&h->clients.attached, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0); + zend_llist_init(&h->clients.finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0); + TSRMLS_SET_CTX(h->ts); + + if (h->ops->init) { + if (!(h = h->ops->init(h, init_arg))) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_POOL, "Could not initialize request pool"); + if (free_h) { + efree(h); + } + } + } + + return h; +} + +PHP_HTTP_API php_http_client_pool_t *php_http_client_pool_copy(php_http_client_pool_t *from, php_http_client_pool_t *to) +{ + if (from->ops->copy) { + return from->ops->copy(from, to); + } + + return NULL; +} + +PHP_HTTP_API void php_http_client_pool_dtor(php_http_client_pool_t *h) +{ + if (h->ops->dtor) { + h->ops->dtor(h); + } + + zend_llist_clean(&h->clients.finished); + zend_llist_clean(&h->clients.attached); + + php_http_resource_factory_free(&h->rf); +} + +PHP_HTTP_API void php_http_client_pool_free(php_http_client_pool_t **h) { + if (*h) { + php_http_client_pool_dtor(*h); + efree(*h); + *h = NULL; + } +} + +PHP_HTTP_API STATUS php_http_client_pool_attach(php_http_client_pool_t *h, zval *client) +{ + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (h->ops->attach) { + char *url = NULL; + char *m = NULL; + php_http_message_body_t *body = NULL; + php_http_client_object_t *obj = zend_object_store_get_object(client TSRMLS_CC); + + if (SUCCESS != php_http_client_object_requesthandler(obj, client, &m, &url, &body TSRMLS_CC)) { + return FAILURE; + } + if (SUCCESS == h->ops->attach(h, obj->client, m, url, body)) { + STR_FREE(url); + Z_ADDREF_P(client); + zend_llist_add_element(&h->clients.attached, &client); + return SUCCESS; + } + STR_FREE(url); + } + + return FAILURE; +} + +static int php_http_client_pool_compare_handles(void *h1, void *h2) +{ + return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2)); +} + + +PHP_HTTP_API STATUS php_http_client_pool_detach(php_http_client_pool_t *h, zval *client) +{ + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (h->ops->detach) { + php_http_client_object_t *obj = zend_object_store_get_object(client TSRMLS_CC); + + if (SUCCESS == h->ops->detach(h, obj->client)) { + zend_llist_del_element(&h->clients.finished, client, php_http_client_pool_compare_handles); + zend_llist_del_element(&h->clients.attached, client, php_http_client_pool_compare_handles); + return SUCCESS; + } + } + + return FAILURE; +} + +PHP_HTTP_API STATUS php_http_client_pool_wait(php_http_client_pool_t *h, struct timeval *custom_timeout) +{ + if (h->ops->wait) { + return h->ops->wait(h, custom_timeout); + } + + return FAILURE; +} + +PHP_HTTP_API int php_http_client_pool_once(php_http_client_pool_t *h) +{ + if (h->ops->once) { + return h->ops->once(h); + } + + return FAILURE; +} + +PHP_HTTP_API STATUS php_http_client_pool_exec(php_http_client_pool_t *h) +{ + if (h->ops->exec) { + return h->ops->exec(h); + } + + return FAILURE; +} + +static void detach(void *r, void *h TSRMLS_DC) +{ + ((php_http_client_pool_t *) h)->ops->detach(h, ((php_http_client_object_t *) zend_object_store_get_object(*((zval **) r) TSRMLS_CC))->client); +} + +PHP_HTTP_API void php_http_client_pool_reset(php_http_client_pool_t *h) +{ + if (h->ops->reset) { + h->ops->reset(h); + } else if (h->ops->detach) { + TSRMLS_FETCH_FROM_CTX(h->ts); + + zend_llist_apply_with_argument(&h->clients.attached, detach, h TSRMLS_CC); + } + + zend_llist_clean(&h->clients.attached); + zend_llist_clean(&h->clients.finished); +} + +PHP_HTTP_API STATUS php_http_client_pool_setopt(php_http_client_pool_t *h, php_http_client_pool_setopt_opt_t opt, void *arg) +{ + if (h->ops->setopt) { + return h->ops->setopt(h, opt, arg); + } + + return FAILURE; +} + +PHP_HTTP_API void php_http_client_pool_requests(php_http_client_pool_t *h, zval ***attached, zval ***finished) +{ + zval **handle; + int i, count; + + if (attached) { + if ((count = zend_llist_count(&h->clients.attached))) { + *attached = ecalloc(count + 1 /* terminating NULL */, sizeof(zval *)); + + for (i = 0, handle = zend_llist_get_first(&h->clients.attached); handle; handle = zend_llist_get_next(&h->clients.attached)) { + Z_ADDREF_PP(handle); + (*attached)[i++] = *handle; + } + } else { + *attached = NULL; + } + } + + if (finished) { + if ((count = zend_llist_count(&h->clients.finished))) { + *finished = ecalloc(count + 1 /* terminating NULL */, sizeof(zval *)); + + for (i = 0, handle = zend_llist_get_first(&h->clients.finished); handle; handle = zend_llist_get_next(&h->clients.finished)) { + Z_ADDREF_PP(handle); + (*finished)[i++] = *handle; + } + } else { + *finished = NULL; + } + } +} + +/*#*/ + +#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientPool, method, 0, req_args) +#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientPool, method, 0) +#define PHP_HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpClientPool, method, PHP_HTTP_ARGS(HttpClientPool, method), visibility) + +PHP_HTTP_EMPTY_ARGS(__destruct); +PHP_HTTP_EMPTY_ARGS(reset); + +PHP_HTTP_BEGIN_ARGS(attach, 1) + PHP_HTTP_ARG_OBJ(http\\Client, client, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_BEGIN_ARGS(detach, 1) + PHP_HTTP_ARG_OBJ(http\\Client, client, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(send); +PHP_HTTP_EMPTY_ARGS(once); +PHP_HTTP_BEGIN_ARGS(wait, 0) + PHP_HTTP_ARG_VAL(timeout, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(valid); +PHP_HTTP_EMPTY_ARGS(current); +PHP_HTTP_EMPTY_ARGS(key); +PHP_HTTP_EMPTY_ARGS(next); +PHP_HTTP_EMPTY_ARGS(rewind); + +PHP_HTTP_EMPTY_ARGS(count); + +PHP_HTTP_EMPTY_ARGS(getAttachedRequests); +PHP_HTTP_EMPTY_ARGS(getFinishedRequests); + +PHP_HTTP_BEGIN_ARGS(enablePipelining, 0) + PHP_HTTP_ARG_VAL(enable, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_BEGIN_ARGS(enableEvents, 0) + PHP_HTTP_ARG_VAL(enable, 0) +PHP_HTTP_END_ARGS; + +zend_class_entry *php_http_client_pool_class_entry; +zend_function_entry php_http_client_pool_method_entry[] = { + PHP_HTTP_REQPOOL_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) + PHP_HTTP_REQPOOL_ME(attach, ZEND_ACC_PUBLIC) + PHP_HTTP_REQPOOL_ME(detach, ZEND_ACC_PUBLIC) + PHP_HTTP_REQPOOL_ME(send, ZEND_ACC_PUBLIC) + PHP_HTTP_REQPOOL_ME(reset, ZEND_ACC_PUBLIC) + + PHP_HTTP_REQPOOL_ME(once, ZEND_ACC_PROTECTED) + PHP_HTTP_REQPOOL_ME(wait, ZEND_ACC_PROTECTED) + + /* implements Iterator */ + PHP_HTTP_REQPOOL_ME(valid, ZEND_ACC_PUBLIC) + PHP_HTTP_REQPOOL_ME(current, ZEND_ACC_PUBLIC) + PHP_HTTP_REQPOOL_ME(key, ZEND_ACC_PUBLIC) + PHP_HTTP_REQPOOL_ME(next, ZEND_ACC_PUBLIC) + PHP_HTTP_REQPOOL_ME(rewind, ZEND_ACC_PUBLIC) + + /* implmenents Countable */ + PHP_HTTP_REQPOOL_ME(count, ZEND_ACC_PUBLIC) + + PHP_HTTP_REQPOOL_ME(getAttachedRequests, ZEND_ACC_PUBLIC) + PHP_HTTP_REQPOOL_ME(getFinishedRequests, ZEND_ACC_PUBLIC) + + PHP_HTTP_REQPOOL_ME(enablePipelining, ZEND_ACC_PUBLIC) + PHP_HTTP_REQPOOL_ME(enableEvents, ZEND_ACC_PUBLIC) + + EMPTY_FUNCTION_ENTRY +}; + +static zend_object_handlers php_http_client_pool_object_handlers; + +extern zend_object_handlers *php_http_client_pool_get_object_handlers(void) +{ + return &php_http_client_pool_object_handlers; +} + +zend_object_value php_http_client_pool_object_new(zend_class_entry *ce TSRMLS_DC) +{ + return php_http_client_pool_object_new_ex(ce, NULL, NULL TSRMLS_CC); +} + +zend_object_value php_http_client_pool_object_new_ex(zend_class_entry *ce, php_http_client_pool_t *p, php_http_client_pool_object_t **ptr TSRMLS_DC) +{ + zend_object_value ov; + php_http_client_pool_object_t *o; + + o = ecalloc(1, sizeof(php_http_client_pool_object_t)); + zend_object_std_init((zend_object *) o, ce TSRMLS_CC); + object_properties_init((zend_object *) o, ce); + + if (!(o->pool = p)) { + o->pool = php_http_client_pool_init(NULL, NULL, NULL, NULL TSRMLS_CC); + } + + if (ptr) { + *ptr = o; + } + + ov.handle = zend_objects_store_put(o, NULL, php_http_client_pool_object_free, NULL TSRMLS_CC); + ov.handlers = &php_http_client_pool_object_handlers; + + return ov; +} + +void php_http_client_pool_object_free(void *object TSRMLS_DC) +{ + php_http_client_pool_object_t *o = (php_http_client_pool_object_t *) object; + + php_http_client_pool_free(&o->pool); + zend_object_std_dtor((zend_object *) o TSRMLS_CC); + efree(o); +} + +static void php_http_client_pool_object_llist2array(zval **req, zval *array TSRMLS_DC) +{ + Z_ADDREF_P(*req); + add_next_index_zval(array, *req); +} + +PHP_METHOD(HttpClientPool, __destruct) +{ + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (SUCCESS != zend_parse_parameters_none()) { + ; /* we always want to clean up */ + } + /* FIXME: move to php_http_client_pool_dtor */ + php_http_client_pool_reset(obj->pool); +} + +PHP_METHOD(HttpClientPool, reset) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + obj->iterator.pos = 0; + php_http_client_pool_reset(obj->pool); + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClientPool, attach) +{ + with_error_handling(EH_THROW, php_http_exception_class_entry) { + zval *request; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_class_entry)) { + with_error_handling(EH_THROW, php_http_exception_class_entry) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->iterator.pos > 0 && obj->iterator.pos < zend_llist_count(&obj->pool->clients.attached)) { + php_http_error(HE_THROW, PHP_HTTP_E_CLIENT_POOL, "Cannot attach to the HttpClientPool while the iterator is active"); + } else { + php_http_client_pool_attach(obj->pool, request); + } + } end_error_handling(); + } + } end_error_handling(); + + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClientPool, detach) +{ + RETVAL_FALSE; + + with_error_handling(EH_THROW, php_http_exception_class_entry) { + zval *request; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_class_entry)) { + with_error_handling(EH_THROW, php_http_exception_class_entry) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + obj->iterator.pos = -1; + php_http_client_pool_detach(obj->pool, request); + } end_error_handling(); + } + } end_error_handling(); + + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClientPool, send) +{ + RETVAL_FALSE; + + with_error_handling(EH_THROW, php_http_exception_class_entry) { + if (SUCCESS == zend_parse_parameters_none()) { + with_error_handling(EH_THROW, php_http_exception_class_entry) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + php_http_client_pool_exec(obj->pool); + } end_error_handling(); + } + } end_error_handling(); + + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClientPool, once) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (0 < php_http_client_pool_once(obj->pool)) { + RETURN_TRUE; + } + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientPool, wait) +{ + double timeout = 0; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|d", &timeout)) { + struct timeval timeout_val; + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + timeout_val.tv_sec = (time_t) timeout; + timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC; + + RETURN_SUCCESS(php_http_client_pool_wait(obj->pool, timeout > 0 ? &timeout_val : NULL)); + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientPool, valid) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + RETURN_BOOL(obj->iterator.pos >= 0 && obj->iterator.pos < zend_llist_count(&obj->pool->clients.attached)); + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientPool, current) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->iterator.pos < zend_llist_count(&obj->pool->clients.attached)) { + long pos = 0; + zval **current = NULL; + zend_llist_position lpos; + + for ( current = zend_llist_get_first_ex(&obj->pool->clients.attached, &lpos); + current && obj->iterator.pos != pos++; + current = zend_llist_get_next_ex(&obj->pool->clients.attached, &lpos)); + if (current) { + RETURN_OBJECT(*current, 1); + } + } + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientPool, key) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + RETURN_LONG(obj->iterator.pos); + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientPool, next) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + ++obj->iterator.pos; + } +} + +PHP_METHOD(HttpClientPool, rewind) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + obj->iterator.pos = 0; + } +} + +PHP_METHOD(HttpClientPool, count) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + RETURN_LONG((long) zend_llist_count(&obj->pool->clients.attached)); + } +} + +PHP_METHOD(HttpClientPool, getAttachedRequests) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + array_init(return_value); + zend_llist_apply_with_argument(&obj->pool->clients.attached, + (llist_apply_with_arg_func_t) php_http_client_pool_object_llist2array, + return_value TSRMLS_CC); + return; + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientPool, getFinishedRequests) +{ + if (SUCCESS == zend_parse_parameters_none()) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + array_init(return_value); + zend_llist_apply_with_argument(&obj->pool->clients.finished, + (llist_apply_with_arg_func_t) php_http_client_pool_object_llist2array, + return_value TSRMLS_CC); + return; + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientPool, enablePipelining) +{ + zend_bool enable = 1; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + php_http_client_pool_setopt(obj->pool, PHP_HTTP_CLIENT_POOL_OPT_ENABLE_PIPELINING, &enable); + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClientPool, enableEvents) +{ + zend_bool enable = 1; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) { + php_http_client_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + php_http_client_pool_setopt(obj->pool, PHP_HTTP_CLIENT_POOL_OPT_USE_EVENTS, &enable); + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_MINIT_FUNCTION(http_client_pool) +{ + PHP_HTTP_REGISTER_CLASS(http\\Client\\Pool, AbstractPool, http_client_pool, php_http_object_class_entry, 0); + php_http_client_pool_class_entry->create_object = php_http_object_new; //php_http_client_pool_object_new; + memcpy(&php_http_client_pool_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + php_http_client_pool_object_handlers.clone_obj = NULL; + + zend_class_implements(php_http_client_pool_class_entry TSRMLS_CC, 2, spl_ce_Countable, zend_ce_iterator); + + return SUCCESS; +} + + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_pool.h b/php_http_client_pool.h new file mode 100644 index 0000000..937bacf --- /dev/null +++ b/php_http_client_pool.h @@ -0,0 +1,122 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_HTTP_CLIENT_POOL_H +#define PHP_HTTP_CLIENT_POOL_H + +typedef enum php_http_client_pool_setopt_opt { + PHP_HTTP_CLIENT_POOL_OPT_ENABLE_PIPELINING, + PHP_HTTP_CLIENT_POOL_OPT_USE_EVENTS, +} php_http_client_pool_setopt_opt_t; + +typedef struct php_http_client_pool *(*php_http_client_pool_init_func_t)(struct php_http_client_pool *p, void *arg); +typedef struct php_http_client_pool *(*php_http_client_pool_copy_func_t)(struct php_http_client_pool *from, struct php_http_client_pool *to); +typedef void (*php_http_client_pool_dtor_func_t)(struct php_http_client_pool *p); +typedef void (*php_http_client_pool_reset_func_t)(struct php_http_client_pool *p); +typedef STATUS (*php_http_client_pool_exec_func_t)(struct php_http_client_pool *p); +typedef STATUS (*php_http_client_pool_wait_func_t)(struct php_http_client_pool *p, struct timeval *custom_timeout); +typedef int (*php_http_client_pool_once_func_t)(struct php_http_client_pool *p); +typedef STATUS (*php_http_client_pool_attach_func_t)(struct php_http_client_pool *p, php_http_client_t *r, const char *m, const char *url, php_http_message_body_t *body); +typedef STATUS (*php_http_client_pool_detach_func_t)(struct php_http_client_pool *p, php_http_client_t *r); +typedef STATUS (*php_http_client_pool_setopt_func_t)(struct php_http_client_pool *p, php_http_client_pool_setopt_opt_t opt, void *arg); + +typedef struct php_http_client_pool_ops { + php_http_resource_factory_ops_t *rsrc; + php_http_client_pool_init_func_t init; + php_http_client_pool_copy_func_t copy; + php_http_client_pool_dtor_func_t dtor; + php_http_client_pool_reset_func_t reset; + php_http_client_pool_exec_func_t exec; + php_http_client_pool_wait_func_t wait; + php_http_client_pool_once_func_t once; + php_http_client_pool_attach_func_t attach; + php_http_client_pool_detach_func_t detach; + php_http_client_pool_setopt_func_t setopt; +} php_http_client_pool_ops_t; + +typedef struct php_http_client_pool { + void *ctx; + php_http_resource_factory_t *rf; + php_http_client_pool_ops_t *ops; + + struct { + zend_llist attached; + zend_llist finished; + } clients; + +#ifdef ZTS + void ***ts; +#endif +} php_http_client_pool_t; + +PHP_HTTP_API php_http_client_pool_t *php_http_client_pool_init(php_http_client_pool_t *pool, php_http_client_pool_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC); +PHP_HTTP_API php_http_client_pool_t *php_http_client_pool_copy(php_http_client_pool_t *from, php_http_client_pool_t *to); +PHP_HTTP_API void php_http_client_pool_dtor(php_http_client_pool_t *pool); +PHP_HTTP_API void php_http_client_pool_free(php_http_client_pool_t **pool); +PHP_HTTP_API void php_http_client_pool_reset(php_http_client_pool_t *pool); +PHP_HTTP_API STATUS php_http_client_pool_exec(php_http_client_pool_t *pool); +PHP_HTTP_API STATUS php_http_client_pool_wait(php_http_client_pool_t *pool, struct timeval *custom_timeout); +PHP_HTTP_API STATUS php_http_client_pool_once(php_http_client_pool_t *pool); +PHP_HTTP_API STATUS php_http_client_pool_attach(php_http_client_pool_t *pool, zval *request); +PHP_HTTP_API STATUS php_http_client_pool_detach(php_http_client_pool_t *pool, zval *request); +PHP_HTTP_API STATUS php_http_client_pool_setopt(php_http_client_pool_t *pool, php_http_client_pool_setopt_opt_t opt, void *arg); +PHP_HTTP_API void php_http_client_pool_requests(php_http_client_pool_t *h, zval ***attached, zval ***finished); + +typedef struct php_http_client_pool_object { + zend_object zo; + php_http_client_pool_t *pool; + zend_object_value factory; + struct { + long pos; + } iterator; +} php_http_client_pool_object_t; + +extern zend_class_entry *php_http_client_pool_class_entry; +extern zend_function_entry php_http_client_pool_method_entry[]; + +extern zend_object_value php_http_client_pool_object_new(zend_class_entry *ce TSRMLS_DC); +extern zend_object_value php_http_client_pool_object_new_ex(zend_class_entry *ce, php_http_client_pool_t *p, php_http_client_pool_object_t **ptr TSRMLS_DC); +extern void php_http_client_pool_object_free(void *object TSRMLS_DC); + +extern zend_object_handlers *php_http_client_pool_get_object_handlers(void); + +PHP_METHOD(HttpClientPool, __destruct); +PHP_METHOD(HttpClientPool, attach); +PHP_METHOD(HttpClientPool, detach); +PHP_METHOD(HttpClientPool, send); +PHP_METHOD(HttpClientPool, reset); +PHP_METHOD(HttpClientPool, once); +PHP_METHOD(HttpClientPool, wait); +PHP_METHOD(HttpClientPool, valid); +PHP_METHOD(HttpClientPool, current); +PHP_METHOD(HttpClientPool, key); +PHP_METHOD(HttpClientPool, next); +PHP_METHOD(HttpClientPool, rewind); +PHP_METHOD(HttpClientPool, count); +PHP_METHOD(HttpClientPool, getAttachedRequests); +PHP_METHOD(HttpClientPool, getFinishedRequests); +PHP_METHOD(HttpClientPool, enablePipelining); +PHP_METHOD(HttpClientPool, enableEvents); + +PHP_MINIT_FUNCTION(http_client_pool); + +#endif /* PHP_HTTP_CLIENT_POOL_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_pool_curl.c b/php_http_client_pool_curl.c new file mode 100644 index 0000000..26e4167 --- /dev/null +++ b/php_http_client_pool_curl.c @@ -0,0 +1,555 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" + +#if PHP_HTTP_HAVE_CURL + +#if PHP_HTTP_HAVE_EVENT +# include +#endif + +typedef struct php_http_client_pool_curl { + CURLM *handle; + + int unfinished; /* int because of curl_multi_perform() */ + +#if PHP_HTTP_HAVE_EVENT + struct event *timeout; + unsigned useevents:1; + unsigned runsocket:1; +#endif +} php_http_client_pool_curl_t; + +static void *php_http_curlm_ctor(void *opaque TSRMLS_DC) +{ + return curl_multi_init(); +} + +static void php_http_curlm_dtor(void *opaque, void *handle TSRMLS_DC) +{ + curl_multi_cleanup(handle); +} + + +static void php_http_client_pool_curl_responsehandler(php_http_client_pool_t *pool) +{ + int remaining = 0; + zval **requests; + php_http_client_pool_curl_t *curl = pool->ctx; + TSRMLS_FETCH_FROM_CTX(pool->ts); + + do { + CURLMsg *msg = curl_multi_info_read(curl->handle, &remaining); + + if (msg && CURLMSG_DONE == msg->msg) { + zval **request; + + if (CURLE_OK != msg->data.result) { + php_http_client_curl_storage_t *st = get_storage(msg->easy_handle); + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "%s; %s (%s)", curl_easy_strerror(msg->data.result), STR_PTR(st->errorbuffer), STR_PTR(st->url)); + } + + php_http_client_pool_requests(pool, &requests, NULL); + for (request = requests; *request; ++request) { + php_http_client_object_t *obj = zend_object_store_get_object(*request TSRMLS_CC); + + if (msg->easy_handle == ((php_http_client_curl_t *) (obj->client->ctx))->handle) { + Z_ADDREF_PP(request); + zend_llist_add_element(&pool->clients.finished, request); + php_http_client_object_responsehandler(obj, *request TSRMLS_CC); + } + + zval_ptr_dtor(request); + } + efree(requests); + } + } while (remaining); +} + + +#if PHP_HTTP_HAVE_EVENT + +typedef struct php_http_client_pool_event { + struct event evnt; + php_http_client_pool_t *pool; +} php_http_client_pool_event_t; + +static inline int etoca(short action) { + switch (action & (EV_READ|EV_WRITE)) { + case EV_READ: + return CURL_CSELECT_IN; + break; + case EV_WRITE: + return CURL_CSELECT_OUT; + break; + case EV_READ|EV_WRITE: + return CURL_CSELECT_IN|CURL_CSELECT_OUT; + break; + default: + return 0; + } +} + +static void php_http_client_pool_curl_timeout_callback(int socket, short action, void *event_data) +{ + php_http_client_pool_t *pool = event_data; + php_http_client_pool_curl_t *curl = pool->ctx; + +#if DBG_EVENTS + fprintf(stderr, "T"); +#endif + if (curl->useevents) { + CURLMcode rc; + TSRMLS_FETCH_FROM_CTX(pool->ts); + + while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished))); + + if (CURLM_OK != rc) { + php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc)); + } + + php_http_client_pool_curl_responsehandler(pool); + } +} + +static void php_http_client_pool_curl_event_callback(int socket, short action, void *event_data) +{ + php_http_client_pool_t *pool = event_data; + php_http_client_pool_curl_t *curl = pool->ctx; + +#if DBG_EVENTS + fprintf(stderr, "E"); +#endif + if (curl->useevents) { + CURLMcode rc = CURLE_OK; + TSRMLS_FETCH_FROM_CTX(pool->ts); + + while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished))); + + if (CURLM_OK != rc) { + php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc)); + } + + php_http_client_pool_curl_responsehandler(pool); + + /* remove timeout if there are no transfers left */ + if (!curl->unfinished && event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) { + event_del(curl->timeout); + } + } +} + +static int php_http_client_pool_curl_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data) +{ + php_http_client_pool_t *pool = socket_data; + php_http_client_pool_curl_t *curl = pool->ctx; + +#if DBG_EVENTS + fprintf(stderr, "S"); +#endif + if (curl->useevents) { + int events = EV_PERSIST; + php_http_client_pool_event_t *ev = assign_data; + TSRMLS_FETCH_FROM_CTX(pool->ts); + + if (!ev) { + ev = ecalloc(1, sizeof(php_http_client_pool_event_t)); + ev->pool = pool; + curl_multi_assign(curl->handle, sock, ev); + event_base_set(PHP_HTTP_G->curl.event_base, &ev->evnt); + } else { + event_del(&ev->evnt); + } + + switch (action) { + case CURL_POLL_IN: + events |= EV_READ; + break; + case CURL_POLL_OUT: + events |= EV_WRITE; + break; + case CURL_POLL_INOUT: + events |= EV_READ|EV_WRITE; + break; + + case CURL_POLL_REMOVE: + efree(ev); + /* no break */ + case CURL_POLL_NONE: + return 0; + + default: + php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "Unknown socket action %d", action); + return -1; + } + + event_set(&ev->evnt, sock, events, php_http_client_pool_curl_event_callback, pool); + event_add(&ev->evnt, NULL); + } + + return 0; +} + +static void php_http_client_pool_curl_timer_callback(CURLM *multi, long timeout_ms, void *timer_data) +{ + php_http_client_pool_t *pool = timer_data; + php_http_client_pool_curl_t *curl = pool->ctx; + +#if DBG_EVENTS + fprintf(stderr, "%ld", timeout_ms); +#endif + if (curl->useevents) { + + if (timeout_ms < 0) { + php_http_client_pool_curl_timeout_callback(CURL_SOCKET_TIMEOUT, CURL_CSELECT_IN|CURL_CSELECT_OUT, pool); + } else if (timeout_ms > 0 || !event_initialized(curl->timeout) || !event_pending(curl->timeout, EV_TIMEOUT, NULL)) { + struct timeval timeout; + TSRMLS_FETCH_FROM_CTX(pool->ts); + + if (!event_initialized(curl->timeout)) { + event_set(curl->timeout, -1, 0, php_http_client_pool_curl_timeout_callback, pool); + event_base_set(PHP_HTTP_G->curl.event_base, curl->timeout); + } else if (event_pending(curl->timeout, EV_TIMEOUT, NULL)) { + event_del(curl->timeout); + } + + timeout.tv_sec = timeout_ms / 1000; + timeout.tv_usec = (timeout_ms % 1000) * 1000; + + event_add(curl->timeout, &timeout); + } + } +} + +#endif /* HAVE_EVENT */ + + +/* pool handler ops */ + +static php_http_client_pool_t *php_http_client_pool_curl_init(php_http_client_pool_t *h, void *handle) +{ + php_http_client_pool_curl_t *curl; + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_POOL, "could not initialize curl pool handle"); + return NULL; + } + + curl = ecalloc(1, sizeof(*curl)); + curl->handle = handle; + curl->unfinished = 0; + h->ctx = curl; + + return h; +} + +static void php_http_client_pool_curl_dtor(php_http_client_pool_t *h) +{ + php_http_client_pool_curl_t *curl = h->ctx; + TSRMLS_FETCH_FROM_CTX(h->ts); + +#if PHP_HTTP_HAVE_EVENT + if (curl->timeout) { + efree(curl->timeout); + curl->timeout = NULL; + } +#endif + curl->unfinished = 0; + php_http_client_pool_reset(h); + + php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC); + + efree(curl); + h->ctx = NULL; +} + +static STATUS php_http_client_pool_curl_attach(php_http_client_pool_t *h, php_http_client_t *r, const char *m, const char *url, php_http_message_body_t *body) +{ + php_http_client_pool_curl_t *curl = h->ctx; + php_http_client_curl_t *recurl = r->ctx; + CURLMcode rs; + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (SUCCESS != php_http_curl_request_prepare(r, m, url, body)) { + return FAILURE; + } + + if (CURLM_OK == (rs = curl_multi_add_handle(curl->handle, recurl->handle))) { + ++curl->unfinished; + return SUCCESS; + } else { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_POOL, "Could not attach request to pool: %s", curl_multi_strerror(rs)); + return FAILURE; + } +} + +static STATUS php_http_client_pool_curl_detach(php_http_client_pool_t *h, php_http_client_t *r) +{ + php_http_client_pool_curl_t *curl = h->ctx; + php_http_client_curl_t *recurl = r->ctx; + CURLMcode rs = curl_multi_remove_handle(curl->handle, recurl->handle); + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (CURLM_OK == rs) { + return SUCCESS; + } else { + php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT_POOL, "Could not detach request from pool: %s", curl_multi_strerror(rs)); + return FAILURE; + } +} + +#ifdef PHP_WIN32 +# define SELECT_ERROR SOCKET_ERROR +#else +# define SELECT_ERROR -1 +#endif + +static STATUS php_http_client_pool_curl_wait(php_http_client_pool_t *h, struct timeval *custom_timeout) +{ + int MAX; + fd_set R, W, E; + struct timeval timeout; + php_http_client_pool_curl_t *curl = h->ctx; + +#if PHP_HTTP_HAVE_EVENT + if (curl->useevents) { + TSRMLS_FETCH_FROM_CTX(h->ts); + + php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented"); + return FAILURE; + } +#endif + + if (custom_timeout && timerisset(custom_timeout)) { + timeout = *custom_timeout; + } else { + long max_tout = 1000; + + if ((CURLM_OK == curl_multi_timeout(curl->handle, &max_tout)) && (max_tout > 0)) { + timeout.tv_sec = max_tout / 1000; + timeout.tv_usec = (max_tout % 1000) * 1000; + } else { + timeout.tv_sec = 0; + timeout.tv_usec = 1000; + } + } + + FD_ZERO(&R); + FD_ZERO(&W); + FD_ZERO(&E); + + if (CURLM_OK == curl_multi_fdset(curl->handle, &R, &W, &E, &MAX)) { + if (MAX == -1) { + php_http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / PHP_HTTP_MCROSEC)); + return SUCCESS; + } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) { + return SUCCESS; + } + } + return FAILURE; +} + +static int php_http_client_pool_curl_once(php_http_client_pool_t *h) +{ + php_http_client_pool_curl_t *curl = h->ctx; + +#if PHP_HTTP_HAVE_EVENT + if (curl->useevents) { + TSRMLS_FETCH_FROM_CTX(h->ts); + php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented"); + return FAILURE; + } +#endif + + while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle, &curl->unfinished)); + + php_http_client_pool_curl_responsehandler(h); + + return curl->unfinished; + +} +#if PHP_HTTP_HAVE_EVENT +static void dolog(int i, const char *m) { + fprintf(stderr, "%d: %s\n", i, m); +} +#endif +static STATUS php_http_client_pool_curl_exec(php_http_client_pool_t *h) +{ + TSRMLS_FETCH_FROM_CTX(h->ts); + +#if PHP_HTTP_HAVE_EVENT + php_http_client_pool_curl_t *curl = h->ctx; + + if (curl->useevents) { + event_set_log_callback(dolog); + do { +#if DBG_EVENTS + fprintf(stderr, "X"); +#endif + event_base_dispatch(PHP_HTTP_G->curl.event_base); + } while (curl->unfinished); + } else +#endif + { + while (php_http_client_pool_curl_once(h)) { + if (SUCCESS != php_http_client_pool_curl_wait(h, NULL)) { +#ifdef PHP_WIN32 + /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */ + php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError()); +#else + php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, strerror(errno)); +#endif + return FAILURE; + } + } + } + + return SUCCESS; +} + +static STATUS php_http_client_pool_curl_setopt(php_http_client_pool_t *h, php_http_client_pool_setopt_opt_t opt, void *arg) +{ + php_http_client_pool_curl_t *curl = h->ctx; + + switch (opt) { + case PHP_HTTP_CLIENT_POOL_OPT_ENABLE_PIPELINING: + if (CURLM_OK != curl_multi_setopt(curl->handle, CURLMOPT_PIPELINING, (long) *((zend_bool *) arg))) { + return FAILURE; + } + break; + + case PHP_HTTP_CLIENT_POOL_OPT_USE_EVENTS: +#if PHP_HTTP_HAVE_EVENT + if ((curl->useevents = *((zend_bool *) arg))) { + if (!curl->timeout) { + curl->timeout = ecalloc(1, sizeof(struct event)); + } + curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, h); + curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, php_http_client_pool_curl_socket_callback); + curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, h); + curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, php_http_client_pool_curl_timer_callback); + } else { + curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, NULL); + curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, NULL); + curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, NULL); + curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, NULL); + } + break; +#endif + + default: + return FAILURE; + } + return SUCCESS; +} + +static php_http_resource_factory_ops_t php_http_curlm_resource_factory_ops = { + php_http_curlm_ctor, + NULL, + php_http_curlm_dtor +}; + +static php_http_client_pool_ops_t php_http_client_pool_curl_ops = { + &php_http_curlm_resource_factory_ops, + php_http_client_pool_curl_init, + NULL /* copy */, + php_http_client_pool_curl_dtor, + NULL /*reset */, + php_http_client_pool_curl_exec, + php_http_client_pool_curl_wait, + php_http_client_pool_curl_once, + php_http_client_pool_curl_attach, + php_http_client_pool_curl_detach, + php_http_client_pool_curl_setopt, +}; + +PHP_HTTP_API php_http_client_pool_ops_t *php_http_client_pool_curl_get_ops(void) +{ + return &php_http_client_pool_curl_ops; +} + +#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientCURL, method, 0, req_args) +#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientCURL, method, 0) +#define PHP_HTTP_CURL_ME(method, visibility) PHP_ME(HttpClientCURL, method, PHP_HTTP_ARGS(HttpClientCURL, method), visibility) +#define PHP_HTTP_CURL_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientCURL, method)) +#define PHP_HTTP_CURL_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClientCURL_##al), PHP_HTTP_ARGS(HttpClientCURL, al), vis) + +zend_class_entry *php_http_client_pool_curl_class_entry; +zend_function_entry php_http_client_pool_curl_method_entry[] = { + EMPTY_FUNCTION_ENTRY +}; + +zend_object_value php_http_client_pool_curl_object_new(zend_class_entry *ce TSRMLS_DC) +{ + return php_http_client_pool_curl_object_new_ex(ce, NULL, NULL TSRMLS_CC); +} + +zend_object_value php_http_client_pool_curl_object_new_ex(zend_class_entry *ce, php_http_client_pool_t *p, php_http_client_pool_object_t **ptr TSRMLS_DC) +{ + zend_object_value ov; + php_http_client_pool_object_t *o; + + o = ecalloc(1, sizeof(php_http_client_pool_object_t)); + zend_object_std_init((zend_object *) o, ce TSRMLS_CC); + object_properties_init((zend_object *) o, ce); + + if (!(o->pool = p)) { + o->pool = php_http_client_pool_init(NULL, &php_http_client_pool_curl_ops, NULL, NULL TSRMLS_CC); + } + + if (ptr) { + *ptr = o; + } + + ov.handle = zend_objects_store_put(o, NULL, php_http_client_pool_object_free, NULL TSRMLS_CC); + ov.handlers = php_http_client_pool_get_object_handlers(); + + return ov; +} + + +PHP_MINIT_FUNCTION(http_client_pool_curl) +{ + if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_client_pool.curl"), &php_http_curlm_resource_factory_ops, NULL, NULL)) { + return FAILURE; + } + + PHP_HTTP_REGISTER_CLASS(http\\Client\\Pool, CURL, http_client_pool_curl, php_http_client_pool_class_entry, 0); + php_http_client_pool_curl_class_entry->create_object = php_http_client_pool_curl_object_new; + + return SUCCESS; +} + +PHP_RINIT_FUNCTION(http_client_pool_curl) +{ +#if PHP_HTTP_HAVE_EVENT + if (!PHP_HTTP_G->curl.event_base && !(PHP_HTTP_G->curl.event_base = event_init())) { + return FAILURE; + } +#endif + + return SUCCESS; +} + +#endif /* PHP_HTTP_HAVE_CURL */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_pool_curl.h b/php_http_client_pool_curl.h new file mode 100644 index 0000000..e17e161 --- /dev/null +++ b/php_http_client_pool_curl.h @@ -0,0 +1,44 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_HTTP_CLIENT_POOL_CURL_H +#define PHP_HTTP_CLIENT_POOL_CURL_H + +#if PHP_HTTP_HAVE_CURL + +PHP_HTTP_API php_http_client_pool_ops_t *php_http_client_pool_curl_get_ops(void); + +extern zend_object_value php_http_client_pool_curl_object_new(zend_class_entry *ce TSRMLS_DC); +extern zend_object_value php_http_client_pool_curl_object_new_ex(zend_class_entry *ce, php_http_client_pool_t *p, php_http_client_pool_object_t **ptr TSRMLS_DC); + +#if PHP_HTTP_HAVE_EVENT +struct php_http_curl_globals { + void *event_base; +}; + +PHP_RINIT_FUNCTION(http_client_pool_curl); +#endif + +PHP_MINIT_FUNCTION(http_client_pool_curl); + +#endif /* PHP_HTTP_HAVE_CURL */ +#endif /* PHP_HTTP_CLIENT_POOL_CURL_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_request.c b/php_http_client_request.c new file mode 100644 index 0000000..fb2b84c --- /dev/null +++ b/php_http_client_request.c @@ -0,0 +1,191 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" + +#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientRequest, method, 0, req_args) +#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientRequest, method, 0) +#define PHP_HTTP_CLIENT_REQUEST_ME(method, visibility) PHP_ME(HttpClientRequest, method, PHP_HTTP_ARGS(HttpClientRequest, method), visibility) +#define PHP_HTTP_CLIENT_REQUEST_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientRequest, method)) +#define PHP_HTTP_CLIENT_REQUEST_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClientRequest_##al), PHP_HTTP_ARGS(HttpClientRequest, al), vis) + +PHP_HTTP_BEGIN_ARGS(__construct, 0) + PHP_HTTP_ARG_VAL(method, 0) + PHP_HTTP_ARG_VAL(url, 0) + PHP_HTTP_ARG_ARR(headers, 1, 0) + PHP_HTTP_ARG_OBJ(http\\Message\\Body, body, 1) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(getContentType); +PHP_HTTP_BEGIN_ARGS(setContentType, 1) + PHP_HTTP_ARG_VAL(content_type, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_EMPTY_ARGS(getQuery); +PHP_HTTP_BEGIN_ARGS(setQuery, 0) + PHP_HTTP_ARG_VAL(query_data, 0) +PHP_HTTP_END_ARGS; + +PHP_HTTP_BEGIN_ARGS(addQuery, 1) + PHP_HTTP_ARG_VAL(query_data, 0) +PHP_HTTP_END_ARGS; + + +PHP_METHOD(HttpClientRequest, __construct) +{ + char *meth_str = NULL, *url_str = NULL; + int meth_len = 0, url_len = 0; + zval *zheaders = NULL, *zbody = NULL; + + with_error_handling(EH_THROW, php_http_exception_class_entry) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!a!O!", &meth_str, &meth_len, &url_str, &url_len, &zheaders, &zbody, php_http_message_body_class_entry)) { + php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + if (obj->message) { + php_http_message_set_type(obj->message, PHP_HTTP_REQUEST); + } else { + obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST TSRMLS_CC); + } + + if (meth_str && meth_len) { + PHP_HTTP_INFO(obj->message).request.method = estrndup(meth_str, meth_len); + } + if (url_str && url_len) { + PHP_HTTP_INFO(obj->message).request.url = estrndup(url_str, url_len); + } + if (zheaders) { + array_copy(Z_ARRVAL_P(zheaders), &obj->message->hdrs); + } + if (zbody) { + php_http_message_body_object_t *body_obj = zend_object_store_get_object(zbody TSRMLS_CC); + + php_http_message_body_dtor(&obj->message->body); + php_http_message_body_copy(body_obj->body, &obj->message->body, 0); + Z_OBJ_ADDREF_P(zbody); + obj->body = Z_OBJVAL_P(zbody); + } + } + } end_error_handling(); +} + + +PHP_METHOD(HttpClientRequest, setContentType) +{ + char *ctype; + int ct_len; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) { + int invalid = 0; + + if (ct_len) { + PHP_HTTP_CHECK_CONTENT_TYPE(ctype, invalid = 1); + } + + if (!invalid) { + zend_update_property_stringl(php_http_client_request_class_entry, getThis(), ZEND_STRL("contentType"), ctype, ct_len TSRMLS_CC); + } + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClientRequest, getContentType) +{ + if (SUCCESS == zend_parse_parameters_none()) { + RETURN_PROP(php_http_client_request_class_entry, "contentType"); + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientRequest, setQuery) +{ + zval *qdata = NULL; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) { + if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) { + zend_update_property_stringl(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), "", 0 TSRMLS_CC); + } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) { + char *query_data_str = NULL; + size_t query_data_len; + + if (SUCCESS == php_http_url_encode_hash(HASH_OF(qdata), NULL, 0, &query_data_str, &query_data_len TSRMLS_CC)) { + zend_update_property_stringl(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), query_data_str, query_data_len TSRMLS_CC); + efree(query_data_str); + } + } else { + zval *data = php_http_ztyp(IS_STRING, qdata); + + zend_update_property_stringl(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), Z_STRVAL_P(data), Z_STRLEN_P(data) TSRMLS_CC); + zval_ptr_dtor(&data); + } + } + RETVAL_ZVAL(getThis(), 1, 0); +} + +PHP_METHOD(HttpClientRequest, getQuery) +{ + if (SUCCESS == zend_parse_parameters_none()) { + RETURN_PROP(php_http_client_request_class_entry, "query"); + } + RETURN_FALSE; +} + +PHP_METHOD(HttpClientRequest, addQuery) +{ + zval *qdata; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) { + char *query_data_str = NULL; + size_t query_data_len = 0; + zval *old_qdata = php_http_ztyp(IS_STRING, zend_read_property(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), 0 TSRMLS_CC)); + + if (SUCCESS == php_http_url_encode_hash(Z_ARRVAL_P(qdata), Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data_str, &query_data_len TSRMLS_CC)) { + zend_update_property_stringl(php_http_client_request_class_entry, getThis(), ZEND_STRL("query"), query_data_str, query_data_len TSRMLS_CC); + efree(query_data_str); + } + + zval_ptr_dtor(&old_qdata); + } + RETVAL_ZVAL(getThis(), 1, 0); + +} + + +zend_class_entry *php_http_client_request_class_entry; +zend_function_entry php_http_client_request_method_entry[] = { + PHP_HTTP_CLIENT_REQUEST_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_HTTP_CLIENT_REQUEST_ME(getQuery, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_REQUEST_ME(setQuery, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_REQUEST_ME(addQuery, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_REQUEST_ME(getContentType, ZEND_ACC_PUBLIC) + PHP_HTTP_CLIENT_REQUEST_ME(setContentType, ZEND_ACC_PUBLIC) + EMPTY_FUNCTION_ENTRY +}; + +PHP_MINIT_FUNCTION(http_client_request) +{ + PHP_HTTP_REGISTER_CLASS(http\\Client, Request, http_client_request, php_http_message_class_entry, 0); + + zend_declare_property_string(php_http_client_request_class_entry, ZEND_STRL("query"), "", ZEND_ACC_PROTECTED TSRMLS_CC); + zend_declare_property_string(php_http_client_request_class_entry, ZEND_STRL("contentType"), "", ZEND_ACC_PROTECTED TSRMLS_CC); + + return SUCCESS; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_response.c b/php_http_client_response.c new file mode 100644 index 0000000..663ce8b --- /dev/null +++ b/php_http_client_response.c @@ -0,0 +1,118 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" + +#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpClientResponse, method, 0, req_args) +#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpClientResponse, method, 0) +#define PHP_HTTP_CLIENT_RESPONSE_ME(method, visibility) PHP_ME(HttpClientResponse, method, PHP_HTTP_ARGS(HttpClientResponse, method), visibility) +#define PHP_HTTP_CLIENT_RESPONSE_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpClientResponse, method)) +#define PHP_HTTP_CLIENT_RESPONSE_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpClientResponse_##al), PHP_HTTP_ARGS(HttpClientResponse, al), vis) + +PHP_HTTP_BEGIN_ARGS(getCookies, 0) + PHP_HTTP_ARG_VAL(flags, 0) + PHP_HTTP_ARG_VAL(allowed_extras, 0) +PHP_HTTP_END_ARGS; + +PHP_METHOD(HttpClientResponse, getCookies) +{ + long flags = 0; + zval *allowed_extras_array = NULL; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la!", &flags, &allowed_extras_array)) { + int i = 0; + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + char **allowed_extras = NULL; + zval **header = NULL, **entry = NULL; + HashPosition pos, pos1, pos2; + php_http_message_object_t *msg = zend_object_store_get_object(getThis() TSRMLS_CC); + + + array_init(return_value); + + if (allowed_extras_array) { + allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *)); + FOREACH_VAL(pos, allowed_extras_array, entry) { + zval *data = php_http_ztyp(IS_STRING, *entry); + allowed_extras[i++] = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data)); + zval_ptr_dtor(&data); + } + } + + FOREACH_HASH_KEYVAL(pos1, &msg->message->hdrs, key, header) { + if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "Set-Cookie")) { + php_http_cookie_list_t *list; + + if (Z_TYPE_PP(header) == IS_ARRAY) { + zval **single_header; + + FOREACH_VAL(pos2, *header, single_header) { + zval *data = php_http_ztyp(IS_STRING, *single_header); + + if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) { + zval *cookie; + + MAKE_STD_ZVAL(cookie); + ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0); + add_next_index_zval(return_value, cookie); + } + zval_ptr_dtor(&data); + } + } else { + zval *data = php_http_ztyp(IS_STRING, *header); + if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) { + zval *cookie; + + MAKE_STD_ZVAL(cookie); + ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0); + add_next_index_zval(return_value, cookie); + } + zval_ptr_dtor(&data); + } + } + } + + if (allowed_extras) { + for (i = 0; allowed_extras[i]; ++i) { + efree(allowed_extras[i]); + } + efree(allowed_extras); + } + + return; + } + RETURN_FALSE; +} + + +zend_class_entry *php_http_client_response_class_entry; +zend_function_entry php_http_client_response_method_entry[] = { + PHP_HTTP_CLIENT_RESPONSE_ME(getCookies, ZEND_ACC_PUBLIC) + EMPTY_FUNCTION_ENTRY +}; + +PHP_MINIT_FUNCTION(http_client_response) +{ + PHP_HTTP_REGISTER_CLASS(http\\Client, Response, http_client_response, php_http_message_class_entry, 0); + + return SUCCESS; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_client_response.h b/php_http_client_response.h new file mode 100644 index 0000000..704f727 --- /dev/null +++ b/php_http_client_response.h @@ -0,0 +1,33 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_HTTP_CLIENT_RESPONSE_H +#define PHP_HTTP_CLIENT_RESPONSE_H + +extern zend_class_entry *php_http_client_response_class_entry; +extern zend_function_entry php_http_client_response_method_entry[]; + +PHP_METHOD(HttpClientResponse, getCookies); + +PHP_MINIT_FUNCTION(http_client_response); + +#endif /* PHP_HTTP_CLIENT_RESPONSE_H */ + + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/php_http_curl.c b/php_http_curl.c index 5fea9fe..569a414 100644 --- a/php_http_curl.c +++ b/php_http_curl.c @@ -14,1894 +14,6 @@ #if PHP_HTTP_HAVE_CURL -#include -#define PHP_HTTP_CURL_VERSION(x, y, z) (LIBCURL_VERSION_NUM >= (((x)<<16) + ((y)<<8) + (z))) - -#if PHP_HTTP_HAVE_EVENT -# include -#endif - -typedef struct php_http_curl_request { - CURL *handle; - - struct { - HashTable cache; - - struct curl_slist *headers; - struct curl_slist *resolve; - php_http_buffer_t cookies; - - long redirects; - - struct { - uint count; - double delay; - } retry; - - } options; - - php_http_request_progress_t progress; - -} php_http_curl_request_t; - -typedef struct php_http_curl_request_storage { - char *url; - char *cookiestore; - char errorbuffer[0x100]; -} php_http_curl_request_storage_t; - -typedef struct php_http_curl_request_pool { - CURLM *handle; - - int unfinished; /* int because of curl_multi_perform() */ - -#if PHP_HTTP_HAVE_EVENT - struct event *timeout; - unsigned useevents:1; - unsigned runsocket:1; -#endif -} php_http_curl_request_pool_t; - -typedef struct php_http_curl_request_datashare { - CURLSH *handle; -} php_http_curl_request_datashare_t; - -#define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \ - { \ - char *K = #OPTION; \ - PHP_HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \ - } -#define PHP_HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \ - if (!strcasecmp(key.str, keyname)) { \ - zval *copy = cache_option(&curl->options.cache, keyname, strlen(keyname)+1, 0, php_http_ztyp(IS_STRING, *param)); \ - if (obdc) { \ - if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(copy) TSRMLS_CC)) { \ - return FAILURE; \ - } \ - } \ - curl_easy_setopt(ch, optname, Z_STRVAL_P(copy)); \ - zval_ptr_dtor(©); \ - continue; \ - } -#define PHP_HTTP_CURL_OPT_LONG(OPTION, ldiff) \ - { \ - char *K = #OPTION; \ - PHP_HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \ - } -#define PHP_HTTP_CURL_OPT_LONG_EX(keyname, optname) \ - if (!strcasecmp(key.str, keyname)) { \ - zval *copy = php_http_ztyp(IS_LONG, *param); \ - curl_easy_setopt(ch, optname, Z_LVAL_P(copy)); \ - zval_ptr_dtor(©); \ - continue; \ - } - -static inline php_http_curl_request_storage_t *get_storage(CURL *ch) { - php_http_curl_request_storage_t *st = NULL; - - curl_easy_getinfo(ch, CURLINFO_PRIVATE, &st); - - if (!st) { - st = pecalloc(1, sizeof(*st), 1); - curl_easy_setopt(ch, CURLOPT_PRIVATE, st); - curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer); - } - - return st; -} - -/* resource_factory ops */ - -static void *php_http_curl_ctor(void *opaque TSRMLS_DC) -{ - void *ch; - - if ((ch = curl_easy_init())) { - get_storage(ch); - return ch; - } - return NULL; -} - -static void *php_http_curl_copy(void *opaque, void *handle TSRMLS_DC) -{ - void *ch; - - if ((ch = curl_easy_duphandle(handle))) { - curl_easy_reset(ch); - get_storage(ch); - return ch; - } - return NULL; -} - -static void php_http_curl_dtor(void *opaque, void *handle TSRMLS_DC) -{ - php_http_curl_request_storage_t *st = get_storage(handle); - - curl_easy_cleanup(handle); - - if (st) { - if (st->url) { - pefree(st->url, 1); - } - if (st->cookiestore) { - pefree(st->cookiestore, 1); - } - pefree(st, 1); - } -} - -static void *php_http_curlm_ctor(void *opaque TSRMLS_DC) -{ - return curl_multi_init(); -} - -static void php_http_curlm_dtor(void *opaque, void *handle TSRMLS_DC) -{ - curl_multi_cleanup(handle); -} - -static void *php_http_curlsh_ctor(void *opaque TSRMLS_DC) -{ - return curl_share_init(); -} - -static void php_http_curlsh_dtor(void *opaque, void *handle TSRMLS_DC) -{ - curl_share_cleanup(handle); -} - -/* callbacks */ - -static size_t php_http_curl_read_callback(void *data, size_t len, size_t n, void *ctx) -{ - php_http_message_body_t *body = ctx; - - if (body) { - TSRMLS_FETCH_FROM_CTX(body->ts); - return php_stream_read(php_http_message_body_stream(body), data, len * n); - } - return 0; -} - -static int php_http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow) -{ - php_http_request_t *h = ctx; - php_http_curl_request_t *curl = h->ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - - curl->progress.state.dl.total = dltotal; - curl->progress.state.dl.now = dlnow; - curl->progress.state.ul.total = ultotal; - curl->progress.state.ul.now = ulnow; - - php_http_request_progress_notify(&curl->progress TSRMLS_CC); - - return 0; -} - -static curlioerr php_http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx) -{ - php_http_message_body_t *body = ctx; - - if (cmd != CURLIOCMD_RESTARTREAD) { - return CURLIOE_UNKNOWNCMD; - } - - if (body) { - TSRMLS_FETCH_FROM_CTX(body->ts); - - if (SUCCESS == php_stream_rewind(php_http_message_body_stream(body))) { - return CURLIOE_OK; - } - } - - return CURLIOE_FAILRESTART; -} - -static int php_http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx) -{ - php_http_request_t *h = ctx; - php_http_curl_request_t *curl = h->ctx; - unsigned flags = 0; - TSRMLS_FETCH_FROM_CTX(h->ts); - - /* catch progress */ - switch (type) { - case CURLINFO_TEXT: - if (php_memnstr(data, ZEND_STRL("About to connect"), data + length)) { - curl->progress.state.info = "resolve"; - } else if (php_memnstr(data, ZEND_STRL("Trying"), data + length)) { - curl->progress.state.info = "connect"; - } else if (php_memnstr(data, ZEND_STRL("Connected"), data + length)) { - curl->progress.state.info = "connected"; - } else if (php_memnstr(data, ZEND_STRL("left intact"), data + length)) { - curl->progress.state.info = "not disconnected"; - } else if (php_memnstr(data, ZEND_STRL("closed"), data + length)) { - curl->progress.state.info = "disconnected"; - } else if (php_memnstr(data, ZEND_STRL("Issue another request"), data + length)) { - curl->progress.state.info = "redirect"; - } - php_http_request_progress_notify(&curl->progress TSRMLS_CC); - break; - case CURLINFO_HEADER_OUT: - case CURLINFO_DATA_OUT: - case CURLINFO_SSL_DATA_OUT: - curl->progress.state.info = "send"; - break; - case CURLINFO_HEADER_IN: - case CURLINFO_DATA_IN: - case CURLINFO_SSL_DATA_IN: - curl->progress.state.info = "receive"; - break; - default: - break; - } - /* process data */ - switch (type) { - case CURLINFO_HEADER_IN: - case CURLINFO_DATA_IN: - case CURLINFO_HEADER_OUT: - case CURLINFO_DATA_OUT: - php_http_buffer_append(h->buffer, data, length); - - if (curl->options.redirects) { - flags |= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS; - } - - if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(h->parser, h->buffer, flags, &h->message)) { - return -1; - } - break; - default: - break; - } - -#if 0 - /* debug */ - _dpf(type, data, length); -#endif - - return 0; -} - -static int php_http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) -{ - return n*l; -} - -static STATUS php_http_curl_request_prepare(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body) -{ - php_http_curl_request_t *curl = h->ctx; - php_http_curl_request_storage_t *storage = get_storage(curl->handle); - TSRMLS_FETCH_FROM_CTX(h->ts); - - storage->errorbuffer[0] = '\0'; - if (storage->url) { - pefree(storage->url, 1); - } - storage->url = pestrdup(url, 1); - curl_easy_setopt(curl->handle, CURLOPT_URL, storage->url); - - /* request method */ - switch (php_http_select_str(meth, 4, "GET", "HEAD", "POST", "PUT")) { - case 0: - curl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1L); - break; - - case 1: - curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L); - break; - - case 2: - curl_easy_setopt(curl->handle, CURLOPT_POST, 1L); - break; - - case 3: - curl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1L); - break; - - default: { - if (meth) { - curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, meth); - } else { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD, "Unsupported request method: '%s' (%s)", meth, url); - return FAILURE; - } - break; - } - } - - /* attach request body */ - if (body) { - /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the - * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.« - * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the - * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method - * does not allow a request body. - */ - size_t body_size = php_http_message_body_size(body); - - curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, body); - curl_easy_setopt(curl->handle, CURLOPT_READDATA, body); - curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size); - curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size); - } - - return SUCCESS; -} - -static void php_http_curl_request_pool_responsehandler(php_http_request_pool_t *pool) -{ - int remaining = 0; - zval **requests; - php_http_curl_request_pool_t *curl = pool->ctx; - TSRMLS_FETCH_FROM_CTX(pool->ts); - - do { - CURLMsg *msg = curl_multi_info_read(curl->handle, &remaining); - - if (msg && CURLMSG_DONE == msg->msg) { - zval **request; - - if (CURLE_OK != msg->data.result) { - php_http_curl_request_storage_t *st = get_storage(msg->easy_handle); - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), STR_PTR(st->errorbuffer), STR_PTR(st->url)); - } - - php_http_request_pool_requests(pool, &requests, NULL); - for (request = requests; *request; ++request) { - php_http_request_object_t *obj = zend_object_store_get_object(*request TSRMLS_CC); - - if (msg->easy_handle == ((php_http_curl_request_t *) (obj->request->ctx))->handle) { - Z_ADDREF_PP(request); - zend_llist_add_element(&pool->requests.finished, request); - php_http_request_object_responsehandler(obj, *request TSRMLS_CC); - } - - zval_ptr_dtor(request); - } - efree(requests); - } - } while (remaining); -} - - -#if PHP_HTTP_HAVE_EVENT - -typedef struct php_http_request_pool_event { - struct event evnt; - php_http_request_pool_t *pool; -} php_http_request_pool_event_t; - -static inline int etoca(short action) { - switch (action & (EV_READ|EV_WRITE)) { - case EV_READ: - return CURL_CSELECT_IN; - break; - case EV_WRITE: - return CURL_CSELECT_OUT; - break; - case EV_READ|EV_WRITE: - return CURL_CSELECT_IN|CURL_CSELECT_OUT; - break; - default: - return 0; - } -} - -static void php_http_curl_request_pool_timeout_callback(int socket, short action, void *event_data) -{ - php_http_request_pool_t *pool = event_data; - php_http_curl_request_pool_t *curl = pool->ctx; - -#if DBG_EVENTS - fprintf(stderr, "T"); -#endif - if (curl->useevents) { - CURLMcode rc; - TSRMLS_FETCH_FROM_CTX(pool->ts); - - while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished))); - - if (CURLM_OK != rc) { - php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc)); - } - - php_http_curl_request_pool_responsehandler(pool); - } -} - -static void php_http_curl_request_pool_event_callback(int socket, short action, void *event_data) -{ - php_http_request_pool_t *pool = event_data; - php_http_curl_request_pool_t *curl = pool->ctx; - -#if DBG_EVENTS - fprintf(stderr, "E"); -#endif - if (curl->useevents) { - CURLMcode rc = CURLE_OK; - TSRMLS_FETCH_FROM_CTX(pool->ts); - - while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished))); - - if (CURLM_OK != rc) { - php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc)); - } - - php_http_curl_request_pool_responsehandler(pool); - - /* remove timeout if there are no transfers left */ - if (!curl->unfinished && event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) { - event_del(curl->timeout); - } - } -} - -static int php_http_curl_request_pool_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data) -{ - php_http_request_pool_t *pool = socket_data; - php_http_curl_request_pool_t *curl = pool->ctx; - -#if DBG_EVENTS - fprintf(stderr, "S"); -#endif - if (curl->useevents) { - int events = EV_PERSIST; - php_http_request_pool_event_t *ev = assign_data; - TSRMLS_FETCH_FROM_CTX(pool->ts); - - if (!ev) { - ev = ecalloc(1, sizeof(php_http_request_pool_event_t)); - ev->pool = pool; - curl_multi_assign(curl->handle, sock, ev); - event_base_set(PHP_HTTP_G->curl.event_base, &ev->evnt); - } else { - event_del(&ev->evnt); - } - - switch (action) { - case CURL_POLL_IN: - events |= EV_READ; - break; - case CURL_POLL_OUT: - events |= EV_WRITE; - break; - case CURL_POLL_INOUT: - events |= EV_READ|EV_WRITE; - break; - - case CURL_POLL_REMOVE: - efree(ev); - /* no break */ - case CURL_POLL_NONE: - return 0; - - default: - php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "Unknown socket action %d", action); - return -1; - } - - event_set(&ev->evnt, sock, events, php_http_curl_request_pool_event_callback, pool); - event_add(&ev->evnt, NULL); - } - - return 0; -} - -static void php_http_curl_request_pool_timer_callback(CURLM *multi, long timeout_ms, void *timer_data) -{ - php_http_request_pool_t *pool = timer_data; - php_http_curl_request_pool_t *curl = pool->ctx; - -#if DBG_EVENTS - fprintf(stderr, "%ld", timeout_ms); -#endif - if (curl->useevents) { - - if (timeout_ms < 0) { - php_http_curl_request_pool_timeout_callback(CURL_SOCKET_TIMEOUT, CURL_CSELECT_IN|CURL_CSELECT_OUT, pool); - } else if (timeout_ms > 0 || !event_initialized(curl->timeout) || !event_pending(curl->timeout, EV_TIMEOUT, NULL)) { - struct timeval timeout; - TSRMLS_FETCH_FROM_CTX(pool->ts); - - if (!event_initialized(curl->timeout)) { - event_set(curl->timeout, -1, 0, php_http_curl_request_pool_timeout_callback, pool); - event_base_set(PHP_HTTP_G->curl.event_base, curl->timeout); - } else if (event_pending(curl->timeout, EV_TIMEOUT, NULL)) { - event_del(curl->timeout); - } - - timeout.tv_sec = timeout_ms / 1000; - timeout.tv_usec = (timeout_ms % 1000) * 1000; - - event_add(curl->timeout, &timeout); - } - } -} - -#endif /* HAVE_EVENT */ - - -static inline zval *cache_option(HashTable *cache, char *key, size_t keylen, ulong h, zval *opt) -{ - Z_ADDREF_P(opt); - - if (h) { - zend_hash_quick_update(cache, key, keylen, h, &opt, sizeof(zval *), NULL); - } else { - zend_hash_update(cache, key, keylen, &opt, sizeof(zval *), NULL); - } - - return opt; -} - -static inline zval *get_option(HashTable *cache, HashTable *options, char *key, size_t keylen, int type) -{ - if (options) { - zval **zoption; - ulong h = zend_hash_func(key, keylen); - - if (SUCCESS == zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) { - zval *option = php_http_ztyp(type, *zoption); - - if (cache) { - zval *cached = cache_option(cache, key, keylen, h, option); - - zval_ptr_dtor(&option); - return cached; - } - return option; - } - } - - return NULL; -} - -static STATUS set_options(php_http_request_t *h, HashTable *options) -{ - zval *zoption; - int range_req = 0; - php_http_curl_request_t *curl = h->ctx; - CURL *ch = curl->handle; - TSRMLS_FETCH_FROM_CTX(h->ts); - - /* proxy */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyhost"), IS_STRING))) { - curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption)); - /* type */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytype"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_PROXYTYPE, Z_LVAL_P(zoption)); - } - /* port */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyport"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption)); - } - /* user:pass */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauth"), IS_STRING)) && Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption)); - } - /* auth method */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauthtype"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption)); - } - /* tunnel */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytunnel"), IS_BOOL)) && Z_BVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 1L); - } - } -#if PHP_HTTP_CURL_VERSION(7,19,4) - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("noproxy"), IS_STRING))) { - curl_easy_setopt(ch, CURLOPT_NOPROXY, Z_STRVAL_P(zoption)); - } -#endif - - /* dns */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_cache_timeout"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, Z_LVAL_P(zoption)); - } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ipresolve"), IS_LONG)) && Z_LVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_IPRESOLVE, Z_LVAL_P(zoption)); - } -#if PHP_HTTP_CURL_VERSION(7,21,3) - if (curl->options.resolve) { - curl_slist_free_all(curl->options.resolve); - curl->options.resolve = NULL; - } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resolve"), IS_ARRAY))) { - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - HashPosition pos; - zval **data; - - FOREACH_KEYVAL(pos, zoption, key, data) { - zval *cpy = php_http_ztyp(IS_STRING, *data); - - curl->options.resolve = curl_slist_append(curl->options.resolve, Z_STRVAL_P(cpy)); - - zval_ptr_dtor(&cpy); - } - } -#endif -#if PHP_HTTP_CURL_VERSION(7,24,0) - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_servers"), IS_STRING)) && Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, Z_STRVAL_P(zoption)); - } -#endif - - /* limits */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_limit"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, Z_LVAL_P(zoption)); - } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_time"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, Z_LVAL_P(zoption)); - } - /* LSF weirdance - if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption)); - } - if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption)); - } - */ - /* crashes - if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption)); - } */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("fresh_connect"), IS_BOOL)) && Z_BVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L); - } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("forbid_reuse"), IS_BOOL)) && Z_BVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 1L); - } - - /* outgoing interface */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("interface"), IS_STRING))) { - curl_easy_setopt(ch, CURLOPT_INTERFACE, Z_STRVAL_P(zoption)); - - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("portrange"), IS_ARRAY))) { - zval **prs, **pre; - - zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption)); - if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) { - zend_hash_move_forward(Z_ARRVAL_P(zoption)); - if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) { - zval *prs_cpy = php_http_ztyp(IS_LONG, *prs); - zval *pre_cpy = php_http_ztyp(IS_LONG, *pre); - - if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) { - curl_easy_setopt(ch, CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy))); - curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L); - } - zval_ptr_dtor(&prs_cpy); - zval_ptr_dtor(&pre_cpy); - } - } - } - } - - /* another port */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("port"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption)); - } - - /* RFC4007 zone_id */ -#if PHP_HTTP_CURL_VERSION(7,19,0) - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("address_scope"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, Z_LVAL_P(zoption)); - } -#endif - - /* auth */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauth"), IS_STRING)) && Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption)); - } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauthtype"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption)); - } - - /* redirects, defaults to 0 */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("redirect"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L); - curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(zoption)); - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("unrestrictedauth"), IS_BOOL))) { - curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption)); - } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("postredir"), IS_BOOL))) { -#if PHP_HTTP_CURL_VERSION(7,19,1) - curl_easy_setopt(ch, CURLOPT_POSTREDIR, Z_BVAL_P(zoption) ? 1L : 0L); -#else - curl_easy_setopt(ch, CURLOPT_POST301, Z_BVAL_P(zoption) ? 1L : 0L); -#endif - } - } else { - curl->options.redirects = 0; - } - - /* retries, defaults to 0 */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrycount"), IS_LONG))) { - curl->options.retry.count = Z_LVAL_P(zoption); - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrydelay"), IS_DOUBLE))) { - curl->options.retry.delay = Z_DVAL_P(zoption); - } else { - curl->options.retry.delay = 0; - } - } else { - curl->options.retry.count = 0; - } - - /* referer */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("referer"), IS_STRING)) && Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption)); - } - - /* useragent, default "PECL::HTTP/version (PHP/version)" */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("useragent"), IS_STRING))) { - /* allow to send no user agent, not even default one */ - if (Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption)); - } else { - curl_easy_setopt(ch, CURLOPT_USERAGENT, NULL); - } - } - - /* resume */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resume"), IS_LONG)) && (Z_LVAL_P(zoption) > 0)) { - range_req = 1; - curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(zoption)); - } - /* or range of kind array(array(0,499), array(100,1499)) */ - else if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("range"), IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) { - HashPosition pos1, pos2; - zval **rr, **rb, **re; - php_http_buffer_t rs; - - php_http_buffer_init(&rs); - FOREACH_VAL(pos1, zoption, rr) { - if (Z_TYPE_PP(rr) == IS_ARRAY) { - zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2); - if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) { - zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2); - if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) { - if ( ((Z_TYPE_PP(rb) == IS_LONG) || ((Z_TYPE_PP(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(rb), Z_STRLEN_PP(rb), NULL, NULL, 1))) && - ((Z_TYPE_PP(re) == IS_LONG) || ((Z_TYPE_PP(re) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(re), Z_STRLEN_PP(re), NULL, NULL, 1)))) { - zval *rbl = php_http_ztyp(IS_LONG, *rb); - zval *rel = php_http_ztyp(IS_LONG, *re); - - if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) { - php_http_buffer_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel)); - } - zval_ptr_dtor(&rbl); - zval_ptr_dtor(&rel); - } - } - } - } - } - - if (PHP_HTTP_BUFFER_LEN(&rs)) { - zval *cached_range; - - /* ditch last comma */ - PHP_HTTP_BUFFER_VAL(&rs)[PHP_HTTP_BUFFER_LEN(&rs)-- -1] = '\0'; - /* cache string */ - MAKE_STD_ZVAL(cached_range); - ZVAL_STRINGL(cached_range, PHP_HTTP_BUFFER_VAL(&rs), PHP_HTTP_BUFFER_LEN(&rs), 0); - curl_easy_setopt(ch, CURLOPT_RANGE, Z_STRVAL_P(cache_option(&curl->options.cache, ZEND_STRS("range"), 0, cached_range))); - zval_ptr_dtor(&cached_range); - } - } - - /* additional headers, array('name' => 'value') */ - if (curl->options.headers) { - curl_slist_free_all(curl->options.headers); - curl->options.headers = NULL; - } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("headers"), IS_ARRAY))) { - php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0); - zval **header_val; - HashPosition pos; - php_http_buffer_t header; - - php_http_buffer_init(&header); - FOREACH_KEYVAL(pos, zoption, header_key, header_val) { - if (header_key.type == HASH_KEY_IS_STRING) { - zval *header_cpy = php_http_ztyp(IS_STRING, *header_val); - - if (!strcasecmp(header_key.str, "range")) { - range_req = 1; - } - - php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy)); - php_http_buffer_fix(&header); - curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header)); - php_http_buffer_reset(&header); - - zval_ptr_dtor(&header_cpy); - } - } - php_http_buffer_dtor(&header); - } - /* etag */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("etag"), IS_STRING)) && Z_STRLEN_P(zoption)) { - zend_bool is_quoted = !((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')); - php_http_buffer_t header; - - php_http_buffer_init(&header); - php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", range_req?"If-Match":"If-None-Match", Z_STRVAL_P(zoption)); - php_http_buffer_fix(&header); - curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header)); - php_http_buffer_dtor(&header); - } - /* compression */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("compress"), IS_BOOL)) && Z_LVAL_P(zoption)) { - curl->options.headers = curl_slist_append(curl->options.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5"); - } - curl_easy_setopt(ch, CURLOPT_HTTPHEADER, curl->options.headers); - - /* lastmodified */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("lastmodified"), IS_LONG))) { - if (Z_LVAL_P(zoption)) { - if (Z_LVAL_P(zoption) > 0) { - curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(zoption)); - } else { - curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(zoption)); - } - curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE)); - } else { - curl_easy_setopt(ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE); - } - } - - /* cookies, array('name' => 'value') */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookies"), IS_ARRAY))) { - php_http_buffer_dtor(&curl->options.cookies); - if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) { - zval *urlenc_cookies = NULL; - /* check whether cookies should not be urlencoded; default is to urlencode them */ - if ((!(urlenc_cookies = get_option(&curl->options.cache, options, ZEND_STRS("encodecookies"), IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) { - if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(zoption), &curl->options.cookies, ZEND_STRS(";"), ZEND_STRS("="), NULL, 0 TSRMLS_CC)) { - php_http_buffer_fix(&curl->options.cookies); - curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data); - } - } else { - HashPosition pos; - php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0); - zval **cookie_val; - - FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) { - if (cookie_key.type == HASH_KEY_IS_STRING) { - zval *val = php_http_ztyp(IS_STRING, *cookie_val); - php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val)); - zval_ptr_dtor(&val); - } - } - - php_http_buffer_fix(&curl->options.cookies); - if (PHP_HTTP_BUFFER_LEN(&curl->options.cookies)) { - curl_easy_setopt(ch, CURLOPT_COOKIE, PHP_HTTP_BUFFER_VAL(&curl->options.cookies)); - } - } - } - } - - /* don't load session cookies from cookiestore */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiesession"), IS_BOOL)) && Z_BVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 1L); - } - - /* cookiestore, read initial cookies from that file and store cookies back into that file */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiestore"), IS_STRING))) { - php_http_curl_request_storage_t *storage = get_storage(curl->handle); - - if (Z_STRLEN_P(zoption)) { - if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(zoption) TSRMLS_CC)) { - return FAILURE; - } - } - if (storage->cookiestore) { - pefree(storage->cookiestore, 1); - } - storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1); - curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore); - curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore); - } - - /* maxfilesize */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("maxfilesize"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption)); - } - - /* http protocol */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("protocol"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption)); - } - - /* timeout, defaults to 0 */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("timeout"), IS_DOUBLE))) { - curl_easy_setopt(ch, CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000)); - } - /* connecttimeout, defaults to 0 */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("connecttimeout"), IS_DOUBLE))) { - curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000)); - } - - /* ssl */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ssl"), IS_ARRAY))) { - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - zval **param; - HashPosition pos; - - FOREACH_KEYVAL(pos, zoption, key, param) { - if (key.type == HASH_KEY_IS_STRING) { - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0); - - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0); - - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0); - PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0); - - PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1); - PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0); - - PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1); -#if PHP_HTTP_CURL_VERSION(7,19,0) - PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT, -3, 1); - #if defined(PHP_HTTP_HAVE_OPENSSL) - PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE, -3, 1); - #endif -#endif -#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) - PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO, -3); -#endif - } - } - } - return SUCCESS; -} - -static STATUS get_info(CURL *ch, HashTable *info) -{ - char *c; - long l; - double d; - struct curl_slist *s, *p; - zval *subarray, array; - INIT_PZVAL_ARRAY(&array, info); - - /* BEGIN::CURLINFO */ - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &c)) { - add_assoc_string_ex(&array, "effective_url", sizeof("effective_url"), c ? c : "", 1); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) { - add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) { - add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) { - add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) { - add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) { - add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) { - add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) { - add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) { - add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) { - add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) { - add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) { - add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) { - add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) { - add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { - add_assoc_double_ex(&array, "content_length_download", sizeof("content_length_download"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_UPLOAD, &d)) { - add_assoc_double_ex(&array, "content_length_upload", sizeof("content_length_upload"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) { - add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) { - add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) { - add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) { - add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) { - add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) { - add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) { - add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) { - add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) { - add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) { - MAKE_STD_ZVAL(subarray); - array_init(subarray); - for (p = s; p; p = p->next) { - if (p->data) { - add_next_index_string(subarray, p->data, 1); - } - } - add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray); - curl_slist_free_all(s); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_COOKIELIST, &s)) { - MAKE_STD_ZVAL(subarray); - array_init(subarray); - for (p = s; p; p = p->next) { - if (p->data) { - add_next_index_string(subarray, p->data, 1); - } - } - add_assoc_zval_ex(&array, "cookies", sizeof("cookies"), subarray); - curl_slist_free_all(s); - } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) { - add_assoc_string_ex(&array, "redirect_url", sizeof("redirect_url"), c ? c : "", 1); - } -#if PHP_HTTP_CURL_VERSION(7,19,0) - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) { - add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1); - } -#endif -#if PHP_HTTP_CURL_VERSION(7,19,0) - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_APPCONNECT_TIME, &d)) { - add_assoc_double_ex(&array, "appconnect_time", sizeof("appconnect_time"), d); - } -#endif -#if PHP_HTTP_CURL_VERSION(7,19,4) - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONDITION_UNMET, &l)) { - add_assoc_long_ex(&array, "condition_unmet", sizeof("condition_unmet"), l); - } -#endif -#if PHP_HTTP_CURL_VERSION(7,21,0) - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_PORT, &l)) { - add_assoc_long_ex(&array, "primary_port", sizeof("primary_port"), l); - } -#endif -#if PHP_HTTP_CURL_VERSION(7,21,0) - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_IP, &c)) { - add_assoc_string_ex(&array, "local_ip", sizeof("local_ip"), c ? c : "", 1); - } -#endif -#if PHP_HTTP_CURL_VERSION(7,21,0) - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_PORT, &l)) { - add_assoc_long_ex(&array, "local_port", sizeof("local_port"), l); - } -#endif - - /* END::CURLINFO */ - -#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) - { - int i; - zval *ci_array; - struct curl_certinfo *ci; - char *colon, *keyname; - - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) { - MAKE_STD_ZVAL(ci_array); - array_init(ci_array); - - for (i = 0; i < ci->num_of_certs; ++i) { - s = ci->certinfo[i]; - - MAKE_STD_ZVAL(subarray); - array_init(subarray); - for (p = s; p; p = p->next) { - if (p->data) { - if ((colon = strchr(p->data, ':'))) { - keyname = estrndup(p->data, colon - p->data); - add_assoc_string_ex(subarray, keyname, colon - p->data + 1, colon + 1, 1); - efree(keyname); - } else { - add_next_index_string(subarray, p->data, 1); - } - } - } - add_next_index_zval(ci_array, subarray); - } - add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array); - } - } -#endif - add_assoc_string_ex(&array, "error", sizeof("error"), get_storage(ch)->errorbuffer, 1); - - return SUCCESS; -} - - -/* request datashare handler ops */ - -static php_http_request_datashare_t *php_http_curl_request_datashare_init(php_http_request_datashare_t *h, void *handle) -{ - php_http_curl_request_datashare_t *curl; - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "could not initialize curl share handle"); - return NULL; - } - - curl = ecalloc(1, sizeof(*curl)); - curl->handle = handle; - h->ctx = curl; - - return h; -} - -static void php_http_curl_request_datashare_dtor(php_http_request_datashare_t *h) -{ - php_http_curl_request_datashare_t *curl = h->ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - - php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC); - - efree(curl); - h->ctx = NULL; -} - -static STATUS php_http_curl_request_datashare_attach(php_http_request_datashare_t *h, php_http_request_t *r) -{ - CURLcode rc; - php_http_curl_request_datashare_t *curl = h->ctx; - php_http_curl_request_t *recurl = r->ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, curl->handle))) { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not attach request to the datashare: %s", curl_easy_strerror(rc)); - return FAILURE; - } - return SUCCESS; -} - -static STATUS php_http_curl_request_datashare_detach(php_http_request_datashare_t *h, php_http_request_t *r) -{ - CURLcode rc; - php_http_curl_request_t *recurl = r->ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - - - if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, NULL))) { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not detach request from the datashare: %s", curl_share_strerror(rc)); - return FAILURE; - } - return SUCCESS; -} - -static STATUS php_http_curl_request_datashare_setopt(php_http_request_datashare_t *h, php_http_request_datashare_setopt_opt_t opt, void *arg) -{ - CURLSHcode rc; - php_http_curl_request_datashare_t *curl = h->ctx; - - switch (opt) { - case PHP_HTTP_REQUEST_DATASHARE_OPT_COOKIES: - if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE))) { - TSRMLS_FETCH_FROM_CTX(h->ts); - - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of cookie data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc)); - return FAILURE; - } - break; - - case PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER: - if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_DNS))) { - TSRMLS_FETCH_FROM_CTX(h->ts); - - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of resolver data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc)); - return FAILURE; - } - break; - -#if PHP_HTTP_CURL_VERSION(7,23,0) - case PHP_HTTP_REQUEST_DATASHARE_OPT_SSLSESSIONS: - if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_SSL_SESSION))) { - TSRMLS_FETCH_FROM_CTX(h->ts); - - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of SSL session data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc)); - return FAILURE; - } - break; -#endif - - default: - return FAILURE; - } - - return SUCCESS; -} - -static php_http_resource_factory_ops_t php_http_curlsh_resource_factory_ops = { - php_http_curlsh_ctor, - NULL, - php_http_curlsh_dtor -}; - -static php_http_request_datashare_ops_t php_http_curl_request_datashare_ops = { - &php_http_curlsh_resource_factory_ops, - php_http_curl_request_datashare_init, - NULL /* copy */, - php_http_curl_request_datashare_dtor, - NULL /*reset */, - php_http_curl_request_datashare_attach, - php_http_curl_request_datashare_detach, - php_http_curl_request_datashare_setopt, -}; - -PHP_HTTP_API php_http_request_datashare_ops_t *php_http_curl_get_request_datashare_ops(void) -{ - return &php_http_curl_request_datashare_ops; -} - - -/* request pool handler ops */ - -static php_http_request_pool_t *php_http_curl_request_pool_init(php_http_request_pool_t *h, void *handle) -{ - php_http_curl_request_pool_t *curl; - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "could not initialize curl pool handle"); - return NULL; - } - - curl = ecalloc(1, sizeof(*curl)); - curl->handle = handle; - curl->unfinished = 0; - h->ctx = curl; - - return h; -} - -static void php_http_curl_request_pool_dtor(php_http_request_pool_t *h) -{ - php_http_curl_request_pool_t *curl = h->ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - -#if PHP_HTTP_HAVE_EVENT - if (curl->timeout) { - efree(curl->timeout); - curl->timeout = NULL; - } -#endif - curl->unfinished = 0; - php_http_request_pool_reset(h); - - php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC); - - efree(curl); - h->ctx = NULL; -} - -static STATUS php_http_curl_request_pool_attach(php_http_request_pool_t *h, php_http_request_t *r, const char *m, const char *url, php_http_message_body_t *body) -{ - php_http_curl_request_pool_t *curl = h->ctx; - php_http_curl_request_t *recurl = r->ctx; - CURLMcode rs; - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (SUCCESS != php_http_curl_request_prepare(r, m, url, body)) { - return FAILURE; - } - - if (CURLM_OK == (rs = curl_multi_add_handle(curl->handle, recurl->handle))) { - ++curl->unfinished; - return SUCCESS; - } else { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not attach request to pool: %s", curl_multi_strerror(rs)); - return FAILURE; - } -} - -static STATUS php_http_curl_request_pool_detach(php_http_request_pool_t *h, php_http_request_t *r) -{ - php_http_curl_request_pool_t *curl = h->ctx; - php_http_curl_request_t *recurl = r->ctx; - CURLMcode rs = curl_multi_remove_handle(curl->handle, recurl->handle); - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (CURLM_OK == rs) { - return SUCCESS; - } else { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not detach request from pool: %s", curl_multi_strerror(rs)); - return FAILURE; - } -} - -#ifdef PHP_WIN32 -# define SELECT_ERROR SOCKET_ERROR -#else -# define SELECT_ERROR -1 -#endif - -static STATUS php_http_curl_request_pool_wait(php_http_request_pool_t *h, struct timeval *custom_timeout) -{ - int MAX; - fd_set R, W, E; - struct timeval timeout; - php_http_curl_request_pool_t *curl = h->ctx; - -#if PHP_HTTP_HAVE_EVENT - if (curl->useevents) { - TSRMLS_FETCH_FROM_CTX(h->ts); - - php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented"); - return FAILURE; - } -#endif - - if (custom_timeout && timerisset(custom_timeout)) { - timeout = *custom_timeout; - } else { - long max_tout = 1000; - - if ((CURLM_OK == curl_multi_timeout(curl->handle, &max_tout)) && (max_tout > 0)) { - timeout.tv_sec = max_tout / 1000; - timeout.tv_usec = (max_tout % 1000) * 1000; - } else { - timeout.tv_sec = 0; - timeout.tv_usec = 1000; - } - } - - FD_ZERO(&R); - FD_ZERO(&W); - FD_ZERO(&E); - - if (CURLM_OK == curl_multi_fdset(curl->handle, &R, &W, &E, &MAX)) { - if (MAX == -1) { - php_http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / PHP_HTTP_MCROSEC)); - return SUCCESS; - } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) { - return SUCCESS; - } - } - return FAILURE; -} - -static int php_http_curl_request_pool_once(php_http_request_pool_t *h) -{ - php_http_curl_request_pool_t *curl = h->ctx; - -#if PHP_HTTP_HAVE_EVENT - if (curl->useevents) { - TSRMLS_FETCH_FROM_CTX(h->ts); - php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented"); - return FAILURE; - } -#endif - - while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle, &curl->unfinished)); - - php_http_curl_request_pool_responsehandler(h); - - return curl->unfinished; - -} -#if PHP_HTTP_HAVE_EVENT -static void dolog(int i, const char *m) { - fprintf(stderr, "%d: %s\n", i, m); -} -#endif -static STATUS php_http_curl_request_pool_exec(php_http_request_pool_t *h) -{ - TSRMLS_FETCH_FROM_CTX(h->ts); - -#if PHP_HTTP_HAVE_EVENT - php_http_curl_request_pool_t *curl = h->ctx; - - if (curl->useevents) { - event_set_log_callback(dolog); - do { -#if DBG_EVENTS - fprintf(stderr, "X"); -#endif - event_base_dispatch(PHP_HTTP_G->curl.event_base); - } while (curl->unfinished); - } else -#endif - { - while (php_http_curl_request_pool_once(h)) { - if (SUCCESS != php_http_curl_request_pool_wait(h, NULL)) { -#ifdef PHP_WIN32 - /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */ - php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError()); -#else - php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, strerror(errno)); -#endif - return FAILURE; - } - } - } - - return SUCCESS; -} - -static STATUS php_http_curl_request_pool_setopt(php_http_request_pool_t *h, php_http_request_pool_setopt_opt_t opt, void *arg) -{ - php_http_curl_request_pool_t *curl = h->ctx; - - switch (opt) { - case PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING: - if (CURLM_OK != curl_multi_setopt(curl->handle, CURLMOPT_PIPELINING, (long) *((zend_bool *) arg))) { - return FAILURE; - } - break; - - case PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS: -#if PHP_HTTP_HAVE_EVENT - if ((curl->useevents = *((zend_bool *) arg))) { - if (!curl->timeout) { - curl->timeout = ecalloc(1, sizeof(struct event)); - } - curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, h); - curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, php_http_curl_request_pool_socket_callback); - curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, h); - curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, php_http_curl_request_pool_timer_callback); - } else { - curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, NULL); - curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, NULL); - curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, NULL); - curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, NULL); - } - break; -#endif - - default: - return FAILURE; - } - return SUCCESS; -} - -static php_http_resource_factory_ops_t php_http_curlm_resource_factory_ops = { - php_http_curlm_ctor, - NULL, - php_http_curlm_dtor -}; - -static php_http_request_pool_ops_t php_http_curl_request_pool_ops = { - &php_http_curlm_resource_factory_ops, - php_http_curl_request_pool_init, - NULL /* copy */, - php_http_curl_request_pool_dtor, - NULL /*reset */, - php_http_curl_request_pool_exec, - php_http_curl_request_pool_wait, - php_http_curl_request_pool_once, - php_http_curl_request_pool_attach, - php_http_curl_request_pool_detach, - php_http_curl_request_pool_setopt, -}; - -PHP_HTTP_API php_http_request_pool_ops_t *php_http_curl_get_request_pool_ops(void) -{ - return &php_http_curl_request_pool_ops; -} - -/* request handler ops */ - -static STATUS php_http_curl_request_reset(php_http_request_t *h); - -static php_http_request_t *php_http_curl_request_init(php_http_request_t *h, void *handle) -{ - php_http_curl_request_t *ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "could not initialize curl handle"); - return NULL; - } - - ctx = ecalloc(1, sizeof(*ctx)); - ctx->handle = handle; - php_http_buffer_init(&ctx->options.cookies); - zend_hash_init(&ctx->options.cache, 0, NULL, ZVAL_PTR_DTOR, 0); - h->ctx = ctx; - -#if defined(ZTS) - curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L); -#endif - curl_easy_setopt(handle, CURLOPT_HEADER, 0L); - curl_easy_setopt(handle, CURLOPT_FILETIME, 1L); - curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L); - curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); - curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L); - curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, NULL); - curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, php_http_curl_dummy_callback); - curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, php_http_curl_raw_callback); - curl_easy_setopt(handle, CURLOPT_READFUNCTION, php_http_curl_read_callback); - curl_easy_setopt(handle, CURLOPT_IOCTLFUNCTION, php_http_curl_ioctl_callback); - curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, php_http_curl_progress_callback); - curl_easy_setopt(handle, CURLOPT_DEBUGDATA, h); - curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, h); - - php_http_curl_request_reset(h); - - return h; -} - -static php_http_request_t *php_http_curl_request_copy(php_http_request_t *from, php_http_request_t *to) -{ - php_http_curl_request_t *ctx = from->ctx; - void *copy; - TSRMLS_FETCH_FROM_CTX(from->ts); - - if (!(copy = php_http_resource_factory_handle_copy(from->rf, ctx->handle TSRMLS_CC))) { - return NULL; - } - - if (to) { - return php_http_curl_request_init(to, copy); - } else { - return php_http_request_init(NULL, from->ops, from->rf, copy TSRMLS_CC); - } -} - -static void php_http_curl_request_dtor(php_http_request_t *h) -{ - php_http_curl_request_t *ctx = h->ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - - curl_easy_setopt(ctx->handle, CURLOPT_NOPROGRESS, 1L); - curl_easy_setopt(ctx->handle, CURLOPT_PROGRESSFUNCTION, NULL); - curl_easy_setopt(ctx->handle, CURLOPT_VERBOSE, 0L); - curl_easy_setopt(ctx->handle, CURLOPT_DEBUGFUNCTION, NULL); - - php_http_resource_factory_handle_dtor(h->rf, ctx->handle TSRMLS_CC); - - php_http_buffer_dtor(&ctx->options.cookies); - zend_hash_destroy(&ctx->options.cache); - - if (ctx->options.headers) { - curl_slist_free_all(ctx->options.headers); - ctx->options.headers = NULL; - } - php_http_request_progress_dtor(&ctx->progress TSRMLS_CC); - - efree(ctx); - h->ctx = NULL; -} -static STATUS php_http_curl_request_reset(php_http_request_t *h) -{ - CURL *ch = ((php_http_curl_request_t *) h->ctx)->handle; - php_http_curl_request_storage_t *st; - - if ((st = get_storage(ch))) { - if (st->url) { - pefree(st->url, 1); - st->url = NULL; - } - if (st->cookiestore) { - pefree(st->cookiestore, 1); - st->cookiestore = NULL; - } - st->errorbuffer[0] = '\0'; - } - - curl_easy_setopt(ch, CURLOPT_URL, NULL); -#if PHP_HTTP_CURL_VERSION(7,19,4) - curl_easy_setopt(ch, CURLOPT_NOPROXY, NULL); -#endif - curl_easy_setopt(ch, CURLOPT_PROXY, NULL); - curl_easy_setopt(ch, CURLOPT_PROXYPORT, 0L); - curl_easy_setopt(ch, CURLOPT_PROXYTYPE, 0L); - /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */ -#if PHP_HTTP_CURL_VERSION(7,19,1) - curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, NULL); - curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, NULL); -#endif - curl_easy_setopt(ch, CURLOPT_PROXYAUTH, 0L); - curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 0L); - curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, 60L); - curl_easy_setopt(ch, CURLOPT_IPRESOLVE, 0); -#if PHP_HTTP_CURL_VERSION(7,21,3) - curl_easy_setopt(ch, CURLOPT_RESOLVE, NULL); -#endif -#if PHP_HTTP_CURL_VERSION(7,24,0) - curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, NULL); -#endif - curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, 0L); - curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, 0L); - /* LFS weirdance - curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0); - curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0); - */ - /* crashes - curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, 5L); */ - curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 0L); - curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 0L); - curl_easy_setopt(ch, CURLOPT_INTERFACE, NULL); - curl_easy_setopt(ch, CURLOPT_PORT, 0L); -#if PHP_HTTP_CURL_VERSION(7,19,0) - curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, 0L); -#endif - curl_easy_setopt(ch, CURLOPT_LOCALPORT, 0L); - curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, 0L); - /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */ -#if PHP_HTTP_CURL_VERSION(7,19,1) - curl_easy_setopt(ch, CURLOPT_USERNAME, NULL); - curl_easy_setopt(ch, CURLOPT_PASSWORD, NULL); -#endif - curl_easy_setopt(ch, CURLOPT_HTTPAUTH, 0L); - curl_easy_setopt(ch, CURLOPT_ENCODING, NULL); - /* we do this ourself anyway */ - curl_easy_setopt(ch, CURLOPT_HTTP_CONTENT_DECODING, 0L); - curl_easy_setopt(ch, CURLOPT_HTTP_TRANSFER_DECODING, 0L); - curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0L); -#if PHP_HTTP_CURL_VERSION(7,19,1) - curl_easy_setopt(ch, CURLOPT_POSTREDIR, 0L); -#else - curl_easy_setopt(ch, CURLOPT_POST301, 0L); -#endif - curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, 0L); - curl_easy_setopt(ch, CURLOPT_REFERER, NULL); - curl_easy_setopt(ch, CURLOPT_USERAGENT, "PECL::HTTP/" PHP_HTTP_EXT_VERSION " (PHP/" PHP_VERSION ")"); - curl_easy_setopt(ch, CURLOPT_HTTPHEADER, NULL); - curl_easy_setopt(ch, CURLOPT_COOKIE, NULL); - curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 0L); - /* these options would enable curl's cookie engine by default which we don't want - curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL); - curl_easy_setopt(ch, CURLOPT_COOKIEJAR, NULL); */ - curl_easy_setopt(ch, CURLOPT_COOKIELIST, NULL); - curl_easy_setopt(ch, CURLOPT_RANGE, NULL); - curl_easy_setopt(ch, CURLOPT_RESUME_FROM, 0L); - curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, 0L); - curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0L); - curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0L); - curl_easy_setopt(ch, CURLOPT_TIMEOUT, 0L); - curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, 3); - curl_easy_setopt(ch, CURLOPT_SSLCERT, NULL); - curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, NULL); - curl_easy_setopt(ch, CURLOPT_SSLCERTPASSWD, NULL); - curl_easy_setopt(ch, CURLOPT_SSLKEY, NULL); - curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, NULL); - curl_easy_setopt(ch, CURLOPT_SSLKEYPASSWD, NULL); - curl_easy_setopt(ch, CURLOPT_SSLENGINE, NULL); - curl_easy_setopt(ch, CURLOPT_SSLVERSION, 0L); - curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L); - curl_easy_setopt(ch, CURLOPT_SSL_CIPHER_LIST, NULL); -#if PHP_HTTP_CURL_VERSION(7,19,0) - curl_easy_setopt(ch, CURLOPT_ISSUERCERT, NULL); -#if defined(PHP_HTTP_HAVE_OPENSSL) - curl_easy_setopt(ch, CURLOPT_CRLFILE, NULL); -#endif -#endif -#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) - curl_easy_setopt(ch, CURLOPT_CERTINFO, NULL); -#endif -#ifdef PHP_HTTP_CURL_CAINFO - curl_easy_setopt(ch, CURLOPT_CAINFO, PHP_HTTP_CURL_CAINFO); -#else - curl_easy_setopt(ch, CURLOPT_CAINFO, NULL); -#endif - curl_easy_setopt(ch, CURLOPT_CAPATH, NULL); - curl_easy_setopt(ch, CURLOPT_RANDOM_FILE, NULL); - curl_easy_setopt(ch, CURLOPT_EGDSOCKET, NULL); - curl_easy_setopt(ch, CURLOPT_POSTFIELDS, NULL); - curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, 0L); - curl_easy_setopt(ch, CURLOPT_HTTPPOST, NULL); - curl_easy_setopt(ch, CURLOPT_IOCTLDATA, NULL); - curl_easy_setopt(ch, CURLOPT_READDATA, NULL); - curl_easy_setopt(ch, CURLOPT_INFILESIZE, 0L); - curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE); - curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, NULL); - curl_easy_setopt(ch, CURLOPT_NOBODY, 0L); - curl_easy_setopt(ch, CURLOPT_POST, 0L); - curl_easy_setopt(ch, CURLOPT_UPLOAD, 0L); - curl_easy_setopt(ch, CURLOPT_HTTPGET, 1L); - - return SUCCESS; -} - -static STATUS php_http_curl_request_exec(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body) -{ - uint tries = 0; - CURLcode result; - php_http_curl_request_t *curl = h->ctx; - php_http_curl_request_storage_t *storage = get_storage(curl->handle); - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (SUCCESS != php_http_curl_request_prepare(h, meth, url, body)) { - return FAILURE; - } - -retry: - if (CURLE_OK != (result = curl_easy_perform(curl->handle))) { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), storage->errorbuffer, storage->url); - - if (EG(exception)) { - add_property_long(EG(exception), "curlCode", result); - } - - if (curl->options.retry.count > tries++) { - switch (result) { - case CURLE_COULDNT_RESOLVE_PROXY: - case CURLE_COULDNT_RESOLVE_HOST: - case CURLE_COULDNT_CONNECT: - case CURLE_WRITE_ERROR: - case CURLE_READ_ERROR: - case CURLE_OPERATION_TIMEDOUT: - case CURLE_SSL_CONNECT_ERROR: - case CURLE_GOT_NOTHING: - case CURLE_SSL_ENGINE_SETFAILED: - case CURLE_SEND_ERROR: - case CURLE_RECV_ERROR: - case CURLE_SSL_ENGINE_INITFAILED: - case CURLE_LOGIN_DENIED: - if (curl->options.retry.delay >= PHP_HTTP_DIFFSEC) { - php_http_sleep(curl->options.retry.delay); - } - goto retry; - default: - break; - } - } else { - return FAILURE; - } - } - - return SUCCESS; -} - -static STATUS php_http_curl_request_setopt(php_http_request_t *h, php_http_request_setopt_opt_t opt, void *arg) -{ - php_http_curl_request_t *curl = h->ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - - switch (opt) { - case PHP_HTTP_REQUEST_OPT_SETTINGS: - return set_options(h, arg); - break; - - case PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK: - if (curl->progress.in_cb) { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Cannot change progress callback while executing it"); - return FAILURE; - } - if (curl->progress.callback) { - php_http_request_progress_dtor(&curl->progress TSRMLS_CC); - } - curl->progress.callback = arg; - break; - - case PHP_HTTP_REQUEST_OPT_COOKIES_ENABLE: - /* are cookies already enabled anyway? */ - if (!get_storage(curl->handle)->cookiestore) { - if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIEFILE, "")) { - return FAILURE; - } - } - break; - - case PHP_HTTP_REQUEST_OPT_COOKIES_RESET: - if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "ALL")) { - return FAILURE; - } - break; - - case PHP_HTTP_REQUEST_OPT_COOKIES_RESET_SESSION: - if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "SESS")) { - return FAILURE; - } - break; - - case PHP_HTTP_REQUEST_OPT_COOKIES_FLUSH: - if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "FLUSH")) { - return FAILURE; - } - break; - - default: - return FAILURE; - } - - return SUCCESS; -} - -static STATUS php_http_curl_request_getopt(php_http_request_t *h, php_http_request_getopt_opt_t opt, void *arg) -{ - php_http_curl_request_t *curl = h->ctx; - - switch (opt) { - case PHP_HTTP_REQUEST_OPT_PROGRESS_INFO: - *((php_http_request_progress_t **) arg) = &curl->progress; - break; - - case PHP_HTTP_REQUEST_OPT_TRANSFER_INFO: - get_info(curl->handle, arg); - break; - - default: - return FAILURE; - } - - return SUCCESS; -} - -static php_http_resource_factory_ops_t php_http_curl_resource_factory_ops = { - php_http_curl_ctor, - php_http_curl_copy, - php_http_curl_dtor -}; - -static php_http_request_ops_t php_http_curl_request_ops = { - &php_http_curl_resource_factory_ops, - php_http_curl_request_init, - php_http_curl_request_copy, - php_http_curl_request_dtor, - php_http_curl_request_reset, - php_http_curl_request_exec, - php_http_curl_request_setopt, - php_http_curl_request_getopt -}; - -PHP_HTTP_API php_http_request_ops_t *php_http_curl_get_request_ops(void) -{ - return &php_http_curl_request_ops; -} - #if defined(ZTS) && defined(PHP_HTTP_HAVE_SSL) # ifdef PHP_WIN32 # define PHP_HTTP_NEED_OPENSSL_TSL @@ -1976,32 +88,8 @@ static struct gcry_thread_cbs php_http_gnutls_tsl = { }; #endif -#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpCURL, method, 0, req_args) -#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpCURL, method, 0) -#define PHP_HTTP_CURL_ME(method, visibility) PHP_ME(HttpCURL, method, PHP_HTTP_ARGS(HttpCURL, method), visibility) -#define PHP_HTTP_CURL_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpCURL, method)) -#define PHP_HTTP_CURL_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpCURL_##al), PHP_HTTP_ARGS(HttpCURL, al), vis) - -PHP_HTTP_EMPTY_ARGS(__construct); - -zend_class_entry *php_http_curl_class_entry; -zend_function_entry php_http_curl_method_entry[] = { - PHP_HTTP_CURL_ME(__construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR) - - EMPTY_FUNCTION_ENTRY -}; - -PHP_METHOD(HttpCURL, __construct) { -} - PHP_MINIT_FUNCTION(http_curl) { - php_http_request_factory_driver_t driver = { - &php_http_curl_request_ops, - &php_http_curl_request_pool_ops, - &php_http_curl_request_datashare_ops - }; - #ifdef PHP_HTTP_NEED_OPENSSL_TSL /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */ if (!CRYPTO_get_id_callback()) { @@ -2025,80 +113,6 @@ PHP_MINIT_FUNCTION(http_curl) return FAILURE; } - if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request_datashare.curl"), &php_http_curlsh_resource_factory_ops, NULL, NULL)) { - return FAILURE; - } - - if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request_pool.curl"), &php_http_curlm_resource_factory_ops, NULL, NULL)) { - return FAILURE; - } - - if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request.curl"), &php_http_curl_resource_factory_ops, NULL, NULL)) { - return FAILURE; - } - - if (SUCCESS != php_http_request_factory_add_driver(ZEND_STRL("curl"), &driver)) { - return FAILURE; - } - - PHP_HTTP_REGISTER_CLASS(http, CURL, http_curl, php_http_curl_class_entry, 0); - - /* - * HTTP Protocol Version Constants - */ - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_1_0"), CURL_HTTP_VERSION_1_0 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_1_1"), CURL_HTTP_VERSION_1_1 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_NONE"), CURL_HTTP_VERSION_NONE TSRMLS_CC); /* to be removed */ - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_ANY"), CURL_HTTP_VERSION_NONE TSRMLS_CC); - - /* - * SSL Version Constants - */ - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_TLSv1"), CURL_SSLVERSION_TLSv1 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_SSLv2"), CURL_SSLVERSION_SSLv2 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_SSLv3"), CURL_SSLVERSION_SSLv3 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_ANY"), CURL_SSLVERSION_DEFAULT TSRMLS_CC); - - /* - * DNS IPvX resolving - */ - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("IPRESOLVE_V4"), CURL_IPRESOLVE_V4 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("IPRESOLVE_V6"), CURL_IPRESOLVE_V6 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("IPRESOLVE_ANY"), CURL_IPRESOLVE_WHATEVER TSRMLS_CC); - - /* - * Auth Constants - */ - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_BASIC"), CURLAUTH_BASIC TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_DIGEST"), CURLAUTH_DIGEST TSRMLS_CC); -#if PHP_HTTP_CURL_VERSION(7,19,3) - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_DIGEST_IE"), CURLAUTH_DIGEST_IE TSRMLS_CC); -#endif - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_NTLM"), CURLAUTH_NTLM TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_GSSNEG"), CURLAUTH_GSSNEGOTIATE TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_ANY"), CURLAUTH_ANY TSRMLS_CC); - - /* - * Proxy Type Constants - */ - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS4"), CURLPROXY_SOCKS4 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS4A"), CURLPROXY_SOCKS5 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS5_HOSTNAME"), CURLPROXY_SOCKS5 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS5"), CURLPROXY_SOCKS5 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_HTTP"), CURLPROXY_HTTP TSRMLS_CC); -# if PHP_HTTP_CURL_VERSION(7,19,4) - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_HTTP_1_0"), CURLPROXY_HTTP_1_0 TSRMLS_CC); -# endif - - /* - * Post Redirection Constants - */ -#if PHP_HTTP_CURL_VERSION(7,19,1) - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("POSTREDIR_301"), CURL_REDIR_POST_301 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("POSTREDIR_302"), CURL_REDIR_POST_302 TSRMLS_CC); - zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("POSTREDIR_ALL"), CURL_REDIR_POST_ALL TSRMLS_CC); -#endif - return SUCCESS; } @@ -2123,17 +137,6 @@ PHP_MSHUTDOWN_FUNCTION(http_curl) return SUCCESS; } -PHP_RINIT_FUNCTION(http_curl) -{ -#if PHP_HTTP_HAVE_EVENT - if (!PHP_HTTP_G->curl.event_base && !(PHP_HTTP_G->curl.event_base = event_init())) { - return FAILURE; - } -#endif - - return SUCCESS; -} - #endif /* PHP_HTTP_HAVE_CURL */ /* diff --git a/php_http_curl.h b/php_http_curl.h index 398c3ae..709ba22 100644 --- a/php_http_curl.h +++ b/php_http_curl.h @@ -15,32 +15,14 @@ #if PHP_HTTP_HAVE_CURL -#include "php_http_request.h" -#include "php_http_request_pool.h" -#include "php_http_request_datashare.h" - -PHP_HTTP_API php_http_request_ops_t *php_http_curl_get_request_ops(void); -PHP_HTTP_API php_http_request_pool_ops_t *php_http_curl_get_request_pool_ops(void); -PHP_HTTP_API php_http_request_datashare_ops_t *php_http_curl_get_request_datashare_ops(void); +#include +#define PHP_HTTP_CURL_VERSION(x, y, z) (LIBCURL_VERSION_NUM >= (((x)<<16) + ((y)<<8) + (z))) extern PHP_MINIT_FUNCTION(http_curl); extern PHP_MSHUTDOWN_FUNCTION(http_curl); -extern PHP_RINIT_FUNCTION(http_curl); - -#if PHP_HTTP_HAVE_EVENT -struct php_http_curl_globals { - void *event_base; -}; -#endif - -extern zend_class_entry *php_http_curl_class_entry; -extern zend_function_entry php_http_curl_method_entry[]; - -#define php_http_curl_new php_http_object_new - -PHP_METHOD(HttpCURL, __construct); #endif /* PHP_HTTP_HAVE_CURL */ + #endif /* PHP_HTTP_CURL_H */ /* diff --git a/php_http_exception.c b/php_http_exception.c index 99c3648..cd5dcfe 100644 --- a/php_http_exception.c +++ b/php_http_exception.c @@ -49,11 +49,9 @@ PHP_MINIT_FUNCTION(http_exception) zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_MESSAGE_TYPE"), PHP_HTTP_E_MESSAGE_TYPE TSRMLS_CC); zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_MESSAGE_BODY"), PHP_HTTP_E_MESSAGE_BODY TSRMLS_CC); zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_ENCODING"), PHP_HTTP_E_ENCODING TSRMLS_CC); - zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_REQUEST"), PHP_HTTP_E_REQUEST TSRMLS_CC); - zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_REQUEST_POOL"), PHP_HTTP_E_REQUEST_POOL TSRMLS_CC); - zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_REQUEST_DATASHARE"), PHP_HTTP_E_REQUEST_DATASHARE TSRMLS_CC); - zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_REQUEST_FACTORY"), PHP_HTTP_E_REQUEST_FACTORY TSRMLS_CC); - zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_REQUEST_METHOD"), PHP_HTTP_E_REQUEST_METHOD TSRMLS_CC); + zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_CLIENT"), PHP_HTTP_E_CLIENT TSRMLS_CC); + zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_CLIENT_POOL"), PHP_HTTP_E_CLIENT_POOL TSRMLS_CC); + zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_CLIENT_DATASHARE"), PHP_HTTP_E_CLIENT_DATASHARE TSRMLS_CC); zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_SOCKET"), PHP_HTTP_E_SOCKET TSRMLS_CC); zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_RESPONSE"), PHP_HTTP_E_RESPONSE TSRMLS_CC); zend_declare_class_constant_long(php_http_exception_class_entry, ZEND_STRL("E_URL"), PHP_HTTP_E_URL TSRMLS_CC); diff --git a/php_http_message.c b/php_http_message.c index b25f697..8ba4f49 100644 --- a/php_http_message.c +++ b/php_http_message.c @@ -542,6 +542,10 @@ PHP_HTTP_BEGIN_ARGS(setBody, 1) PHP_HTTP_ARG_VAL(body, 0) PHP_HTTP_END_ARGS; +PHP_HTTP_BEGIN_ARGS(addBody, 1) + PHP_HTTP_ARG_VAL(body, 0) +PHP_HTTP_END_ARGS; + PHP_HTTP_BEGIN_ARGS(getHeader, 1) PHP_HTTP_ARG_VAL(header, 0) PHP_HTTP_END_ARGS; @@ -610,7 +614,7 @@ PHP_HTTP_BEGIN_ARGS(toCallback, 1) PHP_HTTP_ARG_VAL(callback, 0) PHP_HTTP_END_ARGS; PHP_HTTP_BEGIN_ARGS(toStream, 1) - HTTP_ARG_VAL(stream, 0) + PHP_HTTP_ARG_VAL(stream, 0) PHP_HTTP_END_ARGS; PHP_HTTP_EMPTY_ARGS(count); @@ -647,6 +651,7 @@ zend_function_entry php_http_message_method_entry[] = { PHP_HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_HTTP_MESSAGE_ME(getBody, ZEND_ACC_PUBLIC) PHP_HTTP_MESSAGE_ME(setBody, ZEND_ACC_PUBLIC) + PHP_HTTP_MESSAGE_ME(addBody, ZEND_ACC_PUBLIC) PHP_HTTP_MESSAGE_ME(getHeader, ZEND_ACC_PUBLIC) PHP_HTTP_MESSAGE_ME(setHeader, ZEND_ACC_PUBLIC) PHP_HTTP_MESSAGE_ME(addHeader, ZEND_ACC_PUBLIC) @@ -1258,6 +1263,20 @@ PHP_METHOD(HttpMessage, setBody) RETVAL_ZVAL(getThis(), 1, 0); } +PHP_METHOD(HttpMessage, addBody) +{ + zval *new_body; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &new_body, php_http_message_body_class_entry)) { + php_http_message_body_object_t *old_obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_message_body_object_t *new_obj = zend_object_store_get_object(new_body TSRMLS_CC); + + php_http_message_body_to_callback(old_obj->body, (php_http_pass_callback_t) php_http_message_body_append, new_obj->body, 0, 0); + } + RETVAL_ZVAL(getThis(), 1, 0); +} + + PHP_METHOD(HttpMessage, getHeader) { char *header_str; diff --git a/php_http_message.h b/php_http_message.h index 81e1dd2..c78101d 100644 --- a/php_http_message.h +++ b/php_http_message.h @@ -94,6 +94,7 @@ extern void php_http_message_object_free(void *object TSRMLS_DC); PHP_METHOD(HttpMessage, __construct); PHP_METHOD(HttpMessage, getBody); PHP_METHOD(HttpMessage, setBody); +PHP_METHOD(HttpMessage, addBody); PHP_METHOD(HttpMessage, getHeader); PHP_METHOD(HttpMessage, setHeader); PHP_METHOD(HttpMessage, addHeader); diff --git a/php_http_misc.h b/php_http_misc.h index 19ae642..33be199 100644 --- a/php_http_misc.h +++ b/php_http_misc.h @@ -461,9 +461,9 @@ typedef enum php_http_error { PHP_HTTP_E_MESSAGE_TYPE, PHP_HTTP_E_MESSAGE_BODY, PHP_HTTP_E_ENCODING, - PHP_HTTP_E_REQUEST, - PHP_HTTP_E_REQUEST_POOL, - PHP_HTTP_E_REQUEST_DATASHARE, + PHP_HTTP_E_CLIENT, + PHP_HTTP_E_CLIENT_POOL, + PHP_HTTP_E_CLIENT_DATASHARE, PHP_HTTP_E_REQUEST_FACTORY, PHP_HTTP_E_SOCKET, PHP_HTTP_E_RESPONSE, diff --git a/php_http_request.c b/php_http_request.c deleted file mode 100644 index a7086d3..0000000 --- a/php_http_request.c +++ /dev/null @@ -1,1351 +0,0 @@ -/* - +--------------------------------------------------------------------+ - | PECL :: http | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted provided that the conditions mentioned | - | in the accompanying LICENSE file are met. | - +--------------------------------------------------------------------+ - | Copyright (c) 2004-2011, Michael Wallner | - +--------------------------------------------------------------------+ -*/ - -#include "php_http_api.h" - -#include - -PHP_HTTP_API php_http_request_t *php_http_request_init(php_http_request_t *h, php_http_request_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC) -{ - php_http_request_t *free_h = NULL; - - if (!h) { - free_h = h = emalloc(sizeof(*h)); - } - memset(h, 0, sizeof(*h)); - - h->ops = ops; - h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, h, NULL); - h->buffer = php_http_buffer_init(NULL); - h->parser = php_http_message_parser_init(NULL TSRMLS_CC); - h->message = php_http_message_init(NULL, 0 TSRMLS_CC); - - TSRMLS_SET_CTX(h->ts); - - if (h->ops->init) { - if (!(h = h->ops->init(h, init_arg))) { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Could not initialize request"); - if (free_h) { - h->ops->dtor = NULL; - php_http_request_free(&free_h); - } - } - } - - return h; -} - -PHP_HTTP_API void php_http_request_dtor(php_http_request_t *h) -{ - if (h->ops->dtor) { - h->ops->dtor(h); - } - - php_http_resource_factory_free(&h->rf); - - php_http_message_parser_free(&h->parser); - php_http_message_free(&h->message); - php_http_buffer_free(&h->buffer); -} - -PHP_HTTP_API void php_http_request_free(php_http_request_t **h) -{ - if (*h) { - php_http_request_dtor(*h); - efree(*h); - *h = NULL; - } -} - -PHP_HTTP_API php_http_request_t *php_http_request_copy(php_http_request_t *from, php_http_request_t *to) -{ - if (!from->ops->copy) { - return NULL; - } else { - TSRMLS_FETCH_FROM_CTX(from->ts); - - if (!to) { - to = ecalloc(1, sizeof(*to)); - } - - to->ops = from->ops; - if (from->rf) { - php_http_resource_factory_addref(from->rf); - to->rf = from->rf; - } else { - to->rf = php_http_resource_factory_init(NULL, to->ops->rsrc, to, NULL); - } - to->buffer = php_http_buffer_init(NULL); - to->parser = php_http_message_parser_init(NULL TSRMLS_CC); - to->message = php_http_message_init(NULL, 0 TSRMLS_CC); - - TSRMLS_SET_CTX(to->ts); - - return to->ops->copy(from, to); - } -} - -PHP_HTTP_API STATUS php_http_request_exec(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body) -{ - if (h->ops->exec) { - return h->ops->exec(h, meth, url, body); - } - return FAILURE; -} - -PHP_HTTP_API STATUS php_http_request_reset(php_http_request_t *h) -{ - if (h->ops->reset) { - return h->ops->reset(h); - } - return FAILURE; -} - -PHP_HTTP_API STATUS php_http_request_setopt(php_http_request_t *h, php_http_request_setopt_opt_t opt, void *arg) -{ - if (h->ops->setopt) { - return h->ops->setopt(h, opt, arg); - } - return FAILURE; -} - -PHP_HTTP_API STATUS php_http_request_getopt(php_http_request_t *h, php_http_request_getopt_opt_t opt, void *arg) -{ - if (h->ops->getopt) { - return h->ops->getopt(h, opt, arg); - } - return FAILURE; -} - -#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpRequest, method, 0, req_args) -#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequest, method, 0) -#define PHP_HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, PHP_HTTP_ARGS(HttpRequest, method), visibility) -#define PHP_HTTP_REQUEST_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpRequest, method)) -#define PHP_HTTP_REQUEST_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpRequest_##al), PHP_HTTP_ARGS(HttpRequest, al), vis) - -PHP_HTTP_EMPTY_ARGS(__construct); - -PHP_HTTP_EMPTY_ARGS(getOptions); -PHP_HTTP_BEGIN_ARGS(setOptions, 0) - PHP_HTTP_ARG_VAL(options, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(getSslOptions); -PHP_HTTP_BEGIN_ARGS(setSslOptions, 0) - PHP_HTTP_ARG_VAL(ssl_options, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(addSslOptions, 0) - PHP_HTTP_ARG_VAL(ssl_optins, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(getHeaders); -PHP_HTTP_BEGIN_ARGS(setHeaders, 0) - PHP_HTTP_ARG_VAL(headers, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(addHeaders, 1) - PHP_HTTP_ARG_VAL(headers, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(getCookies); -PHP_HTTP_BEGIN_ARGS(setCookies, 0) - PHP_HTTP_ARG_VAL(cookies, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(addCookies, 1) - PHP_HTTP_ARG_VAL(cookies, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(enableCookies); -PHP_HTTP_BEGIN_ARGS(resetCookies, 0) - PHP_HTTP_ARG_VAL(session_only, 0) -PHP_HTTP_END_ARGS; -PHP_HTTP_EMPTY_ARGS(flushCookies); - -PHP_HTTP_EMPTY_ARGS(getUrl); -PHP_HTTP_BEGIN_ARGS(setUrl, 1) - PHP_HTTP_ARG_VAL(url, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(getMethod); -PHP_HTTP_BEGIN_ARGS(setMethod, 1) - PHP_HTTP_ARG_VAL(request_method, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(getContentType); -PHP_HTTP_BEGIN_ARGS(setContentType, 1) - PHP_HTTP_ARG_VAL(content_type, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(getQueryData); -PHP_HTTP_BEGIN_ARGS(setQueryData, 0) - PHP_HTTP_ARG_VAL(query_data, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(addQueryData, 1) - PHP_HTTP_ARG_VAL(query_data, 0) -PHP_HTTP_END_ARGS; - - -PHP_HTTP_EMPTY_ARGS(getBody); -PHP_HTTP_BEGIN_ARGS(setBody, 0) - PHP_HTTP_ARG_OBJ(http\\Message\\Body, body, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(addBody, 1) - PHP_HTTP_ARG_OBJ(http\\Message\\Body, body, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(getResponseCookies, 0) - PHP_HTTP_ARG_VAL(flags, 0) - PHP_HTTP_ARG_VAL(allowed_extras, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(getResponseBody); -PHP_HTTP_EMPTY_ARGS(getResponseCode); -PHP_HTTP_EMPTY_ARGS(getResponseStatus); -PHP_HTTP_BEGIN_ARGS(getResponseHeader, 0) - PHP_HTTP_ARG_VAL(header_name, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(getMessageClass); -PHP_HTTP_BEGIN_ARGS(setMessageClass, 1) - PHP_HTTP_ARG_VAL(message_class_name, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(getResponseMessage); -PHP_HTTP_EMPTY_ARGS(getRequestMessage); -PHP_HTTP_EMPTY_ARGS(getHistory); -PHP_HTTP_EMPTY_ARGS(clearHistory); -PHP_HTTP_EMPTY_ARGS(send); - -PHP_HTTP_EMPTY_ARGS(getObservers); -PHP_HTTP_BEGIN_ARGS(attach, 1) - PHP_HTTP_ARG_OBJ(SplObserver, observer, 0) -PHP_HTTP_END_ARGS; -PHP_HTTP_BEGIN_ARGS(detach, 1) - PHP_HTTP_ARG_OBJ(SplObserver, observer, 0) -PHP_HTTP_END_ARGS; -PHP_HTTP_EMPTY_ARGS(notify); -PHP_HTTP_EMPTY_ARGS(getProgress); -PHP_HTTP_BEGIN_ARGS(getTransferInfo, 0) - PHP_HTTP_ARG_VAL(name, 0) -PHP_HTTP_END_ARGS; - - -zend_class_entry *php_http_request_class_entry; -zend_function_entry php_http_request_method_entry[] = { - PHP_HTTP_REQUEST_ME(__construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR) - - PHP_HTTP_REQUEST_ME(getObservers, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(notify, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(attach, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(detach, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getProgress, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getTransferInfo, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(setOptions, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getOptions, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(setSslOptions, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getSslOptions, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(addSslOptions, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(addHeaders, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getHeaders, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(setHeaders, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(addCookies, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getCookies, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(setCookies, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(enableCookies, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(resetCookies, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(flushCookies, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(setMethod, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getMethod, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(setUrl, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getUrl, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(setContentType, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getContentType, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(setQueryData, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getQueryData, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(addQueryData, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(setBody, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getBody, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(addBody, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(send, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(getResponseHeader, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getResponseCookies, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getResponseCode, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getResponseStatus, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getResponseBody, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getResponseMessage, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getRequestMessage, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(getHistory, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(clearHistory, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQUEST_ME(getMessageClass, ZEND_ACC_PUBLIC) - PHP_HTTP_REQUEST_ME(setMessageClass, ZEND_ACC_PUBLIC) - - EMPTY_FUNCTION_ENTRY -}; -static zend_object_handlers php_http_request_object_handlers; - -zend_object_value php_http_request_object_new(zend_class_entry *ce TSRMLS_DC) -{ - return php_http_request_object_new_ex(ce, NULL, NULL TSRMLS_CC); -} - -zend_object_value php_http_request_object_new_ex(zend_class_entry *ce, php_http_request_t *r, php_http_request_object_t **ptr TSRMLS_DC) -{ - zend_object_value ov; - php_http_request_object_t *o; - - o = ecalloc(1, sizeof(php_http_request_object_t)); - zend_object_std_init((zend_object *) o, ce TSRMLS_CC); - object_properties_init((zend_object *) o, ce); - - if (!(o->request = r)) { - o->request = php_http_request_init(NULL, NULL, NULL, NULL TSRMLS_CC); - } - - if (ptr) { - *ptr = o; - } - - ov.handle = zend_objects_store_put(o, NULL, php_http_request_object_free, NULL TSRMLS_CC); - ov.handlers = &php_http_request_object_handlers; - - return ov; -} - -zend_object_value php_http_request_object_clone(zval *this_ptr TSRMLS_DC) -{ - zend_object_value new_ov; - php_http_request_object_t *new_obj, *old_obj = zend_object_store_get_object(this_ptr TSRMLS_CC); - - new_ov = php_http_request_object_new_ex(old_obj->zo.ce, php_http_request_copy(old_obj->request, NULL), &new_obj TSRMLS_CC); - zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC); - - return new_ov; -} - -void php_http_request_object_free(void *object TSRMLS_DC) -{ - php_http_request_object_t *o = (php_http_request_object_t *) object; - - php_http_request_free(&o->request); - zend_object_std_dtor((zend_object *) o TSRMLS_CC); - efree(o); -} - -static inline void php_http_request_object_check_request_content_type(zval *this_ptr TSRMLS_DC) -{ - zval *ctype = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("contentType"), 0 TSRMLS_CC); - - if (Z_STRLEN_P(ctype)) { - zval **headers, *opts = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); - - if ( (Z_TYPE_P(opts) == IS_ARRAY) && - (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), ZEND_STRS("headers"), (void *) &headers)) && - (Z_TYPE_PP(headers) == IS_ARRAY)) { - zval **ct_header; - - /* only override if not already set */ - if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), ZEND_STRS("Content-Type"), (void *) &ct_header))) { - add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); - } else - /* or not a string, zero length string or a string of spaces */ - if ((Z_TYPE_PP(ct_header) != IS_STRING) || !Z_STRLEN_PP(ct_header)) { - add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); - } else { - int i, only_space = 1; - - /* check for spaces only */ - for (i = 0; i < Z_STRLEN_PP(ct_header); ++i) { - if (!PHP_HTTP_IS_CTYPE(space, Z_STRVAL_PP(ct_header)[i])) { - only_space = 0; - break; - } - } - if (only_space) { - add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); - } - } - } else { - zval *headers; - - MAKE_STD_ZVAL(headers); - array_init(headers); - add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1); - zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers); - zval_ptr_dtor(&headers); - } - } -} - -static inline zend_object_value php_http_request_object_message(zval *this_ptr, php_http_message_t *msg TSRMLS_DC) -{ - zend_object_value ov; - zval *zcn = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("messageClass"), 0 TSRMLS_CC); - zend_class_entry *class_entry; - - if (Z_STRLEN_P(zcn) - && (class_entry = zend_fetch_class(Z_STRVAL_P(zcn), Z_STRLEN_P(zcn), 0 TSRMLS_CC)) - && (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_message_object_new_ex, php_http_message_class_entry, msg, NULL TSRMLS_CC))) { - return ov; - } else { - return php_http_message_object_new_ex(php_http_message_class_entry, msg, NULL TSRMLS_CC); - } -} - -STATUS php_http_request_object_requesthandler(php_http_request_object_t *obj, zval *this_ptr, char **meth, char **url, php_http_message_body_t **body TSRMLS_DC) -{ - zval *zoptions; - php_http_request_progress_t *progress; - - /* reset request handle */ - php_http_request_reset(obj->request); - /* reset transfer info */ - zend_update_property_null(php_http_request_class_entry, getThis(), ZEND_STRL("info") TSRMLS_CC); - - if (meth) { - *meth = Z_STRVAL_P(zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("method"), 0 TSRMLS_CC)); - } - - if (url) { - php_url *tmp, qdu = {NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL}; - zval *zurl = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("url"), 0 TSRMLS_CC); - zval *zqdata = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("queryData"), 0 TSRMLS_CC); - - if (Z_STRLEN_P(zqdata)) { - qdu.query = Z_STRVAL_P(zqdata); - } - php_http_url(0, tmp = php_url_parse(Z_STRVAL_P(zurl)), &qdu, NULL, url, NULL TSRMLS_CC); - php_url_free(tmp); - } - - if (body) { - zval *zbody = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("requestBody"), 0 TSRMLS_CC); - - if (Z_TYPE_P(zbody) == IS_OBJECT) { - *body = ((php_http_message_body_object_t *)zend_object_store_get_object(zbody TSRMLS_CC))->body; - if (*body) { - php_stream_rewind(php_http_message_body_stream(*body)); - } - } - } - - php_http_request_object_check_request_content_type(getThis() TSRMLS_CC); - zoptions = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_SETTINGS, Z_ARRVAL_P(zoptions)); - - if (SUCCESS == php_http_request_getopt(obj->request, PHP_HTTP_REQUEST_OPT_PROGRESS_INFO, &progress)) { - if (!progress->callback) { - php_http_request_progress_callback_t *callback = emalloc(sizeof(*callback)); - - callback->type = PHP_HTTP_REQUEST_PROGRESS_CALLBACK_USER; - callback->pass_state = 0; - MAKE_STD_ZVAL(callback->func.user); - array_init(callback->func.user); - Z_ADDREF_P(getThis()); - add_next_index_zval(callback->func.user, getThis()); - add_next_index_stringl(callback->func.user, ZEND_STRL("notify"), 1); - - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK, callback); - } - progress->state.info = "start"; - php_http_request_progress_notify(progress TSRMLS_CC); - progress->state.started = 1; - } - return SUCCESS; -} - -static inline void empty_response(zval *this_ptr TSRMLS_DC) -{ - zend_update_property_null(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage") TSRMLS_CC); - zend_update_property_long(php_http_request_class_entry, getThis(), ZEND_STRL("responseCode"), 0 TSRMLS_CC); - zend_update_property_string(php_http_request_class_entry, getThis(), ZEND_STRL("responseStatus"), "" TSRMLS_CC); -} - -STATUS php_http_request_object_responsehandler(php_http_request_object_t *obj, zval *this_ptr TSRMLS_DC) -{ - STATUS ret = SUCCESS; - zval *info; - php_http_message_t *msg; - php_http_request_progress_t *progress; - - /* always fetch info */ - MAKE_STD_ZVAL(info); - array_init(info); - php_http_request_getopt(obj->request, PHP_HTTP_REQUEST_OPT_TRANSFER_INFO, Z_ARRVAL_P(info)); - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("transferInfo"), info TSRMLS_CC); - zval_ptr_dtor(&info); - - if ((msg = obj->request->message)) { - /* update history */ - if (i_zend_is_true(zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) { - zval *new_hist, *old_hist = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC); - zend_object_value ov = php_http_request_object_message(getThis(), php_http_message_copy(msg, NULL) TSRMLS_CC); - - MAKE_STD_ZVAL(new_hist); - ZVAL_OBJVAL(new_hist, ov, 0); - - if (Z_TYPE_P(old_hist) == IS_OBJECT) { - php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC); - } - - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), new_hist TSRMLS_CC); - zval_ptr_dtor(&new_hist); - } - - /* update response info */ - if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, msg)) { - zval *message; - - zend_update_property_long(php_http_request_class_entry, getThis(), ZEND_STRL("responseCode"), msg->http.info.response.code TSRMLS_CC); - zend_update_property_string(php_http_request_class_entry, getThis(), ZEND_STRL("responseStatus"), STR_PTR(msg->http.info.response.status) TSRMLS_CC); - - MAKE_STD_ZVAL(message); - ZVAL_OBJVAL(message, php_http_request_object_message(getThis(), msg TSRMLS_CC), 0); - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), message TSRMLS_CC); - zval_ptr_dtor(&message); - - obj->request->message = php_http_message_init(NULL, 0 TSRMLS_CC); - msg = msg->parent; - } else { - empty_response(getThis() TSRMLS_CC); - } - } else { - /* update properties with empty values */ - empty_response(getThis() TSRMLS_CC); - } - - while (msg && !PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { - msg = msg->parent; - } - if (PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { - zval *message; - - MAKE_STD_ZVAL(message); - ZVAL_OBJVAL(message, php_http_request_object_message(getThis(), php_http_message_copy_ex(msg, NULL, 0) TSRMLS_CC), 0); - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("requestMessage"), message TSRMLS_CC); - zval_ptr_dtor(&message); - } - - if (SUCCESS == php_http_request_getopt(obj->request, PHP_HTTP_REQUEST_OPT_PROGRESS_INFO, &progress)) { - progress->state.info = "finished"; - progress->state.finished = 1; - php_http_request_progress_notify(progress TSRMLS_CC); - } - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK, NULL); - - return ret; -} - -static int apply_pretty_key(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) -{ - zval **zpp = pDest, *arr = va_arg(args, zval *); - - if (hash_key->arKey && hash_key->nKeyLength > 1) { - char *tmp = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 0); - - Z_ADDREF_PP(zpp); - add_assoc_zval_ex(arr, tmp, hash_key->nKeyLength, *zpp); - efree(tmp); - } - return ZEND_HASH_APPLY_KEEP; -} - -static inline void php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys) -{ - zval *old_opts, *new_opts, *opts = NULL, **entry = NULL; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) { - MAKE_STD_ZVAL(new_opts); - array_init(new_opts); - old_opts = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); - if (Z_TYPE_P(old_opts) == IS_ARRAY) { - array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts)); - } - - if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) { - if (overwrite) { - zend_hash_clean(Z_ARRVAL_PP(entry)); - } - if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) { - if (overwrite) { - array_copy(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry)); - } else { - array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, prettify_keys ? ARRAY_JOIN_PRETTIFY : 0); - } - } - } else if (opts) { - if (prettify_keys) { - zval *tmp; - - MAKE_STD_ZVAL(tmp); - array_init_size(tmp, zend_hash_num_elements(Z_ARRVAL_P(opts))); - zend_hash_apply_with_arguments(Z_ARRVAL_P(opts) TSRMLS_CC, apply_pretty_key, 1, tmp); - opts = tmp; - } else { - Z_ADDREF_P(opts); - } - add_assoc_zval_ex(new_opts, key, len, opts); - } - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC); - zval_ptr_dtor(&new_opts); - } - - RETVAL_ZVAL(getThis(), 1, 0); -} - -static inline void php_http_request_object_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len) -{ - if (SUCCESS == zend_parse_parameters_none()) { - zval *opts, **options; - - opts = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); - array_init(return_value); - - if ( (Z_TYPE_P(opts) == IS_ARRAY) && - (SUCCESS == zend_symtable_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) { - convert_to_array(*options); - array_copy(Z_ARRVAL_PP(options), Z_ARRVAL_P(return_value)); - } - } -} - - -PHP_METHOD(HttpRequest, __construct) -{ - with_error_handling(EH_THROW, php_http_exception_class_entry) { - zend_parse_parameters_none(); - } end_error_handling(); -} - -PHP_METHOD(HttpRequest, getObservers) -{ - with_error_handling(EH_THROW, php_http_exception_class_entry) { - if (SUCCESS == zend_parse_parameters_none()) { - RETVAL_PROP(php_http_request_class_entry, "observers"); - } - } end_error_handling(); -} - -static int notify(zend_object_iterator *iter, void *puser TSRMLS_DC) -{ - zval **observer = NULL; - - iter->funcs->get_current_data(iter, &observer TSRMLS_CC); - if (observer) { - zval *retval; - - zend_call_method_with_1_params(observer, NULL, NULL, "update", &retval, puser); - zval_ptr_dtor(&retval); - return SUCCESS; - } - return FAILURE; -} - -PHP_METHOD(HttpRequest, notify) -{ - if (SUCCESS == zend_parse_parameters_none()) { - zval *observers = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC); - - if (Z_TYPE_P(observers) == IS_OBJECT) { - Z_ADDREF_P(getThis()); - spl_iterator_apply(observers, notify, getThis() TSRMLS_CC); - zval_ptr_dtor(&getThis()); - } - } - - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, attach) -{ - zval *observer; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) { - zval *retval, *observers = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC); - zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer); - zval_ptr_dtor(&retval); - } - - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, detach) -{ - zval *observer; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) { - zval *retval, *observers = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC); - zend_call_method_with_1_params(&observers, NULL, NULL, "detach", &retval, observer); - zval_ptr_dtor(&retval); - } - - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, getProgress) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - php_http_request_progress_t *progress; - - php_http_request_getopt(obj->request, PHP_HTTP_REQUEST_OPT_PROGRESS_INFO, &progress); - object_init(return_value); - add_property_bool(return_value, "started", progress->state.started); - add_property_bool(return_value, "finished", progress->state.finished); - add_property_string(return_value, "info", STR_PTR(progress->state.info), 1); - add_property_double(return_value, "dltotal", progress->state.dl.total); - add_property_double(return_value, "dlnow", progress->state.dl.now); - add_property_double(return_value, "ultotal", progress->state.ul.total); - add_property_double(return_value, "ulnow", progress->state.ul.now); - } -} - -PHP_METHOD(HttpRequest, getTransferInfo) -{ - char *info_name = NULL; - int info_len = 0; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) { - zval **infop, *temp = NULL, *info = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("transferInfo"), 0 TSRMLS_CC); - - /* request completed? */ - if (Z_TYPE_P(info) != IS_ARRAY) { - php_http_request_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - MAKE_STD_ZVAL(temp); - array_init(temp); - php_http_request_getopt(obj->request, PHP_HTTP_REQUEST_OPT_TRANSFER_INFO, Z_ARRVAL_P(temp)); - info = temp; - } - - if (info_len && info_name) { - if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(info), php_http_pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) { - RETVAL_ZVAL(*infop, 1, 0); - } else { - php_http_error(HE_NOTICE, PHP_HTTP_E_INVALID_PARAM, "Could not find transfer info named %s", info_name); - RETVAL_FALSE; - } - } else { - RETVAL_ZVAL(info, 1, 0); - } - - if (temp) { - zval_ptr_dtor(&temp); - } - return; - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, setOptions) -{ - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - HashPosition pos; - zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt; - - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) { - RETURN_FALSE; - } - - MAKE_STD_ZVAL(new_opts); - array_init(new_opts); - - if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) { - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC); - zval_ptr_dtor(&new_opts); - } else { - MAKE_STD_ZVAL(add_opts); - array_init(add_opts); - /* some options need extra attention -- thus cannot use array_merge() directly */ - FOREACH_KEYVAL(pos, opts, key, opt) { - if (key.type == HASH_KEY_IS_STRING) { -#define KEYMATCH(k, s) ((sizeof(s)==k.len) && !strcasecmp(k.str, s)) - if (KEYMATCH(key, "headers")) { - zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, *opt); - } else if (KEYMATCH(key, "cookies")) { - zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt); - } else if (KEYMATCH(key, "ssl")) { - zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt); - } else if (KEYMATCH(key, "url") || KEYMATCH(key, "uri")) { - zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt); - } else if (KEYMATCH(key, "method")) { - zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt); - } else if (KEYMATCH(key, "flushcookies")) { - if (i_zend_is_true(*opt)) { - php_http_request_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_COOKIES_FLUSH, NULL); - } - } else if (KEYMATCH(key, "resetcookies")) { - php_http_request_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_COOKIES_RESET, NULL); - } else if (KEYMATCH(key, "enablecookies")) { - php_http_request_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_COOKIES_ENABLE, NULL); - } else if (KEYMATCH(key, "recordHistory")) { - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("recordHistory"), *opt TSRMLS_CC); - } else if (KEYMATCH(key, "messageClass")) { - zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmessageclass", NULL, *opt); - } else if (Z_TYPE_PP(opt) == IS_NULL) { - old_opts = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); - if (Z_TYPE_P(old_opts) == IS_ARRAY) { - zend_symtable_del(Z_ARRVAL_P(old_opts), key.str, key.len); - } - } else { - Z_ADDREF_P(*opt); - add_assoc_zval_ex(add_opts, key.str, key.len, *opt); - } - } - } - - old_opts = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); - if (Z_TYPE_P(old_opts) == IS_ARRAY) { - array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts)); - } - array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0); - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC); - zval_ptr_dtor(&new_opts); - zval_ptr_dtor(&add_opts); - } - - RETVAL_ZVAL(getThis(), 1, 0); -} - - - -PHP_METHOD(HttpRequest, getOptions) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_PROP(php_http_request_class_entry, "options"); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, setSslOptions) -{ - php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("ssl"), 1, 0); -} - -PHP_METHOD(HttpRequest, addSslOptions) -{ - php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("ssl"), 0, 0); -} - -PHP_METHOD(HttpRequest, getSslOptions) -{ - php_http_request_object_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("ssl")); -} - -PHP_METHOD(HttpRequest, addHeaders) -{ - php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("headers"), 0, 1); -} - -PHP_METHOD(HttpRequest, setHeaders) -{ - php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("headers"), 1, 1); -} - -PHP_METHOD(HttpRequest, getHeaders) -{ - php_http_request_object_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("headers")); -} - -PHP_METHOD(HttpRequest, setCookies) -{ - php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("cookies"), 1, 0); -} - -PHP_METHOD(HttpRequest, addCookies) -{ - php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("cookies"), 0, 0); -} - -PHP_METHOD(HttpRequest, getCookies) -{ - php_http_request_object_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_STRS("cookies")); -} - -PHP_METHOD(HttpRequest, enableCookies) -{ - if (SUCCESS == zend_parse_parameters_none()){ - php_http_request_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_COOKIES_ENABLE, NULL); - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, resetCookies) -{ - zend_bool session_only = 0; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &session_only)) { - php_http_request_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (session_only) { - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_COOKIES_RESET_SESSION, NULL); - } else { - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_COOKIES_RESET, NULL); - } - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, flushCookies) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_COOKIES_FLUSH, NULL); - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, setUrl) -{ - char *url_str = NULL; - int url_len; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &url_str, &url_len)) { - zend_update_property_stringl(php_http_request_class_entry, getThis(), ZEND_STRL("url"), url_str, url_len TSRMLS_CC); - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, getUrl) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_PROP(php_http_request_class_entry, "url"); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, setMethod) -{ - char *meth_str; - int meth_len; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &meth_str, &meth_len)) { - zend_update_property_stringl(php_http_request_class_entry, getThis(), ZEND_STRL("method"), meth_str, meth_len TSRMLS_CC); - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, getMethod) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_PROP(php_http_request_class_entry, "method"); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, setContentType) -{ - char *ctype; - int ct_len; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) { - int invalid = 0; - - if (ct_len) { - PHP_HTTP_CHECK_CONTENT_TYPE(ctype, invalid = 1); - } - - if (!invalid) { - zend_update_property_stringl(php_http_request_class_entry, getThis(), ZEND_STRL("contentType"), ctype, ct_len TSRMLS_CC); - } - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, getContentType) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_PROP(php_http_request_class_entry, "contentType"); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, setQueryData) -{ - zval *qdata = NULL; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) { - if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) { - zend_update_property_stringl(php_http_request_class_entry, getThis(), ZEND_STRL("queryData"), "", 0 TSRMLS_CC); - } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) { - char *query_data_str = NULL; - size_t query_data_len; - - if (SUCCESS == php_http_url_encode_hash(HASH_OF(qdata), NULL, 0, &query_data_str, &query_data_len TSRMLS_CC)) { - zend_update_property_stringl(php_http_request_class_entry, getThis(), ZEND_STRL("queryData"), query_data_str, query_data_len TSRMLS_CC); - efree(query_data_str); - } - } else { - zval *data = php_http_ztyp(IS_STRING, qdata); - - zend_update_property_stringl(php_http_request_class_entry, getThis(), ZEND_STRL("queryData"), Z_STRVAL_P(data), Z_STRLEN_P(data) TSRMLS_CC); - zval_ptr_dtor(&data); - } - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, getQueryData) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_PROP(php_http_request_class_entry, "queryData"); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, addQueryData) -{ - zval *qdata; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) { - char *query_data_str = NULL; - size_t query_data_len = 0; - zval *old_qdata = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("queryData"), 0 TSRMLS_CC); - - if (SUCCESS == php_http_url_encode_hash(HASH_OF(qdata), Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data_str, &query_data_len TSRMLS_CC)) { - zend_update_property_stringl(php_http_request_class_entry, getThis(), ZEND_STRL("queryData"), query_data_str, query_data_len TSRMLS_CC); - efree(query_data_str); - } - } - RETVAL_ZVAL(getThis(), 1, 0); - -} - -PHP_METHOD(HttpRequest, setBody) -{ - zval *body = NULL; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O!", &body, php_http_message_body_class_entry)) { - if (body && Z_TYPE_P(body) != IS_NULL) { - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("requestBody"), body TSRMLS_CC); - } else { - zend_update_property_null(php_http_request_class_entry, getThis(), ZEND_STRL("requestBody") TSRMLS_CC); - } - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, addBody) -{ - zval *new_body; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &new_body, php_http_message_body_class_entry)) { - zval *old_body = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("requestBody"), 0 TSRMLS_CC); - - if (Z_TYPE_P(old_body) == IS_OBJECT) { - php_http_message_body_object_t *old_obj = zend_object_store_get_object(old_body TSRMLS_CC); - php_http_message_body_object_t *new_obj = zend_object_store_get_object(new_body TSRMLS_CC); - - php_http_message_body_to_callback(old_obj->body, (php_http_pass_callback_t) php_http_message_body_append, new_obj->body, 0, 0); - } else { - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("requestBody"), new_body TSRMLS_CC); - } - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, getBody) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_PROP(php_http_request_class_entry, "requestBody"); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, getResponseHeader) -{ - zval *header; - char *header_name = NULL; - int header_len = 0; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) { - zval *message = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC); - - if (Z_TYPE_P(message) == IS_OBJECT) { - php_http_message_object_t *msg = zend_object_store_get_object(message TSRMLS_CC); - - if (header_len) { - if ((header = php_http_message_header(msg->message, header_name, header_len + 1, 0))) { - RETURN_ZVAL(header, 1, 1); - } - } else { - array_init(return_value); - zend_hash_copy(Z_ARRVAL_P(return_value), &msg->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); - return; - } - } - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, getResponseCookies) -{ - long flags = 0; - zval *allowed_extras_array = NULL; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la!", &flags, &allowed_extras_array)) { - int i = 0; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - char **allowed_extras = NULL; - zval **header = NULL, **entry = NULL, *message = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC); - HashPosition pos, pos1, pos2; - - if (Z_TYPE_P(message) == IS_OBJECT) { - php_http_message_object_t *msg = zend_object_store_get_object(message TSRMLS_CC); - - array_init(return_value); - - if (allowed_extras_array) { - allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *)); - FOREACH_VAL(pos, allowed_extras_array, entry) { - zval *data = php_http_ztyp(IS_STRING, *entry); - allowed_extras[i++] = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data)); - zval_ptr_dtor(&data); - } - } - - FOREACH_HASH_KEYVAL(pos1, &msg->message->hdrs, key, header) { - if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "Set-Cookie")) { - php_http_cookie_list_t *list; - - if (Z_TYPE_PP(header) == IS_ARRAY) { - zval **single_header; - - FOREACH_VAL(pos2, *header, single_header) { - zval *data = php_http_ztyp(IS_STRING, *single_header); - - if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) { - zval *cookie; - - MAKE_STD_ZVAL(cookie); - ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0); - add_next_index_zval(return_value, cookie); - } - zval_ptr_dtor(&data); - } - } else { - zval *data = php_http_ztyp(IS_STRING, *header); - if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) { - zval *cookie; - - MAKE_STD_ZVAL(cookie); - ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0); - add_next_index_zval(return_value, cookie); - } - zval_ptr_dtor(&data); - } - } - } - - if (allowed_extras) { - for (i = 0; allowed_extras[i]; ++i) { - efree(allowed_extras[i]); - } - efree(allowed_extras); - } - - return; - } - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, getResponseBody) -{ - with_error_handling(EH_THROW, php_http_exception_class_entry) { - if (SUCCESS == zend_parse_parameters_none()) { - zval *message = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC); - - if (Z_TYPE_P(message) == IS_OBJECT) { - RETURN_OBJVAL(((php_http_message_object_t *)zend_object_store_get_object(message TSRMLS_CC))->body, 1); - } else { - php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HttpRequest does not contain a response message"); - } - } - } end_error_handling(); -} - -PHP_METHOD(HttpRequest, getResponseCode) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_PROP(php_http_request_class_entry, "responseCode"); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, getResponseStatus) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_PROP(php_http_request_class_entry, "responseStatus"); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, getResponseMessage) -{ - with_error_handling(EH_THROW, php_http_exception_class_entry) { - if (SUCCESS == zend_parse_parameters_none()) { - zval *message = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC); - - if (Z_TYPE_P(message) == IS_OBJECT) { - RETVAL_OBJECT(message, 1); - } else { - php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HttpRequest does not contain a response message"); - } - } - } end_error_handling(); -} - -PHP_METHOD(HttpRequest, getRequestMessage) -{ - with_error_handling(EH_THROW, php_http_exception_class_entry) { - if (SUCCESS == zend_parse_parameters_none()) { - zval *message = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("requestMessage"), 0 TSRMLS_CC); - - if (Z_TYPE_P(message) == IS_OBJECT) { - RETVAL_OBJECT(message, 1); - } else { - php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HttpRequest does not contain a request message"); - } - } - } end_error_handling(); -} - -PHP_METHOD(HttpRequest, getHistory) -{ - with_error_handling(EH_THROW, php_http_exception_class_entry) { - if (SUCCESS == zend_parse_parameters_none()) { - zval *hist = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC); - - if (Z_TYPE_P(hist) == IS_OBJECT) { - RETVAL_OBJECT(hist, 1); - } else { - php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "The history is empty"); - } - } - } end_error_handling(); -} - -PHP_METHOD(HttpRequest, clearHistory) -{ - if (SUCCESS == zend_parse_parameters_none()) { - zend_update_property_null(php_http_request_class_entry, getThis(), ZEND_STRL("history") TSRMLS_CC); - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, getMessageClass) -{ - if (SUCCESS == zend_parse_parameters_none()) { - RETURN_PROP(php_http_request_class_entry, "messageClass"); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequest, setMessageClass) -{ - char *cn; - int cl; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &cn, &cl)) { - zend_update_property_stringl(php_http_request_class_entry, getThis(), ZEND_STRL("messageClass"), cn, cl TSRMLS_CC); - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequest, send) -{ - RETVAL_FALSE; - - with_error_handling(EH_THROW, php_http_exception_class_entry) { - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - php_http_message_body_t *body = NULL; - char *meth = NULL; - char *url = NULL; - - if (SUCCESS == php_http_request_object_requesthandler(obj, getThis(), &meth, &url, &body TSRMLS_CC)) { - php_http_request_exec(obj->request, meth, url, body); - if (SUCCESS == php_http_request_object_responsehandler(obj, getThis() TSRMLS_CC)) { - RETVAL_PROP(php_http_request_class_entry, "responseMessage"); - } else { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Failed to handle response"); - } - } else { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Failed to handle request"); - } - - STR_FREE(url); - } - } end_error_handling(); -} - -PHP_MINIT_FUNCTION(http_request) -{ - PHP_HTTP_REGISTER_CLASS(http, Request, http_request, php_http_object_class_entry, 0); - php_http_request_class_entry->create_object = php_http_request_object_new; - memcpy(&php_http_request_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); - php_http_request_object_handlers.clone_obj = php_http_request_object_clone; - - zend_class_implements(php_http_request_class_entry TSRMLS_CC, 1, spl_ce_SplSubject); - - zend_declare_property_null(php_http_request_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(php_http_request_class_entry, ZEND_STRL("options"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(php_http_request_class_entry, ZEND_STRL("transferInfo"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(php_http_request_class_entry, ZEND_STRL("responseMessage"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_long(php_http_request_class_entry, ZEND_STRL("responseCode"), 0, ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_string(php_http_request_class_entry, ZEND_STRL("responseStatus"), "", ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(php_http_request_class_entry, ZEND_STRL("requestMessage"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_string(php_http_request_class_entry, ZEND_STRL("method"), "GET", ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_string(php_http_request_class_entry, ZEND_STRL("url"), "", ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_string(php_http_request_class_entry, ZEND_STRL("contentType"), "", ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_string(php_http_request_class_entry, ZEND_STRL("requestBody"), "", ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_string(php_http_request_class_entry, ZEND_STRL("queryData"), "", ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_null(php_http_request_class_entry, ZEND_STRL("history"), ZEND_ACC_PRIVATE TSRMLS_CC); - zend_declare_property_bool(php_http_request_class_entry, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); - zend_declare_property_string(php_http_request_class_entry, ZEND_STRL("messageClass"), "", ZEND_ACC_PRIVATE TSRMLS_CC); - - return SUCCESS; -} - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ - diff --git a/php_http_request.h b/php_http_request.h deleted file mode 100644 index d535dc4..0000000 --- a/php_http_request.h +++ /dev/null @@ -1,243 +0,0 @@ -/* - +--------------------------------------------------------------------+ - | PECL :: http | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted provided that the conditions mentioned | - | in the accompanying LICENSE file are met. | - +--------------------------------------------------------------------+ - | Copyright (c) 2004-2011, Michael Wallner | - +--------------------------------------------------------------------+ -*/ - -#ifndef PHP_HTTP_REQUEST_H -#define PHP_HTTP_REQUEST_H - -#include "php_http_message_body.h" -#include "php_http_message_parser.h" - -typedef struct php_http_request_progress_state { - struct { - double now; - double total; - } ul; - struct { - double now; - double total; - } dl; - const char *info; - unsigned started:1; - unsigned finished:1; -} php_http_request_progress_state_t; - -#define PHP_HTTP_REQUEST_PROGRESS_CALLBACK_USER 0 -#define PHP_HTTP_REQUEST_PROGRESS_CALLBACK_INTERN 1 -typedef struct php_http_request_progress_callback { - union { - zval *user; - void (*intern)(php_http_request_progress_state_t* TSRMLS_DC); - } func; - unsigned type:1; - unsigned pass_state:1; -} php_http_request_progress_callback_t; - -typedef struct php_http_request_progress { - php_http_request_progress_state_t state; - php_http_request_progress_callback_t *callback; - unsigned in_cb:1; -} php_http_request_progress_t; - -static inline void php_http_request_progress_dtor(php_http_request_progress_t *progress TSRMLS_DC) -{ - if (progress->callback) { - if (progress->callback->type == PHP_HTTP_REQUEST_PROGRESS_CALLBACK_USER) { - zval_ptr_dtor(&progress->callback->func.user); - } - efree(progress->callback); - } - memset(progress, 0, sizeof(*progress)); -} - -static inline void php_http_request_progress_notify(php_http_request_progress_t *progress TSRMLS_DC) -{ - if (progress->callback) { - zval retval; - - INIT_PZVAL(&retval); - ZVAL_NULL(&retval); - - with_error_handling(EH_NORMAL, NULL) { - switch (progress->callback->type) { - case PHP_HTTP_REQUEST_PROGRESS_CALLBACK_USER: - if (progress->callback->pass_state) { - zval *param; - - MAKE_STD_ZVAL(param); - array_init(param); - add_assoc_bool(param, "started", progress->state.started); - add_assoc_bool(param, "finished", progress->state.finished); - add_assoc_string(param, "info", estrdup(progress->state.info), 0); - add_assoc_double(param, "dltotal", progress->state.dl.total); - add_assoc_double(param, "dlnow", progress->state.dl.now); - add_assoc_double(param, "ultotal", progress->state.ul.total); - add_assoc_double(param, "ulnow", progress->state.ul.now); - - progress->in_cb = 1; - call_user_function(EG(function_table), NULL, progress->callback->func.user, &retval, 1, ¶m TSRMLS_CC); - progress->in_cb = 0; - - zval_ptr_dtor(¶m); - } else { - progress->in_cb = 1; - call_user_function(EG(function_table), NULL, progress->callback->func.user, &retval, 0, NULL TSRMLS_CC); - progress->in_cb = 0; - } - break; - case PHP_HTTP_REQUEST_PROGRESS_CALLBACK_INTERN: - progress->callback->func.intern(progress->callback->pass_state ? &progress->state : NULL TSRMLS_CC); - break; - default: - break; - } - } end_error_handling(); - - zval_dtor(&retval); - } -} - -typedef enum php_http_request_setopt_opt { - PHP_HTTP_REQUEST_OPT_SETTINGS, /* HashTable* */ - PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK, /* php_http_request_progress_callback_t* */ - PHP_HTTP_REQUEST_OPT_COOKIES_ENABLE, /* - */ - PHP_HTTP_REQUEST_OPT_COOKIES_RESET, /* - */ - PHP_HTTP_REQUEST_OPT_COOKIES_RESET_SESSION, /* - */ - PHP_HTTP_REQUEST_OPT_COOKIES_FLUSH, /* - */ -} php_http_request_setopt_opt_t; - -typedef enum php_http_request_getopt_opt { - PHP_HTTP_REQUEST_OPT_PROGRESS_INFO, /* php_http_request_progress_t** */ - PHP_HTTP_REQUEST_OPT_TRANSFER_INFO, /* HashTable* */ -} php_http_request_getopt_opt_t; - -typedef struct php_http_request php_http_request_t; - -typedef php_http_request_t *(*php_http_request_init_func_t)(php_http_request_t *h, void *arg); -typedef php_http_request_t *(*php_http_request_copy_func_t)(php_http_request_t *from, php_http_request_t *to); -typedef void (*php_http_request_dtor_func_t)(php_http_request_t *h); -typedef STATUS (*php_http_request_exec_func_t)(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body); -typedef STATUS (*php_http_request_reset_func_t)(php_http_request_t *h); -typedef STATUS (*php_http_request_setopt_func_t)(php_http_request_t *h, php_http_request_setopt_opt_t opt, void *arg); -typedef STATUS (*php_http_request_getopt_func_t)(php_http_request_t *h, php_http_request_getopt_opt_t opt, void *arg); - -typedef struct php_http_request_ops { - php_http_resource_factory_ops_t *rsrc; - php_http_request_init_func_t init; - php_http_request_copy_func_t copy; - php_http_request_dtor_func_t dtor; - php_http_request_reset_func_t reset; - php_http_request_exec_func_t exec; - php_http_request_setopt_func_t setopt; - php_http_request_getopt_func_t getopt; -} php_http_request_ops_t; - -PHP_HTTP_API php_http_request_ops_t *php_http_request_get_default_ops(TSRMLS_D); - -struct php_http_request { - void *ctx; - php_http_resource_factory_t *rf; - php_http_request_ops_t *ops; - php_http_message_parser_t *parser; - php_http_message_t *message; - php_http_buffer_t *buffer; -#ifdef ZTS - void ***ts; -#endif -}; - -PHP_HTTP_API php_http_request_t *php_http_request_init(php_http_request_t *h, php_http_request_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC); -PHP_HTTP_API php_http_request_t *php_http_request_copy(php_http_request_t *from, php_http_request_t *to); -PHP_HTTP_API STATUS php_http_request_exec(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body); -PHP_HTTP_API STATUS php_http_request_reset(php_http_request_t *h); -PHP_HTTP_API STATUS php_http_request_setopt(php_http_request_t *h, php_http_request_setopt_opt_t opt, void *arg); -PHP_HTTP_API STATUS php_http_request_getopt(php_http_request_t *h, php_http_request_getopt_opt_t opt, void *arg); -PHP_HTTP_API void php_http_request_dtor(php_http_request_t *h); -PHP_HTTP_API void php_http_request_free(php_http_request_t **h); - -typedef struct php_http_request_object { - zend_object zo; - php_http_request_t *request; -} php_http_request_object_t; - -extern zend_class_entry *php_http_request_class_entry; -extern zend_function_entry php_http_request_method_entry[]; - -extern zend_object_value php_http_request_object_new(zend_class_entry *ce TSRMLS_DC); -extern zend_object_value php_http_request_object_new_ex(zend_class_entry *ce, php_http_request_t *r, php_http_request_object_t **ptr TSRMLS_DC); -extern zend_object_value php_http_request_object_clone(zval *zobject TSRMLS_DC); -extern void php_http_request_object_free(void *object TSRMLS_DC); - -extern STATUS php_http_request_object_requesthandler(php_http_request_object_t *obj, zval *this_ptr, char **meth, char **url, php_http_message_body_t **body TSRMLS_DC); -extern STATUS php_http_request_object_responsehandler(php_http_request_object_t *obj, zval *this_ptr TSRMLS_DC); - -PHP_METHOD(HttpRequest, __construct); -PHP_METHOD(HttpRequest, getObservers); -PHP_METHOD(HttpRequest, notify); -PHP_METHOD(HttpRequest, attach); -PHP_METHOD(HttpRequest, detach); -PHP_METHOD(HttpRequest, getProgress); -PHP_METHOD(HttpRequest, getTransferInfo); -PHP_METHOD(HttpRequest, setOptions); -PHP_METHOD(HttpRequest, getOptions); -PHP_METHOD(HttpRequest, addSslOptions); -PHP_METHOD(HttpRequest, setSslOptions); -PHP_METHOD(HttpRequest, getSslOptions); -PHP_METHOD(HttpRequest, addHeaders); -PHP_METHOD(HttpRequest, getHeaders); -PHP_METHOD(HttpRequest, setHeaders); -PHP_METHOD(HttpRequest, addCookies); -PHP_METHOD(HttpRequest, getCookies); -PHP_METHOD(HttpRequest, setCookies); -PHP_METHOD(HttpRequest, enableCookies); -PHP_METHOD(HttpRequest, resetCookies); -PHP_METHOD(HttpRequest, flushCookies); -PHP_METHOD(HttpRequest, setMethod); -PHP_METHOD(HttpRequest, getMethod); -PHP_METHOD(HttpRequest, setUrl); -PHP_METHOD(HttpRequest, getUrl); -PHP_METHOD(HttpRequest, setContentType); -PHP_METHOD(HttpRequest, getContentType); -PHP_METHOD(HttpRequest, setQueryData); -PHP_METHOD(HttpRequest, getQueryData); -PHP_METHOD(HttpRequest, addQueryData); -PHP_METHOD(HttpRequest, getBody); -PHP_METHOD(HttpRequest, setBody); -PHP_METHOD(HttpRequest, addBody); -PHP_METHOD(HttpRequest, send); -PHP_METHOD(HttpRequest, getResponseData); -PHP_METHOD(HttpRequest, getResponseHeader); -PHP_METHOD(HttpRequest, getResponseCookies); -PHP_METHOD(HttpRequest, getResponseCode); -PHP_METHOD(HttpRequest, getResponseStatus); -PHP_METHOD(HttpRequest, getResponseBody); -PHP_METHOD(HttpRequest, getResponseMessage); -PHP_METHOD(HttpRequest, getRawResponseMessage); -PHP_METHOD(HttpRequest, getRequestMessage); -PHP_METHOD(HttpRequest, getRawRequestMessage); -PHP_METHOD(HttpRequest, getHistory); -PHP_METHOD(HttpRequest, clearHistory); -PHP_METHOD(HttpRequest, getMessageClass); -PHP_METHOD(HttpRequest, setMessageClass); - -extern PHP_MINIT_FUNCTION(http_request); - -#endif - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ - diff --git a/php_http_request_datashare.c b/php_http_request_datashare.c deleted file mode 100644 index 5dbf9dd..0000000 --- a/php_http_request_datashare.c +++ /dev/null @@ -1,332 +0,0 @@ -/* - +--------------------------------------------------------------------+ - | PECL :: http | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted provided that the conditions mentioned | - | in the accompanying LICENSE file are met. | - +--------------------------------------------------------------------+ - | Copyright (c) 2004-2011, Michael Wallner | - +--------------------------------------------------------------------+ -*/ - -#include "php_http_api.h" - -#include - -static int php_http_request_datashare_compare_handles(void *h1, void *h2); - -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) -{ - php_http_request_datashare_t *free_h = NULL; - - if (!h) { - free_h = h = emalloc(sizeof(*h)); - } - memset(h, sizeof(*h), 0); - - zend_llist_init(&h->requests, sizeof(zval *), ZVAL_PTR_DTOR, 0); - h->ops = ops; - h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, NULL, NULL); - TSRMLS_SET_CTX(h->ts); - - if (h->ops->init) { - if (!(h = h->ops->init(h, init_arg))) { - if (free_h) { - efree(free_h); - } - } - } - - return h; -} - -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) -{ - if (from->ops->copy) { - return from->ops->copy(from, to); - } - - return NULL; -} - -PHP_HTTP_API void php_http_request_datashare_dtor(php_http_request_datashare_t *h) -{ - if (h->ops->dtor) { - h->ops->dtor(h); - } - zend_llist_destroy(&h->requests); - php_http_resource_factory_free(&h->rf); -} - -PHP_HTTP_API void php_http_request_datashare_free(php_http_request_datashare_t **h) -{ - php_http_request_datashare_dtor(*h); - efree(*h); - *h = NULL; -} - -PHP_HTTP_API STATUS php_http_request_datashare_attach(php_http_request_datashare_t *h, zval *request) -{ - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (h->ops->attach) { - php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC); - - if (SUCCESS == h->ops->attach(h, obj->request)) { - Z_ADDREF_P(request); - zend_llist_add_element(&h->requests, &request); - return SUCCESS; - } - } - - return FAILURE; -} - -static int php_http_request_datashare_compare_handles(void *h1, void *h2) -{ - return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2)); -} - -PHP_HTTP_API STATUS php_http_request_datashare_detach(php_http_request_datashare_t *h, zval *request) -{ - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (h->ops->detach) { - php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC); - - if (SUCCESS == h->ops->detach(h, obj->request)) { - zend_llist_del_element(&h->requests, request, php_http_request_datashare_compare_handles); - return SUCCESS; - } - } - return FAILURE; -} - -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) -{ - if (h->ops->setopt) { - return h->ops->setopt(h, opt, arg); - } - return FAILURE; -} - -static void detach(void *r, void *h TSRMLS_DC) -{ - ((php_http_request_datashare_t *) h)->ops->detach(h, ((php_http_request_object_t *) zend_object_store_get_object(*((zval **) r) TSRMLS_CC))->request); -} - -PHP_HTTP_API void php_http_request_datashare_reset(php_http_request_datashare_t *h) -{ - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (h->ops->reset) { - h->ops->reset(h); - } else if (h->ops->detach) { - zend_llist_apply_with_argument(&h->requests, detach, h TSRMLS_CC); - } - - zend_llist_clean(&h->requests); -} - -#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpRequestDataShare, method, 0, req_args) -#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequestDataShare, method, 0) -#define PHP_HTTP_RSHARE_ME(method, visibility) PHP_ME(HttpRequestDataShare, method, PHP_HTTP_ARGS(HttpRequestDataShare, method), visibility) - -PHP_HTTP_EMPTY_ARGS(__construct); -PHP_HTTP_EMPTY_ARGS(__destruct); -PHP_HTTP_EMPTY_ARGS(reset); -PHP_HTTP_EMPTY_ARGS(count); - -PHP_HTTP_BEGIN_ARGS(attach, 1) - PHP_HTTP_ARG_OBJ(http\\Request, request, 0) -PHP_HTTP_END_ARGS; -PHP_HTTP_BEGIN_ARGS(detach, 1) - PHP_HTTP_ARG_OBJ(http\\Request, request, 0) -PHP_HTTP_END_ARGS; - -static void php_http_request_datashare_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC); - -#define php_http_request_datashare_class_entry php_http_request_datashare_class_entry -zend_class_entry *php_http_request_datashare_class_entry; -zend_function_entry php_http_request_datashare_method_entry[] = { - PHP_HTTP_RSHARE_ME(__construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR) - PHP_HTTP_RSHARE_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) - PHP_HTTP_RSHARE_ME(count, ZEND_ACC_PUBLIC) - PHP_HTTP_RSHARE_ME(attach, ZEND_ACC_PUBLIC) - PHP_HTTP_RSHARE_ME(detach, ZEND_ACC_PUBLIC) - PHP_HTTP_RSHARE_ME(reset, ZEND_ACC_PUBLIC) - EMPTY_FUNCTION_ENTRY -}; -static zend_object_handlers php_http_request_datashare_object_handlers; - -zend_object_value php_http_request_datashare_object_new(zend_class_entry *ce TSRMLS_DC) -{ - return php_http_request_datashare_object_new_ex(ce, NULL, NULL TSRMLS_CC); -} - -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) -{ - zend_object_value ov; - php_http_request_datashare_object_t *o; - - o = ecalloc(1, sizeof(*o)); - zend_object_std_init((zend_object *) o, ce TSRMLS_CC); - object_properties_init((zend_object *) o, ce); - - if (share) { - o->share = share; - } else { - o->share = php_http_request_datashare_init(NULL, NULL, NULL, NULL TSRMLS_CC); - } - - if (ptr) { - *ptr = o; - } - - ov.handle = zend_objects_store_put(o, NULL, php_http_request_datashare_object_free, NULL TSRMLS_CC); - ov.handlers = &php_http_request_datashare_object_handlers; - - return ov; -} - -void php_http_request_datashare_object_free(void *object TSRMLS_DC) -{ - php_http_request_datashare_object_t *o = (php_http_request_datashare_object_t *) object; - - php_http_request_datashare_free(&o->share); - zend_object_std_dtor((zend_object *) o TSRMLS_CC); - efree(o); -} - -static void php_http_request_datashare_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC) -{ - zend_property_info *pi; - - if ((pi = zend_get_property_info(php_http_request_datashare_class_entry, member, 1 TSRMLS_CC))) { - zend_bool enable = i_zend_is_true(value); - php_http_request_datashare_setopt_opt_t opt; - php_http_request_datashare_object_t *obj = zend_object_store_get_object(object TSRMLS_CC); - - if (!strcmp(pi->name, "cookie")) { - opt = PHP_HTTP_REQUEST_DATASHARE_OPT_COOKIES; - } else if (!strcmp(pi->name, "dns")) { - opt = PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER; - } else if (!strcmp(pi->name, "ssl")) { - opt = PHP_HTTP_REQUEST_DATASHARE_OPT_SSLSESSIONS; - } else { - return; - } - - if (SUCCESS != php_http_request_datashare_setopt(obj->share, opt, &enable)) { - return; - } - } - - zend_get_std_object_handlers()->write_property(object, member, value, literal_key TSRMLS_CC); -} - -static zval **php_http_request_datashare_object_get_prop_ptr(zval *object, zval *member, const zend_literal *literal_key TSRMLS_DC) -{ - zend_property_info *pi; - - if ((pi = zend_get_property_info(php_http_request_datashare_class_entry, member, 1 TSRMLS_CC))) { - return &php_http_property_proxy_init(NULL, object, member, NULL TSRMLS_CC)->myself; - } - - return zend_get_std_object_handlers()->get_property_ptr_ptr(object, member, literal_key TSRMLS_CC); -} - - -PHP_METHOD(HttpRequestDataShare, __construct) -{ - with_error_handling(EH_THROW, php_http_exception_class_entry) { - zend_parse_parameters_none(); - } end_error_handling(); -} - -PHP_METHOD(HttpRequestDataShare, __destruct) -{ - php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - if (SUCCESS == zend_parse_parameters_none()) { - ; /* we always want to clean up */ - } - - php_http_request_datashare_reset(obj->share); -} - -PHP_METHOD(HttpRequestDataShare, count) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - RETURN_LONG(zend_llist_count(&obj->share->requests)); - } - RETURN_FALSE; -} - - -PHP_METHOD(HttpRequestDataShare, attach) -{ - zval *request; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) { - php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - RETURN_SUCCESS(php_http_request_datashare_attach(obj->share, request)); - } - RETURN_FALSE; - -} - -PHP_METHOD(HttpRequestDataShare, detach) -{ - zval *request; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) { - php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - RETURN_SUCCESS(php_http_request_datashare_detach(obj->share, request)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequestDataShare, reset) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_datashare_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - php_http_request_datashare_reset(obj->share); - RETURN_TRUE; - } - RETURN_FALSE; -} - -PHP_MINIT_FUNCTION(http_request_datashare) -{ - PHP_HTTP_REGISTER_CLASS(http\\Request, DataShare, http_request_datashare, php_http_object_class_entry, 0); - php_http_request_datashare_class_entry->create_object = php_http_request_datashare_object_new; - memcpy(&php_http_request_datashare_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); - php_http_request_datashare_object_handlers.clone_obj = NULL; - php_http_request_datashare_object_handlers.write_property = php_http_request_datashare_object_write_prop; - php_http_request_datashare_object_handlers.get_property_ptr_ptr = php_http_request_datashare_object_get_prop_ptr; - - zend_class_implements(php_http_request_datashare_class_entry TSRMLS_CC, 1, spl_ce_Countable); - - zend_declare_property_bool(php_http_request_datashare_class_entry, ZEND_STRL("cookie"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); - zend_declare_property_bool(php_http_request_datashare_class_entry, ZEND_STRL("dns"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); - zend_declare_property_bool(php_http_request_datashare_class_entry, ZEND_STRL("ssl"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); - - return SUCCESS; -} - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ - diff --git a/php_http_request_datashare.h b/php_http_request_datashare.h deleted file mode 100644 index 066a1bd..0000000 --- a/php_http_request_datashare.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - +--------------------------------------------------------------------+ - | PECL :: http | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted provided that the conditions mentioned | - | in the accompanying LICENSE file are met. | - +--------------------------------------------------------------------+ - | Copyright (c) 2004-2011, Michael Wallner | - +--------------------------------------------------------------------+ -*/ - -#ifndef PHP_HTTP_REQUEST_DATASHARE_H -#define PHP_HTTP_REQUEST_DATASHARE_H - -typedef enum php_http_request_datashare_setopt_opt { - PHP_HTTP_REQUEST_DATASHARE_OPT_COOKIES, - PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER, - PHP_HTTP_REQUEST_DATASHARE_OPT_SSLSESSIONS, -} php_http_request_datashare_setopt_opt_t; - -typedef struct php_http_request_datashare php_http_request_datashare_t; - -typedef php_http_request_datashare_t *(*php_http_request_datashare_init_func_t)(php_http_request_datashare_t *h, void *init_arg); -typedef php_http_request_datashare_t *(*php_http_request_datashare_copy_func_t)(php_http_request_datashare_t *from, php_http_request_datashare_t *to); -typedef void (*php_http_request_datashare_dtor_func_t)(php_http_request_datashare_t *h); -typedef void (*php_http_request_datashare_reset_func_t)(php_http_request_datashare_t *h); -typedef STATUS (*php_http_request_datashare_attach_func_t)(php_http_request_datashare_t *h, php_http_request_t *request); -typedef STATUS (*php_http_request_datashare_detach_func_t)(php_http_request_datashare_t *h, php_http_request_t *request); -typedef STATUS (*php_http_request_datashare_setopt_func_t)(php_http_request_datashare_t *h, php_http_request_datashare_setopt_opt_t opt, void *arg); - -typedef struct php_http_request_datashare_ops { - php_http_resource_factory_ops_t *rsrc; - php_http_request_datashare_init_func_t init; - php_http_request_datashare_copy_func_t copy; - php_http_request_datashare_dtor_func_t dtor; - php_http_request_datashare_reset_func_t reset; - php_http_request_datashare_attach_func_t attach; - php_http_request_datashare_detach_func_t detach; - php_http_request_datashare_setopt_func_t setopt; -} php_http_request_datashare_ops_t; - -struct php_http_request_datashare { - void *ctx; - php_http_resource_factory_t *rf; - php_http_request_datashare_ops_t *ops; - zend_llist requests; -#ifdef ZTS - void ***ts; -#endif -}; - -extern PHP_MINIT_FUNCTION(http_request_datashare); -extern PHP_MSHUTDOWN_FUNCTION(http_request_datashare); -extern PHP_RINIT_FUNCTION(http_request_datashare); -extern PHP_RSHUTDOWN_FUNCTION(http_request_datashare); - -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); -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); -PHP_HTTP_API void php_http_request_datashare_dtor(php_http_request_datashare_t *h); -PHP_HTTP_API void php_http_request_datashare_free(php_http_request_datashare_t **h); -PHP_HTTP_API STATUS php_http_request_datashare_attach(php_http_request_datashare_t *h, zval *request); -PHP_HTTP_API STATUS php_http_request_datashare_detach(php_http_request_datashare_t *h, zval *request); -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); -PHP_HTTP_API void php_http_request_datashare_reset(php_http_request_datashare_t *h); - -typedef struct php_http_request_datashare_object { - zend_object zo; - php_http_request_datashare_t *share; -} php_http_request_datashare_object_t; - -extern zend_class_entry *php_http_request_datashare_class_entry; -extern zend_function_entry php_http_request_datashare_method_entry[]; - -extern zend_object_value php_http_request_datashare_object_new(zend_class_entry *ce TSRMLS_DC); -extern 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); -extern void php_http_request_datashare_object_free(void *object TSRMLS_DC); - -PHP_METHOD(HttpRequestDataShare, __construct); -PHP_METHOD(HttpRequestDataShare, __destruct); -PHP_METHOD(HttpRequestDataShare, count); -PHP_METHOD(HttpRequestDataShare, attach); -PHP_METHOD(HttpRequestDataShare, detach); -PHP_METHOD(HttpRequestDataShare, reset); - -#endif /* PHP_HTTP_REQUEST_DATASHARE_H */ - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ - diff --git a/php_http_request_factory.c b/php_http_request_factory.c index e739f53..8d18ab1 100644 --- a/php_http_request_factory.c +++ b/php_http_request_factory.c @@ -123,13 +123,13 @@ PHP_METHOD(HttpRequestFactory, createRequest) zval *zdriver, *os; zend_object_value ov; zend_class_entry *class_entry = NULL; - php_http_request_t *req = NULL; + php_http_client_t *req = NULL; php_http_request_factory_driver_t driver; class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestClass") TSRMLS_CC); if (!class_entry) { - class_entry = php_http_request_class_entry; + class_entry = php_http_client_class_entry; } zdriver = zend_read_property(php_http_request_factory_class_entry, getThis(), ZEND_STRL("driver"), 0 TSRMLS_CC); @@ -152,27 +152,27 @@ PHP_METHOD(HttpRequestFactory, createRequest) efree(name_str); } - req = php_http_request_init(NULL, driver.request_ops, rf, NULL TSRMLS_CC); + req = php_http_client_init(NULL, driver.request_ops, rf, NULL TSRMLS_CC); if (req) { - if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_request_object_new_ex, php_http_request_class_entry, req, NULL TSRMLS_CC)) { + if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_client_object_new_ex, php_http_client_class_entry, req, NULL TSRMLS_CC)) { ZVAL_OBJVAL(return_value, ov, 0); MAKE_STD_ZVAL(os); object_init_ex(os, spl_ce_SplObjectStorage); - zend_update_property(php_http_request_class_entry, return_value, ZEND_STRL("observers"), os TSRMLS_CC); + zend_update_property(php_http_client_class_entry, return_value, ZEND_STRL("observers"), os TSRMLS_CC); zval_ptr_dtor(&os); if (url_str) { - zend_update_property_stringl(php_http_request_class_entry, return_value, ZEND_STRL("url"), url_str, url_len TSRMLS_CC); + zend_update_property_stringl(php_http_client_class_entry, return_value, ZEND_STRL("url"), url_str, url_len TSRMLS_CC); } if (meth_str) { - zend_update_property_stringl(php_http_request_class_entry, return_value, ZEND_STRL("method"), meth_str, meth_len TSRMLS_CC); + zend_update_property_stringl(php_http_client_class_entry, return_value, ZEND_STRL("method"), meth_str, meth_len TSRMLS_CC); } if (options) { zend_call_method_with_1_params(&return_value, Z_OBJCE_P(return_value), NULL, "setoptions", NULL, options); } } else { - php_http_request_free(&req); + php_http_client_free(&req); } } @@ -228,7 +228,7 @@ PHP_METHOD(HttpRequestFactory, createPool) if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_request_pool_object_new_ex, php_http_request_pool_class_entry, pool, NULL TSRMLS_CC)) { ZVAL_OBJVAL(return_value, ov, 0); for (i = 0; i < argc; ++i) { - if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_request_class_entry TSRMLS_CC)) { + if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_client_class_entry TSRMLS_CC)) { php_http_request_pool_attach(pool, *(argv[i])); } } @@ -258,7 +258,7 @@ PHP_METHOD(HttpRequestFactory, createDataShare) zval *zdriver; zend_object_value ov; zend_class_entry *class_entry; - php_http_request_datashare_t *share = NULL; + php_http_client_datashare_t *share = NULL; php_http_request_factory_driver_t driver; if (!(class_entry = php_http_request_factory_get_class_entry(getThis(), ZEND_STRL("requestDataShareClass") TSRMLS_CC))) { @@ -284,12 +284,12 @@ PHP_METHOD(HttpRequestFactory, createDataShare) efree(name_str); } - share = php_http_request_datashare_init(NULL, driver.request_datashare_ops, rf, NULL TSRMLS_CC); + share = php_http_client_datashare_init(NULL, driver.request_datashare_ops, rf, NULL TSRMLS_CC); if (share) { if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_request_datashare_object_new_ex, php_http_request_datashare_class_entry, share, NULL TSRMLS_CC)) { ZVAL_OBJVAL(return_value, ov, 0); for (i = 0; i < argc; ++i) { - if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_request_class_entry TSRMLS_CC)) { + if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), php_http_client_class_entry TSRMLS_CC)) { php_http_request_datashare_attach(share, *(argv[i])); } } diff --git a/php_http_request_factory.h b/php_http_request_factory.h index f443c12..fd6a092 100644 --- a/php_http_request_factory.h +++ b/php_http_request_factory.h @@ -18,7 +18,7 @@ #include "php_http_request_datashare.h" typedef struct php_http_request_factory_driver { - php_http_request_ops_t *request_ops; + php_http_client_ops_t *request_ops; php_http_request_pool_ops_t *request_pool_ops; php_http_request_datashare_ops_t *request_datashare_ops; } php_http_request_factory_driver_t; diff --git a/php_http_request_pool.c b/php_http_request_pool.c deleted file mode 100644 index 352c8ef..0000000 --- a/php_http_request_pool.c +++ /dev/null @@ -1,582 +0,0 @@ -/* - +--------------------------------------------------------------------+ - | PECL :: http | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted provided that the conditions mentioned | - | in the accompanying LICENSE file are met. | - +--------------------------------------------------------------------+ - | Copyright (c) 2004-2011, Michael Wallner | - +--------------------------------------------------------------------+ -*/ - -#include "php_http_api.h" - -PHP_HTTP_API php_http_request_pool_t *php_http_request_pool_init(php_http_request_pool_t *h, php_http_request_pool_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC) -{ - php_http_request_pool_t *free_h = NULL; - - if (!h) { - free_h = h = emalloc(sizeof(*h)); - } - memset(h, 0, sizeof(*h)); - - h->ops = ops; - h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, NULL, NULL); - zend_llist_init(&h->requests.attached, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0); - zend_llist_init(&h->requests.finished, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0); - TSRMLS_SET_CTX(h->ts); - - if (h->ops->init) { - if (!(h = h->ops->init(h, init_arg))) { - php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not initialize request pool"); - if (free_h) { - efree(h); - } - } - } - - return h; -} - -PHP_HTTP_API php_http_request_pool_t *php_http_request_pool_copy(php_http_request_pool_t *from, php_http_request_pool_t *to) -{ - if (from->ops->copy) { - return from->ops->copy(from, to); - } - - return NULL; -} - -PHP_HTTP_API void php_http_request_pool_dtor(php_http_request_pool_t *h) -{ - if (h->ops->dtor) { - h->ops->dtor(h); - } - - zend_llist_clean(&h->requests.finished); - zend_llist_clean(&h->requests.attached); - - php_http_resource_factory_free(&h->rf); -} - -PHP_HTTP_API void php_http_request_pool_free(php_http_request_pool_t **h) { - if (*h) { - php_http_request_pool_dtor(*h); - efree(*h); - *h = NULL; - } -} - -PHP_HTTP_API STATUS php_http_request_pool_attach(php_http_request_pool_t *h, zval *request) -{ - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (h->ops->attach) { - char *url = NULL; - char *m = NULL; - php_http_message_body_t *body = NULL; - php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC); - - if (SUCCESS != php_http_request_object_requesthandler(obj, request, &m, &url, &body TSRMLS_CC)) { - return FAILURE; - } - if (SUCCESS == h->ops->attach(h, obj->request, m, url, body)) { - STR_FREE(url); - Z_ADDREF_P(request); - zend_llist_add_element(&h->requests.attached, &request); - return SUCCESS; - } - STR_FREE(url); - } - - return FAILURE; -} - -static int php_http_request_pool_compare_handles(void *h1, void *h2) -{ - return (Z_OBJ_HANDLE_PP((zval **) h1) == Z_OBJ_HANDLE_P((zval *) h2)); -} - - -PHP_HTTP_API STATUS php_http_request_pool_detach(php_http_request_pool_t *h, zval *request) -{ - TSRMLS_FETCH_FROM_CTX(h->ts); - - if (h->ops->detach) { - php_http_request_object_t *obj = zend_object_store_get_object(request TSRMLS_CC); - - if (SUCCESS == h->ops->detach(h, obj->request)) { - zend_llist_del_element(&h->requests.finished, request, php_http_request_pool_compare_handles); - zend_llist_del_element(&h->requests.attached, request, php_http_request_pool_compare_handles); - return SUCCESS; - } - } - - return FAILURE; -} - -PHP_HTTP_API STATUS php_http_request_pool_wait(php_http_request_pool_t *h, struct timeval *custom_timeout) -{ - if (h->ops->wait) { - return h->ops->wait(h, custom_timeout); - } - - return FAILURE; -} - -PHP_HTTP_API int php_http_request_pool_once(php_http_request_pool_t *h) -{ - if (h->ops->once) { - return h->ops->once(h); - } - - return FAILURE; -} - -PHP_HTTP_API STATUS php_http_request_pool_exec(php_http_request_pool_t *h) -{ - if (h->ops->exec) { - return h->ops->exec(h); - } - - return FAILURE; -} - -static void detach(void *r, void *h TSRMLS_DC) -{ - ((php_http_request_pool_t *) h)->ops->detach(h, ((php_http_request_object_t *) zend_object_store_get_object(*((zval **) r) TSRMLS_CC))->request); -} - -PHP_HTTP_API void php_http_request_pool_reset(php_http_request_pool_t *h) -{ - if (h->ops->reset) { - h->ops->reset(h); - } else if (h->ops->detach) { - TSRMLS_FETCH_FROM_CTX(h->ts); - - zend_llist_apply_with_argument(&h->requests.attached, detach, h TSRMLS_CC); - } - - zend_llist_clean(&h->requests.attached); - zend_llist_clean(&h->requests.finished); -} - -PHP_HTTP_API STATUS php_http_request_pool_setopt(php_http_request_pool_t *h, php_http_request_pool_setopt_opt_t opt, void *arg) -{ - if (h->ops->setopt) { - return h->ops->setopt(h, opt, arg); - } - - return FAILURE; -} - -PHP_HTTP_API void php_http_request_pool_requests(php_http_request_pool_t *h, zval ***attached, zval ***finished) -{ - zval **handle; - int i, count; - - if (attached) { - if ((count = zend_llist_count(&h->requests.attached))) { - *attached = ecalloc(count + 1 /* terminating NULL */, sizeof(zval *)); - - for (i = 0, handle = zend_llist_get_first(&h->requests.attached); handle; handle = zend_llist_get_next(&h->requests.attached)) { - Z_ADDREF_PP(handle); - (*attached)[i++] = *handle; - } - } else { - *attached = NULL; - } - } - - if (finished) { - if ((count = zend_llist_count(&h->requests.finished))) { - *finished = ecalloc(count + 1 /* terminating NULL */, sizeof(zval *)); - - for (i = 0, handle = zend_llist_get_first(&h->requests.finished); handle; handle = zend_llist_get_next(&h->requests.finished)) { - Z_ADDREF_PP(handle); - (*finished)[i++] = *handle; - } - } else { - *finished = NULL; - } - } -} - -/*#*/ - -#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpRequestPool, method, 0, req_args) -#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpRequestPool, method, 0) -#define PHP_HTTP_REQPOOL_ME(method, visibility) PHP_ME(HttpRequestPool, method, PHP_HTTP_ARGS(HttpRequestPool, method), visibility) - -PHP_HTTP_EMPTY_ARGS(__construct); - -PHP_HTTP_EMPTY_ARGS(__destruct); -PHP_HTTP_EMPTY_ARGS(reset); - -PHP_HTTP_BEGIN_ARGS(attach, 1) - PHP_HTTP_ARG_OBJ(http\\Request, request, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(detach, 1) - PHP_HTTP_ARG_OBJ(http\\Request, request, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(send); -PHP_HTTP_EMPTY_ARGS(once); -PHP_HTTP_BEGIN_ARGS(wait, 0) - PHP_HTTP_ARG_VAL(timeout, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_EMPTY_ARGS(valid); -PHP_HTTP_EMPTY_ARGS(current); -PHP_HTTP_EMPTY_ARGS(key); -PHP_HTTP_EMPTY_ARGS(next); -PHP_HTTP_EMPTY_ARGS(rewind); - -PHP_HTTP_EMPTY_ARGS(count); - -PHP_HTTP_EMPTY_ARGS(getAttachedRequests); -PHP_HTTP_EMPTY_ARGS(getFinishedRequests); - -PHP_HTTP_BEGIN_ARGS(enablePipelining, 0) - PHP_HTTP_ARG_VAL(enable, 0) -PHP_HTTP_END_ARGS; - -PHP_HTTP_BEGIN_ARGS(enableEvents, 0) - PHP_HTTP_ARG_VAL(enable, 0) -PHP_HTTP_END_ARGS; - -zend_class_entry *php_http_request_pool_class_entry; -zend_function_entry php_http_request_pool_method_entry[] = { - PHP_HTTP_REQPOOL_ME(__construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR) - PHP_HTTP_REQPOOL_ME(__destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) - PHP_HTTP_REQPOOL_ME(attach, ZEND_ACC_PUBLIC) - PHP_HTTP_REQPOOL_ME(detach, ZEND_ACC_PUBLIC) - PHP_HTTP_REQPOOL_ME(send, ZEND_ACC_PUBLIC) - PHP_HTTP_REQPOOL_ME(reset, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQPOOL_ME(once, ZEND_ACC_PROTECTED) - PHP_HTTP_REQPOOL_ME(wait, ZEND_ACC_PROTECTED) - - /* implements Iterator */ - PHP_HTTP_REQPOOL_ME(valid, ZEND_ACC_PUBLIC) - PHP_HTTP_REQPOOL_ME(current, ZEND_ACC_PUBLIC) - PHP_HTTP_REQPOOL_ME(key, ZEND_ACC_PUBLIC) - PHP_HTTP_REQPOOL_ME(next, ZEND_ACC_PUBLIC) - PHP_HTTP_REQPOOL_ME(rewind, ZEND_ACC_PUBLIC) - - /* implmenents Countable */ - PHP_HTTP_REQPOOL_ME(count, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQPOOL_ME(getAttachedRequests, ZEND_ACC_PUBLIC) - PHP_HTTP_REQPOOL_ME(getFinishedRequests, ZEND_ACC_PUBLIC) - - PHP_HTTP_REQPOOL_ME(enablePipelining, ZEND_ACC_PUBLIC) - PHP_HTTP_REQPOOL_ME(enableEvents, ZEND_ACC_PUBLIC) - - EMPTY_FUNCTION_ENTRY -}; -static zend_object_handlers php_http_request_pool_object_handlers; - -zend_object_value php_http_request_pool_object_new(zend_class_entry *ce TSRMLS_DC) -{ - return php_http_request_pool_object_new_ex(ce, NULL, NULL TSRMLS_CC); -} - -zend_object_value php_http_request_pool_object_new_ex(zend_class_entry *ce, php_http_request_pool_t *p, php_http_request_pool_object_t **ptr TSRMLS_DC) -{ - zend_object_value ov; - php_http_request_pool_object_t *o; - - o = ecalloc(1, sizeof(php_http_request_pool_object_t)); - zend_object_std_init((zend_object *) o, ce TSRMLS_CC); - object_properties_init((zend_object *) o, ce); - - if (!(o->pool = p)) { - o->pool = php_http_request_pool_init(NULL, NULL, NULL, NULL TSRMLS_CC); - } - - if (ptr) { - *ptr = o; - } - - ov.handle = zend_objects_store_put(o, NULL, php_http_request_pool_object_free, NULL TSRMLS_CC); - ov.handlers = &php_http_request_pool_object_handlers; - - return ov; -} - -void php_http_request_pool_object_free(void *object TSRMLS_DC) -{ - php_http_request_pool_object_t *o = (php_http_request_pool_object_t *) object; - - php_http_request_pool_free(&o->pool); - zend_object_std_dtor((zend_object *) o TSRMLS_CC); - efree(o); -} - -static void php_http_request_pool_object_llist2array(zval **req, zval *array TSRMLS_DC) -{ - Z_ADDREF_P(*req); - add_next_index_zval(array, *req); -} - - -PHP_METHOD(HttpRequestPool, __construct) -{ - with_error_handling(EH_THROW, php_http_exception_class_entry) { - zend_parse_parameters_none(); - } end_error_handling(); -} - -PHP_METHOD(HttpRequestPool, __destruct) -{ - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - if (SUCCESS != zend_parse_parameters_none()) { - ; /* we always want to clean up */ - } - - php_http_request_pool_reset(obj->pool); -} - -PHP_METHOD(HttpRequestPool, reset) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - obj->iterator.pos = 0; - php_http_request_pool_reset(obj->pool); - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequestPool, attach) -{ - with_error_handling(EH_THROW, php_http_exception_class_entry) { - zval *request; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) { - with_error_handling(EH_THROW, php_http_exception_class_entry) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - if (obj->iterator.pos > 0 && obj->iterator.pos < zend_llist_count(&obj->pool->requests.attached)) { - php_http_error(HE_THROW, PHP_HTTP_E_REQUEST_POOL, "Cannot attach to the HttpRequestPool while the iterator is active"); - } else { - php_http_request_pool_attach(obj->pool, request); - } - } end_error_handling(); - } - } end_error_handling(); - - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequestPool, detach) -{ - RETVAL_FALSE; - - with_error_handling(EH_THROW, php_http_exception_class_entry) { - zval *request; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_request_class_entry)) { - with_error_handling(EH_THROW, php_http_exception_class_entry) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - obj->iterator.pos = -1; - php_http_request_pool_detach(obj->pool, request); - } end_error_handling(); - } - } end_error_handling(); - - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequestPool, send) -{ - RETVAL_FALSE; - - with_error_handling(EH_THROW, php_http_exception_class_entry) { - if (SUCCESS == zend_parse_parameters_none()) { - with_error_handling(EH_THROW, php_http_exception_class_entry) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - php_http_request_pool_exec(obj->pool); - } end_error_handling(); - } - } end_error_handling(); - - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequestPool, once) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - if (0 < php_http_request_pool_once(obj->pool)) { - RETURN_TRUE; - } - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequestPool, wait) -{ - double timeout = 0; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|d", &timeout)) { - struct timeval timeout_val; - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - timeout_val.tv_sec = (time_t) timeout; - timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC; - - RETURN_SUCCESS(php_http_request_pool_wait(obj->pool, timeout > 0 ? &timeout_val : NULL)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequestPool, valid) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - RETURN_BOOL(obj->iterator.pos >= 0 && obj->iterator.pos < zend_llist_count(&obj->pool->requests.attached)); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequestPool, current) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - if (obj->iterator.pos < zend_llist_count(&obj->pool->requests.attached)) { - long pos = 0; - zval **current = NULL; - zend_llist_position lpos; - - for ( current = zend_llist_get_first_ex(&obj->pool->requests.attached, &lpos); - current && obj->iterator.pos != pos++; - current = zend_llist_get_next_ex(&obj->pool->requests.attached, &lpos)); - if (current) { - RETURN_OBJECT(*current, 1); - } - } - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequestPool, key) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - RETURN_LONG(obj->iterator.pos); - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequestPool, next) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - ++obj->iterator.pos; - } -} - -PHP_METHOD(HttpRequestPool, rewind) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - obj->iterator.pos = 0; - } -} - -PHP_METHOD(HttpRequestPool, count) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - RETURN_LONG((long) zend_llist_count(&obj->pool->requests.attached)); - } -} - -PHP_METHOD(HttpRequestPool, getAttachedRequests) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - array_init(return_value); - zend_llist_apply_with_argument(&obj->pool->requests.attached, - (llist_apply_with_arg_func_t) php_http_request_pool_object_llist2array, - return_value TSRMLS_CC); - return; - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequestPool, getFinishedRequests) -{ - if (SUCCESS == zend_parse_parameters_none()) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - array_init(return_value); - zend_llist_apply_with_argument(&obj->pool->requests.finished, - (llist_apply_with_arg_func_t) php_http_request_pool_object_llist2array, - return_value TSRMLS_CC); - return; - } - RETURN_FALSE; -} - -PHP_METHOD(HttpRequestPool, enablePipelining) -{ - zend_bool enable = 1; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - php_http_request_pool_setopt(obj->pool, PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING, &enable); - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_METHOD(HttpRequestPool, enableEvents) -{ - zend_bool enable = 1; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) { - php_http_request_pool_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - - php_http_request_pool_setopt(obj->pool, PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS, &enable); - } - RETVAL_ZVAL(getThis(), 1, 0); -} - -PHP_MINIT_FUNCTION(http_request_pool) -{ - PHP_HTTP_REGISTER_CLASS(http\\Request, Pool, http_request_pool, php_http_object_class_entry, 0); - php_http_request_pool_class_entry->create_object = php_http_request_pool_object_new; - memcpy(&php_http_request_pool_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); - php_http_request_pool_object_handlers.clone_obj = NULL; - - zend_class_implements(php_http_request_pool_class_entry TSRMLS_CC, 2, spl_ce_Countable, zend_ce_iterator); - - return SUCCESS; -} - - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ - diff --git a/php_http_request_pool.h b/php_http_request_pool.h deleted file mode 100644 index c090330..0000000 --- a/php_http_request_pool.h +++ /dev/null @@ -1,123 +0,0 @@ -/* - +--------------------------------------------------------------------+ - | PECL :: http | - +--------------------------------------------------------------------+ - | Redistribution and use in source and binary forms, with or without | - | modification, are permitted provided that the conditions mentioned | - | in the accompanying LICENSE file are met. | - +--------------------------------------------------------------------+ - | Copyright (c) 2004-2011, Michael Wallner | - +--------------------------------------------------------------------+ -*/ - -#ifndef PHP_HTTP_REQUESTPOOL_H -#define PHP_HTTP_REQUESTPOOL_H - -typedef enum php_http_request_pool_setopt_opt { - PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING, - PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS, -} php_http_request_pool_setopt_opt_t; - -typedef struct php_http_request_pool php_http_request_pool_t; - -typedef php_http_request_pool_t *(*php_http_request_pool_init_func_t)(php_http_request_pool_t *p, void *arg); -typedef php_http_request_pool_t *(*php_http_request_pool_copy_func_t)(php_http_request_pool_t *from, php_http_request_pool_t *to); -typedef void (*php_http_request_pool_dtor_func_t)(php_http_request_pool_t *p); -typedef void (*php_http_request_pool_reset_func_t)(php_http_request_pool_t *p); -typedef STATUS (*php_http_request_pool_exec_func_t)(php_http_request_pool_t *p); -typedef STATUS (*php_http_request_pool_wait_func_t)(php_http_request_pool_t *p, struct timeval *custom_timeout); -typedef int (*php_http_request_pool_once_func_t)(php_http_request_pool_t *p); -typedef STATUS (*php_http_request_pool_attach_func_t)(php_http_request_pool_t *p, php_http_request_t *r, const char *m, const char *url, php_http_message_body_t *body); -typedef STATUS (*php_http_request_pool_detach_func_t)(php_http_request_pool_t *p, php_http_request_t *r); -typedef STATUS (*php_http_request_pool_setopt_func_t)(php_http_request_pool_t *p, php_http_request_pool_setopt_opt_t opt, void *arg); - -typedef struct php_http_request_pool_ops { - php_http_resource_factory_ops_t *rsrc; - php_http_request_pool_init_func_t init; - php_http_request_pool_copy_func_t copy; - php_http_request_pool_dtor_func_t dtor; - php_http_request_pool_reset_func_t reset; - php_http_request_pool_exec_func_t exec; - php_http_request_pool_wait_func_t wait; - php_http_request_pool_once_func_t once; - php_http_request_pool_attach_func_t attach; - php_http_request_pool_detach_func_t detach; - php_http_request_pool_setopt_func_t setopt; -} php_http_request_pool_ops_t; - -struct php_http_request_pool { - void *ctx; - php_http_resource_factory_t *rf; - php_http_request_pool_ops_t *ops; - - struct { - zend_llist attached; - zend_llist finished; - } requests; - -#ifdef ZTS - void ***ts; -#endif -}; - -PHP_HTTP_API php_http_request_pool_t *php_http_request_pool_init(php_http_request_pool_t *pool, php_http_request_pool_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC); -PHP_HTTP_API php_http_request_pool_t *php_http_request_pool_copy(php_http_request_pool_t *from, php_http_request_pool_t *to); -PHP_HTTP_API void php_http_request_pool_dtor(php_http_request_pool_t *pool); -PHP_HTTP_API void php_http_request_pool_free(php_http_request_pool_t **pool); -PHP_HTTP_API void php_http_request_pool_reset(php_http_request_pool_t *pool); -PHP_HTTP_API STATUS php_http_request_pool_exec(php_http_request_pool_t *pool); -PHP_HTTP_API STATUS php_http_request_pool_wait(php_http_request_pool_t *pool, struct timeval *custom_timeout); -PHP_HTTP_API STATUS php_http_request_pool_once(php_http_request_pool_t *pool); -PHP_HTTP_API STATUS php_http_request_pool_attach(php_http_request_pool_t *pool, zval *request); -PHP_HTTP_API STATUS php_http_request_pool_detach(php_http_request_pool_t *pool, zval *request); -PHP_HTTP_API STATUS php_http_request_pool_setopt(php_http_request_pool_t *pool, php_http_request_pool_setopt_opt_t opt, void *arg); -PHP_HTTP_API void php_http_request_pool_requests(php_http_request_pool_t *h, zval ***attached, zval ***finished); - -typedef struct php_http_request_pool_object { - zend_object zo; - php_http_request_pool_t *pool; - zend_object_value factory; - struct { - long pos; - } iterator; -} php_http_request_pool_object_t; - -extern zend_class_entry *php_http_request_pool_class_entry; -extern zend_function_entry php_http_request_pool_method_entry[]; - -extern zend_object_value php_http_request_pool_object_new(zend_class_entry *ce TSRMLS_DC); -extern zend_object_value php_http_request_pool_object_new_ex(zend_class_entry *ce, php_http_request_pool_t *p, php_http_request_pool_object_t **ptr TSRMLS_DC); -extern void php_http_request_pool_object_free(void *object TSRMLS_DC); - -PHP_METHOD(HttpRequestPool, __construct); -PHP_METHOD(HttpRequestPool, __destruct); -PHP_METHOD(HttpRequestPool, attach); -PHP_METHOD(HttpRequestPool, detach); -PHP_METHOD(HttpRequestPool, send); -PHP_METHOD(HttpRequestPool, reset); -PHP_METHOD(HttpRequestPool, once); -PHP_METHOD(HttpRequestPool, wait); -PHP_METHOD(HttpRequestPool, valid); -PHP_METHOD(HttpRequestPool, current); -PHP_METHOD(HttpRequestPool, key); -PHP_METHOD(HttpRequestPool, next); -PHP_METHOD(HttpRequestPool, rewind); -PHP_METHOD(HttpRequestPool, count); -PHP_METHOD(HttpRequestPool, getAttachedRequests); -PHP_METHOD(HttpRequestPool, getFinishedRequests); -PHP_METHOD(HttpRequestPool, enablePipelining); -PHP_METHOD(HttpRequestPool, enableEvents); - -PHP_MINIT_FUNCTION(http_request_pool); - -#endif /* PHP_HTTP_REQUESTPOOL_H */ - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ -