- add flag parameter to http_build_url(); slightly breaks parameter order
authorMichael Wallner <mike@php.net>
Fri, 27 Jan 2006 10:17:13 +0000 (10:17 +0000)
committerMichael Wallner <mike@php.net>
Fri, 27 Jan 2006 10:17:13 +0000 (10:17 +0000)
http.c
http_functions.c
http_message_api.c
http_message_object.c
http_url_api.c
http_util_object.c
php_http_std_defs.h
php_http_url_api.h
php_http_util_object.h

diff --git a/http.c b/http.c
index 768277ca00ab1f6bd7f56e05c5c2270f5e0069b3..4c285ef23c8872f2f9c3af4650e11b7a2387a072 100644 (file)
--- 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 */
index cdddb81143d9ba91bb68227da1ac58674a510f2d..341cec245070baf8d7af3dc89664bf914a30456a 100644 (file)
@@ -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) {
index baeedccef2ece0531fb90f8dfe2b897af0ee882d..0b5908ecab8d5ea4598241fe440ad62929457af5 100644 (file)
@@ -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 {
index 475daf13bb1dc300910ba64d3a2ecbd518e72061..ae4254a2621810a73074912f3c62d0abde9ff237 100644 (file)
@@ -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);
                                
index db4ca96fd7abe76a47fdca84c9473eddcaa44845..dff4caec0a78d6e73483ea9669d3c0f3b458183c 100644 (file)
 #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"))
index 6a45635b6c3b9d60d26d3b88b62de91e33c97a9b..d89cb996dfbf144142b40427808576f8abf860e4 100644 (file)
@@ -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)
index 78a9ae2e2ef2dc900be36d9ca7a9dead5b8a74af..51b9a5165d5339b86e6f76f0939ab87450dddf84 100644 (file)
@@ -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 */
 
index 48b2765bd86c222502bf6983d8bda6cfb0028f24..dc0089909674861d236a78e59d42e45c1f74aa1d 100644 (file)
 
 #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)
index 7d6c15f58c660036da53fdfcd6290c568661ba44..4c263a2e7a9aafa571e1581ec26667f61ca190ab 100644 (file)
@@ -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);