Skip to content

Commit

Permalink
Merge pull request #92 from sirn-se/91-scheme-error
Browse files Browse the repository at this point in the history
91 scheme error
  • Loading branch information
sirn-se authored Jan 10, 2025
2 parents f041a39 + b2a024a commit 6dceb42
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
3 changes: 1 addition & 2 deletions examples/send.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Run in console: php examples/send.php <options> <message>
*
* Console options:
* --uri <uri> : The URI to connect to, default ws://localhost:8000
* --uri <uri> : The URI to connect to, default ws://localhost:80
* --opcode <string> : Opcode to send, default 'text'
* --debug : Output log data (if logger is available)
*/
Expand All @@ -21,7 +21,6 @@

error_reporting(-1);


echo "# Send client! [phrity/websocket]\n";

// Client options specified or default
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/ConnectionFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
class ConnectionFailureException extends Exception implements ConnectionLevelInterface
{
public function __construct()
public function __construct(string|null $message = null)
{
parent::__construct('Connection error');
parent::__construct($message ?? 'Connection error');
}
}
2 changes: 1 addition & 1 deletion src/Http/HttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function readLine(): string
throw new RuntimeException('Could not read Http request.');
}
$data .= $buffer;
} while (!str_ends_with($data, "\r\n"));
} while (!str_ends_with($data, "\n"));
return trim($data);
}
}
5 changes: 4 additions & 1 deletion src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public function setPayload(string $payload = ''): void
public function getFrames(int $frameSize = 4096): array
{
$frames = [];
$split = str_split($this->getPayload(), $frameSize) ?: [''];
$split = str_split($this->getPayload(), $frameSize);
if (empty($split)) {
$split = [''];
}
foreach ($split as $i => $payload) {
$frames[] = new Frame(
$i === 0 ? $this->opcode : 'continuation',
Expand Down
9 changes: 6 additions & 3 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Phrity\Net\{
SocketServer,
StreamCollection,
StreamException,
StreamFactory,
Uri
};
Expand All @@ -23,6 +24,7 @@
use Throwable;
use WebSocket\Exception\{
CloseException,
ConnectionFailureException,
ConnectionLevelInterface,
Exception,
HandshakeException,
Expand Down Expand Up @@ -483,6 +485,7 @@ protected function createSocketServer(): void
// Accept connection on socket server
protected function acceptSocket(SocketServer $socket): void
{
$connection = null;
try {
if (!is_null($this->maxConnections) && $this->getConnectionCount() >= $this->maxConnections) {
$this->logger->warning("[server] Denied connection, reached max {$this->maxConnections}");
Expand Down Expand Up @@ -510,12 +513,12 @@ protected function acceptSocket(SocketServer $socket): void
$connection->getHandshakeResponse(),
]);
$this->dispatch('connect', [$this, $connection, $request]);
} catch (Exception $e) {
} catch (Exception | StreamException $e) {
/** @var Connection|null $connection */
if (isset($connection)) {
$connection->disconnect();
}
$error = "Server failed to accept: {$e->getMessage()}";
throw $e;
throw new ConnectionFailureException("Server failed to accept: {$e->getMessage()}");
}
}

Expand Down

0 comments on commit 6dceb42

Please sign in to comment.