From: Michael Wallner Date: Thu, 1 Jun 2006 14:47:41 +0000 (+0000) Subject: + Added INI entries: http.log.not_found, http.send.not_found_404 X-Git-Tag: RELEASE_1_0_0~10 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=1bb903f3aad484c7adf638af3365cb7c668d3281 + Added INI entries: http.log.not_found, http.send.not_found_404 * 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 --- diff --git a/docs/http.ini b/docs/http.ini index b3124ff..f325595 100644 --- a/docs/http.ini +++ b/docs/http.ini @@ -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 65c0d91..9322e3f 100644 --- 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 diff --git a/http_api.c b/http_api.c index db8f46c..a8257fa 100644 --- a/http_api.c +++ b/http_api.c @@ -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; } diff --git a/http_response_object.c b/http_response_object.c index 6f6cf4d..32bd5f8 100644 --- a/http_response_object.c +++ b/http_response_object.c @@ -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)); } diff --git a/http_send_api.c b/http_send_api.c index 1a77652..88a27e1 100644 --- a/http_send_api.c +++ b/http_send_api.c @@ -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; } diff --git a/package2.xml b/package2.xml index 4c0c11c..a16f81f 100644 --- a/package2.xml +++ b/package2.xml @@ -39,7 +39,10 @@ support. Parallel requests are available for PHP 5 and greater. BSD, revised @@ -197,6 +200,7 @@ support. Parallel requests are available for PHP 5 and greater. + diff --git a/php_http.h b/php_http.h index c45c319..ab53b85 100644 --- a/php_http.h +++ b/php_http.h @@ -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 index 0000000..e7b2cdb --- /dev/null +++ b/tests/HttpResponse_005.phpt @@ -0,0 +1,25 @@ +--TEST-- +HttpResponse file not found +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +Status: 404 +X-Powered-By: PHP/%s +Content-Type: text/plain +Content-Disposition: + +File not found