Connection.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace React\Socket;
  3. use Evenement\EventEmitter;
  4. use React\EventLoop\LoopInterface;
  5. use React\Stream\DuplexResourceStream;
  6. use React\Stream\Util;
  7. use React\Stream\WritableResourceStream;
  8. use React\Stream\WritableStreamInterface;
  9. /**
  10. * The actual connection implementation for ConnectionInterface
  11. *
  12. * This class should only be used internally, see ConnectionInterface instead.
  13. *
  14. * @see ConnectionInterface
  15. * @internal
  16. */
  17. class Connection extends EventEmitter implements ConnectionInterface
  18. {
  19. /**
  20. * Internal flag whether this is a Unix domain socket (UDS) connection
  21. *
  22. * @internal
  23. */
  24. public $unix = false;
  25. /**
  26. * Internal flag whether encryption has been enabled on this connection
  27. *
  28. * Mostly used by internal StreamEncryption so that connection returns
  29. * `tls://` scheme for encrypted connections instead of `tcp://`.
  30. *
  31. * @internal
  32. */
  33. public $encryptionEnabled = false;
  34. /** @internal */
  35. public $stream;
  36. private $input;
  37. public function __construct($resource, LoopInterface $loop)
  38. {
  39. // PHP < 7.3.3 (and PHP < 7.2.15) suffers from a bug where feof() might
  40. // block with 100% CPU usage on fragmented TLS records.
  41. // We try to work around this by always consuming the complete receive
  42. // buffer at once to avoid stale data in TLS buffers. This is known to
  43. // work around high CPU usage for well-behaving peers, but this may
  44. // cause very large data chunks for high throughput scenarios. The buggy
  45. // behavior can still be triggered due to network I/O buffers or
  46. // malicious peers on affected versions, upgrading is highly recommended.
  47. // @link https://bugs.php.net/bug.php?id=77390
  48. $clearCompleteBuffer = \PHP_VERSION_ID < 70215 || (\PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70303);
  49. // PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing big
  50. // chunks of data over TLS streams at once.
  51. // We try to work around this by limiting the write chunk size to 8192
  52. // bytes for older PHP versions only.
  53. // This is only a work-around and has a noticable performance penalty on
  54. // affected versions. Please update your PHP version.
  55. // This applies to all streams because TLS may be enabled later on.
  56. // See https://github.com/reactphp/socket/issues/105
  57. $limitWriteChunks = (\PHP_VERSION_ID < 70018 || (\PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70104));
  58. $this->input = new DuplexResourceStream(
  59. $resource,
  60. $loop,
  61. $clearCompleteBuffer ? -1 : null,
  62. new WritableResourceStream($resource, $loop, null, $limitWriteChunks ? 8192 : null)
  63. );
  64. $this->stream = $resource;
  65. Util::forwardEvents($this->input, $this, array('data', 'end', 'error', 'close', 'pipe', 'drain'));
  66. $this->input->on('close', array($this, 'close'));
  67. }
  68. public function isReadable()
  69. {
  70. return $this->input->isReadable();
  71. }
  72. public function isWritable()
  73. {
  74. return $this->input->isWritable();
  75. }
  76. public function pause()
  77. {
  78. $this->input->pause();
  79. }
  80. public function resume()
  81. {
  82. $this->input->resume();
  83. }
  84. public function pipe(WritableStreamInterface $dest, array $options = array())
  85. {
  86. return $this->input->pipe($dest, $options);
  87. }
  88. public function write($data)
  89. {
  90. return $this->input->write($data);
  91. }
  92. public function end($data = null)
  93. {
  94. $this->input->end($data);
  95. }
  96. public function close()
  97. {
  98. $this->input->close();
  99. $this->handleClose();
  100. $this->removeAllListeners();
  101. }
  102. public function handleClose()
  103. {
  104. if (!\is_resource($this->stream)) {
  105. return;
  106. }
  107. // Try to cleanly shut down socket and ignore any errors in case other
  108. // side already closed. Underlying Stream implementation will take care
  109. // of closing stream resource, so we otherwise keep this open here.
  110. @\stream_socket_shutdown($this->stream, \STREAM_SHUT_RDWR);
  111. }
  112. public function getRemoteAddress()
  113. {
  114. if (!\is_resource($this->stream)) {
  115. return null;
  116. }
  117. return $this->parseAddress(\stream_socket_get_name($this->stream, true));
  118. }
  119. public function getLocalAddress()
  120. {
  121. if (!\is_resource($this->stream)) {
  122. return null;
  123. }
  124. return $this->parseAddress(\stream_socket_get_name($this->stream, false));
  125. }
  126. private function parseAddress($address)
  127. {
  128. if ($address === false) {
  129. return null;
  130. }
  131. if ($this->unix) {
  132. // remove trailing colon from address for HHVM < 3.19: https://3v4l.org/5C1lo
  133. // note that technically ":" is a valid address, so keep this in place otherwise
  134. if (\substr($address, -1) === ':' && \defined('HHVM_VERSION_ID') && \HHVM_VERSION_ID < 31900) {
  135. $address = (string)\substr($address, 0, -1); // @codeCoverageIgnore
  136. }
  137. // work around unknown addresses should return null value: https://3v4l.org/5C1lo and https://bugs.php.net/bug.php?id=74556
  138. // PHP uses "\0" string and HHVM uses empty string (colon removed above)
  139. if ($address === '' || $address[0] === "\x00" ) {
  140. return null; // @codeCoverageIgnore
  141. }
  142. return 'unix://' . $address;
  143. }
  144. // check if this is an IPv6 address which includes multiple colons but no square brackets
  145. $pos = \strrpos($address, ':');
  146. if ($pos !== false && \strpos($address, ':') < $pos && \substr($address, 0, 1) !== '[') {
  147. $address = '[' . \substr($address, 0, $pos) . ']:' . \substr($address, $pos + 1); // @codeCoverageIgnore
  148. }
  149. return ($this->encryptionEnabled ? 'tls' : 'tcp') . '://' . $address;
  150. }
  151. }