I am using PHP to parse the numeric portion of the HTTP status code response. Given a standard "HTTP/1.1 200 OK" response, I'd use:
$data = explode(' ', "HTTP/1.1 200 OK");
$code = $data[1];
I'm not an expert on HTTP. Would I ever encounter a response where the code is not at the position of $data[1] as in the above example? I just want to be sure that this method of delimiting the response code will always work for any response.
Thanks, Brian
The HTTP Parser interprets a byte stream according to the HTTP specification. This Parser is used by the HTTP Client Connector and by the HTTP Server Connector.
Summary. The SecureSphere HTTP Module could not parse the HTTP Request or response due to malformed format. The SecureSphere HTTP Module parses each HTTP request and response it receives from the HTTP Probe (which assembles the TCP packets into a complete request/response).
Prevailing theory is that the status is set to null and the statuscode set to -1 when the response object is constructed, and then something happens to the connection that means the request doesn't complete, so these defaults are never overwritten with real values.
440 Login Time-out. The client's session has expired and must log in again. 449 Retry With. The server cannot honour the request because the user has not provided the required information. 451 Redirect.
When in doubt, check the spec. The spec in this case, for HTTP/1.1, is RFC2616. In Section 6.1, it describes the Status-Line, the first component of a Response, as:
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
That is - a single ASCII space (SP
) must separate the HTTP-Version and the Status-Code - and if you check the definition of HTTP-Version (in Section 3.1) it cannot include a space, and neither can the Status-Code.
So you are good to go with that code.
No if the webserver respect the standards doing an explode and caching the second item of the array is fine
if you really wants to be sure use a regular expression
i.e.
preg_match('|HTTP/\d\.\d\s+(\d+)\s+.*|',$subject,$match);
var_dump($match[1]);
Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With