From bf86dcdcd68677a8c4b7c8193388c529a55a3503 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 7 Feb 2006 16:04:03 +0000 Subject: [PATCH] - use gethostname() if available in http_build_url() --- config.m4 | 4 ++-- http_url_api.c | 30 +++++++++++++++++++++++++++++- php_http.h | 3 +++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/config.m4 b/config.m4 index f8ae079..ca4f3d5 100644 --- 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 diff --git a/http_url_api.c b/http_url_api.c index 01a2e41..5b618a3 100644 --- a/http_url_api.c +++ b/http_url_api.c @@ -23,6 +23,32 @@ #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 { diff --git a/php_http.h b/php_http.h index 7e82c04..4aa2679 100644 --- a/php_http.h +++ b/php_http.h @@ -47,6 +47,9 @@ # elif defined(HAVE_NETDB_H) # define HTTP_HAVE_NETDB # include +# ifdef HAVE_UNISTD_H +# include +# endif # endif #endif -- 2.30.2