- we have const APIs in 5.4+ and do not care about warnings in 5.3
authorMichael Wallner <mike@php.net>
Fri, 4 Jan 2013 13:55:59 +0000 (13:55 +0000)
committerMichael Wallner <mike@php.net>
Fri, 4 Jan 2013 13:55:59 +0000 (13:55 +0000)
- separate php_http_url_to_string()

php_http_client_factory.c
php_http_env_response.c
php_http_url.c
php_http_url.h

index 87c4419eb09c2e07ec8542a4cbba9c86630a14fd..d97f68640f10c3076962cdc0ac4cdc3d01214726 100644 (file)
@@ -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);
        }
index 43a7ea1c86f909f81e7ded17492679cff454bea3..66f4782d5135dd7b91c85b739ad30413d5df7908 100644 (file)
 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;
index 4560848ece40d3e53a754ed9d61b92a23ab05d42..aeee9e81bc3ef9c0d61fb15f76433c039d0b8977 100644 (file)
@@ -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) {
index 65a764e6c1f899394298184b3c471a8a7aef4c06..30f7780e92a548c30f92d3f7b08d89d24a03cc41 100644 (file)
@@ -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;