For testing purposes I’d like to build a simple HTTP server in PHP.
I know the HTTP headers are terminated by a \r\n on an empty line and I’d like to detect that pattern so I know when my server has received all the headers and can respond to the client.
Even if I also simulate the client like so:
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
I’m unable to detect the end of the headers. There must be a problem in my thinking logic. Here is my code wich tries to detect the pattern:
do {
// read client input
$input = socket_read($spawn, 1024, 1) or die("Could not read input\n");
if (preg_match ("/^[\r\n|\r|\n]/", $input)) {
echo "CRLF detected!!\n";
} else {
if (trim($input) != "") {
echo "NO CRLF in: ".trim($input)."\n";
}
}
} while (true);
I have also tried:
if ($input == "\r\n"){
or
if (preg_match ("/[\r\n|\r|\n]{2,}/", $input))
and a lot of other different combinations but none of the options seem to detect the \r\n which indicates the end of the headers.
Any help is much appreciated.
Thank you Marc B.
I did not find a solution for this problem.
The work around I used is:
– Run a PHP script on your favorite HTTP Server.
– inspect the $_REQUEST variable and let the script compose a relevant answer using that info
– Log relevant info/actions to a file.
This way I’m able to test an App which interacts with a web server. The App connects to my script. I know how the App is interacting with the script by tailing (tail -f) the logfile. The script serves the App with an answer.