Skip to content

Commit

Permalink
Merge pull request #70 from WarriorXK/hotfix/3.1.2
Browse files Browse the repository at this point in the history
Hotfix/3.1.2
  • Loading branch information
WarriorXK authored Dec 2, 2022
2 parents adef40d + 47e9255 commit 11ac852
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ cache:
- $HOME/.cache/pip

php:
- 7.3
- 7.4
- 8.0
- 8.1
- nightly

env:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.1
3.1.2
4 changes: 2 additions & 2 deletions src/PHPWebSockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions src/PHPWebSockets/AConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion src/PHPWebSockets/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
16 changes: 12 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) {

Expand All @@ -107,7 +115,7 @@ protected function setUp() : void {

}

$this->_caseCount = (int) $msg;
$this->_caseCount = json_decode($msg);

}

Expand Down
7 changes: 6 additions & 1 deletion tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 11ac852

Please sign in to comment.