- prepare release 0.21.0
[m6w6/ext-http] / http_message_object.c
index e69010c9b694407602d524bf7ab39aff51af5e2c..b9fdc364b7a01768f06f13ec993a24716dc6c611 100644 (file)
@@ -6,7 +6,7 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2005, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2006, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
-#include "php.h"
+
+#include "php_http.h"
 
 #ifdef ZEND_ENGINE_2
 
-#include "php_http.h"
 #include "php_http_api.h"
-#include "php_http_std_defs.h"
+#include "php_http_message_api.h"
 #include "php_http_message_object.h"
 #include "php_http_exception_object.h"
 
-#include "phpstr/phpstr.h"
-#include "missing.h"
-
 #ifndef WONKY
 #      include "zend_interfaces.h"
 #      if defined(HAVE_SPL)
-#              include "ext/spl/spl_array.h"
+/* SPL doesn't install its headers */
+extern PHPAPI zend_class_entry *spl_ce_Countable;
 #      endif
 #endif
 
-ZEND_EXTERN_MODULE_GLOBALS(http);
-
 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args)     HTTP_BEGIN_ARGS_EX(HttpMessage, method, ret_ref, req_args)
 #define HTTP_EMPTY_ARGS(method, ret_ref)                       HTTP_EMPTY_ARGS_EX(HttpMessage, method, ret_ref)
 #define HTTP_MESSAGE_ME(method, visibility)                    PHP_ME(HttpMessage, method, HTTP_ARGS(HttpMessage, method), visibility)
@@ -80,9 +76,9 @@ HTTP_BEGIN_ARGS(setRequestMethod, 0, 1)
        HTTP_ARG_VAL(request_method, 0)
 HTTP_END_ARGS;
 
-HTTP_EMPTY_ARGS(getRequestUri, 0);
-HTTP_BEGIN_ARGS(setRequestUri, 0, 1)
-       HTTP_ARG_VAL(uri, 0)
+HTTP_EMPTY_ARGS(getRequestUrl, 0);
+HTTP_BEGIN_ARGS(setRequestUrl, 0, 1)
+       HTTP_ARG_VAL(url, 0)
 HTTP_END_ARGS;
 
 HTTP_EMPTY_ARGS(getHttpVersion, 0);
@@ -90,7 +86,7 @@ HTTP_BEGIN_ARGS(setHttpVersion, 0, 1)
        HTTP_ARG_VAL(http_version, 0)
 HTTP_END_ARGS;
 
-HTTP_EMPTY_ARGS(getParentMessage, 1);
+HTTP_EMPTY_ARGS(getParentMessage, 0);
 HTTP_EMPTY_ARGS(send, 0);
 HTTP_BEGIN_ARGS(toString, 0, 0)
        HTTP_ARG_VAL(include_parent, 0)
@@ -126,8 +122,8 @@ zend_function_entry http_message_object_fe[] = {
        HTTP_MESSAGE_ME(setResponseCode, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getRequestMethod, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(setRequestMethod, ZEND_ACC_PUBLIC)
-       HTTP_MESSAGE_ME(getRequestUri, ZEND_ACC_PUBLIC)
-       HTTP_MESSAGE_ME(setRequestUri, ZEND_ACC_PUBLIC)
+       HTTP_MESSAGE_ME(getRequestUrl, ZEND_ACC_PUBLIC)
+       HTTP_MESSAGE_ME(setRequestUrl, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getHttpVersion, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC)
        HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC)
@@ -225,7 +221,7 @@ static inline void _http_message_object_declare_default_properties(TSRMLS_D)
        DCL_PROP(PROTECTED, long, type, HTTP_MSG_NONE);
        DCL_PROP(PROTECTED, string, body, "");
        DCL_PROP(PROTECTED, string, requestMethod, "");
-       DCL_PROP(PROTECTED, string, requestUri, "");
+       DCL_PROP(PROTECTED, string, requestUrl, "");
        DCL_PROP(PROTECTED, long, responseCode, 0);
        DCL_PROP_N(PROTECTED, httpVersion);
        DCL_PROP_N(PROTECTED, headers);
@@ -317,10 +313,10 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
                        }
                break;
 
