- 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 768277c..4c285ef 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 cdddb81..341cec2 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 baeedcc..0b5908e 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 475daf1..ae4254a 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 db4ca96..dff4cae 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 6a45635..d89cb99 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 78a9ae2..51b9a51 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 48b2765..dc00899 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 7d6c15f..4c263a2 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);