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;
}
This commit is contained in:
Zeev Suraski 2000-07-29 14:46:09 +00:00
parent d8a4278ab4
commit 52ff887db5
9 changed files with 176 additions and 105 deletions

1
NEWS
View File

@ -2,6 +2,7 @@ PHP 4.0 NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? 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

View File

@ -48,10 +48,6 @@ static void php_output_init_globals(OLS_D)
{
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 @@ PHPAPI void php_output_startup()
{
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 @@ PHPAPI int php_header_write(const char *str, uint str_length)
}
/* Start output buffering */
PHPAPI void php_start_ob_buffering()
PHPAPI void php_start_ob_buffer()
{
OLS_FETCH();
@ -101,32 +97,49 @@ PHPAPI void php_start_ob_buffering()
}
/* 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 @@ static inline void php_ob_allocate(void)
{
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 @@ static void php_ob_init(uint initial_size, uint block_size)
{
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 @@ static void php_ob_destroy()
{
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 @@ static void php_ob_append(const char *text, uint text_length)
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 @@ static void php_ob_prepend(const char *text, uint text_length)
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 @@ static void php_ob_prepend(const char *text, uint text_length)
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 @@ static inline void php_ob_send()
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 @@ int php_ob_get_buffer(pval *p)
{
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 @@ static int php_ub_body_write(const char *str, uint str_length)
Turn on Output Buffering */
PHP_FUNCTION(ob_start)
{
php_start_ob_buffering();
php_start_ob_buffer();
}
/* }}} */
@ -330,7 +358,7 @@ PHP_FUNCTION(ob_start)
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 @@ PHP_FUNCTION(ob_end_flush)
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);
}
/* }}} */

View File

@ -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_FUNCTION(ob_implicit_flush);
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;

View File

@ -630,7 +630,7 @@ int php_request_startup(CLS_D ELS_DC PLS_DC SLS_DC)
}
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 @@ void php_request_shutdown(void *dummy)
}
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();

View File

@ -48,10 +48,6 @@ static void php_output_init_globals(OLS_D)
{
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 @@ PHPAPI void php_output_startup()
{
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 @@ PHPAPI int php_header_write(const char *str, uint str_length)
}
/* Start output buffering */
PHPAPI void php_start_ob_buffering()
PHPAPI void php_start_ob_buffer()
{
OLS_FETCH();
@ -101,32 +97,49 @@ PHPAPI void php_start_ob_buffering()
}
/* 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 @@ static inline void php_ob_allocate(void)
{
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 @@ static void php_ob_init(uint initial_size, uint block_size)
{
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 @@ static void php_ob_destroy()
{
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 @@ static void php_ob_append(const char *text, uint text_length)
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 @@ static void php_ob_prepend(const char *text, uint text_length)
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 @@ static void php_ob_prepend(const char *text, uint text_length)
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 @@ static inline void php_ob_send()
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 @@ int php_ob_get_buffer(pval *p)
{
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 @@ static int php_ub_body_write(const char *str, uint str_length)
Turn on Output Buffering */
PHP_FUNCTION(ob_start)
{
php_start_ob_buffering();
php_start_ob_buffer();
}
/* }}} */
@ -330,7 +358,7 @@ PHP_FUNCTION(ob_start)
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 @@ PHP_FUNCTION(ob_end_flush)
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);
}
/* }}} */

View File

@ -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_FUNCTION(ob_implicit_flush);
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;

View File

@ -320,7 +320,7 @@ PHP_FUNCTION(virtual)
RETURN_FALSE;
}
php_end_ob_buffering(1);
php_end_ob_buffers(1);
php_header();
if (run_sub_req(rr)) {

View File

@ -89,7 +89,7 @@ int apache_php_module_main(request_rec *r, int display_source_mode CLS_DC ELS_DC
}
php_header(); /* Make sure headers have been sent */
php_end_ob_buffering(1);
php_end_ob_buffers(1);
return (OK);
}

View File

@ -485,7 +485,7 @@ any .htaccess restrictions anywhere on your site you can leave doc_root undefine
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 @@ any .htaccess restrictions anywhere on your site you can leave doc_root undefine
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 @@ any .htaccess restrictions anywhere on your site you can leave doc_root undefine
SG(headers_sent) = 1;
}
php_printf("%s\n", PHP_VERSION);
php_end_ob_buffering(1);
php_end_ob_buffers(1);
exit(1);
break;