From f6e558f7886fff36d239ae2fae69d955ac6925ee Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sat, 5 Mar 2005 23:22:34 +0000 Subject: [PATCH] * much smarter & enhanced absolute_uri() using parse_url() --- http_api.c | 147 +++++++++++++++++++++++++++++++++-------------- http_functions.c | 14 +++-- http_methods.c | 8 +-- php_http_api.h | 5 +- 4 files changed, 120 insertions(+), 54 deletions(-) diff --git a/http_api.c b/http_api.c index 4032f78..317e902 100644 --- a/http_api.c +++ b/http_api.c @@ -28,6 +28,7 @@ #include "php_version.h" #include "php_streams.h" #include "snprintf.h" +#include "spprintf.h" #include "ext/standard/md5.h" #include "ext/standard/url.h" #include "ext/standard/base64.h" @@ -879,62 +880,124 @@ 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.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); - } else { - snprintf(URI, HTTP_URI_MAXLEN, "%s://%s/%s", proto, host, url); + 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); + 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; } /* }}} */ @@ -1406,7 +1469,7 @@ PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, HashTable { char *colon = NULL, *line = NULL, *begin = header; zval array; - + Z_ARRVAL(array) = headers; if (header_len < 2) { diff --git a/http_functions.c b/http_functions.c index bab0e43..3ed49b2 100644 --- a/http_functions.c +++ b/http_functions.c @@ -61,7 +61,7 @@ PHP_FUNCTION(http_date) } /* }}} */ -/* {{{ proto string http_absolute_uri(string url[, string proto]) +/* {{{ proto string http_absolute_uri(string url[, string proto[, string host[, int port]]]) * * This function returns an absolute URI constructed from url. * If the url is already abolute but a different proto was supplied, @@ -80,14 +80,15 @@ PHP_FUNCTION(http_date) */ PHP_FUNCTION(http_absolute_uri) { - char *url = NULL, *proto = NULL; - int url_len = 0, proto_len = 0; + char *url = NULL, *proto = NULL, *host = NULL; + int url_len = 0, proto_len = 0, host_len = 0; + long port = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &url, &url_len, &proto, &proto_len) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ssl", &url, &url_len, &proto, &proto_len, &host, &host_len, &port) != SUCCESS) { RETURN_FALSE; } - RETURN_STRING(http_absolute_uri(url, proto), 0); + RETURN_STRING(http_absolute_uri_ex(url, url_len, proto, proto_len, host, host_len, port), 0); } /* }}} */ @@ -467,7 +468,8 @@ PHP_FUNCTION(http_redirect) } } - URI = http_absolute_uri(url, NULL); + URI = http_absolute_uri(url); + if (query_len) { snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s?%s", URI, query); sprintf(RED, "Redirecting to %s?%s.\n", URI, query, URI, query); diff --git a/http_methods.c b/http_methods.c index 93d6454..45a5c61 100644 --- a/http_methods.c +++ b/http_methods.c @@ -439,7 +439,7 @@ PHP_METHOD(HTTPi_Response, send) getObject(httpi_response_object, obj); NO_ARGS; - + do_cache = GET_PROP(obj, cache); do_gzip = GET_PROP(obj, gzip); @@ -668,7 +668,7 @@ PHP_METHOD(HTTPi_Request, getURL) /* {{{ proto bool HTTPi_Request::setMethod(long request_method) * - * Set the request methods; one of the HTTP_HEAD, HTTP_GET or + * Set the request methods; one of the HTTP_HEAD, HTTP_GET or * HTTP_POST constants. */ PHP_METHOD(HTTPi_Request, setMethod) @@ -1087,8 +1087,8 @@ PHP_METHOD(HTTPi_Request, send) info = GET_PROP(obj, responseInfo); resp = GET_PROP(obj, responseData); - // HTTP_URI_MAXLEN+1 big char * - request_uri = http_absolute_uri(Z_STRVAL_P(URL), NULL); + // HTTP_URI_MAXLEN+1 long char * + request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0); if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) { if (!strchr(request_uri, '?')) { diff --git a/php_http_api.h b/php_http_api.h index 2bcd108..22f11d2 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -100,8 +100,9 @@ PHP_HTTP_API STATUS _http_cache_last_modified(const time_t last_modified, const #define http_cache_etag(e, el, cc, ccl) _http_cache_etag((e), (el), (cc), (ccl) TSRMLS_CC) PHP_HTTP_API STATUS _http_cache_etag(const char *etag, const size_t etag_len, const char *cache_control, const size_t cc_len TSRMLS_DC); -#define http_absolute_uri(url, proto) _http_absolute_uri((url), (proto) TSRMLS_CC) -PHP_HTTP_API char *_http_absolute_uri(const char *url, const char *proto TSRMLS_DC); +#define http_absolute_uri(url) http_absolute_uri_ex((url), strlen(url), NULL, 0, NULL, 0, 0) +#define http_absolute_uri_ex(url, url_len, proto, proto_len, host, host_len, port) _http_absolute_uri_ex((url), (url_len), (proto), (proto_len), (host), (host_len), (port) TSRMLS_CC) +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); #define http_negotiate_language(supported, def) _http_negotiate_q("HTTP_ACCEPT_LANGUAGE", (supported), (def) TSRMLS_CC) #define http_negotiate_charset(supported, def) _http_negotiate_q("HTTP_ACCEPT_CHARSET", (supported), (def) TSRMLS_CC) -- 2.30.2