fix some compiler warnings
[m6w6/ext-http] / php_http_request.c
index d3ac782c793202830bdd8c7e7caec7c0aae08054..42416890aaa6239e372b764a75fa8b17b9845493 100644 (file)
@@ -6,18 +6,13 @@
     | 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: php_http_request_api.c 298591 2010-04-26 11:46:55Z mike $ */
-
-#include "php_http.h"
+#include "php_http_api.h"
 
 #include <ext/spl/spl_observer.h>
-#include <ext/spl/spl_iterators.h>
-#include <Zend/zend_interfaces.h>
-
 
 PHP_HTTP_API php_http_request_t *php_http_request_init(php_http_request_t *h, php_http_request_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC)
 {
@@ -29,7 +24,7 @@ PHP_HTTP_API php_http_request_t *php_http_request_init(php_http_request_t *h, ph
        memset(h, 0, sizeof(*h));
 
        h->ops = ops;
-       h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, NULL, NULL);
+       h->rf = rf ? rf : php_http_resource_factory_init(NULL, h->ops->rsrc, h, NULL);
        h->buffer = php_http_buffer_init(NULL);
        h->parser = php_http_message_parser_init(NULL TSRMLS_CC);
        h->message = php_http_message_init(NULL, 0 TSRMLS_CC);
@@ -41,7 +36,7 @@ PHP_HTTP_API php_http_request_t *php_http_request_init(php_http_request_t *h, ph
                        php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Could not initialize request");
                        if (free_h) {
                                h->ops->dtor = NULL;
-                               php_http_request_free(&h);
+                               php_http_request_free(&free_h);
                        }
                }
        }
@@ -57,10 +52,6 @@ PHP_HTTP_API void php_http_request_dtor(php_http_request_t *h)
 
        php_http_resource_factory_free(&h->rf);
 
-       if (h->persistent_handle_id) {
-               zval_ptr_dtor(&h->persistent_handle_id);
-       }
-
        php_http_message_parser_free(&h->parser);
        php_http_message_free(&h->message);
        php_http_buffer_free(&h->buffer);
@@ -77,10 +68,30 @@ PHP_HTTP_API void php_http_request_free(php_http_request_t **h)
 
 PHP_HTTP_API php_http_request_t *php_http_request_copy(php_http_request_t *from, php_http_request_t *to)
 {
-       if (from->ops->copy) {
-               return from->ops->copy(from, to);
+       if (!from->ops->copy) {
+               return NULL;
+       } else {
+               TSRMLS_FETCH_FROM_CTX(from->ts);
+
+               if (!to) {
+                       to = ecalloc(1, sizeof(*to));
+               }
+
+               to->ops = from->ops;
+               if (from->rf) {
+                       php_http_resource_factory_addref(from->rf);
+                       to->rf = from->rf;
+               } else {
+                       to->rf = php_http_resource_factory_init(NULL, to->ops->rsrc, to, NULL);
+               }
+               to->buffer = php_http_buffer_init(NULL);
+               to->parser = php_http_message_parser_init(NULL TSRMLS_CC);
+               to->message = php_http_message_init(NULL, 0 TSRMLS_CC);
+
+               TSRMLS_SET_CTX(to->ts);
+
+               return to->ops->copy(from, to);
        }
-       return NULL;
 }
 
 PHP_HTTP_API STATUS php_http_request_exec(php_http_request_t *h, php_http_request_method_t meth, const char *url, php_http_message_body_t *body)
@@ -213,9 +224,7 @@ PHP_HTTP_BEGIN_ARGS(setMessageClass, 1)
 PHP_HTTP_END_ARGS;
 
 PHP_HTTP_EMPTY_ARGS(getResponseMessage);
-PHP_HTTP_EMPTY_ARGS(getRawResponseMessage);
 PHP_HTTP_EMPTY_ARGS(getRequestMessage);
-PHP_HTTP_EMPTY_ARGS(getRawRequestMessage);
 PHP_HTTP_EMPTY_ARGS(getHistory);
 PHP_HTTP_EMPTY_ARGS(clearHistory);
 PHP_HTTP_EMPTY_ARGS(send);
@@ -422,7 +431,7 @@ STATUS php_http_request_object_requesthandler(php_http_request_object_t *obj, zv
        }
 
        if (url) {
-               php_url *tmp, qdu = {0};
+               php_url *tmp, qdu = {NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL};
                zval *zurl = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("url"), 0 TSRMLS_CC);
                zval *zqdata = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("queryData"), 0 TSRMLS_CC);
 
@@ -438,6 +447,9 @@ STATUS php_http_request_object_requesthandler(php_http_request_object_t *obj, zv
 
                if (Z_TYPE_P(zbody) == IS_OBJECT) {
                        *body = ((php_http_message_body_object_t *)zend_object_store_get_object(zbody TSRMLS_CC))->body;
+                       if (*body) {
+                               php_stream_rewind(php_http_message_body_stream(*body));
+                       }
                }
        }
 
@@ -497,7 +509,7 @@ STATUS php_http_request_object_responsehandler(php_http_request_object_t *obj, z
                        ZVAL_OBJVAL(new_hist, ov, 0);
 
                        if (Z_TYPE_P(old_hist) == IS_OBJECT) {
-                               php_http_message_object_prepend(new_hist, old_hist, 0 TSRMLS_CC);
+                               php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC);
                        }
 
                        zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), new_hist TSRMLS_CC);
