From dedad9f35cbeaee56e7cca145e378cc6548198e3 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 14 Jun 2011 08:12:10 +0000 Subject: [PATCH] http\Env::parseParams() updated php_http_url_encode_hash*() --- php_http.c | 7 ------- php_http_curl.c | 2 +- php_http_env.c | 31 +++++++++++++++++++++++++++++++ php_http_env.h | 1 + php_http_neon.c | 2 +- php_http_querystring.c | 2 +- php_http_request.c | 4 ++-- php_http_url.c | 31 ++++++++++++++----------------- php_http_url.h | 15 +++++++++++++-- 9 files changed, 64 insertions(+), 31 deletions(-) diff --git a/php_http.c b/php_http.c index ebe73cc..0b47165 100644 --- a/php_http.c +++ b/php_http.c @@ -119,12 +119,6 @@ zend_php_http_globals *php_http_globals(void) } #endif #endif -PHP_INI_MH(http_update_persistent_handle_ident) -{ - PHP_HTTP_G->persistent_handle.ident.h = zend_hash_func(new_value, PHP_HTTP_G->persistent_handle.ident.l = new_value_length+1); - return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC); -} - PHP_INI_BEGIN() PHP_HTTP_INI_ENTRY("http.etag.mode", "md5", PHP_INI_ALL, OnUpdateString, env.etag_mode) PHP_HTTP_INI_ENTRY("http.request_datashare.cookie", "0", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.cookie) @@ -132,7 +126,6 @@ PHP_INI_BEGIN() PHP_HTTP_INI_ENTRY("http.request_datashare.ssl", "0", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.ssl) PHP_HTTP_INI_ENTRY("http.request_datashare.connect", "0", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.connect) PHP_HTTP_INI_ENTRY("http.persistent_handle.limit", "-1", PHP_INI_SYSTEM, OnUpdateLong, persistent_handle.limit) - PHP_HTTP_INI_ENTRY("http.persistent_handle.ident", "GLOBAL", PHP_INI_ALL, http_update_persistent_handle_ident, persistent_handle.ident.s) PHP_INI_END() PHP_MINIT_FUNCTION(http) diff --git a/php_http_curl.c b/php_http_curl.c index ae8eb55..377a0ea 100644 --- a/php_http_curl.c +++ b/php_http_curl.c @@ -863,7 +863,7 @@ static STATUS set_options(php_http_request_t *h, HashTable *options) 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_recursive(HASH_OF(zoption), &curl->options.cookies, "; ", lenof("; "), NULL, 0 TSRMLS_CC)) { + 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); } diff --git a/php_http_env.c b/php_http_env.c index 1bd1a14..4b9ea88 100644 --- a/php_http_env.c +++ b/php_http_env.c @@ -593,6 +593,11 @@ PHP_HTTP_BEGIN_ARGS(persistentHandlesIdent, 0) PHP_HTTP_ARG_VAL(name, 0) PHP_HTTP_END_ARGS; +PHP_HTTP_BEGIN_ARGS(parseParams, 1) + PHP_HTTP_ARG_VAL(params, 0) + PHP_HTTP_ARG_VAL(flags, 0) +PHP_HTTP_END_ARGS; + zend_function_entry php_http_env_method_entry[] = { PHP_HTTP_ENV_ME(getRequestHeader) PHP_HTTP_ENV_ME(getRequestBody) @@ -612,6 +617,8 @@ zend_function_entry php_http_env_method_entry[] = { PHP_HTTP_ENV_ME(persistentHandlesStat) PHP_HTTP_ENV_ME(persistentHandlesClean) + PHP_HTTP_ENV_ME(parseParams) + EMPTY_FUNCTION_ENTRY }; @@ -865,6 +872,23 @@ PHP_METHOD(HttpEnv, persistentHandlesClean) } } +PHP_METHOD(HttpEnv, parseParams) +{ + char *param_str; + int param_len; + long flags = PHP_HTTP_PARAMS_DEFAULT; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", ¶m_str, ¶m_len, &flags)) { + RETURN_FALSE; + } + + array_init(return_value); + if (SUCCESS != php_http_params_parse(param_str, flags, php_http_params_parse_default_func, Z_ARRVAL_P(return_value) TSRMLS_CC)) { + zval_dtor(return_value); + RETURN_FALSE; + } +} + zend_class_entry *php_http_env_request_class_entry; #undef PHP_HTTP_BEGIN_ARGS @@ -897,6 +921,13 @@ PHP_METHOD(HttpEnvRequest, __construct) PHP_MINIT_FUNCTION(http_env) { PHP_HTTP_REGISTER_CLASS(http, Env, http_env, NULL, 0); + + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_ALLOW_COMMA"), PHP_HTTP_PARAMS_ALLOW_COMMA TSRMLS_CC); + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_ALLOW_FAILURE"), PHP_HTTP_PARAMS_ALLOW_FAILURE TSRMLS_CC); + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_RAISE_ERROR"), PHP_HTTP_PARAMS_RAISE_ERROR TSRMLS_CC); + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC); + zend_declare_class_constant_long(php_http_env_class_entry, ZEND_STRL("PARAMS_COLON_SEPARATOR"), PHP_HTTP_PARAMS_COLON_SEPARATOR TSRMLS_CC); + PHP_HTTP_REGISTER_CLASS(http\\env, Request, http_env_request, php_http_message_class_entry, 0); return SUCCESS; diff --git a/php_http_env.h b/php_http_env.h index 94fff29..5fb5e5b 100644 --- a/php_http_env.h +++ b/php_http_env.h @@ -88,6 +88,7 @@ PHP_METHOD(HttpEnv, negotiateContentType); PHP_METHOD(HttpEnv, negotiate); PHP_METHOD(HttpEnv, persistentHandlesStat); PHP_METHOD(HttpEnv, persistentHandlesClean); +PHP_METHOD(HttpEnv, parseParams); extern zend_class_entry *php_http_env_request_class_entry; extern zend_function_entry php_http_env_request_method_entry[]; diff --git a/php_http_neon.c b/php_http_neon.c index 99f7cf5..a153f86 100644 --- a/php_http_neon.c +++ b/php_http_neon.c @@ -425,7 +425,7 @@ static STATUS set_options(php_http_request_t *h, HashTable *options) /* check whether cookies should not be urlencoded; default is to urlencode them */ if ((!(urlenc_cookies = get_option(&neon->options.cache, options, ZEND_STRS("encodecookies"), IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) { - php_http_url_encode_hash_recursive(HASH_OF(zoption), &neon->options.headers, "; ", lenof("; "), NULL, 0 TSRMLS_CC); + php_http_url_encode_hash_ex(HASH_OF(zoption), &neon->options.headers, ZEND_STRS("; "), ZEND_STRS("="), NULL, 0 TSRMLS_CC); } else { HashPosition pos; php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0); diff --git a/php_http_querystring.c b/php_http_querystring.c index de40a23..5f67482 100644 --- a/php_http_querystring.c +++ b/php_http_querystring.c @@ -181,7 +181,7 @@ PHP_HTTP_API STATUS php_http_querystring_update(zval *qarray, zval *params, zval char *s; size_t l; - if (SUCCESS == php_http_url_encode_hash(Z_ARRVAL_P(qarray), 0, NULL, 0, &s, &l TSRMLS_CC)) { + if (SUCCESS == php_http_url_encode_hash(Z_ARRVAL_P(qarray), NULL, 0, &s, &l TSRMLS_CC)) { zval_dtor(outstring); ZVAL_STRINGL(outstring, s, l, 0); } else { diff --git a/php_http_request.c b/php_http_request.c index 0812828..b0df031 100644 --- a/php_http_request.c +++ b/php_http_request.c @@ -978,7 +978,7 @@ 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); } @@ -1009,7 +1009,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); } diff --git a/php_http_url.c b/php_http_url.c index 79966b6..5825023 100644 --- a/php_http_url.c +++ b/php_http_url.c @@ -325,33 +325,30 @@ PHP_HTTP_API void php_http_url(int flags, const php_url *old_url, const php_url } } -PHP_HTTP_API STATUS php_http_url_encode_hash(HashTable *hash, zend_bool override_argsep, char *pre_encoded_data, size_t pre_encoded_len, char **encoded_data, size_t *encoded_len TSRMLS_DC) +PHP_HTTP_API STATUS php_http_url_encode_hash(HashTable *hash, const char *pre_encoded_str, size_t pre_encoded_len, char **encoded_str, size_t *encoded_len TSRMLS_DC) { - char *arg_sep; + const char *arg_sep_str; size_t arg_sep_len; php_http_buffer_t *qstr = php_http_buffer_new(); - if (override_argsep || !(arg_sep_len = strlen(arg_sep = INI_STR("arg_separator.output")))) { - arg_sep = PHP_HTTP_URL_ARGSEP; - arg_sep_len = lenof(PHP_HTTP_URL_ARGSEP); - } + php_http_url_argsep(&arg_sep_str, &arg_sep_len TSRMLS_CC); - if (pre_encoded_len && pre_encoded_data) { - php_http_buffer_append(qstr, pre_encoded_data, pre_encoded_len); + if (pre_encoded_len && pre_encoded_str) { + php_http_buffer_append(qstr, pre_encoded_str, pre_encoded_len); } - if (SUCCESS != php_http_url_encode_hash_recursive(hash, qstr, arg_sep, arg_sep_len, NULL, 0 TSRMLS_CC)) { + if (SUCCESS != php_http_url_encode_hash_ex(hash, qstr, arg_sep_str, arg_sep_len, ZEND_STRS("="), NULL, 0 TSRMLS_CC)) { php_http_buffer_free(&qstr); return FAILURE; } - php_http_buffer_data(qstr, encoded_data, encoded_len); + php_http_buffer_data(qstr, encoded_str, encoded_len); php_http_buffer_free(&qstr); return SUCCESS; } -PHP_HTTP_API STATUS php_http_url_encode_hash_recursive(HashTable *ht, php_http_buffer_t *str, const char *arg_sep, size_t arg_sep_len, const char *prefix, size_t prefix_len TSRMLS_DC) +PHP_HTTP_API STATUS php_http_url_encode_hash_ex(HashTable *ht, php_http_buffer_t *str, const char *arg_sep_str, size_t arg_sep_len, const char *val_sep_str, size_t val_sep_len, const char *prefix_str, size_t prefix_len TSRMLS_DC) { php_http_array_hashkey_t key = php_http_array_hashkey_init(0); zval **data = NULL; @@ -390,15 +387,15 @@ PHP_HTTP_API STATUS php_http_url_encode_hash_recursive(HashTable *ht, php_http_b { php_http_buffer_init(&new_prefix); - if (prefix && prefix_len) { - php_http_buffer_append(&new_prefix, prefix, prefix_len); + if (prefix_str && prefix_len) { + php_http_buffer_append(&new_prefix, prefix_str, prefix_len); php_http_buffer_appends(&new_prefix, "%5B"); } php_http_buffer_append(&new_prefix, encoded_key, encoded_len); efree(encoded_key); - if (prefix && prefix_len) { + if (prefix_str && prefix_len) { php_http_buffer_appends(&new_prefix, "%5D"); } php_http_buffer_fix(&new_prefix); @@ -407,7 +404,7 @@ PHP_HTTP_API STATUS php_http_url_encode_hash_recursive(HashTable *ht, php_http_b if (Z_TYPE_PP(data) == IS_ARRAY || Z_TYPE_PP(data) == IS_OBJECT) { STATUS status; ++ht->nApplyCount; - status = php_http_url_encode_hash_recursive(HASH_OF(*data), str, arg_sep, arg_sep_len, PHP_HTTP_BUFFER_VAL(&new_prefix), PHP_HTTP_BUFFER_LEN(&new_prefix) TSRMLS_CC); + status = php_http_url_encode_hash_ex(HASH_OF(*data), str, arg_sep_str, arg_sep_len, val_sep_str, val_sep_len, PHP_HTTP_BUFFER_VAL(&new_prefix), PHP_HTTP_BUFFER_LEN(&new_prefix) TSRMLS_CC); --ht->nApplyCount; if (SUCCESS != status) { php_http_buffer_dtor(&new_prefix); @@ -418,10 +415,10 @@ PHP_HTTP_API STATUS php_http_url_encode_hash_recursive(HashTable *ht, php_http_b zval *val = php_http_ztyp(IS_STRING, *data); if (PHP_HTTP_BUFFER_LEN(str)) { - php_http_buffer_append(str, arg_sep, arg_sep_len); + php_http_buffer_append(str, arg_sep_str, arg_sep_len); } php_http_buffer_append(str, PHP_HTTP_BUFFER_VAL(&new_prefix), PHP_HTTP_BUFFER_LEN(&new_prefix)); - php_http_buffer_appends(str, "="); + php_http_buffer_append(str, val_sep_str, val_sep_len); if (Z_STRLEN_P(val) && Z_STRVAL_P(val)) { char *encoded_val; diff --git a/php_http_url.h b/php_http_url.h index 7b16c01..23f529d 100644 --- a/php_http_url.h +++ b/php_http_url.h @@ -39,8 +39,19 @@ PHP_HTTP_API void php_http_url(int flags, const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC); PHP_HTTP_API char *php_http_url_absolute(const char *url, int flags TSRMLS_DC); -PHP_HTTP_API STATUS php_http_url_encode_hash(HashTable *hash, zend_bool override_argsep, char *pre_encoded_data, size_t pre_encoded_len, char **encoded_data, size_t *encoded_len TSRMLS_DC); -PHP_HTTP_API STATUS php_http_url_encode_hash_recursive(HashTable *ht, php_http_buffer_t *str, const char *arg_sep, size_t arg_sep_len, const char *prefix, size_t prefix_len TSRMLS_DC); +PHP_HTTP_API STATUS php_http_url_encode_hash(HashTable *hash, const char *pre_encoded_str, size_t pre_encoded_len, char **encoded_str, size_t *encoded_len TSRMLS_DC); +PHP_HTTP_API STATUS php_http_url_encode_hash_ex(HashTable *ht, php_http_buffer_t *str, const char *arg_sep_str, size_t arg_sep_len, const char *val_sep_str, size_t val_sep_len, const char *prefix_str, size_t prefix_len TSRMLS_DC); + +static inline void php_http_url_argsep(const char **str, size_t *len TSRMLS_DC) +{ + *str = INI_STR("arg_separator.output"); + *len = strlen(*str); + + if (!*len) { + *str = PHP_HTTP_URL_ARGSEP; + *len = lenof(PHP_HTTP_URL_ARGSEP); + } +} static inline php_url *php_http_url_from_struct(php_url *url, HashTable *ht TSRMLS_DC) { -- 2.30.2