- changed HttpRequest::getHistory() to return a real property
authorMichael Wallner <mike@php.net>
Tue, 14 Feb 2006 09:29:54 +0000 (09:29 +0000)
committerMichael Wallner <mike@php.net>
Tue, 14 Feb 2006 09:29:54 +0000 (09:29 +0000)
http_message_object.c
http_request_object.c
package2.xml
php_http_message_object.h
php_http_request_object.h

index 5345b26112a0540cf804b159de2d8c513c25612b..ad4d8277ef02d829f0c9878288ebee4937689efa 100644 (file)
@@ -209,6 +209,46 @@ PHP_MINIT_FUNCTION(http_message_object)
        return SUCCESS;
 }
 
+void _http_message_object_prepend_ex(zval *this_ptr, zval *prepend, zend_bool top TSRMLS_DC)
+{
+       zval m;
+       http_message *save_parent_msg;
+       zend_object_value save_parent_obj;
+       getObject(http_message_object, obj);
+       getObjectEx(http_message_object, prepend_obj, prepend);
+               
+       INIT_PZVAL(&m);
+       m.type = IS_OBJECT;
+               
+       if (!top) {
+               save_parent_obj = obj->parent;
+               save_parent_msg = obj->message->parent;
+       } else {
+               /* iterate to the most parent object */
+               while (obj->parent.handle) {
+                       m.value.obj = obj->parent;
+                       obj = zend_object_store_get_object(&m TSRMLS_CC);
+               }
+       }
+               
+       /* prepend */
+       obj->parent = prepend->value.obj;
+       obj->message->parent = prepend_obj->message;
+               
+       /* add ref */
+       zend_objects_store_add_ref(prepend TSRMLS_CC);
+       while (prepend_obj->parent.handle) {
+               m.value.obj = prepend_obj->parent;
+               zend_objects_store_add_ref(&m TSRMLS_CC);
+               prepend_obj = zend_object_store_get_object(&m TSRMLS_CC);
+       }
+               
+       if (!top) {
+               prepend_obj->parent = save_parent_obj;
+               prepend_obj->message->parent = save_parent_msg;
+       }
+}
+
 zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC)
 {
        return http_message_object_new_ex(ce, NULL, NULL);
@@ -1329,42 +1369,7 @@ PHP_METHOD(HttpMessage, prepend)
        zend_bool top = 1;
        
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, http_message_object_ce, &top)) {
-               zval m;
-               http_message *save_parent_msg;
-               zend_object_value save_parent_obj;
-               getObject(http_message_object, obj);
-               getObjectEx(http_message_object, prepend_obj, prepend);
-               
-               INIT_PZVAL(&m);
-               m.type = IS_OBJECT;
-               
-               if (!top) {
-                       save_parent_obj = obj->parent;
-                       save_parent_msg = obj->message->parent;
-               } else {
-                       /* iterate to the most parent object */
-                       while (obj->parent.handle) {
-                               m.value.obj = obj->parent;
-                               obj = zend_object_store_get_object(&m TSRMLS_CC);
-                       }
-               }
-               
-               /* prepend */
-               obj->parent = prepend->value.obj;
-               obj->message->parent = prepend_obj->message;
-               
-               /* add ref */
-               zend_objects_store_add_ref(prepend TSRMLS_CC);
-               while (prepend_obj->parent.handle) {
-                       m.value.obj = prepend_obj->parent;
-                       zend_objects_store_add_ref(&m TSRMLS_CC);
-                       prepend_obj = zend_object_store_get_object(&m TSRMLS_CC);
-               }
-               
-               if (!top) {
-                       prepend_obj->parent = save_parent_obj;
-                       prepend_obj->message->parent = save_parent_msg;
-               }
+               http_message_object_prepend_ex(getThis(), prepend, top);
        }
 }
 /* }}} */
