Skip to content

Commit

Permalink
Minor fixes on FollowRedirect middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Apr 13, 2024
1 parent 5e0321b commit 857f0c2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
7 changes: 3 additions & 4 deletions docs/Middleware/FollowRedirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.

Expand Down
7 changes: 6 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
ConnectionClosedException,
ConnectionFailureException,
ConnectionTimeoutException,
Exception
Exception,
ReconnectException,
};
use WebSocket\Message\{
Message,
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/Middleware/FollowRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion tests/suites/middleware/FollowRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 857f0c2

Please sign in to comment.