Server.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Websocket;
  36. use Hoa\Http;
  37. use Hoa\Socket as HoaSocket;
  38. /**
  39. * Class \Hoa\Websocket\Server.
  40. *
  41. * A cross-protocol Websocket server.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Server extends Connection
  47. {
  48. /**
  49. * Request (mainly parser).
  50. *
  51. * @var \Hoa\Http\Request
  52. */
  53. protected $_request = null;
  54. /**
  55. * Create a Websocket server.
  56. *
  57. * @param \Hoa\Socket\Server $server Server.
  58. * @param \Hoa\Http\Request $request Request parser.
  59. * @throws \Hoa\Socket\Exception
  60. */
  61. public function __construct(
  62. HoaSocket\Server $server,
  63. Http\Request $request = null
  64. ) {
  65. parent::__construct($server);
  66. if (null === $request) {
  67. $request = new Http\Request();
  68. }
  69. $this->setRequest($request);
  70. return;
  71. }
  72. /**
  73. * Try the handshake by trying different protocol implementation.
  74. *
  75. * @return void
  76. * @throws \Hoa\Websocket\Exception\BadProtocol
  77. */
  78. protected function doHandshake()
  79. {
  80. $connection = $this->getConnection();
  81. if (true === $connection->getSocket()->isSecured() &&
  82. false === $connection->isEncrypted()) {
  83. $connection->enableEncryption(true, $connection::ENCRYPTION_TLS);
  84. }
  85. $buffer = $connection->read(2048);
  86. $request = $this->getRequest();
  87. $request->parse($buffer);
  88. // Rfc6455.
  89. try {
  90. $rfc6455 = new Protocol\Rfc6455($connection);
  91. $rfc6455->doHandshake($request);
  92. $connection->getCurrentNode()->setProtocolImplementation($rfc6455);
  93. } catch (Exception\BadProtocol $e) {
  94. unset($rfc6455);
  95. // Hybi00.
  96. try {
  97. $hybi00 = new Protocol\Hybi00($connection);
  98. $hybi00->doHandshake($request);
  99. $connection->getCurrentNode()->setProtocolImplementation($hybi00);
  100. } catch (Exception\BadProtocol $e) {
  101. unset($hybi00);
  102. $connection->disconnect();
  103. throw new Exception\BadProtocol('All protocol failed.', 1);
  104. }
  105. }
  106. return;
  107. }
  108. /**
  109. * Set request (mainly parser).
  110. *
  111. * @param \Hoa\Http\Request $request Request.
  112. * @return \Hoa\Http\Request
  113. */
  114. public function setRequest(Http\Request $request)
  115. {
  116. $old = $this->_request;
  117. $this->_request = $request;
  118. return $old;
  119. }
  120. /**
  121. * Get request.
  122. *
  123. * @return \Hoa\Http\Request
  124. */
  125. public function getRequest()
  126. {
  127. return $this->_request;
  128. }
  129. }