Hybi00.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\Protocol;
  36. use Hoa\Http;
  37. use Hoa\Websocket;
  38. /**
  39. * Class \Hoa\Websocket\Protocol\Hybi00.
  40. *
  41. * Protocol implementation: draft-ietf-hybi-thewebsocketprotocol-00.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Hybi00 extends Generic
  47. {
  48. /**
  49. * Do the handshake.
  50. *
  51. * @param \Hoa\Http\Request $request Request.
  52. * @return void
  53. * @throws \Hoa\Websocket\Exception\BadProtocol
  54. */
  55. public function doHandshake(Http\Request $request)
  56. {
  57. $key1 = $request['sec-websocket-key1'];
  58. $key2 = $request['sec-websocket-key2'];
  59. $key3 = $request->getBody();
  60. $location = $request['host'] . $request->getUrl();
  61. $keynumb1 = (float) preg_replace('#[^0-9]#', '', $key1);
  62. $keynumb2 = (float) preg_replace('#[^0-9]#', '', $key2);
  63. $spaces1 = substr_count($key1, ' ');
  64. $spaces2 = substr_count($key2, ' ');
  65. if (0 === $spaces1 || 0 === $spaces2) {
  66. throw new Websocket\Exception\BadProtocol(
  67. 'Header Sec-WebSocket-Key1: %s or ' .
  68. 'Sec-WebSocket-Key2: %s is illegal.',
  69. 0,
  70. [$key1, $key2]
  71. );
  72. }
  73. $part1 = pack('N', (int) ($keynumb1 / $spaces1));
  74. $part2 = pack('N', (int) ($keynumb2 / $spaces2));
  75. $challenge = $part1 . $part2 . $key3;
  76. $response = md5($challenge, true);
  77. $connection = $this->getConnection();
  78. $connection->writeAll(
  79. 'HTTP/1.1 101 WebSocket Protocol Handshake' . "\r\n" .
  80. 'Upgrade: WebSocket' . "\r\n" .
  81. 'Connection: Upgrade' . "\r\n" .
  82. 'Sec-WebSocket-Origin: ' . $request['origin'] . "\r\n" .
  83. 'Sec-WebSocket-Location: ws://' . $location . "\r\n" .
  84. "\r\n" .
  85. $response . "\r\n"
  86. );
  87. $connection->getCurrentNode()->setHandshake(SUCCEED);
  88. return;
  89. }
  90. /**
  91. * Read a frame.
  92. *
  93. * @return array
  94. */
  95. public function readFrame()
  96. {
  97. $buffer = $this->getConnection()->read(2048);
  98. $length = strlen($buffer) - 2;
  99. if (empty($buffer)) {
  100. return [
  101. 'fin' => 0x1,
  102. 'rsv1' => 0x0,
  103. 'rsv2' => 0x0,
  104. 'rsv3' => 0x0,
  105. 'opcode' => Websocket\Connection::OPCODE_CONNECTION_CLOSE,
  106. 'mask' => 0x0,
  107. 'length' => 0,
  108. 'message' => null
  109. ];
  110. }
  111. return [
  112. 'fin' => 0x1,
  113. 'rsv1' => 0x0,
  114. 'rsv2' => 0x0,
  115. 'rsv3' => 0x0,
  116. 'opcode' => Websocket\Connection::OPCODE_TEXT_FRAME,
  117. 'mask' => 0x0,
  118. 'length' => $length,
  119. 'message' => substr($buffer, 1, $length)
  120. ];
  121. }
  122. /**
  123. * Write a frame.
  124. *
  125. * @param string $message Message.
  126. * @param int $opcode Opcode (useless here).
  127. * @param bool $end Whether it is the last frame of the message.
  128. * @param bool $mask Whether the message will be masked or not.
  129. * @return int
  130. */
  131. public function writeFrame(
  132. $message,
  133. $opcode = -1,
  134. $end = true,
  135. $mask = false
  136. ) {
  137. return $this->getConnection()->writeAll(
  138. chr(0) . $message . chr(255)
  139. );
  140. }
  141. /**
  142. * Send a message to a node (if not specified, current node).
  143. *
  144. * @param string $message Message.
  145. * @param int $opcode Opcode.
  146. * @param bool $end Whether it is the last frame of the message.
  147. * @param bool $mask Whether the message will be masked or not.
  148. * @return void
  149. */
  150. public function send($message, $opcode = -1, $end = true, $mask = false)
  151. {
  152. $this->writeFrame($message);
  153. return;
  154. }
  155. /**
  156. * Close a specific node/connection.
  157. *
  158. * @param int $code Code (please, see
  159. * \Hoa\Websocket\Connection::CLOSE_*
  160. * constants).
  161. * @param string $reason Reason.
  162. * @param bool $mask Whether the message will be masked or not.
  163. * @return void
  164. */
  165. public function close(
  166. $code = Websocket\Connection::CLOSE_NORMAL,
  167. $reason = null,
  168. $mask = false
  169. ) {
  170. return;
  171. }
  172. }