zeev            Sat Mar  3 17:09:36 2001 EDT

  Modified files:              
    /php4/ext/sablot    sablot.c 
    /php4/ext/standard  output.c php_output.h 
  Log:
  Improve chunked output buffering - the output handler now knows at which stage it is
  
  
Index: php4/ext/sablot/sablot.c
diff -u php4/ext/sablot/sablot.c:1.36 php4/ext/sablot/sablot.c:1.37
--- php4/ext/sablot/sablot.c:1.36       Tue Feb 27 12:16:35 2001
+++ php4/ext/sablot/sablot.c    Sat Mar  3 17:09:36 2001
@@ -348,7 +348,7 @@
      * current output buffer (so we don't send data twice) 
      */
     if (tRes)
-        php_end_ob_buffer(0);
+        php_end_ob_buffer(0, 0);
     
     PUTS(tRes);
     
@@ -363,7 +363,7 @@
     if (tRes)
         SablotFree(tRes);
     else
-        php_end_ob_buffer(1);
+        php_end_ob_buffer(1, 0);
 }
 /* }}} */
 
Index: php4/ext/standard/output.c
diff -u php4/ext/standard/output.c:1.40 php4/ext/standard/output.c:1.41
--- php4/ext/standard/output.c:1.40     Mon Feb 26 16:09:14 2001
+++ php4/ext/standard/output.c  Sat Mar  3 17:09:36 2001
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: output.c,v 1.40 2001/02/27 00:09:14 zeev Exp $ */
+/* $Id: output.c,v 1.41 2001/03/04 01:09:36 zeev Exp $ */
 
 #include "php.h"
 #include "ext/standard/head.h"
@@ -68,11 +68,16 @@
 PHPAPI void php_output_startup()
 {
        OLS_FETCH();
+       ELS_FETCH();
 
        OG(php_body_write) = php_ub_body_write;
        OG(php_header_write) = sapi_module.ub_write;
        OG(nesting_level) = 0;
        OG(lock) = 0;
+
+       REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_START", 
+PHP_OUTPUT_HANDLER_START, CONST_CS | CONST_PERSISTENT);
+       REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_CONT", 
+PHP_OUTPUT_HANDLER_CONT, CONST_CS | CONST_PERSISTENT);
+       REGISTER_MAIN_LONG_CONSTANT("PHP_OUTPUT_HANDLER_END", PHP_OUTPUT_HANDLER_END, 
+CONST_CS | CONST_PERSISTENT);
 }
 
 PHPAPI int php_body_write(const char *str, uint str_length)
@@ -102,7 +107,7 @@
 
 
 /* End output buffering (one level) */
