* tiny API adjustments
[m6w6/ext-http] / http_api.c
index d0acbf3e7e6e13b68ab2814c7dd4540df3fad652..0e4e604f8ea3226913dd3e03cb3325ba3f18abcf 100644 (file)
@@ -879,62 +879,129 @@ PHP_HTTP_API STATUS _http_cache_etag(const char *etag, const size_t etag_len,
 }
 /* }}} */
 
-/* {{{ char *http_absolute_uri(char *, char *) */
-PHP_HTTP_API char *_http_absolute_uri(const char *url,
-       const char *proto TSRMLS_DC)
+/* {{{ char *http_absolute_uri(char *) */
+PHP_HTTP_API char *_http_absolute_uri_ex(
+       const char *url,        size_t url_len,
+       const char *proto,      size_t proto_len,
+       const char *host,       size_t host_len,
+       unsigned port TSRMLS_DC)
 {
-       char *proto_ptr, *host, *path, *PTR, *URI = ecalloc(1, HTTP_URI_MAXLEN + 1);
-       zval *zhost;
+       php_url *purl, furl = {NULL};
+       struct servent *se;
+       size_t full_len = 0;
+       zval *zhost = NULL;
+       char *scheme = NULL, *URL = ecalloc(1, HTTP_URI_MAXLEN + 1);
+
+       if ((!url || !url_len) && (
+                       (!(url = SG(request_info).request_uri)) ||
+                       (!(url_len = strlen(SG(request_info).request_uri))))) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING,
+                       "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
+               return NULL;
+       }
 
-       if (!url || !strlen(url)) {
-               if (!SG(request_info).request_uri) {
-                       return NULL;
-               }
-               url = SG(request_info).request_uri;
+       if (!(purl = php_url_parse(url))) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse supplied URL");
+               return NULL;
        }
