- use gethostname() if available in http_build_url()
authorMichael Wallner <mike@php.net>
Tue, 7 Feb 2006 16:04:03 +0000 (16:04 +0000)
committerMichael Wallner <mike@php.net>
Tue, 7 Feb 2006 16:04:03 +0000 (16:04 +0000)
config.m4
http_url_api.c
php_http.h

index f8ae079b3120f079f6ed798e4d6a31f563dff54d..ca4f3d51007db13554d26c84729de1b20b51356d 100644 (file)
--- a/config.m4
+++ b/config.m4
@@ -32,9 +32,9 @@ if test "$PHP_HTTP" != "no"; then
        ])
 
 dnl -------
-dnl NETDB.H
+dnl HEADERS
 dnl -------
-       AC_CHECK_HEADERS([netdb.h])
+       AC_CHECK_HEADERS([netdb.h unistd.h])
 
 dnl ----
 dnl ZLIB
index 01a2e41c464e03a22f8757ec695b5586e58656ca..5b618a3645bca1da82f9fc4b480eca41120f1aec 100644 (file)
 #include "php_http_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);
@@ -144,7 +170,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();
                }
        }
        
@@ -341,6 +367,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;
                }
                
@@ -377,6 +404,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 {
index 7e82c04037a7073e6b6ea59561f1fe0b27c7baa5..4aa2679dd2ccb5a7b76574c7422f0ba43085dd7c 100644 (file)
@@ -47,6 +47,9 @@
 #      elif defined(HAVE_NETDB_H)
 #              define HTTP_HAVE_NETDB
 #              include <netdb.h>
+#              ifdef HAVE_UNISTD_H
+#                      include <unistd.h>
+#              endif
 #      endif
 #endif