Skip to content

Commit 720695a

Browse files
committed
sapi/was: set HTTP status 404 if opening the primary script fails
All other SAPIs detect this by doing a stat() on the primary script, but that's a system call I'd like to avoid. It should be enough to open() it later from within persistent_compile_file(). That however throws a fatal error if the file could not be opened; therefore, that function needs to be patched to avoid the fatal error when opening the primary script, allowing the caller to handle the error condition. Now to be able to detect whether the status has been set already, initialize it to zero and set it in sapi_was_send_headers(). If php_execute_script() fails and "opened_path" is still NULL, we can set status 404 (instead of the previous 500 which was misleading).
1 parent 8829e37 commit 720695a

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

ext/opcache/ZendAccelerator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2098,7 +2098,7 @@ static zend_op_array *persistent_compile_file_inner(zend_file_handle *file_handl
20982098
/* open file to resolve the path */
20992099
if (file_handle->type == ZEND_HANDLE_FILENAME
21002100
&& accelerator_orig_zend_stream_open_function(file_handle) == FAILURE) {
2101-
if (!EG(exception)) {
2101+
if (!EG(exception) && !file_handle->primary_script) {
21022102
if (type == ZEND_REQUIRE) {
21032103
zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, ZSTR_VAL(filename));
21042104
} else {

sapi/was/was_main.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ static int sapi_was_send_headers(sapi_headers_struct *sapi_headers)
171171
/* special case for "--precompile" mode */
172172
return SAPI_HEADER_SENT_SUCCESSFULLY;
173173

174+
if (SG(sapi_headers).http_response_code == 0)
175+
SG(sapi_headers).http_response_code = 200;
176+
174177
if (!was_simple_status(w, SG(sapi_headers).http_response_code))
175178
return SAPI_HEADER_SEND_FAILED;
176179

@@ -445,7 +448,8 @@ static void init_request_info(struct was_simple *w, const char *request_uri)
445448
const char *content_type = was_simple_get_header(w, "content-type");
446449
SG(request_info).content_type = content_type ? content_type : "";
447450

448-
SG(sapi_headers).http_response_code = 200;
451+
/* 0 means the http_response_code has not yet been set */
452+
SG(sapi_headers).http_response_code = 0;
449453
}
450454

451455
static zend_result was_module_main(struct was_simple *w)
@@ -456,7 +460,16 @@ static zend_result was_module_main(struct was_simple *w)
456460
zend_file_handle file_handle;
457461
zend_stream_init_filename(&file_handle, SG(request_info).path_translated);
458462
file_handle.primary_script = true;
459-
php_execute_script(&file_handle);
463+
464+
if (!php_execute_script(&file_handle) &&
465+
file_handle.opened_path == NULL &&
466+
SG(sapi_headers).http_response_code == 0) {
467+
/* the primary script could not be opened and no
468+
response has been sent: change the HTTP status to
469+
"404 Not Found" */
470+
SG(sapi_headers).http_response_code = 404;
471+
}
472+
460473
zend_destroy_file_handle(&file_handle);
461474

462475
finish_was_metrics(w);

0 commit comments

Comments
 (0)