@@ -550,8 +562,14 @@ STATUS php_http_request_object_responsehandler(php_http_request_object_t *obj, z
 
 static int apply_pretty_key(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
 {
+       zval **zpp = pDest, *arr = va_arg(args, zval *);
+
        if (hash_key->arKey && hash_key->nKeyLength > 1) {
-               hash_key->h = zend_hash_func(php_http_pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength);
+               char *tmp = php_http_pretty_key(estrndup(hash_key->arKey, hash_key->nKeyLength - 1), hash_key->nKeyLength - 1, 1, 0);
+
+               Z_ADDREF_PP(zpp);
+               add_assoc_zval_ex(arr, tmp, hash_key->nKeyLength, *zpp);
+               efree(tmp);
        }
        return ZEND_HASH_APPLY_KEEP;
 }
@@ -568,7 +586,7 @@ static inline void php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PA
                        array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
                }
 
-               if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
+               if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
                        if (overwrite) {
                                zend_hash_clean(Z_ARRVAL_PP(entry));
                        }
@@ -581,9 +599,15 @@ static inline void php_http_request_object_set_options_subr(INTERNAL_FUNCTION_PA
                        }
                } else if (opts) {
                        if (prettify_keys) {
-                               zend_hash_apply_with_arguments(Z_ARRVAL_P(opts) TSRMLS_CC, apply_pretty_key, 0, NULL);
+                               zval *tmp;
+
+                               MAKE_STD_ZVAL(tmp);
+                               array_init_size(tmp, zend_hash_num_elements(Z_ARRVAL_P(opts)));
+                               zend_hash_apply_with_arguments(Z_ARRVAL_P(opts) TSRMLS_CC, apply_pretty_key, 1, tmp);
+                               opts = tmp;
+                       } else {
+                               Z_ADDREF_P(opts);
                        }
-                       Z_ADDREF_P(opts);
                        add_assoc_zval_ex(new_opts, key, len, opts);
                }
                zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
@@ -602,7 +626,7 @@ static inline void php_http_request_object_get_options_subr(INTERNAL_FUNCTION_PA
                array_init(return_value);
 
                if (    (Z_TYPE_P(opts) == IS_ARRAY) &&
-                               (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
+                               (SUCCESS == zend_symtable_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
                        convert_to_array(*options);
                        array_copy(Z_ARRVAL_PP(options), Z_ARRVAL_P(return_value));
                }
@@ -719,7 +743,7 @@ PHP_METHOD(HttpRequest, getTransferInfo)
                }
 
                if (info_len && info_name) {
-                       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), php_http_pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) {
+                       if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(info), php_http_pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) {
                                RETVAL_ZVAL(*infop, 1, 0);
                        } else {
                                php_http_error(HE_NOTICE, PHP_HTTP_E_INVALID_PARAM, "Could not find transfer info named %s", info_name);
@@ -791,7 +815,7 @@ PHP_METHOD(HttpRequest, setOptions)
                                } else if (Z_TYPE_PP(opt) == IS_NULL) {
                                        old_opts = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
                                        if (Z_TYPE_P(old_opts) == IS_ARRAY) {
-                                               zend_hash_del(Z_ARRVAL_P(old_opts), key.str, key.len);
+                                               zend_symtable_del(Z_ARRVAL_P(old_opts), key.str, key.len);
                                        }
                                } else {
                                        Z_ADDREF_P(*opt);
@@ -1121,7 +1145,7 @@ PHP_METHOD(HttpRequest, getResponseCookies)
                                                FOREACH_VAL(pos2, *header, single_header) {
                                                        zval *data = php_http_ztyp(IS_STRING, *single_header);
 
-                                                       if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), flags, allowed_extras TSRMLS_CC))) {
+                                                       if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) {
                                                                zval *cookie;
 
                                                                MAKE_STD_ZVAL(cookie);
@@ -1132,7 +1156,7 @@ PHP_METHOD(HttpRequest, getResponseCookies)
                                                }
                                        } else {
                                                zval *data = php_http_ztyp(IS_STRING, *header);
-                                               if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), flags, allowed_extras TSRMLS_CC))) {
+                                               if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) {
                                                        zval *cookie;
 
                                                        MAKE_STD_ZVAL(cookie);
@@ -1159,12 +1183,17 @@ PHP_METHOD(HttpRequest, getResponseCookies)
 
 PHP_METHOD(HttpRequest, getResponseBody)
 {
-       if (SUCCESS == zend_parse_parameters_none()) {
-               zval *message = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC);
+       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+               if (SUCCESS == zend_parse_parameters_none()) {
+                       zval *message = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), 0 TSRMLS_CC);
 
-               RETURN_OBJVAL(((php_http_message_object_t *)zend_object_store_get_object(message TSRMLS_CC))->body, 1);
-       }
-       RETURN_FALSE;
+                       if (Z_TYPE_P(message) == IS_OBJECT) {
+                               RETURN_OBJVAL(((php_http_message_object_t *)zend_object_store_get_object(message TSRMLS_CC))->body, 1);
+                       } else {
+                               php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "HttpRequest does not contain a response message");
+                       }
+               }
+       } end_error_handling();
 }
 
 PHP_METHOD(HttpRequest, getResponseCode)