EventHttp::__construct

(PECL event >= 1.2.6-beta)

EventHttp::__constructConstruit un objet EventHttp (le serveur HTTP)

Description

public EventHttp::__construct ( EventBase $base )

Construit l'objet serveur HTTP.

Liste de paramètres

base

Evénement de base associé.

Valeurs de retour

Retourne un objet EventHttp.

Exemples

Exemple #1 Serveur HTTP simple

  1. <?php
  2. /*
  3. * Serveur HTTP simple.
  4. *
  5. * Pour le tester :
  6. * 1) Exécutez-le sur le port de votre choix, i.e. :
  7. * $ php examples/http.php 8010
  8. * 2) Dans un autre terminal, connectez-vous sur une adresse de ce port
  9. * et effectuez une requête GET ou POST (les autres sont désactivées ici),
  10. * i.e. :
  11. * $ nc -t 127.0.0.1 8010
  12. * POST /about HTTP/1.0
  13. * Content-Type: text/plain
  14. * Content-Length: 4
  15. * Connection: close
  16. * (press Enter)
  17. *
  18. * Il devrait afficher :
  19. * a=12
  20. * HTTP/1.0 200 OK
  21. * Content-Type: text/html; charset=ISO-8859-1
  22. * Connection: close
  23. *
  24. * $ nc -t 127.0.0.1 8010
  25. * GET /dump HTTP/1.0
  26. * Content-Type: text/plain
  27. * Content-Encoding: UTF-8
  28. * Connection: close
  29. * (press Enter)
  30. *
  31. * Il devrait afficher :
  32. * HTTP/1.0 200 OK
  33. * Content-Type: text/html; charset=ISO-8859-1
  34. * Connection: close
  35. * (press Enter)
  36. *
  37. * $ nc -t 127.0.0.1 8010
  38. * GET /unknown HTTP/1.0
  39. * Connection: close
  40. *
  41. * Il devrait afficher :
  42. * HTTP/1.0 200 OK
  43. * Content-Type: text/html; charset=ISO-8859-1
  44. * Connection: close
  45. *
  46. * 3) Regardez ce que le serveur affiche sur le précédent terminal.
  47. */
  48.  
  49. function _http_dump($req, $data) {
  50. static $counter = 0;
  51. static $max_requests = 2;
  52.  
  53. if (++$counter >= $max_requests) {
  54. echo "Counter reached max requests $max_requests. Exiting\n";
  55. exit();
  56. }
  57.  
  58. echo __METHOD__, " called\n";
  59. echo "request:"; var_dump($req);
  60. echo "data:"; var_dump($data);
  61.  
  62. echo "\n===== DUMP =====\n";
  63. echo "Command:", $req->getCommand(), PHP_EOL;
  64. echo "URI:", $req->getUri(), PHP_EOL;
  65. echo "Input headers:"; var_dump($req->getInputHeaders());
  66. echo "Output headers:"; var_dump($req->getOutputHeaders());
  67.  
  68. echo "\n >> Sending reply ...";
  69. $req->sendReply(200, "OK");
  70. echo "OK\n";
  71.  
  72. echo "\n >> Reading input buffer ...\n";
  73. $buf = $req->getInputBuffer();
  74. while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
  75. echo $s, PHP_EOL;
  76. }
  77. echo "No more data in the buffer\n";
  78. }
  79.  
  80. function _http_about($req) {
  81. echo __METHOD__, PHP_EOL;
  82. echo "URI: ", $req->getUri(), PHP_EOL;
  83. echo "\n >> Sending reply ...";
  84. $req->sendReply(200, "OK");
  85. echo "OK\n";
  86. }
  87.  
  88. function _http_default($req, $data) {
  89. echo __METHOD__, PHP_EOL;
  90. echo "URI: ", $req->getUri(), PHP_EOL;
  91. echo "\n >> Sending reply ...";
  92. $req->sendReply(200, "OK");
  93. echo "OK\n";
  94. }
  95.  
  96. $port = 8010;
  97. if ($argc > 1) {
  98. $port = (int) $argv[1];
  99. }
  100. if ($port <= 0 || $port > 65535) {
  101. exit("Invalid port");
  102. }
  103.  
  104. $base = new EventBase();
  105. $http = new EventHttp($base);
  106. $http->setAllowedMethods(EventHttpRequest::CMD_GET | EventHttpRequest::CMD_POST);
  107.  
  108. $http->setCallback("/dump", "_http_dump", array(4, 8));
  109. $http->setCallback("/about", "_http_about");
  110. $http->setDefaultCallback("_http_default", "custom data value");
  111.  
  112. $http->bind("0.0.0.0", 8010);
  113. $base->loop();
  114. ?>

L'exemple ci-dessus va afficher quelque chose de similaire à :

a=12
HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close
(press Enter)

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close
LoadingChargement en cours