cvs: php4 /ext/standard output.c php_output.h /main main.c

From: Date: Sat, 02 Sep 2000 18:03:58 +0000
Subject: cvs: php4 /ext/standard output.c php_output.h /main main.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-1504@lists.php.net to get a copy of this message
zeev Sat Sep 2 11:03:58 2000 EDT Modified files: /php4/ext/standard output.c php_output.h /php4/main main.c Log: @- Added support for an optional output handler function for output @ buffering. This enables transparent rendering of XML through XSL, @ transparent compression, etc. (Zeev) Index: php4/ext/standard/output.c diff -u php4/ext/standard/output.c:1.27 php4/ext/standard/output.c:1.28 --- php4/ext/standard/output.c:1.27 Thu Aug 24 20:10:42 2000 +++ php4/ext/standard/output.c Sat Sep 2 11:03:58 2000 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: output.c,v 1.27 2000/08/25 03:10:42 ssb Exp $ */ +/* $Id: output.c,v 1.28 2000/09/02 18:03:58 zeev Exp $ */ #include "php.h" #include "ext/standard/head.h" @@ -29,13 +29,12 @@ static int php_ub_body_write_no_header(const char *str, uint str_length); static int php_b_body_write(const char *str, uint str_length); -static void php_ob_init(uint initial_size, uint block_size); +static void php_ob_init(uint initial_size, uint block_size, zval *output_handler); static void php_ob_destroy(void); static void php_ob_append(const char *text, uint text_length); #if 0 static void php_ob_prepend(const char *text, uint text_length); #endif -static inline void php_ob_send(void); #ifdef ZTS @@ -51,6 +50,7 @@ OG(implicit_flush) = 0; OG(output_start_filename) = NULL; OG(output_start_lineno) = 0; + OG(lock) = 0; } @@ -88,38 +88,76 @@ } /* Start output buffering */ -PHPAPI void php_start_ob_buffer() +PHPAPI int php_start_ob_buffer(zval *output_handler) { OLS_FETCH(); - php_ob_init(4096, 1024); + if (OG(lock)) { + return FAILURE; + } + php_ob_init(4096, 1024, output_handler); OG(php_body_write) = php_b_body_write; + return SUCCESS; } /* End output buffering (one level) */ PHPAPI void php_end_ob_buffer(int send_buffer) { + char *final_buffer=NULL; + int final_buffer_length; + zval *alternate_buffer=NULL; SLS_FETCH(); OLS_FETCH(); if (OG(nesting_level)==0) { return; } + + if (OG(active_ob_buffer).output_handler) { + zval **params[1]; + zval *orig_buffer; + CLS_FETCH(); + + ALLOC_INIT_ZVAL(orig_buffer); + orig_buffer->value.str.val = OG(active_ob_buffer).buffer; + orig_buffer->value.str.len = OG(active_ob_buffer).text_length; + orig_buffer->type = IS_STRING; + orig_buffer->refcount=2; /* don't let call_user_function() destroy our buffer */ + + params[0] = &orig_buffer; + 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) { + convert_to_string_ex(&alternate_buffer); + final_buffer = alternate_buffer->value.str.val; + final_buffer_length = alternate_buffer->value.str.len; + } + OG(lock) = 0; + zval_ptr_dtor(&OG(active_ob_buffer).output_handler); + if (orig_buffer->refcount==2) { /* free the zval */ + FREE_ZVAL(orig_buffer); + } else { + orig_buffer->refcount--; + } + } + + if (!final_buffer) { + final_buffer = OG(active_ob_buffer).buffer; + final_buffer_length = OG(active_ob_buffer).text_length; + } if (OG(nesting_level)==1) { /* end buffering */ if (SG(headers_sent) && !SG(request_info).headers_only) { OG(php_body_write) = php_ub_body_write_no_header; } else { OG(php_body_write) = php_ub_body_write; } - if (send_buffer) { - php_ob_send(); - } - } else { /* only flush the buffer, if necessary */ - if (send_buffer) { - OG(php_body_write)(OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length); - } } + if (send_buffer) { + OG(php_body_write)(final_buffer, final_buffer_length); + } + if (alternate_buffer) { + zval_ptr_dtor(&alternate_buffer); + } php_ob_destroy(); } @@ -170,7 +208,7 @@ } -static void php_ob_init(uint initial_size, uint block_size) +static void php_ob_init(uint initial_size, uint block_size, zval *output_handler) { OLS_FETCH(); @@ -185,6 +223,7 @@ OG(active_ob_buffer).size = initial_size; OG(active_ob_buffer).buffer = (char *) emalloc(initial_size+1); OG(active_ob_buffer).text_length = 0; + OG(active_ob_buffer).output_handler = output_handler; } @@ -245,15 +284,7 @@ } #endif -static inline void php_ob_send() -{ - OLS_FETCH(); - - /* header_write is a simple, unbuffered output function */ - OG(php_body_write)(OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length); -} - /* Return the current output buffer */ int php_ob_get_buffer(pval *p) { @@ -362,7 +393,29 @@ Turn on Output Buffering */ PHP_FUNCTION(ob_start) { - php_start_ob_buffer(); + zval *output_handler; + + switch (ZEND_NUM_ARGS()) { + case 0: + output_handler = NULL; + break; + case 1: { + zval **output_handler_p; + + if (zend_get_parameters_ex(1, &output_handler_p)==FAILURE) { + RETURN_FALSE; + } + SEPARATE_ZVAL(output_handler_p); + output_handler = *output_handler_p; + output_handler->refcount++; + } + break; + } + if (php_start_ob_buffer(output_handler)==FAILURE) { + php_error(E_WARNING, "Cannot use output buffering in output buffering display handlers"); + RETURN_FALSE; + } + RETURN_TRUE; } /* }}} */ Index: php4/ext/standard/php_output.h diff -u php4/ext/standard/php_output.h:1.14 php4/ext/standard/php_output.h:1.15 --- php4/ext/standard/php_output.h:1.14 Thu Aug 24 20:10:42 2000 +++ php4/ext/standard/php_output.h Sat Sep 2 11:03:58 2000 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_output.h,v 1.14 2000/08/25 03:10:42 ssb Exp $ */ +/* $Id: php_output.h,v 1.15 2000/09/02 18:03:58 zeev Exp $ */ #ifndef PHP_OUTPUT_H #define PHP_OUTPUT_H @@ -26,7 +26,7 @@ PHPAPI void php_output_startup(void); PHPAPI int php_body_write(const char *str, uint str_length); PHPAPI int php_header_write(const char *str, uint str_length); -PHPAPI void php_start_ob_buffer(void); +PHPAPI int php_start_ob_buffer(zval *output_handler); PHPAPI void php_end_ob_buffer(int send_buffer); PHPAPI void php_end_ob_buffers(int send_buffer); PHPAPI int php_ob_get_buffer(pval *p); @@ -50,6 +50,7 @@ uint size; uint text_length; int block_size; + zval *output_handler; } php_ob_buffer; typedef struct _php_output_globals { @@ -61,6 +62,7 @@ int output_start_lineno; zend_stack ob_buffers; int nesting_level; + zend_bool lock; } php_output_globals; Index: php4/main/main.c diff -u php4/main/main.c:1.310 php4/main/main.c:1.311 --- php4/main/main.c:1.310 Fri Sep 1 05:06:52 2000 +++ php4/main/main.c Sat Sep 2 11:03:58 2000 @@ -19,7 +19,7 @@ */ -/* $Id: main.c,v 1.310 2000/09/01 12:06:52 zeev Exp $ */ +/* $Id: main.c,v 1.311 2000/09/02 18:03:58 zeev Exp $ */ #include <stdio.h> @@ -607,7 +607,7 @@ } if (PG(output_buffering)) { - php_start_ob_buffer(); + php_start_ob_buffer(NULL); } else if (PG(implicit_flush)) { php_start_implicit_flush(); }

« previous php.cvs (#1504) next »