-               case HTTP_MSG_PROPHASH_REQUEST_URI:
-               case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI:
-                       if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.URI) {
-                               RETVAL_STRING(msg->http.info.request.URI, 1);
+               case HTTP_MSG_PROPHASH_REQUEST_URL:
+               case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
+                       if (HTTP_MSG_TYPE(REQUEST, msg) && msg->http.info.request.url) {
+                               RETVAL_STRING(msg->http.info.request.url, 1);
                        } else {
                                RETVAL_NULL();
                        }
@@ -360,6 +356,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
 {
        getObjectEx(http_message_object, obj, object);
        http_message *msg = obj->message;
+       zval *cpy = NULL;
 #ifdef WONKY
        ulong h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
 #else
@@ -370,7 +367,9 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
                return;
        }
 #endif
-
+       
+       cpy = zval_copy(Z_TYPE_P(value), value);
+       
 #ifdef WONKY
        switch (h)
 #else
@@ -379,28 +378,28 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
        {
                case HTTP_MSG_PROPHASH_TYPE:
                case HTTP_MSG_CHILD_PROPHASH_TYPE:
-                       convert_to_long_ex(&value);
-                       http_message_set_type(msg, Z_LVAL_P(value));
+                       convert_to_long(cpy);
+                       http_message_set_type(msg, Z_LVAL_P(cpy));
                break;
 
                case HTTP_MSG_PROPHASH_HTTP_VERSION:
                case HTTP_MSG_CHILD_PROPHASH_HTTP_VERSION:
-                       convert_to_double_ex(&value);
-                       msg->http.version = Z_DVAL_P(value);
+                       convert_to_double(cpy);
+                       msg->http.version = Z_DVAL_P(cpy);
                break;
 
                case HTTP_MSG_PROPHASH_BODY:
                case HTTP_MSG_CHILD_PROPHASH_BODY:
-                       convert_to_string_ex(&value);
+                       convert_to_string(cpy);
                        phpstr_dtor(PHPSTR(msg));
-                       phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(value), Z_STRLEN_P(value));
+                       phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
                break;
 
                case HTTP_MSG_PROPHASH_HEADERS:
                case HTTP_MSG_CHILD_PROPHASH_HEADERS:
-                       convert_to_array_ex(&value);
+                       convert_to_array(cpy);
                        zend_hash_clean(&msg->hdrs);
-                       zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(value), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+                       zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
                break;
 
                case HTTP_MSG_PROPHASH_PARENT_MESSAGE:
@@ -419,32 +418,32 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
                case HTTP_MSG_PROPHASH_REQUEST_METHOD:
                case HTTP_MSG_CHILD_PROPHASH_REQUEST_METHOD:
                        if (HTTP_MSG_TYPE(REQUEST, msg)) {
-                               convert_to_string_ex(&value);
-                               STR_SET(msg->http.info.request.method, estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value)));
+                               convert_to_string(cpy);
+                               STR_SET(msg->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
                        }
                break;
 
-               case HTTP_MSG_PROPHASH_REQUEST_URI:
-               case HTTP_MSG_CHILD_PROPHASH_REQUEST_URI:
+               case HTTP_MSG_PROPHASH_REQUEST_URL:
+               case HTTP_MSG_CHILD_PROPHASH_REQUEST_URL:
                        if (HTTP_MSG_TYPE(REQUEST, msg)) {
-                               convert_to_string_ex(&value);
-                               STR_SET(msg->http.info.request.URI, estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value)));
+                               convert_to_string(cpy);
+                               STR_SET(msg->http.info.request.url, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
                        }
                break;
 
                case HTTP_MSG_PROPHASH_RESPONSE_CODE:
                case HTTP_MSG_CHILD_PROPHASH_RESPONSE_CODE:
                        if (HTTP_MSG_TYPE(RESPONSE, msg)) {
-                               convert_to_long_ex(&value);
-                               msg->http.info.response.code = Z_LVAL_P(value);
+                               convert_to_long(cpy);
+                               msg->http.info.response.code = Z_LVAL_P(cpy);
                        }
                break;
                
                case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
                case HTTP_MSG_CHILD_PROPHASH_RESPONSE_STATUS:
                        if (HTTP_MSG_TYPE(RESPONSE, msg)) {
-                               convert_to_string_ex(&value);
-                               STR_SET(msg->http.info.response.status, estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value)));
+                               convert_to_string(cpy);
+                               STR_SET(msg->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
                        }
                break;
                
@@ -454,6 +453,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
 #endif
                break;
        }
+       zval_free(&cpy);
 }
 
 static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
@@ -471,7 +471,7 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
                char *m_prop_name; \
                int m_prop_len; \
                zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
-               add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+4, val); \
+               add_assoc_ ##ptype## _ex(&array, m_prop_name, sizeof(name)+3, val); \
                efree(m_prop_name); \
        }
 #define ASSOC_STRING(array, name, val) ASSOC_STRINGL(array, name, val, strlen(val))