index ef8445c30c122f8c56e95af711b0ceaf6023007f..0c9a4cdd3f6b4cfc175e7438a3b8f17ecc41bb29 100644 (file)
@@ -313,7 +313,6 @@ zend_object_value _http_request_object_new_ex(zend_class_entry *ce, CURL *ch, ht
        o = ecalloc(1, sizeof(http_request_object));
        o->zo.ce = ce;
        o->request = http_request_init_ex(NULL, ch, 0, NULL);
-       phpstr_init(&o->history);
        
        if (ptr) {
                *ptr = o;
@@ -343,7 +342,6 @@ zend_object_value _http_request_object_clone_obj(zval *this_ptr TSRMLS_DC)
        }
        
        zend_objects_clone_members(&new_obj->zo, new_ov, old_zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
-       phpstr_append(&new_obj->history, old_obj->history.data, old_obj->history.used);
        phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used);
        phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used);
        
@@ -368,7 +366,7 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D)
        DCL_PROP(PRIVATE, string, rawPostData, "");
        DCL_PROP(PRIVATE, string, queryData, "");
        DCL_PROP(PRIVATE, string, putFile, "");
-
+       DCL_PROP_N(PRIVATE, history);
        DCL_PROP(PUBLIC, bool, recordHistory, 0);
 
 #ifndef WONKY
@@ -431,7 +429,6 @@ void _http_request_object_free(zend_object *object TSRMLS_DC)
                FREE_HASHTABLE(OBJ_PROP(o));
        }
        http_request_free(&o->request);
-       phpstr_dtor(&o->history);
        efree(o);
 }
 
@@ -573,25 +570,57 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
                         * the requests and the responses in separate chains
                         * for redirects
                         */
-                       http_message *response = msg, *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
-                       http_message *free_msg = request;
-
-                       do {
-                               char *message;
-                               size_t msglen;
-
-                               http_message_tostring(response, &message, &msglen);
-                               phpstr_append(&obj->history, message, msglen);
-                               efree(message);
-
-                               http_message_tostring(request, &message, &msglen);
-                               phpstr_append(&obj->history, message, msglen);
-                               efree(message);
-
-                       } while ((response = response->parent) && (request = request->parent));
-
-                       http_message_free(&free_msg);
-                       phpstr_fix(&obj->history);
+                       http_message *response = http_message_dup(msg);
+                       http_message *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
+                       
+                       if (request && response) {
+                               int num_req, num_resp;
+                               
+                               http_message_count(num_req, request);
+                               http_message_count(num_resp, response);
+                               
+                               /*
+                                       stuck request messages in between response messages
+                               
+                                       response   request
+                                          v          v
+                                       response   request
+                                          v          v
+                                       response   request
+                                       ==================
+                                       response > request
+                                          ,---'
+                                       response > request
+                                          ,---'
+                                       response > request
+                               */
+                               if (num_req == num_resp) {
+                                       int i;
+                                       zval *hist, *history = GET_PROP(history);
+                                       http_message *res_tmp = response, *req_tmp = request, *req_par, *res_par;
+                                       
+                                       for (i = 0; i < num_req; ++i) {
+                                               res_par = res_tmp->parent;
+                                               req_par = req_tmp->parent;
+                                               res_tmp->parent = req_tmp;
+                                               req_tmp->parent = res_par;
+                                               res_tmp = res_par;
+                                               req_tmp = req_par;
+                                       }
+                                       
+                                       MAKE_STD_ZVAL(hist);
+                                       ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, response, NULL), 0);
+                                       if (Z_TYPE_P(history) == IS_OBJECT) {
+                                               http_message_object_prepend(hist, history);
+                                       }
+                                       SET_PROP(history, hist);
+                                       zval_ptr_dtor(&hist);
+                               }
+                               /* TODO: error? */
+                       } else {
+                               http_message_free(&response);
+                               http_message_free(&request);
+                       }
                }
 
                UPD_PROP(long, responseCode, msg->http.info.response.code);
@@ -1808,8 +1837,8 @@ PHP_METHOD(HttpRequest, getRawResponseMessage)
  *
  * Get all sent requests and received responses as an HttpMessage object.
  * 
- * If you don't want to record history at all, set the instance variable
- * HttpRequest::$recordHistory to FALSE. 
+ * If you want to record history, set the instance variable
+ * HttpRequest::$recordHistory to TRUE.
  * 
  * Returns an HttpMessage object representing the complete request/response
  * history.
