]> granicus.if.org Git - php/commitdiff
Fixed bug #42848 (Status: header incorrect under FastCGI)
authorDmitry Stogov <dmitry@php.net>
Thu, 1 Nov 2007 15:23:14 +0000 (15:23 +0000)
committerDmitry Stogov <dmitry@php.net>
Thu, 1 Nov 2007 15:23:14 +0000 (15:23 +0000)
NEWS
sapi/cgi/cgi_main.c
sapi/cgi/tests/003.phpt
sapi/cgi/tests/008.phpt

diff --git a/NEWS b/NEWS
index d0482c8a9742b0a8f142926c5721fbfb40e001c5..51e153901dd1d842102538968e97ea4a0edcd798 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -195,6 +195,7 @@ PHP                                                                        NEWS
 - Fixed PECL bug #11216 (crash in ZipArchive::addEmptyDir when a directory
   already exists). (Pierre)
 
+- Fixed bug #42848 (Status: header incorrect under FastCGI). (Dmitry)
 - Fixed bug #42368 (Incorrect error message displayed by pg_escape_string).
   (Ilia)
 - Fixed bug #42365 (glob() crashes and/or accepts way too many flags).
index 69461c901829e050b76b003b1d308b2c26e99289..cba37fe19a5ebaea979b0b42caa293135872e036 100644 (file)
@@ -311,6 +311,52 @@ static void sapi_cgibin_flush(void *server_context)
 
 #define SAPI_CGI_MAX_HEADER_LENGTH 1024
 
+typedef struct _http_error {
+  int code;
+  const char* msg;
+} http_error;
+
+static const http_error http_error_codes[] = {
+       {100, "Continue"},
+       {101, "Switching Protocols"},
+       {200, "OK"},
+       {201, "Created"},
+       {202, "Accepted"},
+       {203, "Non-Authoritative Information"},
+       {204, "No Content"},
+       {205, "Reset Content"},
+       {206, "Partial Content"},
+       {300, "Multiple Choices"},
+       {301, "Moved Permanently"},
+       {302, "Moved Temporarily"},
+       {303, "See Other"},
+       {304, "Not Modified"},
+       {305, "Use Proxy"},
+       {400, "Bad Request"},
+       {401, "Unauthorized"},
+       {402, "Payment Required"},
+       {403, "Forbidden"},
+       {404, "Not Found"},
+       {405, "Method Not Allowed"},
+       {406, "Not Acceptable"},
+       {407, "Proxy Authentication Required"},
+       {408, "Request Time-out"},
+       {409, "Conflict"},
+       {410, "Gone"},
+       {411, "Length Required"},
+       {412, "Precondition Failed"},
+       {413, "Request Entity Too Large"},
+       {414, "Request-URI Too Large"},
+       {415, "Unsupported Media Type"},
+       {500, "Internal Server Error"},
+       {501, "Not Implemented"},
+       {502, "Bad Gateway"},
+       {503, "Service Unavailable"},
+       {504, "Gateway Time-out"},
+       {505, "HTTP Version not supported"},
+       {0,   NULL}
+};
+
 static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
 {
        char buf[SAPI_CGI_MAX_HEADER_LENGTH];
@@ -324,6 +370,7 @@ static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
        if (CGIG(nph) || SG(sapi_headers).http_response_code != 200)
        {
                int len;
+               zend_bool has_status = 0;
 
                if (CGIG(rfc2616_headers) && SG(sapi_headers).http_status_line) {
                        len = slprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH,
@@ -342,11 +389,35 @@ static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
                            strncasecmp(SG(sapi_headers).http_status_line, "HTTP/", 5) == 0) {
                                len = slprintf(buf, sizeof(buf), "Status:%s\r\n", s);
                        } else {
-                               len = slprintf(buf, sizeof(buf), "Status: %d\r\n", SG(sapi_headers).http_response_code);
+                               h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
+                               while (h) {
+                                       if (h->header_len > sizeof("Status:")-1 &&
+                                           strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0) {
+                                               has_status = 1;
+                                               break;
+                                       }
+                                       h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
+                               }
+                               if (!has_status) {
+                                       http_error *err = (http_error*)http_error_codes;
+
+                                       while (err->code != 0) {
+                                           if (err->code == SG(sapi_headers).http_response_code) {
+                                                       break;
+                                               }
+                                               err++;
+                                       }
+                                       if (err->msg) {
+                                               len = slprintf(buf, sizeof(buf), "Status: %d %s\r\n", SG(sapi_headers).http_response_code, err->msg);
+                                       } else {
+                                               len = slprintf(buf, sizeof(buf), "Status: %d\r\n", SG(sapi_headers).http_response_code);
+                                       }
+                               }
                        }
                }
-
-               PHPWRITE_H(buf, len);
+               if (!has_status) {
+                       PHPWRITE_H(buf, len);
+               }
        }
 
        h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
index 0da3ca8602a352107049831badd802bd04f1e714..ea418d104ca507585d46eb07989c9f2da8d8dfdc 100644 (file)
@@ -48,7 +48,7 @@ Content-type: text/html
 <?php
  class test { public $var = "test"; private $pri; function foo() { } } ?>
 "
-string(%d) "Status: 404
+string(%d) "Status: 404 Not Found
 X-Powered-By: PHP/%s
 Content-type: text/html
 
index d541ca430d64b21c9b1697978ef6b89c1a4bf8d0..40140e87925e40a106c499779535c02b02d7b250 100644 (file)
@@ -45,7 +45,7 @@ Content-type: text/html
 <br /><span style="color: #0000BB">&lt;?php<br />$test&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"var"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//var<br />/*&nbsp;test&nbsp;class&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">test&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$var&nbsp;</span><span style="color: #007700">=&nbsp;array();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">Test&nbsp;$arg</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$o&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;<br /></span>
 </span>
 </code>"
-string(%d) "Status: 404
+string(%d) "Status: 404 Not Found
 X-Powered-By: PHP/%s
 Content-type: text/html