Skip to content

Commit 230c60d

Browse files
Apply suggestions from code review by @SimonFrings
Co-authored-by: Simon Frings <[email protected]>
1 parent f56c1fd commit 230c60d

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

src/Io/Transaction.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -264,28 +264,23 @@ private function onResponseRedirect(ResponseInterface $response, RequestInterfac
264264
*/
265265
private function makeRedirectRequest(RequestInterface $request, UriInterface $location, $statusCode)
266266
{
267-
$originalHost = $request->getUri()->getHost();
268-
$request = $request->withoutHeader('Host')->withUri($location);
269-
270-
if (!$this->isTemporaryOrPermanentRedirect($statusCode)) {
271-
$request = $request
272-
->withoutHeader('Content-Type')
273-
->withoutHeader('Content-Length');
274-
}
275267

276268
// Remove authorization if changing hostnames (but not if just changing ports or protocols).
269+
$originalHost = $request->getUri()->getHost();
277270
if ($location->getHost() !== $originalHost) {
278271
$request = $request->withoutHeader('Authorization');
279272
}
280273

281-
if ($this->isTemporaryOrPermanentRedirect($statusCode)) {
274+
if ($statusCode === Response::STATUS_TEMPORARY_REDIRECT || $statusCode === Response::STATUS_PERMANENT_REDIRECT) {
282275
if ($request->getBody() instanceof ReadableStreamInterface) {
283276
throw new \RuntimeException('Unable to redirect request with streaming body');
284277
}
285278
} else {
286-
// naïve approach..
287-
$method = ($request->getMethod() === 'HEAD') ? 'HEAD' : 'GET';
288-
$request = $request->withMethod($method)->withBody(new EmptyBodyStream());
279+
$request = $request
280+
->withMethod($request->getMethod() === 'HEAD' ? 'HEAD' : 'GET')
281+
->withoutHeader('Content-Type')
282+
->withoutHeader('Content-Length')
283+
->withBody(new EmptyBodyStream());
289284
}
290285

291286
return $request;
@@ -311,14 +306,4 @@ private function progress($name, array $args = array())
311306
echo PHP_EOL;
312307
}
313308

314-
/**
315-
* @param $statusCode
316-
* @return bool
317-
*/
318-
public function isTemporaryOrPermanentRedirect($statusCode)
319-
{
320-
return
321-
$statusCode === Response::STATUS_TEMPORARY_REDIRECT
322-
|| $statusCode === Response::STATUS_PERMANENT_REDIRECT;
323-
}
324309
}

tests/Io/TransactionTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,13 @@ public function testRedirectingStreamingBodyWith307Or308ShouldThrowCantRedirectS
768768
$transaction = new Transaction($sender, $loop);
769769
$promise = $transaction->send($request);
770770

771-
$this->setExpectedException('RuntimeException', 'Unable to redirect request with streaming body');
772-
\React\Async\await($promise, $loop);
771+
$exception = null;
772+
$promise->then(null, function ($reason) use (&$exception) {
773+
$exception = $reason;
774+
});
775+
776+
assert($exception instanceof RuntimeException);
777+
$this->assertEquals('Unable to redirect request with streaming body', $exception->getMessage());
773778
}
774779

775780
public function testCancelTransactionWillCancelRequest()

0 commit comments

Comments
 (0)