Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code moved from amp/http-client PR #270
- Loading branch information
Showing
12 changed files
with
1,257 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,46 @@ | ||
<?php | ||
|
||
use Amp\Http\Client\HttpClientBuilder; | ||
use Amp\Http\Client\Psr7\PsrAdapter; | ||
use Amp\Http\Client\Request; | ||
use Amp\Http\Client\Response; | ||
use Amp\Loop; | ||
use GuzzleHttp\Client; | ||
use Laminas\Diactoros\RequestFactory; | ||
use Laminas\Diactoros\ResponseFactory; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
Loop::run(static function () { | ||
$httpClient = (new HttpClientBuilder) | ||
->build(); | ||
Loop::run( | ||
static function () { | ||
$httpClient = (new HttpClientBuilder) | ||
->build(); | ||
|
||
/** @var Response $firstResponse */ | ||
$firstResponse = yield $httpClient->request(new Request('https://google.com/')); | ||
yield $firstResponse->getBody()->buffer(); | ||
$psrAdapter = new PsrAdapter(); | ||
$psrResponseFactory = new ResponseFactory(); | ||
$psrRequestFactory = new RequestFactory(); | ||
|
||
/** @var Response $secondResponse */ | ||
$secondResponse = yield $httpClient->request(new Request('https://google.com/')); | ||
yield $secondResponse->getBody()->buffer(); | ||
}); | ||
$firstPsrRequest = $psrRequestFactory->createRequest('GET', 'https://google.com/'); | ||
/** @var Request $firstAmpRequest */ | ||
$firstAmpRequest = yield $psrAdapter->fromPsrRequest($firstPsrRequest); | ||
// TODO: Investigate if this client bug or if Host header must be cleaned automatically | ||
$firstAmpRequest->removeHeader('Host'); | ||
/** @var Response $firstAmpResponse */ | ||
$firstAmpResponse = yield $httpClient->request($firstAmpRequest); | ||
/** @var ResponseInterface $firstPsrResponse */ | ||
$firstPsrResponse = yield $psrAdapter->toPsrResponse($psrResponseFactory, $firstAmpResponse); | ||
$body = $firstPsrResponse->getBody(); | ||
$body->rewind(); | ||
$body->getContents(); | ||
|
||
$secondAmpRequest = new Request('https://google.com/'); | ||
/** @var RequestInterface $secondPsrRequest */ | ||
$secondPsrRequest = yield $psrAdapter->toPsrRequest($psrRequestFactory, $secondAmpRequest); | ||
$secondPsrResponse = (new Client)->send($secondPsrRequest); | ||
/** @var Response $secondAmpResponse */ | ||
$secondAmpResponse = yield $psrAdapter->fromPsrResponse($secondPsrResponse, $secondAmpRequest); | ||
yield $secondAmpResponse->getBody()->buffer(); | ||
} | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Amp\Http\Client\Psr7\Internal; | ||
|
||
use Amp\ByteStream\InputStream; | ||
use Amp\Promise; | ||
use Amp\Success; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class PsrInputStream implements InputStream | ||
{ | ||
private const DEFAULT_CHUNK_SIZE = 8192; | ||
|
||
/** | ||
* @var StreamInterface | ||
*/ | ||
private $stream; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $chunkSize; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $tryRewind = true; | ||
|
||
public function __construct(StreamInterface $stream, int $chunkSize = self::DEFAULT_CHUNK_SIZE) | ||
{ | ||
if ($chunkSize < 1) { | ||
throw new \Error("Invalid chunk size: {$chunkSize}"); | ||
} | ||
$this->stream = $stream; | ||
$this->chunkSize = $chunkSize; | ||
} | ||
|
||
public function read(): Promise | ||
{ | ||
if (!$this->stream->isReadable()) { | ||
return new Success(); | ||
} | ||
if ($this->tryRewind) { | ||
$this->tryRewind = false; | ||
if ($this->stream->isSeekable()) { | ||
$this->stream->rewind(); | ||
} | ||
} | ||
if ($this->stream->eof()) { | ||
return new Success(); | ||
} | ||
|
||
$data = $this->stream->read($this->chunkSize); | ||
|
||
return new Success($data); | ||
} | ||
} |
Oops, something went wrong.