@@ -480,7 +480,7 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
                char *m_prop_name; \
                int m_prop_len; \
                zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 0); \
-               add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
+               add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+3, val, len, 1); \
                efree(m_prop_name); \
        }
 
@@ -493,14 +493,14 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
                        ASSOC_PROP(array, long, "responseCode", 0);
                        ASSOC_STRINGL(array, "responseStatus", "", 0);
                        ASSOC_STRING(array, "requestMethod", msg->http.info.request.method);
-                       ASSOC_STRING(array, "requestUri", msg->http.info.request.URI);
+                       ASSOC_STRING(array, "requestUrl", msg->http.info.request.url);
                break;
 
                case HTTP_MSG_RESPONSE:
                        ASSOC_PROP(array, long, "responseCode", msg->http.info.response.code);
                        ASSOC_STRING(array, "responseStatus", msg->http.info.response.status);
                        ASSOC_STRINGL(array, "requestMethod", "", 0);
-                       ASSOC_STRINGL(array, "requestUri", "", 0);
+                       ASSOC_STRINGL(array, "requestUrl", "", 0);
                break;
 
                case HTTP_MSG_NONE:
@@ -508,7 +508,7 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
                        ASSOC_PROP(array, long, "responseCode", 0);
                        ASSOC_STRINGL(array, "responseStatus", "", 0);
                        ASSOC_STRINGL(array, "requestMethod", "", 0);
-                       ASSOC_STRINGL(array, "requestUri", "", 0);
+                       ASSOC_STRINGL(array, "requestUrl", "", 0);
                break;
        }
 
@@ -546,7 +546,7 @@ PHP_METHOD(HttpMessage, __construct)
                http_message *msg = obj->message;
                
                http_message_dtor(msg);
-               if (obj->message = http_message_parse_ex(msg, message, length)) {
+               if ((obj->message = http_message_parse_ex(msg, message, length))) {
                        if (obj->message->parent) {
                                obj->parent = http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL);
                        }
@@ -582,7 +582,7 @@ PHP_METHOD(HttpMessage, fromString)
        
        SET_EH_THROW_HTTP();
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
-               if (msg = http_message_parse(string, length)) {
+               if ((msg = http_message_parse(string, length))) {
                        ZVAL_OBJVAL(return_value, http_message_object_new_ex(http_message_object_ce, msg, NULL));
                }
        }
@@ -835,35 +835,35 @@ PHP_METHOD(HttpMessage, setRequestMethod)
 }
 /* }}} */
 
-/* {{{ proto string HttpMessage::getRequestUri()
+/* {{{ proto string HttpMessage::getRequestUrl()
  *
- * Get the Request URI of the Message.
+ * Get the Request URL of the Message.
  * 
- * Returns the request uri as string on success, or FALSE if the message
+ * Returns the request url as string on success, or FALSE if the message
  * is not of type HttpMessage::TYPE_REQUEST. 
  */
-PHP_METHOD(HttpMessage, getRequestUri)
+PHP_METHOD(HttpMessage, getRequestUrl)
 {
        NO_ARGS;
 
        IF_RETVAL_USED {
                getObject(http_message_object, obj);
                HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
-               RETURN_STRING(obj->message->http.info.request.URI, 1);
+               RETURN_STRING(obj->message->http.info.request.url, 1);
        }
 }
 /* }}} */
 
-/* {{{ proto bool HttpMessage::setRequestUri(string URI)
+/* {{{ proto bool HttpMessage::setRequestUrl(string url)
  *
- * Set the Request URI of the HTTP Message.
+ * Set the Request URL of the HTTP Message.
  * 
- * Expects a string parameters containing the request uri.
+ * Expects a string parameters containing the request url.
  * 
  * Returns TRUE on success, or FALSE if the message is not of type
- * HttpMessage::TYPE_REQUEST or supplied URI was empty.
+ * HttpMessage::TYPE_REQUEST or supplied URL was empty.
  */
-PHP_METHOD(HttpMessage, setRequestUri)
+PHP_METHOD(HttpMessage, setRequestUrl)
 {
        char *URI;
        int URIlen;
@@ -874,11 +874,11 @@ PHP_METHOD(HttpMessage, setRequestUri)
        }
        HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
        if (URIlen < 1) {
-               http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
+               http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUrl to an empty string");
                RETURN_FALSE;
        }
 
-       STR_SET(obj->message->http.info.request.URI, estrndup(URI, URIlen));
+       STR_SET(obj->message->http.info.request.url, estrndup(URI, URIlen));
        RETURN_TRUE;
 }
 /* }}} */