From c64a69c8b885ac138fa5971c8f784919fd003ca5 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 31 Jan 2012 09:58:26 +0000 Subject: [PATCH] MFH: r322963 fix headers print to stdout/stderr if no output written; fix crashes and invalid usage of output control in cli server while passing by --- main/main.c | 5 ++-- main/output.c | 53 ++++++++++++++++++++++++++------------- sapi/cli/php_cli_server.c | 5 ++-- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/main/main.c b/main/main.c index 7c4758a9bf..1f1101ef61 100644 --- a/main/main.c +++ b/main/main.c @@ -1738,12 +1738,11 @@ void php_request_shutdown(void *dummy) } else { php_output_end_all(TSRMLS_C); } - php_output_deactivate(TSRMLS_C); } zend_end_try(); - /* 4. Send the set HTTP headers (note: This must be done AFTER php_output_discard_all() / php_output_end_all() !!) */ + /* 4. Shutdown output layer (send the set HTTP headers, cleanup output handlers, etc.) */ zend_try { - sapi_send_headers(TSRMLS_C); + php_output_deactivate(TSRMLS_C); } zend_end_try(); /* 5. Reset max_execution_time (no longer executing php code after response sent) */ diff --git a/main/output.c b/main/output.c index b2ced15bc5..ffa7606d5d 100644 --- a/main/output.c +++ b/main/output.c @@ -103,6 +103,29 @@ static int php_output_stderr(const char *str, size_t str_len) static int (*php_output_direct)(const char *str, size_t str_len) = php_output_stderr; /* }}} */ +/* {{{ void php_output_header(TSRMLS_D) */ +static void php_output_header(TSRMLS_D) +{ + if (!SG(headers_sent)) { + if (!OG(output_start_filename)) { + if (zend_is_compiling(TSRMLS_C)) { + OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C); + OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C); + } else if (zend_is_executing(TSRMLS_C)) { + OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C); + OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C); + } +#if PHP_OUTPUT_DEBUG + fprintf(stderr, "!!! output started at: %s (%d)\n", OG(output_start_filename), OG(output_start_lineno)); +#endif + } + if (!php_header(TSRMLS_C)) { + OG(flags) |= PHP_OUTPUT_DISABLED; + } + } +} +/* }}} */ + /* {{{ void php_output_startup(void) * Set up module globals and initalize the conflict and reverse conflict hash tables */ PHPAPI void php_output_startup(void) @@ -149,6 +172,9 @@ PHPAPI void php_output_deactivate(TSRMLS_D) { php_output_handler **handler = NULL; + php_output_header(TSRMLS_C); + + OG(flags) ^= PHP_OUTPUT_ACTIVATED; OG(active) = NULL; OG(running) = NULL; @@ -161,7 +187,6 @@ PHPAPI void php_output_deactivate(TSRMLS_D) zend_stack_destroy(&OG(handlers)); } - OG(flags) ^= PHP_OUTPUT_ACTIVATED; } /* }}} */ @@ -1045,26 +1070,20 @@ static inline void php_output_op(int op, const char *str, size_t len TSRMLS_DC) } if (context.out.data && context.out.used) { + php_output_header(TSRMLS_C); + + if (!(OG(flags) & PHP_OUTPUT_DISABLED)) { #if PHP_OUTPUT_DEBUG - fprintf(stderr, "::: sapi_write('%s', %zu)\n", context.out.data, context.out.used); + fprintf(stderr, "::: sapi_write('%s', %zu)\n", context.out.data, context.out.used); #endif - if (!SG(headers_sent) && php_header(TSRMLS_C)) { - if (zend_is_compiling(TSRMLS_C)) { - OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C); - OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C); - } else if (zend_is_executing(TSRMLS_C)) { - OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C); - OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C); + sapi_module.ub_write(context.out.data, context.out.used TSRMLS_CC); + + if (OG(flags) & PHP_OUTPUT_IMPLICITFLUSH) { + sapi_flush(TSRMLS_C); } -#if PHP_OUTPUT_DEBUG - fprintf(stderr, "!!! output started at: %s (%d)\n", OG(output_start_filename), OG(output_start_lineno)); -#endif - } - sapi_module.ub_write(context.out.data, context.out.used TSRMLS_CC); - if (OG(flags) & PHP_OUTPUT_IMPLICITFLUSH) { - sapi_flush(TSRMLS_C); + + OG(flags) |= PHP_OUTPUT_SENT; } - OG(flags) |= PHP_OUTPUT_SENT; } php_output_context_dtor(&context); } diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index d702e8ad1d..ffb971618f 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -325,6 +325,9 @@ static const char *get_template_string(int code) /* {{{ */ static void append_http_status_line(smart_str *buffer, int protocol_version, int response_code, int persistent) /* {{{ */ { + if (!response_code) { + response_code = 200; + } smart_str_appendl_ex(buffer, "HTTP", 4, persistent); smart_str_appendc_ex(buffer, '/', persistent); smart_str_append_generic_ex(buffer, protocol_version / 100, persistent, int, _unsigned); @@ -1752,13 +1755,11 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server int err = 0; zval *style = NULL; zend_try { - php_output_activate(TSRMLS_C); php_output_start_user(NULL, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC); php_info_print_style(TSRMLS_C); MAKE_STD_ZVAL(style); php_output_get_contents(style TSRMLS_CC); php_output_discard(TSRMLS_C); - php_output_deactivate(TSRMLS_C); if (style && Z_STRVAL_P(style)) { char *block = pestrndup(Z_STRVAL_P(style), Z_STRLEN_P(style), 1); php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new(block, block, Z_STRLEN_P(style)); -- 2.40.0