* typo
[m6w6/ext-http] / http_message_object.c
index 08dc0dd61b98981615d2e0c96f4d8f73f18d2c43..e23f555861a1c6a70100d66e0cfb729372f0ba43 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "php.h"
 
 
 #include "php.h"
 
+#include "php_http.h"
 #include "php_http_std_defs.h"
 #include "php_http_message_object.h"
 
 #include "php_http_std_defs.h"
 #include "php_http_message_object.h"
 
@@ -39,7 +40,19 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC);
 zend_class_entry *http_message_object_ce;
 zend_function_entry http_message_object_fe[] = {
        PHP_ME(HttpMessage, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
 zend_class_entry *http_message_object_ce;
 zend_function_entry http_message_object_fe[] = {
        PHP_ME(HttpMessage, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
-       PHP_ME(HttpMessage, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
+       PHP_ME(HttpMessage, setRaw, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getBody, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getHeaders, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getType, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getResponseCode, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getRequestMethod, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getRequestUri, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, getHttpVersion, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessage, toString, NULL, ZEND_ACC_PUBLIC)
+
+       ZEND_MALIAS(HttpMessage, __toString, toString, NULL, ZEND_ACC_PUBLIC)
+
+       PHP_ME(HttpMessage, fromString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
        {NULL, NULL, NULL}
 };
 static zend_object_handlers http_message_object_handlers;
        {NULL, NULL, NULL}
 };
 static zend_object_handlers http_message_object_handlers;
@@ -48,19 +61,28 @@ void _http_message_object_init(INIT_FUNC_ARGS)
 {
        HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
 
 {
        HTTP_REGISTER_CLASS_EX(HttpMessage, http_message_object, NULL, 0);
 
+       HTTP_LONG_CONSTANT("HTTP_MSG_NONE", HTTP_MSG_NONE);
+       HTTP_LONG_CONSTANT("HTTP_MSG_REQUEST", HTTP_MSG_REQUEST);
+       HTTP_LONG_CONSTANT("HTTP_MSG_RESPONSE", HTTP_MSG_RESPONSE);
+
        http_message_object_handlers.read_property = http_message_object_read_prop;
        http_message_object_handlers.write_property = http_message_object_write_prop;
        http_message_object_handlers.get_properties = http_message_object_get_props;
 }
 
 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
        http_message_object_handlers.read_property = http_message_object_read_prop;
        http_message_object_handlers.write_property = http_message_object_write_prop;
        http_message_object_handlers.get_properties = http_message_object_get_props;
 }
 
 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
+{
+       return http_message_object_new_ex(ce, NULL);
+}
+
+zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message *msg TSRMLS_DC)
 {
        zend_object_value ov;
        http_message_object *o;
 
        o = ecalloc(1, sizeof(http_message_object));
        o->zo.ce = ce;
 {
        zend_object_value ov;
        http_message_object *o;
 
        o = ecalloc(1, sizeof(http_message_object));
        o->zo.ce = ce;
-       o->message = http_message_new();
+       o->message = msg ? msg : http_message_new();
 
        ALLOC_HASHTABLE(OBJ_PROP(o));
        zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
 
        ALLOC_HASHTABLE(OBJ_PROP(o));
        zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
@@ -82,7 +104,7 @@ static inline void _http_message_object_declare_default_properties(TSRMLS_D)
 
        DCL_PROP(PROTECTED, string, requestMethod, "");
        DCL_PROP(PROTECTED, string, requestUri, "");
 
        DCL_PROP(PROTECTED, string, requestMethod, "");
        DCL_PROP(PROTECTED, string, requestUri, "");
-       DCL_PROP(PROTECTED, long, responseStatus, 0);
+       DCL_PROP(PROTECTED, long, responseCode, 0);
 
        DCL_PROP_N(PROTECTED, httpVersion);
        DCL_PROP_N(PROTECTED, headers);
 
        DCL_PROP_N(PROTECTED, httpVersion);
        DCL_PROP_N(PROTECTED, headers);
@@ -100,6 +122,7 @@ static void _http_message_object_free(zend_object *object TSRMLS_DC)
        if (o->message) {
                http_message_free(o->message);
        }
        if (o->message) {
                http_message_free(o->message);
        }
+       zval_dtor(&o->_tmp_property);
        efree(o);
 }
 
        efree(o);
 }
 
@@ -107,15 +130,14 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
 {
        getObjectEx(http_message_object, obj, object);
        http_message *msg = obj->message;
 {
        getObjectEx(http_message_object, obj, object);
        http_message *msg = obj->message;
-       zval *return_value;
+       zval *return_value = &obj->_tmp_property;
 
        if (!EG(scope) || !instanceof_function(EG(scope), obj->zo.ce TSRMLS_CC)) {
                zend_error(E_WARNING, "Cannot access protected property %s::$%s", obj->zo.ce->name, Z_STRVAL_P(member));
                return EG(uninitialized_zval_ptr);
        }
 
 
        if (!EG(scope) || !instanceof_function(EG(scope), obj->zo.ce TSRMLS_CC)) {
                zend_error(E_WARNING, "Cannot access protected property %s::$%s", obj->zo.ce->name, Z_STRVAL_P(member));
                return EG(uninitialized_zval_ptr);
        }
 
-    ALLOC_ZVAL(return_value);
-    return_value->refcount = 0;
+    zval_dtor(return_value);
 
 #if 0
        fprintf(stderr, "Reading property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member), Z_STRLEN_P(member), strlen(Z_STRVAL_P(member)),
 
 #if 0
        fprintf(stderr, "Reading property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member), Z_STRLEN_P(member), strlen(Z_STRVAL_P(member)),
@@ -151,7 +173,7 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
                                if (msg->len) {
                                        RETVAL_STRINGL(msg->raw, msg->len, 1);
                                } else {
                                if (msg->len) {
                                        RETVAL_STRINGL(msg->raw, msg->len, 1);
                                } else {
-                                       RETVAL_STRINGL(empty_string, 0, 0);
+                                       RETVAL_STRINGL("", 0, 1);
                                }
                        } else {
                                RETVAL_NULL();
                                }
                        } else {
                                RETVAL_NULL();
@@ -164,8 +186,8 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
                break;
 
                case HTTP_MSG_PROPHASH_HEADERS:
                break;
 
                case HTTP_MSG_PROPHASH_HEADERS:
-                       Z_TYPE_P(return_value) = IS_ARRAY;
-                       Z_ARRVAL_P(return_value) = &msg->hdrs;
+                       array_init(return_value);
+                       zend_hash_copy(Z_ARRVAL_P(return_value), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
                break;
 
                case HTTP_MSG_PROPHASH_NESTED_MESSAGE:
                break;
 
                case HTTP_MSG_PROPHASH_NESTED_MESSAGE:
@@ -188,9 +210,9 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type
                        }
                break;
 
                        }
                break;
 
-               case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
+               case HTTP_MSG_PROPHASH_RESPONSE_CODE:
                        if (msg->type == HTTP_MSG_RESPONSE) {
                        if (msg->type == HTTP_MSG_RESPONSE) {
-                               RETVAL_LONG(msg->info.response.status);
+                               RETVAL_LONG(msg->info.response.code);
                        } else {
                                RETVAL_NULL();
                        }
                        } else {
                                RETVAL_NULL();
                        }
@@ -274,9 +296,9 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va
                        }
                break;
 
                        }
                break;
 
-               case HTTP_MSG_PROPHASH_RESPONSE_STATUS:
+               case HTTP_MSG_PROPHASH_RESPONSE_CODE:
                        if (msg->type == HTTP_MSG_RESPONSE) {
                        if (msg->type == HTTP_MSG_RESPONSE) {
-                               msg->info.response.status = Z_LVAL_P(value);
+                               msg->info.response.code = Z_LVAL_P(value);
                        }
                break;
        }
                        }
                break;
        }
@@ -305,7 +327,7 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
                int m_prop_len; \
                Z_ARRVAL(array) = OBJ_PROP(obj); \
                zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
                int m_prop_len; \
                Z_ARRVAL(array) = OBJ_PROP(obj); \
                zend_mangle_property_name(&m_prop_name, &m_prop_len, "*", 1, name, lenof(name), 1); \
-               add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, val != empty_string); \
+               add_assoc_stringl_ex(&array, m_prop_name, sizeof(name)+4, val, len, 1); \
        }
 
        zend_hash_clean(OBJ_PROP(obj));
        }
 
        zend_hash_clean(OBJ_PROP(obj));
