diff --git a/.travis.yml b/.travis.yml index 806cf78..18dce52 100755 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,9 @@ cache: - $HOME/.cache/pip php: - - 7.3 - 7.4 - 8.0 + - 8.1 - nightly env: diff --git a/VERSION b/VERSION index 94ff29c..ef538c2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.1 +3.1.2 diff --git a/src/PHPWebSockets.php b/src/PHPWebSockets.php index 7cd9c3f..f090762 100755 --- a/src/PHPWebSockets.php +++ b/src/PHPWebSockets.php @@ -370,8 +370,8 @@ public static function ValidateUTF8(string $str, int &$state = self::UTF8_ACCEPT 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9f 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // a0..bf 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c0..df - 0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef - 0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff + 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef + 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4 diff --git a/src/PHPWebSockets/AConnection.php b/src/PHPWebSockets/AConnection.php index 051a904..649efa6 100644 --- a/src/PHPWebSockets/AConnection.php +++ b/src/PHPWebSockets/AConnection.php @@ -257,13 +257,17 @@ public function isWriteBufferEmpty() : bool { * Attempts to write until the write buffer is empty * Note: This will discard any reads that happen during this period * - * @param float|null $timeout + * @param float|null $totalTimeout + * @param float|null $lastUpdateTimeout * * @return bool */ - public function writeUntilEmpty(float $timeout = NULL) : bool { + public function writeUntilEmpty(float $totalTimeout = NULL, float $lastUpdateTimeout = NULL) : bool { $start = microtime(TRUE); + $lastTimeWritten = $start; + $previousWriteBufferSize = ($this->_writeBuffer !== NULL ? strlen($this->_writeBuffer) : 0); + do { iterator_to_array(\PHPWebSockets::MultiUpdate([$this], 1.0)); @@ -272,7 +276,19 @@ public function writeUntilEmpty(float $timeout = NULL) : bool { throw new \RuntimeException('Connection closed during write empty'); } - if ($timeout !== NULL && microtime(TRUE) - $start > $timeout) { + if ($totalTimeout !== NULL && microtime(TRUE) - $start > $totalTimeout) { + return FALSE; + } + + $currentWriteBufferSize = ($this->_writeBuffer !== NULL ? strlen($this->_writeBuffer) : 0); + if ($currentWriteBufferSize !== $previousWriteBufferSize) { + + $lastTimeWritten = microtime(TRUE); + $previousWriteBufferSize = $currentWriteBufferSize; + + } + + if ($lastUpdateTimeout !== NULL && microtime(TRUE) - $lastTimeWritten > $lastUpdateTimeout) { return FALSE; } diff --git a/src/PHPWebSockets/Client.php b/src/PHPWebSockets/Client.php index e17aa71..90242d6 100644 --- a/src/PHPWebSockets/Client.php +++ b/src/PHPWebSockets/Client.php @@ -175,10 +175,13 @@ protected function _afterOpen() : void { parent::_afterOpen(); $this->_resourceIndex = (int) $this->getStream(); + $addressInfo = parse_url($this->getAddress()); + + $host = $addressInfo['host'] . (isset($addressInfo['port']) ? ':' . $addressInfo['port'] : ''); $headerParts = [ 'GET ' . $this->getPath() . ' HTTP/1.1', - 'Host: ' . $this->getAddress(), + 'Host: ' . $host, 'User-Agent: ' . $this->getUserAgent(), 'Upgrade: websocket', 'Connection: Upgrade', diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 1e10e3e..31870e9 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -76,14 +76,22 @@ protected function setUp() : void { } - $this->assertContains($this->_bufferType, static::VALID_BUFFER_TYPES); + if ($this->_bufferType === NULL) { + $this->_bufferType = getenv('BUFFERTYPE') ?: NULL; + } + + $this->assertContains($this->_bufferType, static::VALID_BUFFER_TYPES, 'Invalid buffer type'); \PHPWebSockets::Log(LogLevel::INFO, 'Using buffer type ' . $this->_bufferType); $descriptorSpec = [['pipe', 'r'], STDOUT, STDERR]; $this->_autobahnProcess = proc_open('wstest -m fuzzingserver -s Resources/Autobahn/fuzzingserver.json', $descriptorSpec, $pipes, realpath(__DIR__ . '/../')); - sleep(2); + $sleepSec = 2; + + \PHPWebSockets::Log(LogLevel::INFO, 'Sleeping ' . $sleepSec . ' seconds to wait for the fuzzing server to start'); + + sleep($sleepSec); $client = $this->_createClient(); $connectResult = $client->connect(static::ADDRESS, '/getCaseCount'); @@ -94,7 +102,7 @@ protected function setUp() : void { foreach ($client->update(NULL) as $key => $value) { - \PHPWebSockets::Log(LogLevel::INFO, $value . ''); + \PHPWebSockets::Log(LogLevel::DEBUG, 'Got message: ' . $value); if ($value instanceof Read && $value->getCode() === Read::C_READ) { @@ -107,7 +115,7 @@ protected function setUp() : void { } - $this->_caseCount = (int) $msg; + $this->_caseCount = json_decode($msg); } diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 8d0744f..86df36d 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -82,7 +82,12 @@ protected function setUp() : void { } - $this->assertContains($this->_bufferType, static::VALID_BUFFER_TYPES); + if ($this->_bufferType === NULL) { + $this->_bufferType = getenv('BUFFERTYPE') ?: NULL; + } + + $this->assertContains($this->_bufferType, static::VALID_BUFFER_TYPES, 'Invalid buffer type'); + \PHPWebSockets::Log(LogLevel::INFO, 'Using buffer type ' . $this->_bufferType); $this->_wsServer = new \PHPWebSockets\Server(self::ADDRESS);