- need php_stream_open_wrapper_*ex* to pass a stream context
[m6w6/ext-http] / http_message_object.c
index 5345b26112a0540cf804b159de2d8c513c25612b..c642754c73641b81bf4182bee3a71d25ebc7f658 100644 (file)
@@ -209,6 +209,96 @@ PHP_MINIT_FUNCTION(http_message_object)
        return SUCCESS;
 }
 
+void _http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_DC)
+{
+       int i;
+       getObject(http_message_object, obj);
+       
+       /* count */
+       http_message_count(i, obj->message);
+       
+       if (i > 1) {
+               zval o;
+               zend_object_value *ovalues = NULL;
+               http_message_object **objects = NULL;
+               int last = i - 1;
+               
+               objects = ecalloc(i, sizeof(http_message_object *));
+               ovalues = ecalloc(i, sizeof(zend_object_value));
+       
+               /* we are the first message */
+               objects[0] = obj;
+               ovalues[0] = getThis()->value.obj;
+       
+               /* fetch parents */
+               INIT_PZVAL(&o);
+               o.type = IS_OBJECT;
+               for (i = 1; obj->parent.handle; ++i) {
+                       o.value.obj = obj->parent;
+                       ovalues[i] = o.value.obj;
+                       objects[i] = obj = zend_object_store_get_object(&o TSRMLS_CC);
+               }
+               
+               /* reorder parents */
+               for (last = --i; i; --i) {
+                       objects[i]->message->parent = objects[i-1]->message;
+                       objects[i]->parent = ovalues[i-1];
+               }
+               objects[0]->message->parent = NULL;
+               objects[0]->parent.handle = 0;
+               objects[0]->parent.handlers = NULL;
+               
+               /* add ref (why?) */
+               Z_OBJ_ADDREF_P(getThis());
+               RETVAL_OBJVAL(ovalues[last], 1);
+               
+               efree(objects);
+               efree(ovalues);
+       } else {
+               RETURN_ZVAL(getThis(), 1, 0);
+       }
+}
+
+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 +1419,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);
        }
 }
 /* }}} */
@@ -1377,53 +1432,8 @@ PHP_METHOD(HttpMessage, prepend)
  */
 PHP_METHOD(HttpMessage, reverse)
 {
-       int i;
-       getObject(http_message_object, obj);
-       
-       NO_ARGS;
-       
-       /* count */
-       http_message_count(i, obj->message);
-       
-       if (i > 1) {
-               zval o;
-               zend_object_value *ovalues = NULL;
-               http_message_object **objects = NULL;
-               int last = i - 1;
-               
-               objects = ecalloc(i, sizeof(http_message_object *));
-               ovalues = ecalloc(i, sizeof(zend_object_value));
-       
-               /* we are the first message */
-               objects[0] = obj;
-               ovalues[0] = getThis()->value.obj;
-       
-               /* fetch parents */
-               INIT_PZVAL(&o);
-               o.type = IS_OBJECT;
-               for (i = 1; obj->parent.handle; ++i) {
-                       o.value.obj = obj->parent;
-                       ovalues[i] = o.value.obj;
-                       objects[i] = obj = zend_object_store_get_object(&o TSRMLS_CC);
-               }
-               
-               /* reorder parents */
-               for (last = --i; i; --i) {
-                       objects[i]->message->parent = objects[i-1]->message;
-                       objects[i]->parent = ovalues[i-1];
-               }
-               objects[0]->message->parent = NULL;
-               objects[0]->parent.handle = 0;
-               objects[0]->parent.handlers = NULL;
-               
-               /* add ref (why?) */
-               Z_OBJ_ADDREF_P(getThis());
-               RETVAL_OBJVAL(ovalues[last], 1);
-               
-               efree(objects);
-               efree(ovalues);
-       } else {
-               RETURN_ZVAL(getThis(), 1, 0);
+       NO_ARGS {
+               http_message_object_reverse(getThis(), return_value);
        }
 }
 /* }}} */