- split off query strin API and use it in
[m6w6/ext-http] / http_url_api.c
index dff4caec0a78d6e73483ea9669d3c0f3b458183c..1ccd4dba72a529f92929d8016bc735a63d3d0051 100644 (file)
 #include "ext/standard/php_string.h"
 
 #include "php_http_api.h"
+#include "php_http_querystring_api.h"
 #include "php_http_url_api.h"
 
+static inline char *localhostname(void)
+{
+       char hostname[1024] = {0};
+       
+#ifdef PHP_WIN32
+       if (SUCCESS == gethostname(hostname, lenof(hostname))) {
+               return estrdup(hostname);
+       }
+#elif defined(HAVE_UNISTD_H)
+       if (SUCCESS == gethostname(hostname, lenof(hostname))) {
+               size_t hlen = strlen(hostname);
+               
+               if (hlen <= lenof(hostname) - lenof("(none)")) {
+                       hostname[hlen++] = '.';
+                       if (SUCCESS == getdomainname(&hostname[hlen], lenof(hostname) - hlen)) {
+                               if (!strcmp(&hostname[hlen], "(none)")) {
+                                       hostname[hlen - 1] = '\0';
+                               }
+                               return estrdup(hostname);
+                       }
+               }
+       }
+#endif
+       return estrdup("localhost");
+}
+
 PHP_MINIT_FUNCTION(http_url)
 {
        HTTP_LONG_CONSTANT("HTTP_URL_REPLACE", HTTP_URL_REPLACE);
@@ -40,16 +67,22 @@ PHP_MINIT_FUNCTION(http_url)
 
 PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC)
 {
-       char *abs = estrdup(url);
-       php_url *purl = php_url_parse(abs);
+       char *abs = NULL;
+       php_url *purl = NULL;
        
-       STR_SET(abs, NULL);
+       if (url) {
+               purl = php_url_parse(abs = estrdup(url));
+               STR_SET(abs, NULL);
+               if (!purl) {
+                       http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", url);
+                       return NULL;
+               }
+       }
+       
+       http_build_url(0, purl, NULL, NULL, &abs, NULL);
        
        if (purl) {
-               http_build_url(0, purl, NULL, NULL, &abs, NULL);
                php_url_free(purl);
-       } else {
-               http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", url);
        }
        
        return abs;
@@ -99,10 +132,21 @@ PHP_HTTP_API void _http_build_url(int flags, const php_url *old_url, const php_u
        }
        if (!(flags & HTTP_URL_STRIP_QUERY)) {
                if ((flags & HTTP_URL_JOIN_QUERY) && __URLSET(new_url, query) && __URLSET(old_url, query)) {
-                       url->query = ecalloc(1, strlen(new_url->query) + strlen(old_url->query) + 1 + 1);
-                       strcat(url->query, old_url->query);
-                       strcat(url->query, "&");
-                       strcat(url->query, new_url->query);
+                       zval qarr, qstr;
+                       
+                       INIT_PZVAL(&qstr);
+                       INIT_PZVAL(&qarr);
+                       array_init(&qarr);
+                       
+                       ZVAL_STRING(&qstr, old_url->query, 0);
+                       http_querystring_modify(&qarr, &qstr);
+                       ZVAL_STRING(&qstr, new_url->query, 0);
+                       http_querystring_modify(&qarr, &qstr);
+                       
+                       ZVAL_NULL(&qstr);
+                       http_querystring_update(&qarr, &qstr);
+                       url->query = Z_STRVAL(qstr);
+                       zval_dtor(&qarr);
                } else {
                        __URLCPY(query);
                }
@@ -144,7 +188,7 @@ PHP_HTTP_API void _http_build_url(int flags, const php_url *old_url, const php_u
                                (zhost = http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost)) {
                        url->host = estrndup(Z_STRVAL_P(zhost), Z_STRLEN_P(zhost));
                } else {
-                       url->host = estrndup("localhost", lenof("localhost"));
+                       url->host = localhostname();
                }
        }
        
@@ -208,6 +252,11 @@ PHP_HTTP_API void _http_build_url(int flags, const php_url *old_url, const php_u
                                                memmove(&ptr[1], pos, end - pos);
                                        }
                                break;
+                               
+                               default:
+                                       /* something else */
+                                       ++ptr;
+                               break;
                        }
                }
        }
@@ -245,9 +294,9 @@ PHP_HTTP_API void _http_build_url(int flags, const php_url *old_url, const php_u
                strlcat(*url_str, url->host, HTTP_URL_MAXLEN);
                
                if (url->port) {
-                       char port_str[6] = {0};
+                       char port_str[8] = {0};
                        
-                       snprintf(port_str, 5, "%d", (int) url->port);
+                       snprintf(port_str, lenof(port_str), "%d", (int) url->port);
                        strlcat(*url_str, ":", HTTP_URL_MAXLEN);
                        strlcat(*url_str, port_str, HTTP_URL_MAXLEN);
                }
@@ -336,6 +385,7 @@ PHP_HTTP_API STATUS _http_urlencode_hash_recursive(HashTable *ht, phpstr *str, c
                phpstr new_prefix;
                
                if (!data || !*data) {
+                       phpstr_dtor(str);
                        return FAILURE;
                }
                
@@ -353,14 +403,14 @@ PHP_HTTP_API STATUS _http_urlencode_hash_recursive(HashTable *ht, phpstr *str, c
                        phpstr_init(&new_prefix);
                        if (prefix && prefix_len) {
                                phpstr_append(&new_prefix, prefix, prefix_len);
-                               phpstr_appends(&new_prefix, "[");
+                               phpstr_appends(&new_prefix, "%5B");
                        }
                        
                        phpstr_append(&new_prefix, encoded_key, encoded_len);
                        efree(encoded_key);
                        
                        if (prefix && prefix_len) {
-                               phpstr_appends(&new_prefix, "]");
+                               phpstr_appends(&new_prefix, "%5D");
                        }
                        phpstr_fix(&new_prefix);
                }
@@ -372,6 +422,7 @@ PHP_HTTP_API STATUS _http_urlencode_hash_recursive(HashTable *ht, phpstr *str, c
                        --ht->nApplyCount;
                        if (SUCCESS != status) {
                                phpstr_dtor(&new_prefix);
+                               phpstr_dtor(str);
                                return FAILURE;
                        }
                } else {