-       /* Mess around with already absolute URIs */
-       else if (proto_ptr = strstr(url, "://")) {
-               if (!proto || !strncmp(url, proto, strlen(proto))) {
-                       strncpy(URI, url, HTTP_URI_MAXLEN);
-                       return URI;
-               } else {
-                       snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
-                       return URI;
-               }
+
+       furl.user               = purl->user;
+       furl.pass               = purl->pass;
+       furl.path               = purl->path;
+       furl.query              = purl->query;
+       furl.fragment   = purl->fragment;
+
+       if (proto) {
+               furl.scheme = scheme = estrdup(proto);
+       } else if (purl->scheme) {
+               furl.scheme = purl->scheme;
+       } else if (port && (se = getservbyport(htons(port), "tcp"))) {
+               furl.scheme = (scheme = estrdup(se->s_name));
+       } else {
+               furl.scheme = "http";
        }
 
-       /* protocol defaults to http */
-       if (!proto || !strlen(proto)) {
-               proto = "http";
+       if (port) {
+               furl.port = port;
+       } else if (purl->port) {
+               furl.port = purl->port;
+       } else if (strncmp(furl.scheme, "http", 4) && (se = getservbyname(furl.scheme, "tcp"))) {
+               furl.port = ntohs(se->s_port);
+       } else {
+               furl.port = furl.scheme[5] ? 443 : 80;
        }
 
-       /* get host name */
-       if (    (zhost = http_get_server_var("HTTP_HOST")) ||
-                       (zhost = http_get_server_var("SERVER_NAME"))) {
-               host = Z_STRVAL_P(zhost);
+       if (host) {
+               furl.host = (char *) host;
+       } else if (purl->host) {
+               furl.host = purl->host;
+       } else if (     (zhost = http_get_server_var("HTTP_HOST")) ||
+                               (zhost = http_get_server_var("SERVER_NAME"))) {
+               furl.host = Z_STRVAL_P(zhost);
        } else {
-               host = "localhost";
+               furl.host = "localhost";
+       }
+
+#define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
+#define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
+#define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
+       if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
+               php_error_docref(NULL TSRMLS_CC, E_NOTICE, \
+                       "Absolute URI would have exceeded max URI length (%d bytes) - " \
+                       "tried to add %d bytes ('%s')", \
+                       HTTP_URI_MAXLEN, add_len, add_string); \
+               if (scheme) { \
+                       efree(scheme); \
+               } \
+               php_url_free(purl); \
+               return URL; \
+       } else { \
+               strcat(URL, add_string); \
+       }
+
+       HTTP_URI_STRLCATL(URL, full_len, furl.scheme);
+       HTTP_URI_STRLCATS(URL, full_len, "://");
+
+       if (furl.user) {
+               HTTP_URI_STRLCATL(URL, full_len, furl.user);
+               if (furl.pass) {
+                       HTTP_URI_STRLCATS(URL, full_len, ":");
+                       HTTP_URI_STRLCATL(URL, full_len, furl.pass);
+               }
+               HTTP_URI_STRLCATS(URL, full_len, "@");
        }
 
+       HTTP_URI_STRLCATL(URL, full_len, furl.host);
 
-       /* glue together */
-       if (url[0] == '/') {
-               snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s", proto, host, url);
-       } else if (SG(request_info).request_uri) {
-               path = estrdup(SG(request_info).request_uri);
-               php_dirname(path, strlen(path));
-               snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s/%s", proto, host, path, url);
-               efree(path);
+       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) {
+               HTTP_URI_STRLCATL(URL, full_len, furl.path);
        } else {
-               snprintf(URI, HTTP_URI_MAXLEN, "%s://%s/%s", proto, host, url);
+               HTTP_URI_STRLCATS(URL, full_len, "/");
+       }
+
+       if (furl.query) {
+               HTTP_URI_STRLCATS(URL, full_len, "?");
+               HTTP_URI_STRLCATL(URL, full_len, furl.query);
+       }
+
+       if (furl.fragment) {
+               HTTP_URI_STRLCATS(URL, full_len, "#");
+               HTTP_URI_STRLCATL(URL, full_len, furl.fragment);
        }
 
-       /* strip everything after a new line */
-       if ((PTR = strchr(URI, '\r')) || (PTR = strchr(URI, '\n'))) {
-               PTR = 0;
+       if (scheme) {
+               efree(scheme);
        }
+       php_url_free(purl);
 
-       return URI;
+       return URL;
 }
 /* }}} */
 
@@ -1378,35 +1445,37 @@ PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
 
 /* {{{ proto STATUS http_split_response_ex(char *, size_t, zval *, zval *) */
 PHP_HTTP_API STATUS _http_split_response_ex(char *response,
-       size_t response_len, zval *zheaders, zval *zbody TSRMLS_DC)
+       size_t response_len, HashTable *headers, char **body, size_t *body_len TSRMLS_DC)
 {
-       char *body = NULL;
        char *header = response;
+       *body = NULL;
 
        while (0 < (response_len - (response - header + 4))) {
                if (    (*response++ == '\r') &&
                                (*response++ == '\n') &&
                                (*response++ == '\r') &&
                                (*response++ == '\n')) {
-                       body = response;
+                       *body = response;
                        break;
                }
        }
 
-       if (body && (response_len - (body - header))) {
-               ZVAL_STRINGL(zbody, body, response_len - (body - header) - 1, 1);
-       } else {
-               Z_TYPE_P(zbody) = IS_NULL;
+       if (*body && (*body_len = response_len - (*body - header))) {
+               *body = estrndup(*body, *body_len - 1);
        }
 
-       return http_parse_headers(header, body ? body - header : response_len, zheaders);
+       return http_parse_headers_ex(header, *body ? *body - header : response_len, headers, 1);
 }
 /* }}} */
 
 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
-PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *array TSRMLS_DC)
+PHP_HTTP_API STATUS _http_parse_headers_ex(char *header, int header_len, 
+       HashTable *headers, zend_bool prettify TSRMLS_DC)
 {
        char *colon = NULL, *line = NULL, *begin = header;
+       zval array;
+
+       Z_ARRVAL(array) = headers;
 
        if (header_len < 2) {
                return FAILURE;
@@ -1415,7 +1484,7 @@ PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *arra
        /* status code */
        if (!strncmp(header, "HTTP/1.", 7)) {
                char *end = strstr(header, HTTP_CRLF);
-               add_assoc_stringl(array, "Status",
+               add_assoc_stringl(&array, "Status",
                        header + sizeof("HTTP/1.x ") - 1,
                        end - (header + sizeof("HTTP/1.x ") - 1), 1);
                header = end + 2;
@@ -1436,6 +1505,11 @@ PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *arra
                                        /* skip empty key */
                                        if (header != colon) {
                                                char *key = estrndup(header, colon - header);
+
+                                               if (prettify) {
+                                                       key = pretty_key(key, colon - header, 1, 1);
+                                               }
+
                                                value_len += line - colon - 1;
 
                                                /* skip leading ws */
@@ -1445,9 +1519,9 @@ PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *arra
 
                                                if (value_len < 1) {
                                                        /* hm, empty header? */
-                                                       add_assoc_stringl(array, key, "", 0, 1);
+                                                       add_assoc_stringl(&array, key, "", 0, 1);
                                                } else {
-                                                       add_assoc_stringl(array, key, colon, value_len, 1);
+                                                       add_assoc_stringl(&array, key, colon, value_len, 1);
                                                }
                                                efree(key);
                                        }
@@ -1469,17 +1543,26 @@ PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *arra
 }
 /* }}} */
 
-/* {{{ void http_get_request_headers(zval *) */
-PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC)
+/* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
+PHP_HTTP_API void _http_get_request_headers_ex(HashTable *headers, zend_bool prettify TSRMLS_DC)
 {
     char *key = NULL;
     long idx = 0;
+    zval array;
+    
+    Z_ARRVAL(array) = headers;
 
     FOREACH_HASH_KEY(HTTP_SERVER_VARS, key, idx) {
         if (key && !strncmp(key, "HTTP_", 5)) {
             zval **header;
+            
+            if (prettify) {
+               key = pretty_key(key + 5, strlen(key) - 5, 1, 1);
+            }
+            
             zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
-            add_assoc_stringl(array, pretty_key(key + 5, strlen(key) - 5, 1, 1), Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
+            add_assoc_stringl(&array, key, Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
+            key = NULL;
         }
     }
 }