* run tests on linux - flushing changes
[m6w6/ext-http] / http_api.c
index 2161d6d571d14f2807c022450122e174a0971e98..48387837c4fd068ed13f87298389230d468feba6 100644 (file)
@@ -23,6 +23,7 @@
 #endif
 
 #include <ctype.h>
+#include <netdb.h>
 
 #include "php.h"
 #include "php_version.h"
@@ -918,7 +919,7 @@ PHP_HTTP_API char *_http_absolute_uri_ex(
        } else if (purl->scheme) {
                furl.scheme = purl->scheme;
 #ifdef ZEND_ENGINE_2
-       } else if (port && (se = getservbyport(htons(port), "tcp"))) {
+       } else if (port && (se = getservbyport(port, "tcp"))) {
                furl.scheme = (scheme = estrdup(se->s_name));
 #endif
        } else {
@@ -932,7 +933,7 @@ PHP_HTTP_API char *_http_absolute_uri_ex(
        } else if (strncmp(furl.scheme, "http", 4)) {
 #ifdef ZEND_ENGINE_2
                if (se = getservbyname(furl.scheme, "tcp")) {
-                       furl.port = ntohs(se->s_port);
+                       furl.port = se->s_port;
                } else
 #endif
                furl.port = 80;
@@ -982,14 +983,17 @@ PHP_HTTP_API char *_http_absolute_uri_ex(
 
        HTTP_URI_STRLCATL(URL, full_len, furl.host);
 
-       if (    (strcmp(furl.scheme, "http") && (furl.port != 80)) ||
-                       (strcmp(furl.scheme, "https") && (furl.port != 443))) {
+       if (    (!strcmp(furl.scheme, "http") && (furl.port != 80)) ||
+                       (!strcmp(furl.scheme, "https") && (furl.port != 443))) {
                char port_string[8] = {0};
                snprintf(port_string, 7, ":%u", furl.port);
                HTTP_URI_STRLCATL(URL, full_len, port_string);
        }
 
        if (furl.path) {
+               if (furl.path[0] != '/') {
+                       HTTP_URI_STRLCATS(URL, full_len, "/");
+               }
                HTTP_URI_STRLCATL(URL, full_len, furl.path);
        } else {
                HTTP_URI_STRLCATS(URL, full_len, "/");
@@ -1388,7 +1392,7 @@ PHP_HTTP_API STATUS _http_send_stream_ex(php_stream *file,
 }
 /* }}} */
 
-/* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
+/* {{{ STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
 PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
        const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
 {
@@ -1452,28 +1456,39 @@ PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
 }
 /* }}} */
 
-/* {{{ proto STATUS http_split_response_ex(char *, size_t, zval *, zval *) */
+/* {{{ STATUS http_split_response(zval *, zval *, zval *) */
+PHP_HTTP_API STATUS _http_split_response(zval *response, zval *headers, zval *body TSRMLS_DC)
+{
+       char *b = NULL;
+       size_t l = 0;
+       STATUS status = http_split_response_ex(Z_STRVAL_P(response), Z_STRLEN_P(response), Z_ARRVAL_P(headers), &b, &l);
+       ZVAL_STRINGL(body, b, l, 0);
+       return status;
+}
+/* }}} */
+
+/* {{{ STATUS http_split_response(char *, size_t, HashTable *, char **, size_t *) */
 PHP_HTTP_API STATUS _http_split_response_ex(char *response,
        size_t response_len, HashTable *headers, char **body, size_t *body_len TSRMLS_DC)
 {
-       char *header = response;
-       *body = NULL;
+       char *header = response, *real_body = NULL;
 
        while (0 < (response_len - (response - header + 4))) {
                if (    (*response++ == '\r') &&
                                (*response++ == '\n') &&
                                (*response++ == '\r') &&
                                (*response++ == '\n')) {
-                       *body = response;
+                       real_body = response;
                        break;
                }
        }
 
-       if (*body && (*body_len = (response_len - (*body - header)))) {
-               *body = estrndup(*body, *body_len);
+       if (real_body && (*body_len = (response_len - (real_body - header)))) {
+               *body = ecalloc(1, *body_len + 1);
+               memcpy(*body, real_body, *body_len);
        }
 
-       return http_parse_headers_ex(header, *body ? response_len - *body_len : response_len, headers, 1);
+       return http_parse_headers_ex(header, real_body ? response_len - *body_len : response_len, headers, 1);
 }
 /* }}} */