Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion frankenphp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,34 @@ static void set_thread_name(char *thread_name) {
#endif
}

/* Like php-fpm, answer a script that can't be opened instead of compiling it */
static bool open_primary_script(zend_file_handle *file_handle) {
bool orig_display_errors = PG(display_errors);
PG(display_errors) = 0;
zend_result result = zend_stream_open(file_handle);
PG(display_errors) = orig_display_errors;
if (EXPECTED(result == SUCCESS)) {
return true;
}

int open_errno = errno;
zend_try {
if (open_errno == EACCES) {
SG(sapi_headers).http_response_code = 403;
PUTS("Access denied.\n");
} else {
SG(sapi_headers).http_response_code = 404;
PUTS("No input file specified.\n");
}
}
zend_catch {}
zend_end_try();

/* a worker whose script disappeared must count as failing */
EG(exit_status) = 255;
return false;
}

static inline void reset_sandboxed_environment() {
if (sandboxed_env != NULL) {
zend_hash_release(sandboxed_env);
Expand Down Expand Up @@ -1790,7 +1818,9 @@ static void *php_thread(void *arg) {
EG(exit_status) = 0;

/* Execute the PHP script, potential bailout to zend_catch */
php_execute_script(&file_handle);
if (EXPECTED(open_primary_script(&file_handle))) {
php_execute_script(&file_handle);
}
#ifndef PHP_WIN32
if (UNEXPECTED(is_forked_child)) {
_exit(EG(exit_status));
Expand Down
8 changes: 8 additions & 0 deletions frankenphp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ func testHelloWorld(t *testing.T, opts *testOptions) {
}, opts)
}

func TestMissingScript(t *testing.T) {
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
body, resp := testGet("http://example.com/missing.php", handler, t)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
assert.Equal(t, "No input file specified.\n", body)
}, nil)
}

func TestEnvVarsInPhpIni(t *testing.T) {
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
body, _ := testGet("http://example.com/ini.php?key=opcache.enable", handler, t)
Expand Down
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestServer(t *testing.T) {
assert.Equal(t, "requests:1", body3, "could not access the worker by matcher on server 2")
assert.Equal(t, "requests:2", body4, "could not access the worker by matcher on server 2")
assert.Contains(t, body5, "I am by birth a Genevese (i not set)", "could not access the worker by path on server 2")
assert.Contains(t, body6, "Failed opening required", "worker is scoped to server 1, so should not be found on server 2")
assert.Equal(t, "No input file specified.\n", body6, "worker is scoped to server 1, so should not be found on server 2")
})

t.Run("worker_env_inheritance", func(t *testing.T) {
Expand Down
Loading