Bug #70273 [Nab]: header(): Setting the Location header will set a status code of 202 to 302.

From: Date: Wed, 19 Aug 2015 15:04:17 +0000
Subject: Bug #70273 [Nab]: header(): Setting the Location header will set a status code of 202 to 302.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195345@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70273&edit=1

 ID:                 70273
 User updated by:    jeff at nd4c dot com
 Reported by:        jeff at nd4c dot com
 Summary:            header(): Setting the Location header will set a
                     status code of 202 to 302.
 Status:             Not a bug
 Type:               Bug
 Package:            HTTP related
 Operating System:   Any
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

@cmb


7.1.2.  Location

   The "Location" header field is used in some responses to refer to a
   specific resource in relation to the response.  The type of
   relationship is defined by the combination of request method and
   status code semantics.

Neither is there mention of 302.  The references to 201 and 3xx are explaining how the Location
header would typically be used IF the 201 and 3xx status codes are used.

Also, to be clear, 3xx does NOT mean 302 specifically.  So why force the status code to 302?  Where
does it say that?

I repeat:

I have not found any documentation, RFC or the like to infer that whenever a Location header is set
it MUST have a status of 302.  I have not found anything to suggest that whenever a Location header
is set it SHOULD have a status of 302.  It definitely COULD, but it COULD also have ANY OTHER status
code. There is no documentation to state otherwise.  It's true that a status code of 302 SHOULD
(or even MUST) have a Location header, but that does not logically infer the reverse.

So why does the PHP header function force a 302 as a 'magic' side-effect?


Previous Comments:
------------------------------------------------------------------------
[2015-08-19 10:34:57] ab@php.net

@jeff, 

http://www.ietf.org/rfc/rfc2119.txt 

"3. SHOULD   This word, or the adjective "RECOMMENDED", mean that there
   may exist valid reasons in particular circumstances to ignore a
   particular item, but the full implications must be understood and
   carefully weighed before choosing a different course."

Taking in account also the to paragraphs quoted by @cmb and @requinix

- some status should be set, in the PHP implementation it's set 302 by default. In PHP
implementation the "particular item" is not ignored
- there is no single definition about status 202 and Location header combination
- the status can be overridden in PHP implementation, which gives the full freedom to set it to
whatever a programmer wants

Further - the REST specification has chosen to set the status 202+Location under some circumstances.
That is perfectly valid with the HTTP specification too, because the Location header is not bound to
any specific status. 

However where your position is not valid - the PHP implementation is a generic one, it is not
dedicated specifically to REST, AJAX, anything else. But, the PHP implementation makes possible to
implement whatever a programmer needs. It were valid if you were filing a ticket about a REST
specific implementation, maybe. Or if there were no possibility to override the status. Given this
and also with the regard to the backward compatibility it doesn't look like a bug. There are
many REST frameworks which could be probably more of use for you, if you don't want to
implement it yourself.

Thanks.

------------------------------------------------------------------------
[2015-08-18 16:07:00] jeff at nd4c dot com

It's definitely a bug. Whether it will be fixed or not is another question.

1. https://bugs.php.net/bug.php?id=25044 was
fixed, but IMO not correctly.  The proper fix would have been to remove the 'magic'
side-effect of setting a Location header. 

Quote from bug report 25044:
It may even be appropriate to not change the status for any value other than 200 (the default).  As
a result, if a user explicitly sets the status to something other than 200, then setting the
Location header should not change that value again (even if the combination of the status and header
do not make sense).  The reasoning here is that HTTP status codes are extensible, meaning that new
specifications can add new status codes that may use the Location header.  If only testing for the
above list, new codes would be overlooked and therefore automatically changed.

2. https://bugs.php.net/bug.php?id=51749 turned
into a flame war with the developers - obviously it was rejected as not a bug. ;-)  The developer
stated the following:
 [2010-05-18 09:47 UTC] mike@php.net
...
You are writing *pages* about code that's *years* old and worked that way for a long long time,
and there's very little chance to become that changed. You know, PHP is an acronym for BC, or
was it the other way round...

I find that a little disheartening.  I'm guessing that quote's been pasted up in many
closed source companies' boardrooms! ;-)

I have not found any documentation, RFC or the like to infer that whenever a Location header is set
it MUST have a status of 302.  I have not found anything to suggest that whenever a Location header
is set it SHOULD have a status of 302.  It definitely COULD, but it COULD also have ANY OTHER status
code. There is no documentation to state otherwise.  It's true that a status code of 302 SHOULD
(or even MUST) have a Location header, but that does not logically infer the reverse.

So why does the PHP header function force a 302 as a 'magic' side-effect?

------------------------------------------------------------------------
[2015-08-18 12:10:44] cmb@php.net

According to bug #51749 this ticket is not a bug either (or maybe
"won't fix").

------------------------------------------------------------------------
[2015-08-17 16:35:01] jeff at nd4c dot com

After some more thought and research, I've again changed my mind.  I do believe that there is a
real bug with header() function.  But the bug description is now different...

header(): Setting the Location header will set the status code to 302 even if the status code has
been passed as the third parameter with any previous header call.

Test scripts:

// This format would only 
// require knowing if we want to force the status code
// Passing status code 202 as a "forced" status code
header("HTTP/1.1 202 Accepted", true, 202);

// This will still set the status code to 302,
// even though the 202 was forced.
header('Location: /some/arbitrary/uri');


// This format would require knowing both:
// if we want to force the status code AND
// also knowing when we are passing a header
// that might change the status code
// Passing status code 202 as a "forced" status code
header("HTTP/1.1 202 Accepted", true, 202);

// This will work as expected with a forced 202 status code.
header('Location: /some/arbitrary/uri', false, 202);


// Current solution: To force the status code as the last header.

// This format would only require
// knowing if we want to force the status code
// This will still set the status code to 302
header('Location: /some/arbitrary/uri');

// Passing status code 202 as a "forced" status code as the last header
header("HTTP/1.1 202 Accepted", true, 202);

I believe that since settings header should be allowed in any order since the RFC on headers does
not set any requirements on the order of setting headers.

I also believe that passing in a Status Code as a forced status code, it should remain
'forced' and not be overwritten by any following header calls unless a status code is
passed in as a new forced header.

I also question the benefit of having a Location header automatically setting the status code to a
302.  The setting of a Location header would always be done in a context where the program would
know whether or not to set the status code.  There is no need or even desire for a 'magic'
side effect.  These are typically frowned upon!!

So, at this point, I now consider this a bug.

I hope that helps.

------------------------------------------------------------------------
[2015-08-14 23:32:03] requinix@php.net

Actually, I think this is alright.

RFC 2616 says <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>
> The entity returned with this response SHOULD include an indication of the
> request's current status and either a pointer to a status monitor or some
> estimate of when the user can expect the request to be fulfilled.
I could reasonably interpret "pointer to a status monitor" to include a Location header.

But what do the major browsers do? Do they follow the redirect?

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=70273


--
Edit this bug report at https://bugs.php.net/bug.php?id=70273&edit=1


Thread (12 messages)

« previous php.bugs (#195345) next »