X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_message_object.c;h=f1a6705cdccea600e1ccb1473fcf9fc272a06ebb;hp=244e29da292569359834051902169c13548f51e3;hb=16ea91ddd08d15dd9b7206229fec6158f212adaf;hpb=b7d46301e6a672db53cec3363ab0d2f31ccb6de9 diff --git a/http_message_object.c b/http_message_object.c index 244e29d..f1a6705 100644 --- a/http_message_object.c +++ b/http_message_object.c @@ -26,6 +26,8 @@ #include "php_http_std_defs.h" #include "php_http_message_object.h" +#include "phpstr/phpstr.h" + #ifdef ZEND_ENGINE_2 #define http_message_object_declare_default_properties() _http_message_object_declare_default_properties(TSRMLS_C) @@ -39,6 +41,7 @@ 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) PHP_ME(HttpMessage, getBody, NULL, ZEND_ACC_PUBLIC) PHP_ME(HttpMessage, getHeaders, NULL, ZEND_ACC_PUBLIC) PHP_ME(HttpMessage, setHeaders, NULL, ZEND_ACC_PUBLIC) @@ -53,6 +56,8 @@ zend_function_entry http_message_object_fe[] = { PHP_ME(HttpMessage, setRequestUri, NULL, ZEND_ACC_PUBLIC) PHP_ME(HttpMessage, getHttpVersion, NULL, ZEND_ACC_PUBLIC) PHP_ME(HttpMessage, setHttpVersion, NULL, ZEND_ACC_PUBLIC) + PHP_ME(HttpMessage, getParentMessage, NULL, ZEND_ACC_PUBLIC) + PHP_ME(HttpMessage, send, NULL, ZEND_ACC_PUBLIC) PHP_ME(HttpMessage, toString, NULL, ZEND_ACC_PUBLIC) ZEND_MALIAS(HttpMessage, __toString, toString, NULL, ZEND_ACC_PUBLIC) @@ -87,17 +92,15 @@ zend_object_value _http_message_object_new_ex(zend_class_entry *ce, http_message o = ecalloc(1, sizeof(http_message_object)); o->zo.ce = ce; - - MAKE_STD_ZVAL(o->nested); - Z_TYPE_P(o->nested) = IS_OBJECT; + o->message = NULL; + o->parent.handle = 0; + o->parent.handlers = NULL; if (msg) { o->message = msg; - if (msg->nested) { - o->nested->value.obj = http_message_object_from_msg(msg->nested); + if (msg->parent) { + o->parent = http_message_object_from_msg(msg->parent); } - } else { - o->message = http_message_new(); } ALLOC_HASHTABLE(OBJ_PROP(o)); @@ -114,20 +117,16 @@ static inline void _http_message_object_declare_default_properties(TSRMLS_D) zend_class_entry *ce = http_message_object_ce; 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, long, responseCode, 0); - DCL_PROP_N(PROTECTED, httpVersion); DCL_PROP_N(PROTECTED, headers); - - DCL_PROP_N(PUBLIC, nestedMessage); + DCL_PROP_N(PROTECTED, parentMessage); } -static void _http_message_object_free(zend_object *object TSRMLS_DC) +void _http_message_object_free(zend_object *object TSRMLS_DC) { http_message_object *o = (http_message_object *) object; @@ -139,8 +138,6 @@ static void _http_message_object_free(zend_object *object TSRMLS_DC) http_message_dtor(o->message); efree(o->message); } - FREE_ZVAL(o->nested); - zval_dtor(&o->_tmp_property); efree(o); } @@ -148,25 +145,18 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type { getObjectEx(http_message_object, obj, object); http_message *msg = obj->message; - zval *return_value = &obj->_tmp_property; - - /* public */ - if (strcmp(Z_STRVAL_P(member), "nestedMessage")) { - 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); - } - } + zval *return_value; - zval_dtor(return_value); + /* tmp var */ + ALLOC_ZVAL(return_value); + return_value->refcount = 0; -#if 0 - fprintf(stderr, "Reading property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member), Z_STRLEN_P(member), strlen(Z_STRVAL_P(member)), - zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1) - ); -#endif + 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); + } - switch (zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1)) + switch (zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1)) { case HTTP_MSG_PROPHASH_TYPE: RETVAL_LONG(msg->type); @@ -175,10 +165,6 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type case HTTP_MSG_PROPHASH_HTTP_VERSION: switch (msg->type) { - case HTTP_MSG_NONE: - RETVAL_NULL(); - break; - case HTTP_MSG_REQUEST: RETVAL_DOUBLE(msg->info.request.http_version); break; @@ -186,6 +172,11 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type case HTTP_MSG_RESPONSE: RETVAL_DOUBLE(msg->info.response.http_version); break; + + case HTTP_MSG_NONE: + default: + RETVAL_NULL(); + break; } break; @@ -199,15 +190,19 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type 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: - RETVAL_NULL(); - if (msg->nested) { - return obj->nested; + case HTTP_MSG_PROPHASH_PARENT_MESSAGE: + if (msg->parent) { + RETVAL_OBJVAL(obj->parent); + Z_TYPE_P(return_value) = IS_OBJECT; + return_value->value.obj = obj->parent; + zend_objects_store_add_ref(return_value TSRMLS_CC); + } else { + RETVAL_NULL(); } break; case HTTP_MSG_PROPHASH_REQUEST_METHOD: - if (msg->type == HTTP_MSG_REQUEST && msg->info.request.method) { + if (HTTP_MSG_TYPE(REQUEST, msg) && msg->info.request.method) { RETVAL_STRING(msg->info.request.method, 1); } else { RETVAL_NULL(); @@ -215,7 +210,7 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type break; case HTTP_MSG_PROPHASH_REQUEST_URI: - if (msg->type == HTTP_MSG_REQUEST && msg->info.request.URI) { + if (HTTP_MSG_TYPE(REQUEST, msg) && msg->info.request.URI) { RETVAL_STRING(msg->info.request.URI, 1); } else { RETVAL_NULL(); @@ -223,7 +218,7 @@ static zval *_http_message_object_read_prop(zval *object, zval *member, int type break; case HTTP_MSG_PROPHASH_RESPONSE_CODE: - if (msg->type == HTTP_MSG_RESPONSE) { + if (HTTP_MSG_TYPE(RESPONSE, msg)) { RETVAL_LONG(msg->info.response.code); } else { RETVAL_NULL(); @@ -243,24 +238,16 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va getObjectEx(http_message_object, obj, object); http_message *msg = obj->message; - /* public */ - if (strcmp("nestedMessage", Z_STRVAL_P(member))) { - 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)); - } + 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)); } -#if 0 - fprintf(stderr, "Writing property: %s(%d==%d) (%lu)\n", Z_STRVAL_P(member), Z_STRLEN_P(member), strlen(Z_STRVAL_P(member)), - zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1) - ); -#endif - - switch (zend_get_hash_value(Z_STRVAL_P(member), strlen(Z_STRVAL_P(member)) + 1)) + switch (zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1)) { case HTTP_MSG_PROPHASH_TYPE: + convert_to_long_ex(&value); if (Z_LVAL_P(value) != msg->type) { - if (msg->type == HTTP_MSG_REQUEST) { + if (HTTP_MSG_TYPE(REQUEST, msg)) { if (msg->info.request.method) { efree(msg->info.request.method); } @@ -269,7 +256,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va } } msg->type = Z_LVAL_P(value); - if (msg->type == HTTP_MSG_REQUEST) { + if (HTTP_MSG_TYPE(REQUEST, msg)) { msg->info.request.method = NULL; msg->info.request.URI = NULL; } @@ -278,6 +265,7 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va break; case HTTP_MSG_PROPHASH_HTTP_VERSION: + convert_to_double_ex(&value); switch (msg->type) { case HTTP_MSG_REQUEST: @@ -291,23 +279,30 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va break; case HTTP_MSG_PROPHASH_BODY: + convert_to_string_ex(&value); phpstr_dtor(PHPSTR(msg)); phpstr_from_string_ex(PHPSTR(msg), Z_STRVAL_P(value), Z_STRLEN_P(value)); break; case HTTP_MSG_PROPHASH_HEADERS: + convert_to_array_ex(&value); zend_hash_clean(&msg->hdrs); zend_hash_copy(&msg->hdrs, Z_ARRVAL_P(value), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); break; - case HTTP_MSG_PROPHASH_NESTED_MESSAGE: - zval_ptr_dtor(&obj->nested); - obj->nested->value.obj = value->value.obj; - zval_add_ref(&value); + case HTTP_MSG_PROPHASH_PARENT_MESSAGE: + if (msg->parent) { + zval tmp; + tmp.value.obj = obj->parent; + zend_objects_store_del_ref(&tmp TSRMLS_CC); + } + zend_objects_store_add_ref(value TSRMLS_CC); + obj->parent = value->value.obj; break; case HTTP_MSG_PROPHASH_REQUEST_METHOD: - if (msg->type == HTTP_MSG_REQUEST) { + convert_to_string_ex(&value); + if (HTTP_MSG_TYPE(REQUEST, msg)) { if (msg->info.request.method) { efree(msg->info.request.method); } @@ -316,7 +311,8 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va break; case HTTP_MSG_PROPHASH_REQUEST_URI: - if (msg->type == HTTP_MSG_REQUEST) { + convert_to_string_ex(&value); + if (HTTP_MSG_TYPE(REQUEST, msg)) { if (msg->info.request.URI) { efree(msg->info.request.URI); } @@ -325,13 +321,12 @@ static void _http_message_object_write_prop(zval *object, zval *member, zval *va break; case HTTP_MSG_PROPHASH_RESPONSE_CODE: - if (msg->type == HTTP_MSG_RESPONSE) { + convert_to_long_ex(&value); + if (HTTP_MSG_TYPE(RESPONSE, msg)) { msg->info.response.code = Z_LVAL_P(value); } break; } - zval_dtor(value); - FREE_ZVAL(value); } static HashTable *_http_message_object_get_props(zval *object TSRMLS_DC)