From 88770c6b3fb365da405b4aede6def9ce88dbca7c Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sun, 12 Jun 2011 09:08:17 +0000 Subject: [PATCH] * request message and history --- php_http_message.c | 9 +++-- php_http_message.h | 1 + php_http_request.c | 87 +++++++++++++++++++++++++--------------------- 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/php_http_message.c b/php_http_message.c index 318470e..ad08d7f 100644 --- a/php_http_message.c +++ b/php_http_message.c @@ -566,7 +566,7 @@ PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC) } */ -PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from, php_http_message_t *to) +PHP_HTTP_API php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_message_t *to, zend_bool parents) { php_http_message_t *temp, *copy = NULL; php_http_info_t info; @@ -581,7 +581,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from, zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); php_http_message_body_copy(&from->body, &temp->body, 1); - while (from->parent) { + if (parents) while (from->parent) { info.type = from->parent->type; info.http = from->parent->http; @@ -598,6 +598,11 @@ PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from, return copy; } +PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from, php_http_message_t *to) +{ + return php_http_message_copy_ex(from, to, 1); +} + PHP_HTTP_API void php_http_message_dtor(php_http_message_t *message) { if (message) { diff --git a/php_http_message.h b/php_http_message.h index 47db451..65509e7 100644 --- a/php_http_message.h +++ b/php_http_message.h @@ -43,6 +43,7 @@ PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **messa PHP_HTTP_API php_http_message_t *php_http_message_init(php_http_message_t *m, php_http_message_type_t t TSRMLS_DC); PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m, php_http_message_type_t t TSRMLS_DC); PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from, php_http_message_t *to); +PHP_HTTP_API php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_message_t *to, zend_bool parents); PHP_HTTP_API void php_http_message_dtor(php_http_message_t *message); PHP_HTTP_API void php_http_message_free(php_http_message_t **message); diff --git a/php_http_request.c b/php_http_request.c index 61b9395..0812828 100644 --- a/php_http_request.c +++ b/php_http_request.c @@ -40,7 +40,8 @@ PHP_HTTP_API php_http_request_t *php_http_request_init(php_http_request_t *h, ph if (!(h = h->ops->init(h, init_arg))) { php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Could not initialize request"); if (free_h) { - efree(free_h); + h->ops->dtor = NULL; + php_http_request_free(&h); } } } @@ -465,6 +466,13 @@ STATUS php_http_request_object_requesthandler(php_http_request_object_t *obj, zv return SUCCESS; } +static inline void empty_response(zval *this_ptr TSRMLS_DC) +{ + zend_update_property_null(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage") TSRMLS_CC); + zend_update_property_long(php_http_request_class_entry, getThis(), ZEND_STRL("responseCode"), 0 TSRMLS_CC); + zend_update_property_string(php_http_request_class_entry, getThis(), ZEND_STRL("responseStatus"), "" TSRMLS_CC); +} + STATUS php_http_request_object_responsehandler(php_http_request_object_t *obj, zval *this_ptr TSRMLS_DC) { STATUS ret = SUCCESS; @@ -479,54 +487,55 @@ STATUS php_http_request_object_responsehandler(php_http_request_object_t *obj, z zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("transferInfo"), info TSRMLS_CC); zval_ptr_dtor(&info); - /* update history * / - if (i_zend_is_true(zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) { - zval *new_hist, *old_hist = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC); - zend_object_value ov = php_http_request_object_message(getThis(), obj->request->message_parser.message TSRMLS_CC); + if ((msg = obj->request->message)) { + /* update history */ + if (i_zend_is_true(zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) { + zval *new_hist, *old_hist = zend_read_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC); + zend_object_value ov = php_http_request_object_message(getThis(), php_http_message_copy(msg, NULL) TSRMLS_CC); + + MAKE_STD_ZVAL(new_hist); + ZVAL_OBJVAL(new_hist, ov, 0); - MAKE_STD_ZVAL(new_hist); - 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); + } - if (Z_TYPE_P(old_hist) == IS_OBJECT) { - php_http_message_object_prepend(new_hist, old_hist, 0 TSRMLS_CC); + zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), new_hist TSRMLS_CC); + zval_ptr_dtor(&new_hist); } - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("history"), new_hist TSRMLS_CC); - zval_ptr_dtor(&new_hist); - } -*/ -// if ((msg = obj->request->_current.request)) { -// /* update request message */ -// zval *message; -// -// MAKE_STD_ZVAL(message); -// ZVAL_OBJVAL(message, php_http_request_object_message(getThis(), msg TSRMLS_CC), 1); -// zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("requestMessage"), message TSRMLS_CC); -// } -// fprintf(stderr, "RESPONSE MESSAGE: %p\n", obj->request->parser.msg); - if ((msg = obj->request->message)) { - /* update properties with response info */ - zval *message; + /* update response info */ + if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, msg)) { + zval *message; - zend_update_property_long(php_http_request_class_entry, getThis(), ZEND_STRL("responseCode"), msg->http.info.response.code TSRMLS_CC); - zend_update_property_string(php_http_request_class_entry, getThis(), ZEND_STRL("responseStatus"), STR_PTR(msg->http.info.response.status) TSRMLS_CC); + zend_update_property_long(php_http_request_class_entry, getThis(), ZEND_STRL("responseCode"), msg->http.info.response.code TSRMLS_CC); + zend_update_property_string(php_http_request_class_entry, getThis(), ZEND_STRL("responseStatus"), STR_PTR(msg->http.info.response.status) TSRMLS_CC); - MAKE_STD_ZVAL(message); - ZVAL_OBJVAL(message, php_http_request_object_message(getThis(), msg TSRMLS_CC), 0); - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), message TSRMLS_CC); - zval_ptr_dtor(&message); - obj->request->message = php_http_message_init(NULL, 0 TSRMLS_CC); + MAKE_STD_ZVAL(message); + ZVAL_OBJVAL(message, php_http_request_object_message(getThis(), msg TSRMLS_CC), 0); + zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), message TSRMLS_CC); + zval_ptr_dtor(&message); + + obj->request->message = php_http_message_init(NULL, 0 TSRMLS_CC); + msg = msg->parent; + } else { + empty_response(getThis() TSRMLS_CC); + } } else { /* update properties with empty values */ - zval *znull; + empty_response(getThis() TSRMLS_CC); + } - MAKE_STD_ZVAL(znull); - ZVAL_NULL(znull); - zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("responseMessage"), znull TSRMLS_CC); - zval_ptr_dtor(&znull); + while (msg && !PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { + msg = msg->parent; + } + if (PHP_HTTP_MESSAGE_TYPE(REQUEST, msg)) { + zval *message; - zend_update_property_long(php_http_request_class_entry, getThis(), ZEND_STRL("responseCode"), 0 TSRMLS_CC); - zend_update_property_string(php_http_request_class_entry, getThis(), ZEND_STRL("responseStatus"), "" TSRMLS_CC); + MAKE_STD_ZVAL(message); + ZVAL_OBJVAL(message, php_http_request_object_message(getThis(), php_http_message_copy_ex(msg, NULL, 0) TSRMLS_CC), 0); + zend_update_property(php_http_request_class_entry, getThis(), ZEND_STRL("requestMessage"), message TSRMLS_CC); + zval_ptr_dtor(&message); } if (SUCCESS == php_http_request_getopt(obj->request, PHP_HTTP_REQUEST_OPT_PROGRESS_INFO, &progress)) { -- 2.30.2