+ Added INI entries: http.log.not_found, http.send.not_found_404
authorMichael Wallner <mike@php.net>
Thu, 1 Jun 2006 14:47:41 +0000 (14:47 +0000)
committerMichael Wallner <mike@php.net>
Thu, 1 Jun 2006 14:47:41 +0000 (14:47 +0000)
* Fixed empty Cache-Control header if not customly set with HttpResponse
* Reset Content-Disposition and Content-Type if file is not found by http_send_file() etc

docs/http.ini
http.c
http_api.c
http_response_object.c
http_send_api.c
package2.xml
php_http.h
tests/HttpResponse_005.phpt [new file with mode: 0644]

index b3124ff5ee64db91e5ff64a3a1a9735919cf8268..f325595e44bf50569b28154c447de146b7839a84 100644 (file)
@@ -8,6 +8,11 @@
 ; disable if you want php not to exit in case of redirects and cache hits
 ;http.force_exit = 0
 
+; disable if you don't want 404 Not found status messages
+; being sent, if a file attempted to be sent with
+; http_send_file() etc. cannot be found
+;http.send.not_found_404 = 0
+
 ; the hashing algorithm with wich ETags are generated (MD5, SHA1, CRC32B)
 ; if ext/hash is available, this can be set to any hash algorithm ext/hash supports
 ; MD5 is the default and fallback algorithm
@@ -28,6 +33,9 @@ http.etag.mode = "MD5"
 ; log file for redirects
 ;http.log.redirect =
 
+; log file for responses with http_send_file() etc. where the file's not been found
+;http.log.not_found =
+
 ; log file for requests with an unallowed request method
 ;http.log.allowed_methods =
 
diff --git a/http.c b/http.c
index 65c0d91c8918a3cd015fe053522f0fe0f7fde5d4..9322e3fd54feb30432c07cce7b5488a9780910f8 100644 (file)
--- a/http.c
+++ b/http.c
@@ -191,6 +191,7 @@ static inline void _http_globals_init(zend_http_globals *G TSRMLS_DC)
        G->request.time = time(NULL);
 #endif
        G->send.buffer_size = HTTP_SENDBUF_SIZE;
+       G->send.not_found_404 = 1;
        G->read_post_data = 0;
 }
 
@@ -231,6 +232,7 @@ PHP_INI_BEGIN()
        HTTP_PHP_INI_ENTRY("http.etag.mode", "MD5", PHP_INI_ALL, OnUpdateString, etag.mode)
        HTTP_PHP_INI_ENTRY("http.log.cache", "", PHP_INI_ALL, OnUpdateString, log.cache)
        HTTP_PHP_INI_ENTRY("http.log.redirect", "", PHP_INI_ALL, OnUpdateString, log.redirect)