-PHPAPI void php_end_ob_buffer(int send_buffer)
+PHPAPI void php_end_ob_buffer(zend_bool send_buffer, zend_bool just_flush)
 {
        char *final_buffer=NULL;
        int final_buffer_length=0;
@@ -116,8 +121,9 @@
        }
 
        if (OG(active_ob_buffer).output_handler) {
-               zval **params[1];
+               zval **params[2];
                zval *orig_buffer;
+               zval *z_status;
                CLS_FETCH();
 
                ALLOC_INIT_ZVAL(orig_buffer);
@@ -126,9 +132,23 @@
                orig_buffer->type = IS_STRING;
                orig_buffer->refcount=2;        /* don't let call_user_function() 
destroy our buffer */
 
+               ALLOC_INIT_ZVAL(z_status);
+               Z_TYPE_P(z_status) = IS_LONG;
+               Z_LVAL_P(z_status) = 0;
+               if (!OG(active_ob_buffer).status & PHP_OUTPUT_HANDLER_START) {
+                       /* our first call */
+                       Z_LVAL_P(z_status) |= PHP_OUTPUT_HANDLER_START;
+               }
+               if (just_flush) {
+                       Z_LVAL_P(z_status) |= PHP_OUTPUT_HANDLER_CONT;
+               } else {
+                       Z_LVAL_P(z_status) |= PHP_OUTPUT_HANDLER_END;
+               }
+
                params[0] = &orig_buffer;
+               params[1] = &z_status;
                OG(lock) = 1;
-               if (call_user_function_ex(CG(function_table), NULL, 
OG(active_ob_buffer).output_handler, &alternate_buffer, 1, params, 1, NULL)==SUCCESS) {
+               if (call_user_function_ex(CG(function_table), NULL, 
+OG(active_ob_buffer).output_handler, &alternate_buffer, 2, params, 1, NULL)==SUCCESS) 
+{
                        convert_to_string_ex(&alternate_buffer);
                        final_buffer = alternate_buffer->value.str.val;
                        final_buffer_length = alternate_buffer->value.str.len;
@@ -140,6 +160,7 @@
                } else {
                        orig_buffer->refcount-=2;
                }
+               zval_ptr_dtor(&z_status);
        }
 
        if (!final_buffer) {
@@ -157,16 +178,18 @@
 
        to_be_destroyed_buffer = OG(active_ob_buffer).buffer;
 
-       if (OG(nesting_level)>1) { /* restore previous buffer */
-               php_ob_buffer *ob_buffer_p;
-
-               zend_stack_top(&OG(ob_buffers), (void **) &ob_buffer_p);
-               OG(active_ob_buffer) = *ob_buffer_p;
-               zend_stack_del_top(&OG(ob_buffers));
-               if (OG(nesting_level)==2) { /* destroy the stack */
-                       zend_stack_destroy(&OG(ob_buffers));
+       if (!just_flush) {
+               if (OG(nesting_level)>1) { /* restore previous buffer */
+                       php_ob_buffer *ob_buffer_p;
+
+                       zend_stack_top(&OG(ob_buffers), (void **) &ob_buffer_p);
+                       OG(active_ob_buffer) = *ob_buffer_p;
+                       zend_stack_del_top(&OG(ob_buffers));
+                       if (OG(nesting_level)==2) { /* destroy the stack */
+                               zend_stack_destroy(&OG(ob_buffers));
+                       }
                }
-       } 
+       }
 
        if (send_buffer) {
                OG(php_body_write)(final_buffer, final_buffer_length);
@@ -176,19 +199,25 @@
                zval_ptr_dtor(&alternate_buffer);
        }
 
-       efree(to_be_destroyed_buffer);
+       if (!just_flush) {
+               efree(to_be_destroyed_buffer);
 
-       OG(nesting_level)--;
+               OG(nesting_level)--;
+       } else {
+               OG(active_ob_buffer).text_length = 0;
+               OG(active_ob_buffer).status |= PHP_OUTPUT_HANDLER_START;
+               OG(php_body_write) = php_b_body_write;
+       }
 }
 
 
 /* End output buffering (all buffers) */
-PHPAPI void php_end_ob_buffers(int send_buffer)
+PHPAPI void php_end_ob_buffers(zend_bool send_buffer)
 {
        OLS_FETCH();
 
        while (OG(nesting_level)!=0) {
-               php_end_ob_buffer(send_buffer);
+               php_end_ob_buffer(send_buffer, 0);
        }
 }
 
@@ -197,7 +226,7 @@
 {
        OLS_FETCH();
 
-       php_end_ob_buffer(1);           /* Switch out of output buffering if we're in 
it */
+       php_end_ob_buffer(1, 0);                /* Switch out of output buffering if 
+we're in it */
        OG(implicit_flush)=1;
 }
 
@@ -245,6 +274,7 @@
        OG(active_ob_buffer).text_length = 0;
        OG(active_ob_buffer).output_handler = output_handler;
        OG(active_ob_buffer).chunk_size = chunk_size;
+       OG(active_ob_buffer).status = 0;
 }
 
 
@@ -270,8 +300,7 @@
                if (output_handler) {
                        output_handler->refcount++;
                }
-               php_end_ob_buffer(1);
-               php_start_ob_buffer(output_handler, chunk_size);
+               php_end_ob_buffer(1, 1);
                return;
        }
 }
@@ -453,7 +482,7 @@
    Flush (send) the output buffer, and turn off output buffering */
 PHP_FUNCTION(ob_end_flush)
 {
-       php_end_ob_buffer(1);
+       php_end_ob_buffer(1, 0);
 }
 /* }}} */
 
@@ -462,7 +491,7 @@
    Clean (erase) the output buffer, and turn off output buffering */
 PHP_FUNCTION(ob_end_clean)
 {
-       php_end_ob_buffer(0);
+       php_end_ob_buffer(0, 0);
 }
 /* }}} */
 
Index: php4/ext/standard/php_output.h
diff -u php4/ext/standard/php_output.h:1.18 php4/ext/standard/php_output.h:1.19
--- php4/ext/standard/php_output.h:1.18 Mon Feb 26 16:09:14 2001
+++ php4/ext/standard/php_output.h      Sat Mar  3 17:09:36 2001
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: php_output.h,v 1.18 2001/02/27 00:09:14 zeev Exp $ */
+/* $Id: php_output.h,v 1.19 2001/03/04 01:09:36 zeev Exp $ */
 
 #ifndef PHP_OUTPUT_H
 #define PHP_OUTPUT_H
@@ -27,8 +27,8 @@
 PHPAPI int  php_body_write(const char *str, uint str_length);
 PHPAPI int  php_header_write(const char *str, uint str_length);
 PHPAPI int php_start_ob_buffer(zval *output_handler, uint chunk_size);
-PHPAPI void php_end_ob_buffer(int send_buffer);
-PHPAPI void php_end_ob_buffers(int send_buffer);
+PHPAPI void php_end_ob_buffer(zend_bool send_buffer, zend_bool just_flush);
+PHPAPI void php_end_ob_buffers(zend_bool send_buffer);
 PHPAPI int php_ob_get_buffer(pval *p);
 PHPAPI int php_ob_get_length(pval *p);
 PHPAPI void php_start_implicit_flush(void);
@@ -52,6 +52,7 @@
        int block_size;
        zval *output_handler;
        uint chunk_size;
+       int status;
 } php_ob_buffer;
 
 typedef struct _php_output_globals {
@@ -81,5 +82,8 @@
 ZEND_API extern php_output_globals output_globals;
 #endif
 
+#define PHP_OUTPUT_HANDLER_START               (1<<0)
+#define PHP_OUTPUT_HANDLER_CONT                        (1<<1)
+#define PHP_OUTPUT_HANDLER_END                 (1<<2)
 
 #endif /* PHP_OUTPUT_H */



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to