From 1b8751d5926235de8d0a9e26809ed774db709cde Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 4 Jan 2013 13:55:59 +0000 Subject: [PATCH] - we have const APIs in 5.4+ and do not care about warnings in 5.3 - separate php_http_url_to_string() --- php_http_client_factory.c | 4 +-- php_http_env_response.c | 17 ++++------- php_http_url.c | 46 +----------------------------- php_http_url.h | 60 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 59 deletions(-) diff --git a/php_http_client_factory.c b/php_http_client_factory.c index 87c4419..d97f686 100644 --- a/php_http_client_factory.c +++ b/php_http_client_factory.c @@ -38,10 +38,8 @@ PHP_HTTP_API STATUS php_http_client_factory_get_driver(const char *name_str, siz static zend_class_entry *php_http_client_factory_find_class_entry(zval *this_ptr, const char *for_str, size_t for_len TSRMLS_DC) { /* stupid non-const api */ - char *sc = estrndup(for_str, for_len); - zval *cn = zend_read_property(Z_OBJCE_P(getThis()), getThis(), sc, for_len, 0 TSRMLS_CC); + zval *cn = zend_read_property(Z_OBJCE_P(getThis()), getThis(), for_str, for_len, 0 TSRMLS_CC); - efree(sc); if (Z_TYPE_P(cn) == IS_STRING && Z_STRLEN_P(cn)) { return zend_fetch_class(Z_STRVAL_P(cn), Z_STRLEN_P(cn), 0 TSRMLS_CC); } diff --git a/php_http_env_response.c b/php_http_env_response.c index 43a7ea1..66f4782 100644 --- a/php_http_env_response.c +++ b/php_http_env_response.c @@ -15,27 +15,24 @@ static void set_option(zval *options, const char *name_str, size_t name_len, int type, void *value_ptr, size_t value_len TSRMLS_DC) { if (Z_TYPE_P(options) == IS_OBJECT) { - /* stupid non-const api */ - char *name = estrndup(name_str, name_len); if (value_ptr) { switch (type) { case IS_DOUBLE: - zend_update_property_double(Z_OBJCE_P(options), options, name, name_len, *(double *)value_ptr TSRMLS_CC); + zend_update_property_double(Z_OBJCE_P(options), options, name_str, name_len, *(double *)value_ptr TSRMLS_CC); break; case IS_LONG: - zend_update_property_long(Z_OBJCE_P(options), options, name, name_len, *(long *)value_ptr TSRMLS_CC); + zend_update_property_long(Z_OBJCE_P(options), options, name_str, name_len, *(long *)value_ptr TSRMLS_CC); break; case IS_STRING: - zend_update_property_stringl(Z_OBJCE_P(options), options, name, name_len, value_ptr, value_len TSRMLS_CC); + zend_update_property_stringl(Z_OBJCE_P(options), options, name_str, name_len, value_ptr, value_len TSRMLS_CC); break; case IS_OBJECT: - zend_update_property(Z_OBJCE_P(options), options, name, name_len, value_ptr TSRMLS_CC); + zend_update_property(Z_OBJCE_P(options), options, name_str, name_len, value_ptr TSRMLS_CC); break; } } else { - zend_update_property_null(Z_OBJCE_P(options), options, name, name_len TSRMLS_CC); + zend_update_property_null(Z_OBJCE_P(options), options, name_str, name_len TSRMLS_CC); } - efree(name); } else { convert_to_array(options); if (value_ptr) { @@ -66,9 +63,7 @@ static zval *get_option(zval *options, const char *name_str, size_t name_len TSR zval *val, **valptr; if (Z_TYPE_P(options) == IS_OBJECT) { - char *name = estrndup(name_str, name_len); - val = zend_read_property(Z_OBJCE_P(options), options, name, name_len, 0 TSRMLS_CC); - efree(name); + val = zend_read_property(Z_OBJCE_P(options), options, name_str, name_len, 0 TSRMLS_CC); } else { if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(options), name_str, name_len + 1, (void *) &valptr)) { val = *valptr; diff --git a/php_http_url.c b/php_http_url.c index 4560848..aeee9e8 100644 --- a/php_http_url.c +++ b/php_http_url.c @@ -287,51 +287,7 @@ PHP_HTTP_API void php_http_url(int flags, const php_url *old_url, const php_url } if (url_str) { - size_t len; - - *url_str = emalloc(PHP_HTTP_URL_MAXLEN + 1); - - **url_str = '\0'; - strlcat(*url_str, url->scheme, PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, "://", PHP_HTTP_URL_MAXLEN); - - if (url->user && *url->user) { - strlcat(*url_str, url->user, PHP_HTTP_URL_MAXLEN); - if (url->pass && *url->pass) { - strlcat(*url_str, ":", PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, url->pass, PHP_HTTP_URL_MAXLEN); - } - strlcat(*url_str, "@", PHP_HTTP_URL_MAXLEN); - } - - strlcat(*url_str, url->host, PHP_HTTP_URL_MAXLEN); - - if (url->port) { - char port_str[8]; - - snprintf(port_str, sizeof(port_str), "%d", (int) url->port); - strlcat(*url_str, ":", PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, port_str, PHP_HTTP_URL_MAXLEN); - } - - strlcat(*url_str, url->path, PHP_HTTP_URL_MAXLEN); - - if (url->query && *url->query) { - strlcat(*url_str, "?", PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, url->query, PHP_HTTP_URL_MAXLEN); - } - - if (url->fragment && *url->fragment) { - strlcat(*url_str, "#", PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, url->fragment, PHP_HTTP_URL_MAXLEN); - } - - if (PHP_HTTP_URL_MAXLEN == (len = strlen(*url_str))) { - php_http_error(HE_NOTICE, PHP_HTTP_E_URL, "Length of URL exceeds PHP_HTTP_URL_MAXLEN"); - } - if (url_len) { - *url_len = len; - } + php_http_url_to_string(url, url_str, url_len TSRMLS_CC); } if (url_ptr) { diff --git a/php_http_url.h b/php_http_url.h index 65a764e..30f7780 100644 --- a/php_http_url.h +++ b/php_http_url.h @@ -48,6 +48,66 @@ static inline void php_http_url_argsep(const char **str, size_t *len TSRMLS_DC) } } +static inline void php_http_url_to_string(php_url *url, char **url_str, size_t *url_len TSRMLS_DC) +{ + php_http_buffer_t buf; + + php_http_buffer_init(&buf); + + if (url->scheme && *url->scheme) { + php_http_buffer_appendl(&buf, url->scheme); + php_http_buffer_appends(&buf, "://"); + } else { + php_http_buffer_appends(&buf, "//"); + } + + if (url->user && *url->user) { + php_http_buffer_appendl(&buf, url->user); + if (url->pass && *url->pass) { + php_http_buffer_appends(&buf, ":"); + php_http_buffer_appendl(&buf, url->pass); + } + php_http_buffer_appends(&buf, "@"); + } + + if (url->host && *url->host) { + php_http_buffer_appendl(&buf, url->host); + } else { + php_http_buffer_appends(&buf, "localhost"); + } + + if (url->port) { + php_http_buffer_appendf(&buf, ":%hu", url->port); + } + + if (url->path && *url->path) { + php_http_buffer_appendl(&buf, url->path); + } + + if (url->query && *url->query) { + php_http_buffer_appends(&buf, "?"); + php_http_buffer_appendl(&buf, url->query); + } + + if (url->fragment && *url->fragment) { + php_http_buffer_appends(&buf, "#"); + php_http_buffer_appendl(&buf, url->fragment); + } + + php_http_buffer_shrink(&buf); + php_http_buffer_fix(&buf); + + if (url_len) { + *url_len = buf.used; + } + + if (url_str) { + *url_str = buf.data; + } else { + php_http_buffer_dtor(&buf); + } +} + static inline php_url *php_http_url_from_struct(php_url *url, HashTable *ht TSRMLS_DC) { zval **e; -- 2.30.2