From d046681a7b929ef813849cfe38fbceb333f0b0eb Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 27 Jan 2006 10:17:13 +0000 Subject: [PATCH] - add flag parameter to http_build_url(); slightly breaks parameter order --- http.c | 3 +- http_functions.c | 9 ++-- http_message_api.c | 2 +- http_message_object.c | 2 +- http_url_api.c | 102 ++++++++++++++++++++++++++++++++++++----- http_util_object.c | 8 ++++ php_http_std_defs.h | 7 +-- php_http_url_api.h | 17 ++++++- php_http_util_object.h | 1 + 9 files changed, 128 insertions(+), 23 deletions(-) diff --git a/http.c b/http.c index 768277c..4c285ef 100644 --- a/http.c +++ b/http.c @@ -65,7 +65,7 @@ ZEND_GET_MODULE(http) zend_function_entry http_functions[] = { PHP_FE(http_test, NULL) PHP_FE(http_date, NULL) - PHP_FE(http_build_url, http_arg_pass_ref_3) + PHP_FE(http_build_url, http_arg_pass_ref_4) PHP_FE(http_negotiate_language, http_arg_pass_ref_2) PHP_FE(http_negotiate_charset, http_arg_pass_ref_2) PHP_FE(http_negotiate_content_type, http_arg_pass_ref_2) @@ -236,6 +236,7 @@ PHP_MINIT_FUNCTION(http) if ( (SUCCESS != PHP_MINIT_CALL(http_support)) || (SUCCESS != PHP_MINIT_CALL(http_send)) || + (SUCCESS != PHP_MINIT_CALL(http_url)) || #ifdef HTTP_HAVE_CURL (SUCCESS != PHP_MINIT_CALL(http_request)) || #endif /* HTTP_HAVE_CURL */ diff --git a/http_functions.c b/http_functions.c index cdddb81..341cec2 100644 --- a/http_functions.c +++ b/http_functions.c @@ -61,7 +61,7 @@ PHP_FUNCTION(http_date) } /* }}} */ -/* {{{ proto string http_build_url(mixed url[, mixed parts[, array &new_url]]) +/* {{{ proto string http_build_url(mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE[, array &new_url]]]) * * Returns the new URL as string on success or FALSE on failure. */ @@ -69,10 +69,11 @@ PHP_FUNCTION(http_build_url) { char *url_str = NULL; size_t url_len = 0; + long flags = HTTP_URL_REPLACE; zval *z_old_url = NULL, *z_new_url = NULL, *z_composed_url = NULL; php_url *old_url = NULL, *new_url = NULL, *composed_url = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/|z/z", &z_old_url, &z_new_url, &z_composed_url) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/|z/lz", &z_old_url, &z_new_url, &flags, &z_composed_url) != SUCCESS) { RETURN_FALSE; } @@ -102,7 +103,7 @@ PHP_FUNCTION(http_build_url) } if (z_composed_url) { - http_build_url(old_url, new_url, &composed_url, &url_str, &url_len); + http_build_url(flags, old_url, new_url, &composed_url, &url_str, &url_len); zval_dtor(z_composed_url); array_init(z_composed_url); @@ -132,7 +133,7 @@ PHP_FUNCTION(http_build_url) } php_url_free(composed_url); } else { - http_build_url(old_url, new_url, NULL, &url_str, &url_len); + http_build_url(flags, old_url, new_url, NULL, &url_str, &url_len); } if (new_url) { diff --git a/http_message_api.c b/http_message_api.c index baeedcc..0b5908e 100644 --- a/http_message_api.c +++ b/http_message_api.c @@ -491,7 +491,7 @@ PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC) parts.host = estrndup(Z_STRVAL_PP(zhost), Z_STRLEN_PP(zhost)); } - http_build_url(url, &parts, NULL, &uri, NULL); + http_build_url(HTTP_URL_REPLACE, url, &parts, NULL, &uri, NULL); php_url_free(url); efree(parts.host); } else { diff --git a/http_message_object.c b/http_message_object.c index 475daf1..ae4254a 100644 --- a/http_message_object.c +++ b/http_message_object.c @@ -1085,7 +1085,7 @@ PHP_METHOD(HttpMessage, toMessageTypeObject) memset(&hurl, 0, sizeof(php_url)); hurl.host = host ? Z_STRVAL_P(host) : NULL; - http_build_url(purl, &hurl, NULL, &url, NULL); + http_build_url(HTTP_URL_REPLACE, purl, &hurl, NULL, &url, NULL); php_url_free(purl); add_assoc_string(array, "url", url, 0); diff --git a/http_url_api.c b/http_url_api.c index db4ca96..dff4cae 100644 --- a/http_url_api.c +++ b/http_url_api.c @@ -23,6 +23,21 @@ #include "php_http_api.h" #include "php_http_url_api.h" +PHP_MINIT_FUNCTION(http_url) +{ + HTTP_LONG_CONSTANT("HTTP_URL_REPLACE", HTTP_URL_REPLACE); + HTTP_LONG_CONSTANT("HTTP_URL_JOIN_PATH", HTTP_URL_JOIN_PATH); + HTTP_LONG_CONSTANT("HTTP_URL_JOIN_QUERY", HTTP_URL_JOIN_QUERY); + HTTP_LONG_CONSTANT("HTTP_URL_STRIP_USER", HTTP_URL_STRIP_USER); + HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PASS", HTTP_URL_STRIP_PASS); + HTTP_LONG_CONSTANT("HTTP_URL_STRIP_AUTH", HTTP_URL_STRIP_AUTH); + HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PORT", HTTP_URL_STRIP_PORT); + HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PATH", HTTP_URL_STRIP_PATH); + HTTP_LONG_CONSTANT("HTTP_URL_STRIP_QUERY", HTTP_URL_STRIP_QUERY); + HTTP_LONG_CONSTANT("HTTP_URL_STRIP_FRAGMENT", HTTP_URL_STRIP_FRAGMENT); + return SUCCESS; +} + PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC) { char *abs = estrdup(url); @@ -31,7 +46,7 @@ PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC) STR_SET(abs, NULL); if (purl) { - http_build_url(purl, NULL, NULL, &abs, NULL); + http_build_url(0, purl, NULL, NULL, &abs, NULL); php_url_free(purl); } else { http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", url); @@ -40,24 +55,61 @@ PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC) return abs; } -/* {{{ void http_build_url(const php_url *, const php_url *, php_url **, char **, size_t *) */ -PHP_HTTP_API void _http_build_url(const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC) +/* {{{ void http_build_url(int flags, const php_url *, const php_url *, php_url **, char **, size_t *) */ +PHP_HTTP_API void _http_build_url(int flags, const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC) { #ifdef HTTP_HAVE_NETDB struct servent *se; #endif - php_url *url = emalloc(sizeof(php_url)); + php_url *url = ecalloc(1, sizeof(php_url)); +#define __URLSET(u,n) \ + ((u)&&(u)->n) #define __URLCPY(n) \ - url->n = (new_url&&new_url->n) ? estrdup(new_url->n) : ((old_url&&old_url->n) ? estrdup(old_url->n) : NULL) - url->port = (new_url&&new_url->port) ? new_url->port : ((old_url) ? old_url->port : 0); + url->n = __URLSET(new_url,n) ? estrdup(new_url->n) : (__URLSET(old_url,n) ? estrdup(old_url->n) : NULL) + + if (!(flags & HTTP_URL_STRIP_PORT)) { + url->port = (new_url&&new_url->port) ? new_url->port : ((old_url) ? old_url->port : 0); + } + if ((!(flags & HTTP_URL_STRIP_AUTH)) && (!(flags & HTTP_URL_STRIP_USER))) { + __URLCPY(user); + } + if ((!(flags & HTTP_URL_STRIP_AUTH)) && (!(flags & HTTP_URL_STRIP_PASS))) { + __URLCPY(pass); + } + __URLCPY(scheme); - __URLCPY(user); - __URLCPY(pass); __URLCPY(host); - __URLCPY(path); - __URLCPY(query); - __URLCPY(fragment); + + if (!(flags & HTTP_URL_STRIP_PATH)) { + if ((flags & HTTP_URL_JOIN_PATH) && __URLSET(old_url, path) && __URLSET(new_url, path) && *new_url->path != '/') { + size_t old_path_len = strlen(old_url->path), new_path_len = strlen(new_url->path); + + url->path = ecalloc(1, old_path_len + new_path_len + 1 + 1); + + strcat(url->path, old_url->path); + if (url->path[old_path_len - 1] != '/') { + php_dirname(url->path, old_path_len); + strcat(url->path, "/"); + } + strcat(url->path, new_url->path); + } else { + __URLCPY(path); + } + } + if (!(flags & HTTP_URL_STRIP_QUERY)) { + if ((flags & HTTP_URL_JOIN_QUERY) && __URLSET(new_url, query) && __URLSET(old_url, query)) { + url->query = ecalloc(1, strlen(new_url->query) + strlen(old_url->query) + 1 + 1); + strcat(url->query, old_url->query); + strcat(url->query, "&"); + strcat(url->query, new_url->query); + } else { + __URLCPY(query); + } + } + if (!(flags & HTTP_URL_STRIP_FRAGMENT)) { + __URLCPY(fragment); + } if (!url->scheme) { switch (url->port) @@ -131,6 +183,34 @@ PHP_HTTP_API void _http_build_url(const php_url *old_url, const php_url *new_url STR_SET(url->path, uri); } } + if (url->path) { + char *ptr, *end = url->path + strlen(url->path) + 1; + + for (ptr = strstr(url->path, "/."); ptr; ptr = strstr(ptr, "/.")) { + switch (ptr[2]) + { + case '\0': + ptr[1] = '\0'; + break; + + case '/': + memmove(&ptr[1], &ptr[3], end - &ptr[3]); + break; + + case '.': + if (ptr[3] == '/') { + char *pos = &ptr[4]; + while (ptr != url->path) { + if (*--ptr == '/') { + break; + } + } + memmove(&ptr[1], pos, end - pos); + } + break; + } + } + } if (url->port) { if ( ((url->port == 80) && !strcmp(url->scheme, "http")) diff --git a/http_util_object.c b/http_util_object.c index 6a45635..d89cb99 100644 --- a/http_util_object.c +++ b/http_util_object.c @@ -16,6 +16,8 @@ #ifdef ZEND_ENGINE_2 +#include "ext/standard/php_http.h" + #include "php_http_util_object.h" #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpUtil, method, 0, req_args) @@ -27,9 +29,14 @@ HTTP_BEGIN_ARGS(date, 0) HTTP_ARG_VAL(timestamp, 0) HTTP_END_ARGS; +HTTP_BEGIN_ARGS(buildQuery, 1) + HTTP_ARG_VAL(query, 0) +HTTP_END_ARGS; + HTTP_BEGIN_ARGS(buildUrl, 1) HTTP_ARG_VAL(url, 0) HTTP_ARG_VAL(parts, 0) + HTTP_ARG_VAL(flags, 0) HTTP_ARG_VAL(composed, 1) HTTP_END_ARGS; @@ -97,6 +104,7 @@ zend_class_entry *http_util_object_ce; zend_function_entry http_util_object_fe[] = { HTTP_UTIL_ALIAS(date, http_date) HTTP_UTIL_ALIAS(buildUrl, http_build_url) + HTTP_UTIL_ALIAS(buildQuery, http_build_query) HTTP_UTIL_ALIAS(negotiateLanguage, http_negotiate_language) HTTP_UTIL_ALIAS(negotiateCharset, http_negotiate_charset) HTTP_UTIL_ALIAS(negotiateContentType, http_negotiate_content_type) diff --git a/php_http_std_defs.h b/php_http_std_defs.h index 78a9ae2..51b9a51 100644 --- a/php_http_std_defs.h +++ b/php_http_std_defs.h @@ -377,15 +377,16 @@ typedef int STATUS; ZEND_END_ARG_INFO(); \ \ static \ - ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_3, 0) \ + ZEND_BEGIN_ARG_INFO(http_arg_pass_ref_4, 0) \ + ZEND_ARG_PASS_INFO(0) \ ZEND_ARG_PASS_INFO(0) \ ZEND_ARG_PASS_INFO(0) \ ZEND_ARG_PASS_INFO(1) \ ZEND_END_ARG_INFO(); # else # define HTTP_DECLARE_ARG_PASS_INFO() \ - static unsigned char http_arg_pass_ref_2[] = {2, BYREF_NONE, BYREF_FORCE};\ - static unsigned char http_arg_pass_ref_3[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE}; + static unsigned char http_arg_pass_ref_2[] = {2, BYREF_NONE, BYREF_FORCE}; \ + static unsigned char http_arg_pass_ref_4[] = {4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE}; # endif /* ZEND_ENGINE_2 */ #endif /* HTTP_HAVE_CURL */ diff --git a/php_http_url_api.h b/php_http_url_api.h index 48b2765..dc00899 100644 --- a/php_http_url_api.h +++ b/php_http_url_api.h @@ -17,11 +17,24 @@ #include "ext/standard/url.h" +extern PHP_MINIT_FUNCTION(http_url); + #define http_absolute_url(u) _http_absolute_url((u) TSRMLS_CC) PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC); -#define http_build_url(o, n, p, s, l) _http_build_url((o), (n), (p), (s), (l) TSRMLS_CC) -PHP_HTTP_API void _http_build_url(const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC); +#define HTTP_URL_REPLACE 0x000 +#define HTTP_URL_JOIN_PATH 0x001 +#define HTTP_URL_JOIN_QUERY 0x002 +#define HTTP_URL_STRIP_USER 0x004 +#define HTTP_URL_STRIP_PASS 0x008 +#define HTTP_URL_STRIP_AUTH 0x010 +#define HTTP_URL_STRIP_PORT 0x020 +#define HTTP_URL_STRIP_PATH 0x040 +#define HTTP_URL_STRIP_QUERY 0x080 +#define HTTP_URL_STRIP_FRAGMENT 0x100 + +#define http_build_url(f, o, n, p, s, l) _http_build_url((f), (o), (n), (p), (s), (l) TSRMLS_CC) +PHP_HTTP_API void _http_build_url(int flags, const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC); #define http_urlencode_hash(h, q) _http_urlencode_hash_ex((h), 1, NULL, 0, (q), NULL TSRMLS_CC) #define http_urlencode_hash_ex(h, o, p, pl, q, ql) _http_urlencode_hash_ex((h), (o), (p), (pl), (q), (ql) TSRMLS_CC) diff --git a/php_http_util_object.h b/php_http_util_object.h index 7d6c15f..4c263a2 100644 --- a/php_http_util_object.h +++ b/php_http_util_object.h @@ -23,6 +23,7 @@ extern PHP_MINIT_FUNCTION(http_util_object); PHP_METHOD(HttpUtil, date); PHP_METHOD(HttpUtil, buildUrl); +PHP_METHOD(HttpUtil, buildQuery); PHP_METHOD(HttpUtil, negotiateLanguage); PHP_METHOD(HttpUtil, negotiateCharset); PHP_METHOD(HttpUtil, negotiateContentType); -- 2.30.2