Answer the question
In order to leave comments, you need to log in
HTTP Termination Symbol
Good afternoon colleagues,
for the purpose of self-development, I am trying to write a small asynchronous HTTP Proxy server in PHP.
I can’t catch the moment when the browser stops transmitting data. I understand that the termination symbol in HTTP is two CLRFs in a row.
But for some reason the code never exits the loop. Tried separately to use only \r and only \n. Tried even \0
I will not understand in any way in what business. What am I doing wrong?
Thanks in advance.
$buffer = "";
$termination = 0;
do {
$data = @socket_read($client, 8192, PHP_NORMAL_READ);
$buffer .= $data;
if ($data == "\r\n") {
$termination++;
}
} while($termination < 2 || $data !== false);
die($buffer);
Answer the question
In order to leave comments, you need to log in
Yes, you need two CRLFs in a row, but the comparison
$data == "\r\n"
does not do what it should. This comparison will only return true once when the second CRLF arrives, because the first one was not on its own, but completed the last line of the header. In general, a typical HTTP request looks something like this (only there are usually more fields):GET / HTTP/1.1\r\n
Host: habrahabr.ru\r\n
Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\r\n
Connection: keep-alive\r\n
Referer: http://habrahabr.ru/\r\n
\r\n
You're right:
Both types of message consist of a start-line, zero or more header fields (also known as "headers"), an empty line (ie, a line with nothing preceding the CRLF) indicating the end of the header fields, and possibly a message-body.
So you need to debug - look at what came in $data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question