03-http-server.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. // Simple HTTP server example (for illustration purposes only).
  3. // This shows how a plaintext TCP/IP connection is accepted to then receive an
  4. // application level protocol message (HTTP request) and reply with an
  5. // application level protocol message (HTTP response) in return.
  6. //
  7. // This example exists for illustraion purposes only. It does not actually
  8. // parse incoming HTTP requests, so you can actually send *anything* and will
  9. // still respond with a valid HTTP response.
  10. // Real applications should use react/http instead!
  11. //
  12. // Just start this server and send a request to it:
  13. //
  14. // $ php examples/03-http-server.php 127.0.0.1:8000
  15. // $ curl -v http://localhost:8000/
  16. // $ ab -n1000 -c10 http://localhost:8000/
  17. // $ docker run -it --rm --net=host jordi/ab -n1000 -c10 http://localhost:8000/
  18. //
  19. // You can also run a secure HTTPS echo server like this:
  20. //
  21. // $ php examples/03-http-server.php tls://127.0.0.1:8000 examples/localhost.pem
  22. // $ curl -v --insecure https://localhost:8000/
  23. // $ ab -n1000 -c10 https://localhost:8000/
  24. // $ docker run -it --rm --net=host jordi/ab -n1000 -c10 https://localhost:8000/
  25. //
  26. // You can also run a Unix domain socket (UDS) server like this:
  27. //
  28. // $ php examples/03-http-server.php unix:///tmp/server.sock
  29. // $ nc -U /tmp/server.sock
  30. //
  31. // You can also use systemd socket activation and listen on an inherited file descriptor:
  32. //
  33. // $ systemd-socket-activate -l 8000 php examples/03-http-server.php php://fd/3
  34. // $ curl -v --insecure https://localhost:8000/
  35. // $ ab -n1000 -c10 https://localhost:8000/
  36. // $ docker run -it --rm --net=host jordi/ab -n1000 -c10 https://localhost:8000/
  37. require __DIR__ . '/../vendor/autoload.php';
  38. $socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '127.0.0.1:0', array(
  39. 'tls' => array(
  40. 'local_cert' => isset($argv[2]) ? $argv[2] : (__DIR__ . '/localhost.pem')
  41. )
  42. ));
  43. $socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
  44. echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;
  45. $connection->once('data', function () use ($connection) {
  46. $body = "<html><h1>Hello world!</h1></html>\r\n";
  47. $connection->end("HTTP/1.1 200 OK\r\nContent-Length: " . strlen($body) . "\r\nConnection: close\r\n\r\n" . $body);
  48. });
  49. $connection->on('close', function () use ($connection) {
  50. echo '[' . $connection->getRemoteAddress() . ' disconnected]' . PHP_EOL;
  51. });
  52. });
  53. $socket->on('error', function (Exception $e) {
  54. echo 'Error: ' . $e->getMessage() . PHP_EOL;
  55. });
  56. echo 'Listening on ' . strtr($socket->getAddress(), array('tcp:' => 'http:', 'tls:' => 'https:')) . PHP_EOL;