- fix segv if http_absolute_uri returns NULL
[m6w6/ext-http] / http_url_api.c
index aa0c1f37c835692631866bd1083565604c39a6d6..163dec349de66318ef55d652df0fa951aaccbcaa 100644 (file)
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
-#include "php.h"
+
+#define HTTP_WANT_NETDB
+#include "php_http.h"
 
 #include "SAPI.h"
 #include "zend_ini.h"
 #include "php_output.h"
 #include "ext/standard/url.h"
 
-#include "php_http.h"
 #include "php_http_api.h"
 #include "php_http_url_api.h"
-#include "php_http_std_defs.h"
-
-#include "phpstr/phpstr.h"
-
-#ifdef PHP_WIN32
-#      include <winsock2.h>
-#elif defined(HAVE_NETDB_H)
-#      include <netdb.h>
-#endif
 
 ZEND_EXTERN_MODULE_GLOBALS(http);
 
@@ -77,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 {
@@ -88,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;
+               if ((se = getservbyname(furl.scheme, "tcp"))) {
+                       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;
@@ -214,6 +206,7 @@ PHP_HTTP_API STATUS _http_urlencode_hash_recursive(HashTable *ht, phpstr *str, c
        uint len = 0;
        ulong idx = 0;
        zval **data = NULL;
+       HashPosition pos;
 
        if (!ht || !str) {
                http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid parameters");
@@ -223,7 +216,7 @@ PHP_HTTP_API STATUS _http_urlencode_hash_recursive(HashTable *ht, phpstr *str, c
                return SUCCESS;
        }
        
-       FOREACH_HASH_KEYLENVAL(ht, key, len, idx, data) {
+       FOREACH_HASH_KEYLENVAL(pos, ht, key, len, idx, data) {
                char *encoded_key;
                int encoded_len;
                phpstr new_prefix;