Skip to content

Commit

Permalink
Merge pull request #69 from WarriorXK/hotfix/3.1.1
Browse files Browse the repository at this point in the history
[WIP] Hotfix/3.1.1
  • Loading branch information
WarriorXK authored Jul 7, 2021
2 parents 3bb00d5 + 4469bb6 commit adef40d
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 33 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.0
3.1.1
2 changes: 1 addition & 1 deletion src/PHPWebSockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
55 changes: 46 additions & 9 deletions src/PHPWebSockets/AConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -131,7 +131,7 @@ abstract class AConnection implements IStreamContainer, LoggerAwareInterface, IT
/**
* The timestamp since when this connection has been opened
*
* @var int
* @var float|null
*/
protected $_openedTimestamp = NULL;

Expand Down Expand Up @@ -215,7 +215,7 @@ abstract class AConnection implements IStreamContainer, LoggerAwareInterface, IT
/**
* The resource stream
*
* @var resource
* @var resource|null
*/
protected $_stream = NULL;

Expand Down Expand Up @@ -478,9 +478,10 @@ protected function _handlePacket(string $newData) : \Generator {

if ($res === FALSE) {

$this->close();
yield new Update\Error(Update\Error::C_WRITE_INVALID_TARGET_STREAM, $this);

$this->close();

}

} else {
Expand Down Expand Up @@ -540,10 +541,10 @@ protected function _handlePacket(string $newData) : \Generator {

$this->_isClosed = TRUE;

$this->close();

yield new Update\Read(Update\Read::C_SOCK_DISCONNECT, $this);

$this->close();

} elseif (!$this->_weSentDisconnect) {

$this->_log(LogLevel::DEBUG, ' Remote initiated the disconnect, echo disconnect');
Expand Down Expand Up @@ -717,7 +718,18 @@ public function writeMultiFramed(string $data, int $opcode = \PHPWebSockets::OPC
throw new \LogicException('FrameSize should be at least 1');
}

$frames = str_split($data, $frameSize);
$dataLen = strlen($data);
if (strlen($data) > $frameSize) {

$frames = str_split($data, $frameSize);
if ($frames === FALSE) {
throw new \UnexpectedValueException('Unable to split ' . $dataLen . ' bytes into frames of ' . $frameSize . ' bytes');
}

} else {
$frames = [$data];
}

end($frames);
$lastKey = key($frames);

Expand Down Expand Up @@ -757,6 +769,10 @@ protected function _getStreamForNewMessage(array $headers) {
*/
public function writeRaw(string $data, bool $priority) : void {

if (!$this->isOpen()) {
throw new \LogicException('Unable to write: Connection is closed');
}

if ($priority) {
$this->_priorityFramesBuffer[] = $data;
} else {
Expand Down Expand Up @@ -859,7 +875,7 @@ public function setNewMessageStreamCallback(callable $callable = NULL) : void {
/**
* Returns the stream object for this connection
*
* @return resource
* @return resource|null
*/
public function getStream() {
return $this->_stream;
Expand Down Expand Up @@ -960,10 +976,31 @@ public function close() : void {
$this->_isClosed = TRUE;

if (is_resource($this->_stream)) {
fclose($this->_stream);

$stream = $this->_stream;
$this->_stream = NULL;

fclose($stream);

}

// To prevent the warning that there was still data to write
$this->_clearBuffers();
$this->_resetFrameData();

}

/**
* Clears all buffers
*/
private function _clearBuffers() : void {

$this->_currentFrameRemainingBytes = 0;
$this->_priorityFramesBuffer = [];
$this->_framesBuffer = [];
$this->_writeBuffer = NULL;
$this->_readBuffer = NULL;

}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/AUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/BasicLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/Framer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions src/PHPWebSockets/IStreamContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -76,7 +76,7 @@ public function handleRead() : \Generator;
/**
* Returns the stream object for this connection
*
* @return resource
* @return resource|null
*/
public function getStream();

Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/ITaggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/Server/AcceptingConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/Server/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/TLogAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/TStreamContainerDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/Update/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/PHPWebSockets/Update/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 1 addition & 5 deletions src/PHPWebSockets/UpdatesWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -535,7 +535,6 @@ private function _onInvalidPayload(Update\Error $update) : void {
$source = $update->getSourceConnection();

$this->_triggerErrorHandler($source, $update->getCode());
$this->_triggerDisconnectHandler($source, FALSE, NULL);

$this->_handledDisconnects[$source->getResourceIndex()] = TRUE;

Expand All @@ -561,7 +560,6 @@ private function _onProtocolError(Update\Error $update) : void {
$source = $update->getSourceConnection();

$this->_triggerErrorHandler($source, $update->getCode());
$this->_triggerDisconnectHandler($source, FALSE, NULL);

$this->_handledDisconnects[$source->getResourceIndex()] = TRUE;

Expand All @@ -572,7 +570,6 @@ private function _onInvalidRSVBit(Update\Error $update) : void {
$source = $update->getSourceConnection();

$this->_triggerErrorHandler($source, $update->getCode());
$this->_triggerDisconnectHandler($source, FALSE, NULL);

$this->_handledDisconnects[$source->getResourceIndex()] = TRUE;

Expand All @@ -587,7 +584,6 @@ private function _acceptTimeoutPassed(Update\Error $update) : void {
$source = $update->getSourceConnection();

$this->_triggerErrorHandler($source, $update->getCode());
$this->_triggerDisconnectHandler($source, FALSE, NULL);

$this->_handledDisconnects[$source->getResourceIndex()] = TRUE;

Expand Down
2 changes: 1 addition & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 26 additions & 0 deletions tests/Helpers/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@

declare(strict_types = 1);

/*
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* - - - - - - - - - - - - - - END LICENSE BLOCK - - - - - - - - - - - - -
*/

require_once __DIR__ . '/../../vendor/autoload.php';

$cliArgs = [
Expand Down
2 changes: 1 addition & 1 deletion tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion tests/UpdatesWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
* The MIT License (MIT)
*
* Copyright (c) 2020 Kevin Meijer
* Copyright (c) 2021 Kevin Meijer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down

0 comments on commit adef40d

Please sign in to comment.