Skip to content

Commit

Permalink
Clone request each time before following redirect
Browse files Browse the repository at this point in the history
Fixes #341.
  • Loading branch information
trowski authored and kelunik committed Aug 15, 2023
1 parent 1a8d999 commit 069fdae
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Interceptor/FollowRedirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ private function followRedirects(
$requestNr = 2;

do {
$request = $this->createRedirectRequest($clonedRequest, $response);
$request = $this->updateRequestForRedirect($clonedRequest, $response);
if ($request === null) {
return $response;
}

$clonedRequest = $this->cloneRequest($request);

$redirectResponse = $client->request($request, $cancellationToken);
$redirectResponse->setPreviousResponse($response);

Expand Down Expand Up @@ -178,7 +180,7 @@ private function cloneRequest(Request $originalRequest): Request
return $request;
}

private function createRedirectRequest(Request $clonedRequest, Response $response): ?Request
private function updateRequestForRedirect(Request $request, Response $response): ?Request
{
$redirectUri = $this->getRedirectUri($response);
if ($redirectUri === null) {
Expand All @@ -188,21 +190,21 @@ private function createRedirectRequest(Request $clonedRequest, Response $respons
$originalUri = $response->getRequest()->getUri();
$isSameHost = $redirectUri->getAuthority() === $originalUri->getAuthority();

$clonedRequest->setUri($redirectUri);
$request->setUri($redirectUri);

if (!$isSameHost) {
// Avoid copying headers for security reasons, any interceptor headers will be added again,
// but application headers will be discarded.
$clonedRequest->setHeaders([]);
$request->setHeaders([]);
}

if ($this->autoReferrer) {
$this->assignRedirectRefererHeader($clonedRequest, $originalUri, $redirectUri);
$this->assignRedirectRefererHeader($request, $originalUri, $redirectUri);
}

$this->discardResponseBody($response);

return $clonedRequest;
return $request;
}

/**
Expand Down

0 comments on commit 069fdae

Please sign in to comment.