@@ -1817,23 +1846,21 @@ PHP_METHOD(HttpRequest, getRawResponseMessage)
  * The object references the last received response, use HttpMessage::getParentMessage() 
  * to access the data of previously sent requests and received responses.
  * 
- * Note that the internal history is immutable, that means that any changes
- * you make the the message list won't affect a history message list newly 
- * created by another call to HttpRequest::getHistory().
- * 
- * Throws HttpMalformedHeaderException, HttpEncodingException.
+ * Throws HttpRuntimeException.
  */
 PHP_METHOD(HttpRequest, getHistory)
 {
        NO_ARGS;
 
        IF_RETVAL_USED {
-               http_message *msg;
-               getObject(http_request_object, obj);
-
+               zval *hist;
+               
                SET_EH_THROW_HTTP();
-               if ((msg = http_message_parse(PHPSTR_VAL(&obj->history), PHPSTR_LEN(&obj->history)))) {
-                       RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
+               hist = GET_PROP(history);
+               if (Z_TYPE_P(hist) == IS_OBJECT) {
+                       RETVAL_OBJECT(hist, 1);
+               } else {
+                       http_error(HE_WARNING, HTTP_E_RUNTIME, "The history is empty");
                }
                SET_EH_NORMAL();
        }
@@ -1847,8 +1874,12 @@ PHP_METHOD(HttpRequest, getHistory)
 PHP_METHOD(HttpRequest, clearHistory)
 {
        NO_ARGS {
-               getObject(http_request_object, obj);
-               phpstr_dtor(&obj->history);
+               zval *hist;
+               
+               MAKE_STD_ZVAL(hist);
+               ZVAL_NULL(hist);
+               SET_PROP(history, hist);
+               zval_ptr_dtor(hist);
        }
 }
 /* }}} */
index 785c7e9bee44f48bf7659a2cb187bdbe7986928b..09fab0a73d2cc88d7642209ea1508b81d5cdefd7 100644 (file)
@@ -50,12 +50,14 @@ HttpResponse
 + Added 'encodecookies' request option (defaults to true).
 + Added missing accessors for response status text to HttpMessage and HttpRequest classes.
 + Added HttpMessage::reverse().
++ Added bool must_revalidate parameter to HttpResponse::setCacheControl()
 
 - Reimplemented http_parse_cookie().
 - Changed HttpRequest::getResponseCookie() to HttpRequest::getResponseCookies(), 
   compliant to http_parse_cookie().
 - http_build_url() now tries to "resolve" public hostname prior falling back 
   to localhost if neither HTTP_HOST nor SERVER_NAME is set.
+- Changed HttpRequest::getHistory() to return a real property.
 
 * Fixed PHP-4.4 and PHP-5.0 build.
 ]]></notes>
index 3545b4542f26de2dd4f901e09cfce22b3fb52b63..4c084703fce5b4b171a4c2f0947c31e8c2fb21a0 100644 (file)
@@ -28,6 +28,10 @@ extern zend_function_entry http_message_object_fe[];
 
 extern PHP_MINIT_FUNCTION(http_message_object);
 
+#define http_message_object_prepend(o, p) http_message_object_prepend_ex((o), (p), 1)
+#define http_message_object_prepend_ex(o, p, t) _http_message_object_prepend_ex((o), (p), (t) TSRMLS_CC)
+extern void _http_message_object_prepend_ex(zval *this_ptr, zval *prepend, zend_bool top TSRMLS_DC);
+
 #define http_message_object_new(ce) _http_message_object_new((ce) TSRMLS_CC)
 extern zend_object_value _http_message_object_new(zend_class_entry *ce TSRMLS_DC);
 #define http_message_object_new_ex(ce, msg, ptr) _http_message_object_new_ex((ce), (msg), (ptr) TSRMLS_CC)
index ebd5bf532222d02fc7de2014c72520f0379b8a2c..4d2ab24f51fbe0108049669d708fd18cd3232952 100644 (file)
@@ -23,7 +23,6 @@ typedef struct {
        zend_object zo;
        http_request *request;
        http_request_pool *pool;
-       phpstr history;
 } http_request_object;
 
 extern zend_class_entry *http_request_object_ce;