X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_request.c;h=ea4d4dd39de76b5108c8e118cebae5fc56c82e7e;hp=63d68db035aca60c639330c0c2b8c1ee8ebd5e56;hb=aacbe6aa75ca07853bf88d5b3647f67f107d1af7;hpb=df06e2dbf48a3b0d96f2c62071c1b5fc907a98d0 diff --git a/php_http_request.c b/php_http_request.c index 63d68db..ea4d4dd 100644 --- a/php_http_request.c +++ b/php_http_request.c @@ -6,20 +6,15 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2010, Michael Wallner | + | Copyright (c) 2004-2011, Michael Wallner | +--------------------------------------------------------------------+ */ -/* $Id: php_http_request_api.c 298591 2010-04-26 11:46:55Z mike $ */ - -#include "php_http.h" +#include "php_http_api.h" #include -#include -#include - -PHP_HTTP_API php_http_request_t *php_http_request_init(php_http_request_t *h, php_http_request_ops_t *ops, void *init_arg TSRMLS_DC) +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; @@ -29,7 +24,8 @@ PHP_HTTP_API php_http_request_t *php_http_request_init(php_http_request_t *h, ph memset(h, 0, sizeof(*h)); h->ops = ops; - h->buffer = php_http_buffer_init(NULL TSRMLS_CC); + h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, NULL, 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); @@ -39,7 +35,8 @@ PHP_HTTP_API php_http_request_t *php_http_request_init(php_http_request_t *h, ph if (!(h = h->ops->init(h, init_arg))) { php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Could not initialize request"); if (free_h) { - efree(free_h); + h->ops->dtor = NULL; + php_http_request_free(&h); } } } @@ -53,6 +50,12 @@ PHP_HTTP_API void php_http_request_dtor(php_http_request_t *h) h->ops->dtor(h); } + php_http_resource_factory_free(&h->rf); + + if (h->persistent_handle_id) { + zval_ptr_dtor(&h->persistent_handle_id); + } + php_http_message_parser_free(&h->parser); php_http_message_free(&h->message); php_http_buffer_free(&h->buffer); @@ -180,11 +183,11 @@ 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_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_ARG_OBJ(http\\Message\\Body, body, 0) PHP_HTTP_END_ARGS; PHP_HTTP_BEGIN_ARGS(getResponseCookies, 0) @@ -205,9 +208,7 @@ PHP_HTTP_BEGIN_ARGS(setMessageClass, 1) PHP_HTTP_END_ARGS; PHP_HTTP_EMPTY_ARGS(getResponseMessage); -PHP_HTTP_EMPTY_ARGS(getRawResponseMessage); PHP_HTTP_EMPTY_ARGS(getRequestMessage); -PHP_HTTP_EMPTY_ARGS(getRawRequestMessage); PHP_HTTP_EMPTY_ARGS(getHistory); PHP_HTTP_EMPTY_ARGS(clearHistory); PHP_HTTP_EMPTY_ARGS(send); @@ -306,7 +307,7 @@ zend_object_value php_http_request_object_new_ex(zend_class_entry *ce, php_http_ object_properties_init((zend_object *) o, ce); if (!(o->request = r)) { - o->request = php_http_request_init(NULL, NULL, NULL TSRMLS_CC); + o->request = php_http_request_init(NULL, NULL, NULL, NULL TSRMLS_CC); } if (ptr) { @@ -439,25 +440,32 @@ STATUS php_http_request_object_requesthandler(php_http_request_object_t *obj, zv if (SUCCESS == php_http_request_getopt(obj->request, PHP_HTTP_REQUEST_OPT_PROGRESS_INFO, &progress)) { if (!progress->callback) { - zval *pcb; - int no = 0; + php_http_request_progress_callback_t *callback = emalloc(sizeof(*callback)); - MAKE_STD_ZVAL(pcb); - array_init(pcb); + 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(pcb, getThis()); - add_next_index_stringl(pcb, ZEND_STRL("notify"), 1); + 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, pcb); - php_http_request_setopt(obj->request, PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK_WANTS_STATE, &no); - zval_ptr_dtor(&pcb); + 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; @@ -472,57 +480,59 @@ STATUS php_http_request_object_responsehandler(php_http_request_object_t *obj, z zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("transferInfo"), info TSRMLS_CC); zval_ptr_dtor(&info); - /* 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(), obj->request->message_parser.message TSRMLS_CC); + 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); - 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, 0 TSRMLS_CC); + } - if (Z_TYPE_P(old_hist) == IS_OBJECT) { - php_http_message_object_prepend(new_hist, old_hist, 0 TSRMLS_CC); + zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), new_hist TSRMLS_CC); + zval_ptr_dtor(&new_hist); } - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), new_hist TSRMLS_CC); - zval_ptr_dtor(&new_hist); - } -*/ -// if ((msg = obj->request->_current.request)) { -// /* update request message */ -// zval *message; -// -// MAKE_STD_ZVAL(message); -// ZVAL_OBJVAL(message, php_http_request_object_message(getThis(), msg TSRMLS_CC), 1); -// zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("requestMessage"), message TSRMLS_CC); -// } -// fprintf(stderr, "RESPONSE MESSAGE: %p\n", obj->request->parser.msg); - if ((msg = obj->request->message)) { - /* update properties with response info */ - zval *message; + /* 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); + 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); + 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 */ - zval *znull; + empty_response(getThis() TSRMLS_CC); + } - MAKE_STD_ZVAL(znull); - ZVAL_NULL(znull); - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), znull TSRMLS_CC); - zval_ptr_dtor(&znull); + while (msg && !PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { + msg = msg->parent; + } + if (PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { + zval *message; - 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); + 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); } @@ -531,9 +541,10 @@ STATUS php_http_request_object_responsehandler(php_http_request_object_t *obj, z return ret; } -static int apply_pretty_key(void *pDest, int num_args, va_list args, zend_hash_key *hash_key) +static int apply_pretty_key(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) { if (hash_key->arKey && hash_key->nKeyLength > 1) { + /* FIXME: this seems evil */ hash_key->h = zend_hash_func(php_http_pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength); } return ZEND_HASH_APPLY_KEEP; @@ -551,7 +562,7 @@ static inline void php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PA array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts)); } - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) { + if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) { if (overwrite) { zend_hash_clean(Z_ARRVAL_PP(entry)); } @@ -585,7 +596,7 @@ static inline void php_http_request_object_get_options_subr(INTERNAL_FUNCTION_PA array_init(return_value); if ( (Z_TYPE_P(opts) == IS_ARRAY) && - (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) { + (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)); } @@ -595,14 +606,14 @@ static inline void php_http_request_object_get_options_subr(INTERNAL_FUNCTION_PA PHP_METHOD(HttpRequest, __construct) { - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) { + 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_EX_CE(runtime)) { + with_error_handling(EH_THROW, php_http_exception_class_entry) { if (SUCCESS == zend_parse_parameters_none()) { RETVAL_PROP(php_http_request_class_entry, "observers"); } @@ -675,6 +686,7 @@ PHP_METHOD(HttpRequest, getProgress) 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); @@ -701,7 +713,7 @@ PHP_METHOD(HttpRequest, getTransferInfo) } if (info_len && info_name) { - if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), php_http_pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) { + 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); @@ -773,7 +785,7 @@ PHP_METHOD(HttpRequest, setOptions) } 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_hash_del(Z_ARRVAL_P(old_opts), key.str, key.len); + zend_symtable_del(Z_ARRVAL_P(old_opts), key.str, key.len); } } else { Z_ADDREF_P(*opt); @@ -960,12 +972,12 @@ PHP_METHOD(HttpRequest, setQueryData) char *query_data_str = NULL; size_t query_data_len; - if (SUCCESS == php_http_url_encode_hash(HASH_OF(qdata), 0, NULL, 0, &query_data_str, &query_data_len TSRMLS_CC)) { + 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_zsep(IS_STRING, qdata); + 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); @@ -991,7 +1003,7 @@ PHP_METHOD(HttpRequest, addQueryData) 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), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data_str, &query_data_len 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); } @@ -1087,7 +1099,7 @@ PHP_METHOD(HttpRequest, getResponseCookies) 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_zsep(IS_STRING, *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); } @@ -1101,9 +1113,9 @@ PHP_METHOD(HttpRequest, getResponseCookies) zval **single_header; FOREACH_VAL(pos2, *header, single_header) { - zval *data = php_http_zsep(IS_STRING, *single_header); + zval *data = php_http_ztyp(IS_STRING, *single_header); - if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), flags, allowed_extras TSRMLS_CC))) { + 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); @@ -1113,8 +1125,8 @@ PHP_METHOD(HttpRequest, getResponseCookies) zval_ptr_dtor(&data); } } else { - zval *data = php_http_zsep(IS_STRING, *header); - if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), flags, allowed_extras TSRMLS_CC))) { + 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); @@ -1167,7 +1179,7 @@ PHP_METHOD(HttpRequest, getResponseStatus) PHP_METHOD(HttpRequest, getResponseMessage) { - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) { + 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); @@ -1182,7 +1194,7 @@ PHP_METHOD(HttpRequest, getResponseMessage) PHP_METHOD(HttpRequest, getRequestMessage) { - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) { + 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); @@ -1197,7 +1209,7 @@ PHP_METHOD(HttpRequest, getRequestMessage) PHP_METHOD(HttpRequest, getHistory) { - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) { + 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); @@ -1241,7 +1253,7 @@ PHP_METHOD(HttpRequest, send) { RETVAL_FALSE; - with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) { + 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_request_method_t meth = PHP_HTTP_NO_REQUEST_METHOD; @@ -1271,7 +1283,7 @@ PHP_MINIT_FUNCTION(http_request) 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, 2, spl_ce_SplSubject, php_http_fluently_callable_class_entry); + 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);