@@ -324,24 +346,24 @@ static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)
        {
                case HTTP_MSG_REQUEST:
                        ASSOC_PROP(obj, double, "httpVersion", msg->info.request.http_version);
        {
                case HTTP_MSG_REQUEST:
                        ASSOC_PROP(obj, double, "httpVersion", msg->info.request.http_version);
-                       ASSOC_PROP(obj, long, "responseStatus", 0);
+                       ASSOC_PROP(obj, long, "responseCode", 0);
                        ASSOC_STRING(obj, "requestMethod", msg->info.request.method);
                        ASSOC_STRING(obj, "requestUri", msg->info.request.URI);
                break;
 
                case HTTP_MSG_RESPONSE:
                        ASSOC_PROP(obj, double, "httpVersion", msg->info.response.http_version);
                        ASSOC_STRING(obj, "requestMethod", msg->info.request.method);
                        ASSOC_STRING(obj, "requestUri", msg->info.request.URI);
                break;
 
                case HTTP_MSG_RESPONSE:
                        ASSOC_PROP(obj, double, "httpVersion", msg->info.response.http_version);
-                       ASSOC_PROP(obj, long, "responseStatus", msg->info.response.status);
-                       ASSOC_STRING(obj, "requestMethod", empty_string);
-                       ASSOC_STRING(obj, "requestUri", empty_string);
+                       ASSOC_PROP(obj, long, "responseCode", msg->info.response.code);
+                       ASSOC_STRING(obj, "requestMethod", "");
+                       ASSOC_STRING(obj, "requestUri", "");
                break;
 
                case HTTP_MSG_NONE:
                default:
                        ASSOC_PROP(obj, double, "httpVersion", 0.0);
                break;
 
                case HTTP_MSG_NONE:
                default:
                        ASSOC_PROP(obj, double, "httpVersion", 0.0);
-                       ASSOC_PROP(obj, long, "responseStatus", 0);
-                       ASSOC_STRING(obj, "requestMethod", empty_string);
-                       ASSOC_STRING(obj, "requestUri", empty_string);
+                       ASSOC_PROP(obj, long, "responseCode", 0);
+                       ASSOC_STRING(obj, "requestMethod", "");
+                       ASSOC_STRING(obj, "requestUri", "");
                break;
        }
 
                break;
        }