remodel file adding to accept paths, streams and plain data as upload files
[m6w6/ext-http] / php_http_url.c
index 0bfd6576e70f63a80b2a40835460bcbcb367262f..4a5d20d65d314ffbd1d8319ba7be367a11dd3e2b 100644 (file)
@@ -6,16 +6,11 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id: http_url_api.c 292841 2009-12-31 08:48:57Z mike $ */
-
-#include "php_http.h"
-
-#include <main/SAPI.h>
-#include <ext/standard/php_string.h>
+#include "php_http_api.h"
 
 static inline char *localhostname(void)
 {
@@ -248,7 +243,7 @@ PHP_HTTP_API void php_http_url(int flags, const php_url *old_url, const php_url
                                                /* .. at the end */
                                                ptr[1] = '\0';
                                        }
-                                       /* fallthrough */
+                                       /* no break */
                                
                                default:
                                        /* something else */
@@ -325,33 +320,30 @@ PHP_HTTP_API void php_http_url(int flags, const php_url *old_url, const php_url
        }
 }
 
-PHP_HTTP_API STATUS php_http_url_encode_hash(HashTable *hash, zend_bool override_argsep,       char *pre_encoded_data, size_t pre_encoded_len, char **encoded_data, size_t *encoded_len TSRMLS_DC)
+PHP_HTTP_API STATUS php_http_url_encode_hash(HashTable *hash, const char *pre_encoded_str, size_t pre_encoded_len, char **encoded_str, size_t *encoded_len TSRMLS_DC)
 {
-       char *arg_sep;
+       const char *arg_sep_str;
        size_t arg_sep_len;
        php_http_buffer_t *qstr = php_http_buffer_new();
 
-       if (override_argsep || !(arg_sep_len = strlen(arg_sep = INI_STR("arg_separator.output")))) {
-               arg_sep = PHP_HTTP_URL_ARGSEP;
-               arg_sep_len = lenof(PHP_HTTP_URL_ARGSEP);
-       }
+       php_http_url_argsep(&arg_sep_str, &arg_sep_len TSRMLS_CC);
 
-       if (pre_encoded_len && pre_encoded_data) {
-               php_http_buffer_append(qstr, pre_encoded_data, pre_encoded_len);
+       if (pre_encoded_len && pre_encoded_str) {
+               php_http_buffer_append(qstr, pre_encoded_str, pre_encoded_len);
        }
 
-       if (SUCCESS != php_http_url_encode_hash_recursive(hash, qstr, arg_sep, arg_sep_len, NULL, 0)) {
+       if (SUCCESS != php_http_url_encode_hash_ex(hash, qstr, arg_sep_str, arg_sep_len, ZEND_STRL("="), NULL, 0 TSRMLS_CC)) {
                php_http_buffer_free(&qstr);
                return FAILURE;
        }
 
-       php_http_buffer_data(qstr, encoded_data, encoded_len);
+       php_http_buffer_data(qstr, encoded_str, encoded_len);
        php_http_buffer_free(&qstr);
 
        return SUCCESS;
 }
 
-PHP_HTTP_API STATUS php_http_url_encode_hash_recursive(HashTable *ht, php_http_buffer_t *str, const char *arg_sep, size_t arg_sep_len, const char *prefix, size_t prefix_len TSRMLS_DC)
+PHP_HTTP_API STATUS php_http_url_encode_hash_ex(HashTable *ht, php_http_buffer_t *str, const char *arg_sep_str, size_t arg_sep_len, const char *val_sep_str, size_t val_sep_len, const char *prefix_str, size_t prefix_len TSRMLS_DC)
 {
        php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
        zval **data = NULL;
@@ -390,15 +382,15 @@ PHP_HTTP_API STATUS php_http_url_encode_hash_recursive(HashTable *ht, php_http_b
                
                {
                        php_http_buffer_init(&new_prefix);
-                       if (prefix && prefix_len) {
-                               php_http_buffer_append(&new_prefix, prefix, prefix_len);
+                       if (prefix_str && prefix_len) {
+                               php_http_buffer_append(&new_prefix, prefix_str, prefix_len);
                                php_http_buffer_appends(&new_prefix, "%5B");
                        }
                        
                        php_http_buffer_append(&new_prefix, encoded_key, encoded_len);
                        efree(encoded_key);
                        
-                       if (prefix && prefix_len) {
+                       if (prefix_str && prefix_len) {
                                php_http_buffer_appends(&new_prefix, "%5D");
                        }
                        php_http_buffer_fix(&new_prefix);
@@ -407,7 +399,7 @@ PHP_HTTP_API STATUS php_http_url_encode_hash_recursive(HashTable *ht, php_http_b
                if (Z_TYPE_PP(data) == IS_ARRAY || Z_TYPE_PP(data) == IS_OBJECT) {
                        STATUS status;
                        ++ht->nApplyCount;
-                       status = php_http_url_encode_hash_recursive(HASH_OF(*data), str, arg_sep, arg_sep_len, PHP_HTTP_BUFFER_VAL(&new_prefix), PHP_HTTP_BUFFER_LEN(&new_prefix) TSRMLS_CC);
+                       status = php_http_url_encode_hash_ex(HASH_OF(*data), str, arg_sep_str, arg_sep_len, val_sep_str, val_sep_len, PHP_HTTP_BUFFER_VAL(&new_prefix), PHP_HTTP_BUFFER_LEN(&new_prefix) TSRMLS_CC);
                        --ht->nApplyCount;
                        if (SUCCESS != status) {
                                php_http_buffer_dtor(&new_prefix);
@@ -418,10 +410,10 @@ PHP_HTTP_API STATUS php_http_url_encode_hash_recursive(HashTable *ht, php_http_b
                        zval *val = php_http_ztyp(IS_STRING, *data);
                        
                        if (PHP_HTTP_BUFFER_LEN(str)) {
-                               php_http_buffer_append(str, arg_sep, arg_sep_len);
+                               php_http_buffer_append(str, arg_sep_str, arg_sep_len);
                        }
                        php_http_buffer_append(str, PHP_HTTP_BUFFER_VAL(&new_prefix), PHP_HTTP_BUFFER_LEN(&new_prefix));
-                       php_http_buffer_appends(str, "=");
+                       php_http_buffer_append(str, val_sep_str, val_sep_len);
                        
                        if (Z_STRLEN_P(val) && Z_STRVAL_P(val)) {
                                char *encoded_val;
@@ -450,9 +442,15 @@ PHP_HTTP_BEGIN_ARGS(__construct, 0)
 PHP_HTTP_END_ARGS;
 PHP_HTTP_EMPTY_ARGS(toString);
 
+PHP_HTTP_BEGIN_ARGS(mod, 1)
+       PHP_HTTP_ARG_VAL(more_url_parts, 0)
+       PHP_HTTP_ARG_VAL(flags, 0)
+PHP_HTTP_END_ARGS;
+
 zend_class_entry *php_http_url_class_entry;
 zend_function_entry php_http_url_method_entry[] = {
        PHP_HTTP_URL_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+       PHP_HTTP_URL_ME(mod, ZEND_ACC_PUBLIC)
        PHP_HTTP_URL_ME(toString, ZEND_ACC_PUBLIC)
        ZEND_MALIAS(HttpUrl, __toString, toString, PHP_HTTP_ARGS(HttpUrl, toString), ZEND_ACC_PUBLIC)
        EMPTY_FUNCTION_ENTRY
@@ -523,6 +521,48 @@ PHP_METHOD(HttpUrl, __construct)
        } end_error_handling();
 }
 
+PHP_METHOD(HttpUrl, mod)
+{
+       zval *new_url = NULL;
+       long flags = PHP_HTTP_URL_JOIN_PATH | PHP_HTTP_URL_JOIN_QUERY;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!|l", &new_url, &flags)) {
+               php_url *res_purl, *new_purl = NULL, *old_purl = NULL;
+
+               if (new_url) {
+                       switch (Z_TYPE_P(new_url)) {
+                               case IS_OBJECT:
+                               case IS_ARRAY:
+                                       new_purl = php_http_url_from_struct(NULL, HASH_OF(new_url) TSRMLS_CC);
+                                       break;
+                               default: {
+                                       zval *cpy = php_http_ztyp(IS_STRING, new_url);
+
+                                       new_purl = php_url_parse(Z_STRVAL_P(new_url));
+                                       zval_ptr_dtor(&cpy);
+                                       break;
+                               }
+                       }
+                       if (!new_purl) {
+                               return;
+                       }
+               }
+
+               if ((old_purl = php_http_url_from_struct(NULL, HASH_OF(getThis()) TSRMLS_CC))) {
+                       php_http_url(flags, old_purl, new_purl, &res_purl, NULL, NULL TSRMLS_CC);
+
+                       Z_OBJVAL_P(return_value) = zend_objects_clone_obj(getThis() TSRMLS_CC);
+                       php_http_url_to_struct(res_purl, return_value TSRMLS_CC);
+
+                       php_url_free(res_purl);
+                       php_url_free(old_purl);
+               }
+               if (new_purl) {
+                       php_url_free(new_purl);
+               }
+       }
+}
+
 PHP_METHOD(HttpUrl, toString)
 {
        if (SUCCESS == zend_parse_parameters_none()) {