Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/RequestResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class RequestResolver {
private array $headerList;
/** @var array<string|null> */
private array $integrityList;
/** @var array<int|null> */
private array $maxRedirectsList;
/** @var array<object|null> */
private array $signalList;

Expand All @@ -38,6 +40,7 @@ public function __construct(
$this->responseList = [];
$this->headerList = [];
$this->integrityList = [];
$this->maxRedirectsList = [];
$this->signalList = [];
}

Expand Down Expand Up @@ -92,6 +95,10 @@ public function add(
array_push($this->curlMultiList, $curlMulti);
array_push($this->deferredList, $deferred);
array_push($this->integrityList, $integrity);
array_push(
$this->maxRedirectsList,
$curlOptArray[CURLOPT_MAXREDIRS] ?? null
);
array_push($this->responseList, $bodyResponse);
array_push($this->headerList, "");
array_push($this->signalList, $signal);
Expand Down Expand Up @@ -198,7 +205,7 @@ private function writeHeader(
}

if(str_starts_with(strtolower($headerLine), "location: ")) {
if($ch->getInfo(CURLOPT_MAXREDIRS) === 0) {
if($this->maxRedirectsList[$i] === 0) {
throw new FetchException("Redirect is disallowed");
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/phpunit/Helper/NativeHandleTestCurl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace GT\Fetch\Test\Helper;

class NativeHandleTestCurl extends TestCurl {
public function getHandle() {
return $this->ch;
}
}
12 changes: 7 additions & 5 deletions test/phpunit/Helper/ResponseSimulator.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace GT\Fetch\Test\Helper;

use GT\Curl\Curl;
use GT\Curl\CurlInterface;

class ResponseSimulator {
const RANDOM_BODY_WORDS = ["pursuit","forest","gravel","timber","wonder","eject","slogan","monkey","construct","earthquake","respect","publish","forward","circle","summer","define","highlight","refuse","salon","theater","lily","earwax","variant","account","resource"];
Expand All @@ -25,6 +25,7 @@ static public function setBodyCallback(callable $callback) {

static public function start() {
self::$started = true;
self::$redirectAdded = false;
self::$headerBuffer = self::generateHeaders();
self::$bodyBuffer = self::generateBody();
}
Expand Down Expand Up @@ -73,11 +74,11 @@ static public function hasStarted():bool {
return self::$started;
}

static public function sendChunk(Curl $ch):int {
static public function sendChunk(CurlInterface $ch):int {
// TODO: If the URL is test://should-redirect, send the redirect header here
$url = $ch->getInfo(CURLINFO_EFFECTIVE_URL);
if($url === "test://should-redirect") {
$locationRedirect = "Location: /redirected";
$locationRedirect = "Location: /redirected\r\n";
if(!self::$redirectAdded) {
array_splice(self::$headerBuffer, 1, 0, $locationRedirect);
self::$redirectAdded = true;
Expand All @@ -88,7 +89,7 @@ static public function sendChunk(Curl $ch):int {

call_user_func(
self::$headerCallback,
$ch,
$ch->getHandle(),
$data
);

Expand All @@ -100,13 +101,14 @@ static public function sendChunk(Curl $ch):int {

call_user_func(
self::$bodyCallback,
$ch,
$ch->getHandle(),
$data
);

return 1;
}
else {
self::$started = false;
return 0;
}
}
Expand Down
46 changes: 46 additions & 0 deletions test/phpunit/RequestResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace GT\Fetch\Test;

use GT\Fetch\FetchException;
use GT\Fetch\RequestResolver;
use GT\Fetch\Test\Helper\NativeHandleTestCurl;
use GT\Fetch\Test\Helper\TestCurlMulti;
use Gt\Async\Loop;
use Gt\Http\Response;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use ReflectionProperty;

class RequestResolverTest extends TestCase {
public function testWriteHeaderRejectsRedirectsForNativeCurlHandle():void {
$sut = new RequestResolver(
new Loop(),
NativeHandleTestCurl::class,
TestCurlMulti::class,
);
$curl = new NativeHandleTestCurl("test://should-redirect");
$response = new Response();
$response->startDeferredResponse($curl);

$this->setProperty($sut, "curlList", [$curl]);
$this->setProperty($sut, "responseList", [$response]);
$this->setProperty($sut, "headerList", [""]);
$this->setProperty($sut, "maxRedirectsList", [0]);
$this->setProperty($sut, "signalList", [null]);

$writeHeader = new ReflectionMethod($sut, "writeHeader");

self::expectException(FetchException::class);
self::expectExceptionMessage("Redirect is disallowed");
$writeHeader->invoke($sut, $curl->getHandle(), "Location: /redirected\r\n");
}

private function setProperty(
RequestResolver $requestResolver,
string $property,
array $value,
):void {
$reflectionProperty = new ReflectionProperty($requestResolver, $property);
$reflectionProperty->setValue($requestResolver, $value);
}
}
Loading