Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Custom 404 error handler. Response caching. #5697

Closed
iRedds opened this issue Feb 15, 2022 · 7 comments · Fixed by #5703
Closed

Bug: Custom 404 error handler. Response caching. #5697

iRedds opened this issue Feb 15, 2022 · 7 comments · Fixed by #5703
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@iRedds
Copy link
Collaborator

iRedds commented Feb 15, 2022

PHP Version

7.3

CodeIgniter4 Version

4.1.8

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

No response

What happened?

If the custom 404 error handler does not output data to the buffer, then an empty body is returned.

Steps to Reproduce

$routes->set404Override('App\Controllers\Home::err');

public function err()
{
    return \Config\Services::response()->setJSON([1, 2, 3]);
}

Expected Output

[
    1, 
    2,
    3,
]

Anything else?

In the CodeIgniter::display404errors() handler, the result of the CodeIgniter::runController() method is not passed to the CodeIgniter::gatherOutput() method.

$controller = $this->createController();
$this->runController($controller);
}
unset($override);
$cacheConfig = new Cache();
$this->gatherOutput($cacheConfig);
$this->sendResponse();

CodeIgniter::gatherOutput() If the data is not passed and there was no output to the buffer, then the response body is reset to an empty string.

$this->output = ob_get_contents(); // empty string
$this->response->setBody($this->output); // reset

I think that we need to consider not only what the CodeIgniter::runController() method returns, but also what is contained in the response body.
That is, it is necessary to define a priority between echo, return and Response::setBody().

@iRedds iRedds added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 15, 2022
@kenjis
Copy link
Member

kenjis commented Feb 17, 2022

That is, it is necessary to define a priority between echo, return and Response::setBody().

The current gatherOutput() is:

  • echoed output + (returned response's body or returned string)

It seems no problem.

@iRedds
Copy link
Collaborator Author

iRedds commented Feb 17, 2022

I don't think this is the correct approach. Although I may be wrong.

if (is_string($returned)) {
$this->output .= $returned;
}

@kenjis
Copy link
Member

kenjis commented Feb 17, 2022

It may not be correct, but what's wrong?

@iRedds
Copy link
Collaborator Author

iRedds commented Feb 17, 2022

I mean the concatenation of the data from the buffer and the returned string.
It seems to me that it is better to use "protection against the fool". And in the future, refuse to capture the buffer.

@kenjis
Copy link
Member

kenjis commented Feb 17, 2022

Do you think the following is better?

  • echoed output
  • or returned response's body
  • or returned string

I don't understand why it is "protection against the fool".

@iRedds
Copy link
Collaborator Author

iRedds commented Feb 17, 2022

For the same reason why you write a return type.
This makes the code smaller. This simplifies the code.
I would prefer just return ResponseInterface =)

@lonnieezell
Copy link
Member

I think it's dangerous to change that. I don't recall the reasoning it was there exactly but if it would be changed it would need a good chunk of additional tests to ensure all of the varieties of responses are covered, no matter how crazy they might be, so we don't break a less experienced coder's mess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants