spraff spraff - 1 month ago 10
HTTP Question

Is the effective HTTP status code the same as the last one given, when there are many for one request?

PHP snippet for a HTTP HEAD:

$context = stream_context_create (
Array
(
'http' => Array ('method' => 'HEAD')
));

$fd = fopen ("http://google.com", 'rb', false, $context);

print_r (stream_get_meta_data ($fd));


I am interested in the status code. Here is the result of the above

Array
(
[wrapper_data] => Array
(
[0] => HTTP/1.0 302 Found
[1] => Cache-Control: private
[2] => Content-Type: text/html; charset=UTF-8
[3] => Location: http://www.google.co.uk/?gfe_rd=cr&ei=jsMQWK3GAarA8gfKnbGQAg
[4] => Content-Length: 261
[5] => Date: Wed, 26 Oct 2016 14:54:06 GMT
[6] => HTTP/1.0 200 OK
[7] => Date: Wed, 26 Oct 2016 14:54:06 GMT
[8] => Expires: -1
[9] => Cache-Control: private, max-age=0
[10] => Content-Type: text/html; charset=ISO-8859-1
[11] => P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
[12] => Server: gws
[13] => X-XSS-Protection: 1; mode=block
[14] => X-Frame-Options: SAMEORIGIN
[15] => Set-Cookie: NID=89=Mmm0qhGNf70J0iWVbQO_whrKBG3-RNGXpkKBm9DorsWkEzMMXQfZ5qC9s5MhVwHrRXYPFmi6Q_5B0h1au7zLKDwBmgtmTPzljoQcG_jm7qqupQjR6YO38TXmnTQMNskH; expires=Thu, 27-Apr-2017 14:54:06 GMT; path=/; domain=.google.co.uk; HttpOnly
[16] => Accept-Ranges: none
[17] => Vary: Accept-Encoding
)

[wrapper_type] => http
[stream_type] => tcp_socket/ssl
[mode] => rb
[unread_bytes] => 0
[seekable] =>
[uri] => http://google.com
[timed_out] =>
[blocked] => 1
[eof] =>
)


Note the following:

wrapper_data[0] => HTTP/1.0 302 Found
wrapper_data[3] => Location: http://www.google.co.uk/?gfe_rd=cr&ei=jsMQWK3GAarA8gfKnbGQAg
wrapper_data[6] => HTTP/1.0 200 OK


What is the correct procedure, in general, for determining the effective (what the user will see in a browser) HTTP status code?

I am tempted to simply take the last line matching
/^HTTP\/[\d\.]+ (\d+)/
, is that going to work all the time or is it naive?

Answer

The effect of 302 is that a new Request is sent, so in this case there have really been two Requests and two Responses. The first status code is not relevant to the user unless the second Request failed (there was no Response).

It would be safe to treat the status code of the last received response as the relevant one in virtually all cases (except if debugging or tracing is what you're after). Any status codes before it were only needed to get to that last status code.

Comments