]> granicus.if.org Git - php/commitdiff
fix headers print to stdout/stderr if no output written; need to make some more tests...
authorMichael Wallner <mike@php.net>
Tue, 31 Jan 2012 08:51:24 +0000 (08:51 +0000)
committerMichael Wallner <mike@php.net>
Tue, 31 Jan 2012 08:51:24 +0000 (08:51 +0000)
main/main.c
main/output.c
sapi/cli/php_cli_server.c

index 7c4758a9bfbc3a12a916d82a98ff80cd78c7ec2e..1f1101ef611e83a59f755deccecc5f221c9913a4 100644 (file)
@@ -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) */
index 4dba4122cc7bfdf920b4ee8dd2b228a4a01b6ca0..f0a130fcc36869178ed4a251282066a4a8dadce8 100644 (file)
@@ -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);
 }
index d702e8ad1da004222d24a9845c1ad9b936a828d9..ffb971618fae03344d479c02c092a7b80f5d52f3 100644 (file)
@@ -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));