cvs: php4 / NEWS /ext/standard output.c php_output.h /main main.c /sapi/apache php_apache.c sapi_apache.c /sapi/cgi cgi_main.c

From: Date: Sat, 29 Jul 2000 14:46:10 +0000
Subject: cvs: php4 / NEWS /ext/standard output.c php_output.h /main main.c /sapi/apache php_apache.c sapi_apache.c /sapi/cgi cgi_main.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-979@lists.php.net to get a copy of this message
zeev Sat Jul 29 07:46:10 2000 EDT Modified files: /php4 NEWS /php4/ext/standard output.c php_output.h /php4/main main.c /php4/sapi/apache php_apache.c sapi_apache.c /php4/sapi/cgi cgi_main.c Log: Made ob_start() and friends reentrant. It's now possible to implement this long-requested functionality, now that output buffering is re-entrant: function eval_ret($code) { ob_start(); eval($code); $retval = ob_get_contents(); ob_end_clean(); return $retval; } Index: php4/NEWS diff -u php4/NEWS:1.378 php4/NEWS:1.379 --- php4/NEWS:1.378 Fri Jul 28 20:59:05 2000 +++ php4/NEWS Sat Jul 29 07:46:08 2000 @@ -2,6 +2,7 @@ ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 2000, Version 4.0.2 +- Improved the output-buffering functions to be re-entrant (Zeev) - Made ldap_add(), ldap_modify(), ldap_mod_add(), ldap_mod_replace() binary-safe. Original patch: Terrence Miao <terrence_miao@email.com> (Jani) - CGI aka. command line version has now an option '-l' for syntax check Index: php4/ext/standard/output.c diff -u php4/ext/standard/output.c:1.25 php4/ext/standard/output.c:1.26 --- php4/ext/standard/output.c:1.25 Sun Jul 23 18:39:49 2000 +++ php4/ext/standard/output.c Sat Jul 29 07:46:08 2000 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: output.c,v 1.25 2000/07/24 01:39:49 david Exp $ */ +/* $Id: output.c,v 1.26 2000/07/29 14:46:08 zeev Exp $ */ #include "php.h" #include "ext/standard/head.h" @@ -48,10 +48,6 @@ { OG(php_body_write) = NULL; OG(php_header_write) = NULL; - OG(ob_buffer) = NULL; - OG(ob_size) = 0; - OG(ob_block_size) = 0; - OG(ob_text_length) = 0; OG(implicit_flush) = 0; OG(output_start_filename) = NULL; OG(output_start_lineno) = 0; @@ -74,9 +70,9 @@ { OLS_FETCH(); - OG(ob_buffer) = NULL; OG(php_body_write) = php_ub_body_write; OG(php_header_write) = sapi_module.ub_write; + OG(nesting_level) = 0; } PHPAPI int php_body_write(const char *str, uint str_length) @@ -92,7 +88,7 @@ } /* Start output buffering */ -PHPAPI void php_start_ob_buffering() +PHPAPI void php_start_ob_buffer() { OLS_FETCH(); @@ -101,32 +97,49 @@ } -/* End output buffering */ -PHPAPI void php_end_ob_buffering(int send_buffer) +/* End output buffering (one level) */ +PHPAPI void php_end_ob_buffer(int send_buffer) { SLS_FETCH(); OLS_FETCH(); - if (!OG(ob_buffer)) { + if (OG(nesting_level)==0) { return; } - 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(); + 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); + } } php_ob_destroy(); } +/* End output buffering (all buffers) */ +PHPAPI void php_end_ob_buffers(int send_buffer) +{ + OLS_FETCH(); + + while (OG(nesting_level)!=0) { + php_end_ob_buffer(send_buffer); + } +} + + PHPAPI void php_start_implicit_flush() { OLS_FETCH(); - php_end_ob_buffering(1); /* Switch out of output buffering if we're in it */ + php_end_ob_buffer(1); /* Switch out of output buffering if we're in it */ OG(implicit_flush)=1; } @@ -147,11 +160,12 @@ { OLS_FETCH(); - if (OG(ob_size)<OG(ob_text_length)) { - while (OG(ob_size) <= OG(ob_text_length)) - OG(ob_size)+=OG(ob_block_size); + if (OG(active_ob_buffer).size<OG(active_ob_buffer).text_length) { + while (OG(active_ob_buffer).size <= OG(active_ob_buffer).text_length) { + OG(active_ob_buffer).size+=OG(active_ob_buffer).block_size; + } - OG(ob_buffer) = (char *) erealloc(OG(ob_buffer), OG(ob_size)+1); + OG(active_ob_buffer).buffer = (char *) erealloc(OG(active_ob_buffer).buffer, OG(active_ob_buffer).size+1); } } @@ -160,13 +174,17 @@ { OLS_FETCH(); - if (OG(ob_buffer)) { - return; + if (OG(nesting_level)>0) { + if (OG(nesting_level)==1) { /* initialize stack */ + zend_stack_init(&OG(ob_buffers)); + } + zend_stack_push(&OG(ob_buffers), &OG(active_ob_buffer), sizeof(php_ob_buffer)); } - OG(ob_block_size) = block_size; - OG(ob_size) = initial_size; - OG(ob_buffer) = (char *) emalloc(initial_size+1); - OG(ob_text_length) = 0; + OG(nesting_level)++; + OG(active_ob_buffer).block_size = block_size; + OG(active_ob_buffer).size = initial_size; + OG(active_ob_buffer).buffer = (char *) emalloc(initial_size+1); + OG(active_ob_buffer).text_length = 0; } @@ -174,10 +192,20 @@ { OLS_FETCH(); - if (OG(ob_buffer)) { - efree(OG(ob_buffer)); - OG(ob_buffer) = NULL; + if (OG(nesting_level)>0) { + efree(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)); + } + } } + OG(nesting_level)--; } @@ -187,11 +215,11 @@ int original_ob_text_length; OLS_FETCH(); - original_ob_text_length=OG(ob_text_length); + original_ob_text_length=OG(active_ob_buffer).text_length; - OG(ob_text_length) += text_length; + OG(active_ob_buffer).text_length += text_length; php_ob_allocate(); - target = OG(ob_buffer)+original_ob_text_length; + target = OG(active_ob_buffer).buffer+original_ob_text_length; memcpy(target, text, text_length); target[text_length]=0; } @@ -202,7 +230,7 @@ char *p, *start; OLS_FETCH(); - OG(ob_text_length) += text_length; + OG(active_ob_buffer).text_length += text_length; php_ob_allocate(); /* php_ob_allocate() may change OG(ob_buffer), so we can't initialize p&start earlier */ @@ -213,7 +241,7 @@ p[text_length] = *p; } memcpy(OG(ob_buffer), text, text_length); - OG(ob_buffer)[OG(ob_text_length)]=0; + OG(ob_buffer)[OG(active_ob_buffer).text_length]=0; } #endif @@ -222,7 +250,7 @@ OLS_FETCH(); /* header_write is a simple, unbuffered output function */ - OG(php_body_write)(OG(ob_buffer), OG(ob_text_length)); + OG(php_body_write)(OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length); } @@ -231,12 +259,12 @@ { OLS_FETCH(); - if (!OG(ob_buffer)) { + if (OG(nesting_level)==0) { return FAILURE; } p->type = IS_STRING; - p->value.str.val = estrndup(OG(ob_buffer), OG(ob_text_length)); - p->value.str.len = OG(ob_text_length); + p->value.str.val = estrndup(OG(active_ob_buffer).buffer, OG(active_ob_buffer).text_length); + p->value.str.len = OG(active_ob_buffer).text_length; return SUCCESS; } @@ -321,7 +349,7 @@ Turn on Output Buffering */ PHP_FUNCTION(ob_start) { - php_start_ob_buffering(); + php_start_ob_buffer(); } /* }}} */ @@ -330,7 +358,7 @@ Flush (send) the output buffer, and turn off output buffering */ PHP_FUNCTION(ob_end_flush) { - php_end_ob_buffering(1); + php_end_ob_buffer(1); } /* }}} */ @@ -339,7 +367,7 @@ Clean (erase) the output buffer, and turn off output buffering */ PHP_FUNCTION(ob_end_clean) { - php_end_ob_buffering(0); + php_end_ob_buffer(0); } /* }}} */ Index: php4/ext/standard/php_output.h diff -u php4/ext/standard/php_output.h:1.12 php4/ext/standard/php_output.h:1.13 --- php4/ext/standard/php_output.h:1.12 Sun Jul 23 18:39:49 2000 +++ php4/ext/standard/php_output.h Sat Jul 29 07:46:08 2000 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_output.h,v 1.12 2000/07/24 01:39:49 david Exp $ */ +/* $Id: php_output.h,v 1.13 2000/07/29 14:46:08 zeev Exp $ */ #ifndef PHP_OUTPUT_H #define PHP_OUTPUT_H @@ -26,8 +26,9 @@ 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_buffering(void); -PHPAPI void php_end_ob_buffering(int send_buffer); +PHPAPI void php_start_ob_buffer(void); +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); PHPAPI void php_start_implicit_flush(void); PHPAPI void php_end_implicit_flush(void); @@ -42,16 +43,22 @@ PHP_GINIT_FUNCTION(output); -typedef struct { +typedef struct _php_ob_buffer { + char *buffer; + uint size; + uint text_length; + int block_size; +} php_ob_buffer; + +typedef struct _php_output_globals { int (*php_body_write)(const char *str, uint str_length); /* string output */ int (*php_header_write)(const char *str, uint str_length); /* unbuffer string output */ - char *ob_buffer; - uint ob_size; - uint ob_block_size; - uint ob_text_length; + php_ob_buffer active_ob_buffer; unsigned char implicit_flush; char *output_start_filename; int output_start_lineno; + zend_stack ob_buffers; + int nesting_level; } php_output_globals; Index: php4/main/main.c diff -u php4/main/main.c:1.285 php4/main/main.c:1.286 --- php4/main/main.c:1.285 Sat Jul 29 03:48:57 2000 +++ php4/main/main.c Sat Jul 29 07:46:08 2000 @@ -19,7 +19,7 @@ */ -/* $Id: main.c,v 1.285 2000/07/29 10:48:57 zeev Exp $ */ +/* $Id: main.c,v 1.286 2000/07/29 14:46:08 zeev Exp $ */ #include <stdio.h> @@ -630,7 +630,7 @@ } if (PG(output_buffering)) { - php_start_ob_buffering(); + php_start_ob_buffer(); } else if (PG(implicit_flush)) { php_start_implicit_flush(); } @@ -659,7 +659,7 @@ } sapi_send_headers(); - php_end_ob_buffering(SG(request_info).headers_only?0:1); + php_end_ob_buffers(SG(request_info).headers_only?0:1); php_call_shutdown_functions(); Index: php4/sapi/apache/php_apache.c diff -u php4/sapi/apache/php_apache.c:1.17 php4/sapi/apache/php_apache.c:1.18 --- php4/sapi/apache/php_apache.c:1.17 Thu Jun 15 16:45:04 2000 +++ php4/sapi/apache/php_apache.c Sat Jul 29 07:46:08 2000 @@ -17,7 +17,7 @@ | David Sklar <sklar@student.net> | +----------------------------------------------------------------------+ */ -/* $Id: php_apache.c,v 1.17 2000/06/15 23:45:04 andi Exp $ */ +/* $Id: php_apache.c,v 1.18 2000/07/29 14:46:08 zeev Exp $ */ #define NO_REGEX_EXTRA_H @@ -320,7 +320,7 @@ RETURN_FALSE; } - php_end_ob_buffering(1); + php_end_ob_buffers(1); php_header(); if (run_sub_req(rr)) { Index: php4/sapi/apache/sapi_apache.c diff -u php4/sapi/apache/sapi_apache.c:1.18 php4/sapi/apache/sapi_apache.c:1.19 --- php4/sapi/apache/sapi_apache.c:1.18 Mon Jul 10 00:57:01 2000 +++ php4/sapi/apache/sapi_apache.c Sat Jul 29 07:46:08 2000 @@ -19,7 +19,7 @@ | Stig Bakken <ssb@fast.no> | +----------------------------------------------------------------------+ */ -/* $Id: sapi_apache.c,v 1.18 2000/07/10 07:57:01 thies Exp $ */ +/* $Id: sapi_apache.c,v 1.19 2000/07/29 14:46:08 zeev Exp $ */ #define NO_REGEX_EXTRA_H #ifdef WIN32 @@ -89,7 +89,7 @@ } php_header(); /* Make sure headers have been sent */ - php_end_ob_buffering(1); + php_end_ob_buffers(1); return (OK); } Index: php4/sapi/cgi/cgi_main.c diff -u php4/sapi/cgi/cgi_main.c:1.63 php4/sapi/cgi/cgi_main.c:1.64 --- php4/sapi/cgi/cgi_main.c:1.63 Fri Jul 28 08:21:20 2000 +++ php4/sapi/cgi/cgi_main.c Sat Jul 29 07:46:09 2000 @@ -485,7 +485,7 @@ php_output_startup(); SG(headers_sent) = 1; php_cgi_usage(argv[0]); - php_end_ob_buffering(1); + php_end_ob_buffers(1); exit(1); break; } @@ -556,7 +556,7 @@ php_output_startup(); SG(headers_sent) = 1; php_cgi_usage(argv[0]); - php_end_ob_buffering(1); + php_end_ob_buffers(1); exit(1); break; @@ -606,7 +606,7 @@ SG(headers_sent) = 1; } php_printf("%s\n", PHP_VERSION); - php_end_ob_buffering(1); + php_end_ob_buffers(1); exit(1); break;

« previous php.cvs (#979) next »