Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing HTTP status code

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

like image 495
Brian Avatar asked Sep 18 '09 03:09

Brian


People also ask

What is parsing in HTTP?

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.

What is HTTP parsing error?

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).

What is HTTP status code1?

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.

What HTTP 440?

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.


2 Answers

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.

like image 172
caf Avatar answered Oct 17 '22 07:10

caf


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

like image 26
RageZ Avatar answered Oct 17 '22 07:10

RageZ