-
Notifications
You must be signed in to change notification settings - Fork 53
Add always seekable body plugin #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,98 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Common\Plugin; | ||
|
||
use Http\Client\Common\Plugin; | ||
use Http\Client\Common\Plugin\AlwaysSeekableBodyPlugin; | ||
use Http\Client\Promise\HttpFulfilledPromise; | ||
use Http\Message\Stream\BufferedStream; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
class AlwaysSeekableBodyPluginSpec extends ObjectBehavior | ||
{ | ||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(AlwaysSeekableBodyPlugin::class); | ||
} | ||
|
||
public function it_is_a_plugin() | ||
{ | ||
$this->shouldImplement(Plugin::class); | ||
} | ||
|
||
public function it_decorate_response_body_if_not_seekable(RequestInterface $request, ResponseInterface $response, StreamInterface $responseStream, StreamInterface $requestStream) | ||
{ | ||
$next = function () use ($response) { | ||
return new HttpFulfilledPromise($response->getWrappedObject()); | ||
}; | ||
|
||
$request->getBody()->shouldBeCalled()->willReturn($requestStream); | ||
$requestStream->isSeekable()->shouldBeCalled()->willReturn(true); | ||
|
||
$response->getBody()->shouldBeCalled()->willReturn($responseStream); | ||
$responseStream->isSeekable()->shouldBeCalled()->willReturn(false); | ||
$responseStream->getSize()->willReturn(null); | ||
|
||
$response->withBody(Argument::type(BufferedStream::class))->shouldBeCalled()->willReturn($response); | ||
|
||
$this->handleRequest($request, $next, function () {}); | ||
} | ||
|
||
public function it_does_not_decorate_response_body_if_seekable(RequestInterface $request, ResponseInterface $response, StreamInterface $responseStream, StreamInterface $requestStream) | ||
{ | ||
$next = function () use ($response) { | ||
return new HttpFulfilledPromise($response->getWrappedObject()); | ||
}; | ||
|
||
$request->getBody()->shouldBeCalled()->willReturn($requestStream); | ||
$requestStream->isSeekable()->shouldBeCalled()->willReturn(true); | ||
|
||
$response->getBody()->shouldBeCalled()->willReturn($responseStream); | ||
$responseStream->isSeekable()->shouldBeCalled()->willReturn(true); | ||
$responseStream->getSize()->willReturn(null); | ||
|
||
$response->withBody(Argument::type(BufferedStream::class))->shouldNotBeCalled(); | ||
|
||
$this->handleRequest($request, $next, function () {}); | ||
} | ||
|
||
public function it_decorate_request_body_if_not_seekable(RequestInterface $request, ResponseInterface $response, StreamInterface $responseStream, StreamInterface $requestStream) | ||
{ | ||
$next = function () use ($response) { | ||
return new HttpFulfilledPromise($response->getWrappedObject()); | ||
}; | ||
|
||
$response->getBody()->willReturn($responseStream); | ||
$responseStream->isSeekable()->willReturn(true); | ||
|
||
$request->getBody()->shouldBeCalled()->willReturn($requestStream); | ||
$requestStream->isSeekable()->shouldBeCalled()->willReturn(false); | ||
$requestStream->getSize()->willReturn(null); | ||
|
||
$request->withBody(Argument::type(BufferedStream::class))->shouldBeCalled(); | ||
|
||
$this->handleRequest($request, $next, function () {}); | ||
} | ||
|
||
public function it_does_not_decorate_request_body_if_seekable(RequestInterface $request, ResponseInterface $response, StreamInterface $responseStream, StreamInterface $requestStream) | ||
{ | ||
$next = function () use ($response) { | ||
return new HttpFulfilledPromise($response->getWrappedObject()); | ||
}; | ||
|
||
$response->getBody()->willReturn($responseStream); | ||
$responseStream->isSeekable()->willReturn(true); | ||
|
||
$request->getBody()->shouldBeCalled()->willReturn($requestStream); | ||
$requestStream->isSeekable()->shouldBeCalled()->willReturn(true); | ||
$requestStream->getSize()->willReturn(null); | ||
|
||
$request->withBody(Argument::type(BufferedStream::class))->shouldNotBeCalled(); | ||
|
||
$this->handleRequest($request, $next, function () {}); | ||
} | ||
} |
This file contains hidden or 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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Http\Client\Common\Plugin; | ||
|
||
use Http\Client\Common\Plugin; | ||
use Http\Message\Stream\BufferedStream; | ||
use Http\Promise\Promise; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Decorate the body of the request and the response if it's not seekable by using Http\Message\Stream\BufferedStream. | ||
dbu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @author Joel Wurtz <[email protected]> | ||
*/ | ||
final class AlwaysSeekableBodyPlugin implements Plugin | ||
{ | ||
private $useFileBuffer; | ||
|
||
private $memoryBufferSize; | ||
|
||
/** | ||
* @param array $config { | ||
* | ||
* @var bool $use_file_buffer Whether this plugin should use a file as a buffer if the stream is too big, defaults to true | ||
* @var int $memory_buffer_size Max memory size in bytes to use for the buffer before it use a file, defaults to 2097152 (2 mb) | ||
* } | ||
*/ | ||
public function __construct(array $config = []) | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults([ | ||
'use_file_buffer' => true, | ||
'memory_buffer_size' => 2097152, | ||
]); | ||
$resolver->setAllowedTypes('use_file_buffer', 'bool'); | ||
$resolver->setAllowedTypes('memory_buffer_size', 'int'); | ||
|
||
$options = $resolver->resolve($config); | ||
|
||
$this->useFileBuffer = $options['use_file_buffer']; | ||
$this->memoryBufferSize = $options['memory_buffer_size']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise | ||
{ | ||
if (!$request->getBody()->isSeekable()) { | ||
$request = $request->withBody(new BufferedStream($request->getBody(), $this->useFileBuffer, $this->memoryBufferSize)); | ||
} | ||
|
||
return $next($request)->then(function (ResponseInterface $response) { | ||
if ($response->getBody()->isSeekable()) { | ||
return $response; | ||
} | ||
|
||
return $response->withBody(new BufferedStream($response->getBody(), $this->useFileBuffer, $this->memoryBufferSize)); | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.