From f9d92cff35589f414e369228502c91f35a4e3643 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 7 Dec 2005 21:02:57 +0000 Subject: [PATCH] - fix segv if http_absolute_uri returns NULL - translate ports with htons and ntohs - check if supllied host actaually has a length --- http_functions.c | 9 ++++++--- http_url_api.c | 10 +++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/http_functions.c b/http_functions.c index f7ecd7a..5cb8a16 100644 --- a/http_functions.c +++ b/http_functions.c @@ -79,7 +79,7 @@ PHP_FUNCTION(http_date) * If a port is pecified in either the url or as sperate parameter, * it will be added if it differs from te default port for HTTP(S). * - * Returns the absolute URI as string. + * Returns the absolute URI as string on success or false on failure. * * Examples: *
@@ -90,7 +90,7 @@ PHP_FUNCTION(http_date)
  */
 PHP_FUNCTION(http_build_uri)
 {
-	char *url = NULL, *proto = NULL, *host = NULL;
+	char *url = NULL, *proto = NULL, *host = NULL, *built = NULL;
 	int url_len = 0, proto_len = 0, host_len = 0;
 	long port = 0;
 
@@ -98,7 +98,10 @@ PHP_FUNCTION(http_build_uri)
 		RETURN_FALSE;
 	}
 
-	RETURN_STRING(http_absolute_uri_ex(url, url_len, proto, proto_len, host, host_len, port), 0);
+	if ((built = http_absolute_uri_ex(url, url_len, proto, proto_len, host, host_len, port))) {
+		RETURN_STRING(built, 0);
+	}
+	RETURN_FALSE;
 }
 /* }}} */
 
diff --git a/http_url_api.c b/http_url_api.c
index 3fbf008..163dec3 100644
--- a/http_url_api.c
+++ b/http_url_api.c
@@ -69,7 +69,7 @@ PHP_HTTP_API char *_http_absolute_url_ex(
 	} else if (purl->scheme) {
 		furl.scheme = purl->scheme;
 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
-	} else if (port && (se = getservbyport(port, "tcp"))) {
+	} else if (port && (se = getservbyport(htons(port), "tcp"))) {
 		furl.scheme = (scheme = estrdup(se->s_name));
 #endif
 	} else {
@@ -80,17 +80,17 @@ PHP_HTTP_API char *_http_absolute_url_ex(
 		furl.port = port;
 	} else if (purl->port) {
 		furl.port = purl->port;
-	} else if (strncmp(furl.scheme, "http", 4)) {
+	} else if ((!furl.scheme) || strncmp(furl.scheme, "http", 4)) {
 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
 		if ((se = getservbyname(furl.scheme, "tcp"))) {
-			furl.port = se->s_port;
+			furl.port = ntohs(se->s_port);
 		}
 #endif
 	} else {
-		furl.port = (furl.scheme[4] == 's') ? 443 : 80;
+		furl.port = (furl.scheme && furl.scheme[4] == 's') ? 443 : 80;
 	}
 
-	if (host) {
+	if (host && host_len) {
 		furl.host = (char *) host;
 	} else if (purl->host) {
 		furl.host = purl->host;
-- 
2.30.2