+       HTTP_PHP_INI_ENTRY("http.log.not_found", "", PHP_INI_ALL, OnUpdateString, log.not_found)
        HTTP_PHP_INI_ENTRY("http.log.allowed_methods", "", PHP_INI_ALL, OnUpdateString, log.allowed_methods)
        HTTP_PHP_INI_ENTRY("http.log.composite", "", PHP_INI_ALL, OnUpdateString, log.composite)
        HTTP_PHP_INI_ENTRY("http.request.methods.allowed", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
@@ -241,6 +243,7 @@ PHP_INI_BEGIN()
        HTTP_PHP_INI_ENTRY("http.send.deflate.start_auto", "0", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, send.deflate.start_auto)
        HTTP_PHP_INI_ENTRY("http.send.deflate.start_flags", "0", PHP_INI_ALL, OnUpdateLong, send.deflate.start_flags)
 #endif
+       HTTP_PHP_INI_ENTRY("http.send.not_found_404", "1", PHP_INI_ALL, OnUpdateBool, send.not_found_404)
 #ifdef ZEND_ENGINE_2
        HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
 #endif
index db8f46c0097d0432408ba17d52ee500755736bbd..a8257faf5f1f3a2fc02e0f4ef189d998b112169e 100644 (file)
@@ -145,7 +145,7 @@ static void http_ob_blackhole(char *output, uint output_len, char **handled_outp
 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
 {
        if (    (send_header && (SUCCESS != http_send_status_header(status, header))) ||
-                       (!send_header && status && (SUCCESS != http_send_status(status)))) {
+                       (status && (SUCCESS != http_send_status(status)))) {
                http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
                STR_FREE(header);
                STR_FREE(body);
@@ -166,6 +166,7 @@ STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header
                case 305:       http_log(HTTP_G->log.redirect, "305-REDIRECT", header);                 break;
                case 307:       http_log(HTTP_G->log.redirect, "307-REDIRECT", header);                 break;
                case 304:       http_log(HTTP_G->log.cache, "304-CACHE", header);                               break;
+               case 404:       http_log(HTTP_G->log.not_found, "404-NOTFOUND", NULL);                  break;
                case 405:       http_log(HTTP_G->log.allowed_methods, "405-ALLOWED", header);   break;
                default:        http_log(NULL, header, body);                                                                   break;
        }
index 6f6cf4dab10bb503bf4074a5bcc670948af68cbb..32bd5f87443886aa1a2dc5df8bf08d9025ef3d75 100644 (file)
@@ -1094,7 +1094,11 @@ PHP_METHOD(HttpResponse, send)
                cctl = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(cacheControl), &cctl_p);
                
                if (Z_LVAL_P(lmod) || Z_STRLEN_P(etag)) {
-                       http_send_cache_control(Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
+                       if (Z_STRLEN_P(cctl)) {
+                               http_send_cache_control(Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
+                       } else {
+                               http_send_cache_control(HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL));
+                       }
                        if (Z_STRLEN_P(etag)) {
                                http_send_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag));
                        }
index 1a7765282ee8afa88a302f87040b5eddf19da48b..88a27e1a7684cf83d6a82b422a76cf59fcd7e90c 100644 (file)
@@ -455,6 +455,16 @@ PHP_HTTP_API STATUS _http_send_stream_ex(php_stream *file, zend_bool close_strea
        php_stream_statbuf ssb;
 
        if ((!file) || php_stream_stat(file, &ssb)) {
+               char *defct = sapi_get_default_content_type(TSRMLS_C);
+               
+               http_send_content_type(defct, strlen(defct));
+               http_send_header_string("Content-Disposition:");
+               http_error(HE_WARNING, HTTP_E_RESPONSE, "File not found; stat failed");
+               STR_FREE(defct);
+               
+               if (HTTP_G->send.not_found_404) {
+                       http_exit_ex(404, NULL, estrdup("File not found\n"), 0);
+               }
                return FAILURE;
        }
 
index 4c0c11c0233967cf85bf39830465c4efc32d67ff..a16f81f6caf0e06fc07aa1c977332034497e4908 100644 (file)
@@ -39,7 +39,10 @@ support. Parallel requests are available for PHP 5 and greater.
  </stability>
  <license>BSD, revised</license>
  <notes><![CDATA[
++ Added INI entries: http.log.not_found, http.send.not_found_404
 * Fixed build on Debian systems where access to Curl_* functions is prohibited
+* Fixed empty Cache-Control header if not customly set with HttpResponse
+* Reset Content-Disposition and Content-Type if file is not found by http_send_file() etc
 ]]></notes>
  <contents>
   <dir name="/">
@@ -197,6 +200,7 @@ support. Parallel requests are available for PHP 5 and greater.
     <file role="test" name="HttpResponse_002.phpt"/>
     <file role="test" name="HttpResponse_003.phpt"/>
     <file role="test" name="HttpResponse_004.phpt"/>
+    <file role="test" name="HttpResponse_005.phpt"/>
     <file role="test" name="match_request_header_001.phpt"/>
     <file role="test" name="negotiation_001.phpt"/>
     <file role="test" name="ob_deflatehandler_001.phpt"/>
index c45c319dee9b792c911bb1cd00b7283d4fcfc38e..ab53b85de4a8cc9d6517f1352f4d519767ca69b2 100644 (file)
@@ -89,6 +89,7 @@ ZEND_BEGIN_MODULE_GLOBALS(http)
        struct _http_globals_log {
                char *cache;
                char *redirect;
+               char *not_found;
                char *allowed_methods;
                char *composite;
        } log;
@@ -110,6 +111,7 @@ ZEND_BEGIN_MODULE_GLOBALS(http)
                        long start_flags;
                        void *stream;
                } inflate;
+               zend_bool not_found_404;
        } send;
 
        struct _http_globals_request {
diff --git a/tests/HttpResponse_005.phpt b/tests/HttpResponse_005.phpt
new file mode 100644 (file)
index 0000000..e7b2cdb
--- /dev/null
@@ -0,0 +1,25 @@
+--TEST--
+HttpResponse file not found
+--SKIPIF--
+<?php
+include 'skip.inc';
+checkcgi();
+checkmin(5.1);
+?>
+--FILE--
+<?php
+ini_set("error_reporting", 0);
+ini_set("default_mimetype", "text/plain");
+
+HttpResponse::setContentType("application/pdf");
+HttpResponse::setContentDisposition("doc.pdf");
+HttpResponse::setFile("__nonexistant__.pdf");
+HttpResponse::send();
+?>
+--EXPECTF--
+Status: 404
+X-Powered-By: PHP/%s
+Content-Type: text/plain
+Content-Disposition:
+
+File not found