diff --git a/docs/Middleware/FollowRedirect.md b/docs/Middleware/FollowRedirect.md index 83bef9e..adfc738 100644 --- a/docs/Middleware/FollowRedirect.md +++ b/docs/Middleware/FollowRedirect.md @@ -9,9 +9,8 @@ During handshake, it reacts to `3xx` HTTP status and reconnect the Client to pro ## Client -Will follow redirect by setting new URI and reconnection the Client. - -* Server response during handshake have a `3xx` status +Will follow redirect by setting new URI and reconnecy the Client if; +* Server response during handshake has a `3xx` status * Server response also includes a `Location` header * Maximum number of redirects has not been exceeded @@ -23,7 +22,7 @@ $client->connect(); ## Maximum number of redirects By default, maximum number of redirects in `10`. -If middleware receive additional redirect instructions after that, it will throw a HandshakeException. +If middleware receive additional redirect instructions after that, it will throw a `HandshakeException`. It is also possible to specify maximum number of redirects as parameter. diff --git a/src/Connection.php b/src/Connection.php index d6be7d6..4087f34 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -26,7 +26,8 @@ ConnectionClosedException, ConnectionFailureException, ConnectionTimeoutException, - Exception + Exception, + ReconnectException, }; use WebSocket\Message\{ Message, @@ -352,6 +353,10 @@ public function getHandshakeResponse(): Response|null protected function throwException(Throwable $e): never { // Internal exceptions are handled and re-thrown + if ($e instanceof ReconnectException) { + $this->logger->info("[connection] {$e->getMessage()}"); + throw $e; + } if ($e instanceof Exception) { $this->logger->error("[connection] {$e->getMessage()}"); throw $e; diff --git a/src/Middleware/FollowRedirect.php b/src/Middleware/FollowRedirect.php index acf55cc..80a69a1 100644 --- a/src/Middleware/FollowRedirect.php +++ b/src/Middleware/FollowRedirect.php @@ -36,7 +36,7 @@ class FollowRedirect implements LoggerAwareInterface, ProcessHttpIncomingInterfa use StringableTrait; private $limit; - private $attempts = 0; + private $attempts = 1; public function __construct(int $limit = 10) { @@ -53,15 +53,14 @@ public function processHttpIncoming(ProcessHttpStack $stack, Connection $connect && $locationHeader = $message->getHeaderLine('Location') ) { $note = "{$this->attempts} of {$this->limit} redirect attempts"; - if ($this->attempts >= $this->limit) { + if ($this->attempts > $this->limit) { $this->logger->debug("[follow-redirect] Too many redirect attempts, giving up"); - throw new HandshakeException("{$note}, giving up", $message); + throw new HandshakeException("Too many redirect attempts, giving up", $message); } $this->attempts++; $this->logger->debug("[follow-redirect] {$message->getStatusCode()} {$locationHeader} ($note)"); throw new ReconnectException(new Uri($locationHeader)); } - $this->logger->debug("Exp {$message->getStatusCode()} "); return $message; } } diff --git a/tests/suites/middleware/FollowRedirectTest.php b/tests/suites/middleware/FollowRedirectTest.php index b754cfb..7b5c1cc 100644 --- a/tests/suites/middleware/FollowRedirectTest.php +++ b/tests/suites/middleware/FollowRedirectTest.php @@ -102,7 +102,7 @@ public function testMaxRedirect(): void $this->expectSocketStreamIsConnected(); $this->expectSocketStreamClose(); $this->expectException(HandshakeException::class); - $this->expectExceptionMessage('0 of 0 redirect attempts, giving up'); + $this->expectExceptionMessage('Too many redirect attempts, giving up'); $connection->pullHttp(); }