Respect explicit response status code when Location response header is given (PHP SAPI)#191
Respect explicit response status code when Location response header is given (PHP SAPI)#191
Location response header is given (PHP SAPI)#191Conversation
|
Would you mind, adding the |
|
Hey @jkrzefski, thanks for bringing this up 👍 First up I want to take the time to tell you that I really appreciate the amount of information you presented, really helps understanding where you're coming from and what your intentions are, nice job 💪 Can you also add tests to confirm that your changes work as expected. In this case you need to add some acceptance tests inside the |
|
Hi @SimonFrings, thank you for your feedback. I will have a look at the tests and add my case there. |
|
@SimonFrings I added some test cases and ran them with and without my change. Prior to my change, the case for status 202 fails. With my change it passes. Is this what you had in mind for the tests? |
SimonFrings
left a comment
There was a problem hiding this comment.
@jkrzefski I tested this with PHP's development web server and agree with you that if you explicitly define the status code, it shouldn't be changed by PHP in this case. I can also confirm that this is covered by your tests, you got my approval 👍
Location response header is given (PHP SAPI)
clue
left a comment
There was a problem hiding this comment.
@jkrzefski Thank you very much for this high-quality PR! This is indeed a nasty one and I'm glad we could come up with some test cases to confirm that rearranging a single line of code actually addresses this subtle behavior in PHP. Keep it up! 👍
Let's say, this is your code:
So, why does this happen?
When you use
header(...)to set theLocationheader, PHP will update the status code to 302. This behavior is even mentioned in the official documentation: PHP: header - ManualHere is the corresponding code in
php/php-src: https://github.com/php/php-src/blob/php-8.1.11/main/SAPI.c#L806-L821Should this happen?
I think, this behavior is more confusing than it is helpful. It should not be the decision of PHP to override my status code. According to RFC 9110, the
Locationheader has a defined behavior for the status codes 201 and 3xx.But that is not an exclusive definition. Any HTTP response may contain a
Locationheader, but the expected behavior is not defined by this RFC. That means, it can be used by a proprietary API definition for other status codes.What does this change do?
The status code is now set after setting all other headers. This will override any status code that has priorly been set by PHP. So now my choice of status code is respected.