-
Notifications
You must be signed in to change notification settings - Fork 53
Add Content-type plugin to set content-type header automatically #85
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
889f2df
Add Content-type plugin to set content-type header automatically (for…
5d43a86
Use guzzle/psr7 implementation for unit tests
9ef69cf
Return early if body size is 0 or unknown
2d1a885
Reset the previous value at the end
bc7b746
Rewind stream before each content test
77d63c4
Add options to skip detection for too large stream
6162941
Skip detection if stream is not seekable
e6ba6f1
Add null comparison
93c6bf2
Check if stream size is null only when skipDetection is true
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
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,109 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Common\Plugin; | ||
|
||
use PhpSpec\Exception\Example\SkippingException; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class ContentTypePluginSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Client\Common\Plugin\ContentTypePlugin'); | ||
} | ||
|
||
function it_is_a_plugin() | ||
{ | ||
$this->shouldImplement('Http\Client\Common\Plugin'); | ||
} | ||
|
||
function it_adds_json_content_type_header(RequestInterface $request) | ||
{ | ||
$request->hasHeader('Content-Type')->shouldBeCalled()->willReturn(false); | ||
$request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for(json_encode(['foo' => 'bar']))); | ||
$request->withHeader('Content-Type', 'application/json')->shouldBeCalled()->willReturn($request); | ||
|
||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_adds_xml_content_type_header(RequestInterface $request) | ||
{ | ||
$request->hasHeader('Content-Type')->shouldBeCalled()->willReturn(false); | ||
$request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('<foo>bar</foo>')); | ||
$request->withHeader('Content-Type', 'application/xml')->shouldBeCalled()->willReturn($request); | ||
|
||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_does_not_set_content_type_header(RequestInterface $request) | ||
{ | ||
$request->hasHeader('Content-Type')->shouldBeCalled()->willReturn(false); | ||
$request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('foo')); | ||
$request->withHeader('Content-Type', null)->shouldNotBeCalled(); | ||
|
||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_does_not_set_content_type_header_if_already_one(RequestInterface $request) | ||
{ | ||
$request->hasHeader('Content-Type')->shouldBeCalled()->willReturn(true); | ||
$request->getBody()->shouldNotBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('foo')); | ||
$request->withHeader('Content-Type', null)->shouldNotBeCalled(); | ||
|
||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_does_not_set_content_type_header_if_size_0_or_unknown(RequestInterface $request) | ||
{ | ||
$request->hasHeader('Content-Type')->shouldBeCalled()->willReturn(false); | ||
$request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for()); | ||
$request->withHeader('Content-Type', null)->shouldNotBeCalled(); | ||
|
||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_adds_xml_content_type_header_if_size_limit_is_not_reached_using_default_value(RequestInterface $request) | ||
{ | ||
$this->beConstructedWith([ | ||
'skip_detection' => true | ||
]); | ||
|
||
$request->hasHeader('Content-Type')->shouldBeCalled()->willReturn(false); | ||
$request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('<foo>bar</foo>')); | ||
$request->withHeader('Content-Type', 'application/xml')->shouldBeCalled()->willReturn($request); | ||
|
||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_adds_xml_content_type_header_if_size_limit_is_not_reached(RequestInterface $request) | ||
{ | ||
$this->beConstructedWith([ | ||
'skip_detection' => true, | ||
'size_limit' => 32000000 | ||
]); | ||
|
||
$request->hasHeader('Content-Type')->shouldBeCalled()->willReturn(false); | ||
$request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('<foo>bar</foo>')); | ||
$request->withHeader('Content-Type', 'application/xml')->shouldBeCalled()->willReturn($request); | ||
|
||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_does_not_set_content_type_header_if_size_limit_is_reached(RequestInterface $request) | ||
{ | ||
$this->beConstructedWith([ | ||
'skip_detection' => true, | ||
'size_limit' => 8 | ||
]); | ||
|
||
$request->hasHeader('Content-Type')->shouldBeCalled()->willReturn(false); | ||
$request->getBody()->shouldBeCalled()->willReturn(\GuzzleHttp\Psr7\stream_for('<foo>bar</foo>')); | ||
$request->withHeader('Content-Type', null)->shouldNotBeCalled(); | ||
|
||
$this->handleRequest($request, function () {}, 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,123 @@ | ||
<?php | ||
|
||
namespace Http\Client\Common\Plugin; | ||
|
||
use Http\Client\Common\Plugin; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Allow to set the correct content type header on the request automatically only if it is not set. | ||
* | ||
* @author Karim Pinchon <[email protected]> | ||
*/ | ||
final class ContentTypePlugin implements Plugin | ||
{ | ||
/** | ||
* Allow to disable the content type detection when stream is too large (as it can consume a lot of resource). | ||
* | ||
* @var bool | ||
* | ||
* true skip the content type detection | ||
* false detect the content type (default value) | ||
*/ | ||
protected $skipDetection; | ||
|
||
/** | ||
* Determine the size stream limit for which the detection as to be skipped (default to 16Mb). | ||
* | ||
* @var int | ||
*/ | ||
protected $sizeLimit; | ||
|
||
/** | ||
* @param array $config { | ||
* | ||
* @var bool $skip_detection True skip detection if stream size is bigger than $size_limit. | ||
* @var int $size_limit size stream limit for which the detection as to be skipped. | ||
* } | ||
*/ | ||
public function __construct(array $config = []) | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults([ | ||
'skip_detection' => false, | ||
'size_limit' => 16000000, | ||
]); | ||
$resolver->setAllowedTypes('skip_detection', 'bool'); | ||
$resolver->setAllowedTypes('size_limit', 'int'); | ||
|
||
$options = $resolver->resolve($config); | ||
|
||
$this->skipDetection = $options['skip_detection']; | ||
$this->sizeLimit = $options['size_limit']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleRequest(RequestInterface $request, callable $next, callable $first) | ||
{ | ||
if (!$request->hasHeader('Content-Type')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should early return here if the body size of the stream is equal to 0 |
||
$stream = $request->getBody(); | ||
$streamSize = $stream->getSize(); | ||
|
||
if (!$stream->isSeekable()) { | ||
return $next($request); | ||
} | ||
|
||
if (0 === $streamSize) { | ||
return $next($request); | ||
} | ||
|
||
if ($this->skipDetection && (null === $streamSize || $streamSize >= $this->sizeLimit)) { | ||
return $next($request); | ||
} | ||
|
||
if ($this->isJson($stream)) { | ||
$request = $request->withHeader('Content-Type', 'application/json'); | ||
|
||
return $next($request); | ||
} | ||
|
||
if ($this->isXml($stream)) { | ||
$request = $request->withHeader('Content-Type', 'application/xml'); | ||
|
||
return $next($request); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some content type that can be added to check (but not necessarly in this PR) :
|
||
} | ||
|
||
return $next($request); | ||
} | ||
|
||
/** | ||
* @param $stream StreamInterface | ||
* | ||
* @return bool | ||
*/ | ||
private function isJson($stream) | ||
{ | ||
$stream->rewind(); | ||
|
||
json_decode($stream->getContents()); | ||
|
||
return json_last_error() == JSON_ERROR_NONE; | ||
} | ||
|
||
/** | ||
* @param $stream StreamInterface | ||
* | ||
* @return \SimpleXMLElement|false | ||
*/ | ||
private function isXml($stream) | ||
{ | ||
$stream->rewind(); | ||
|
||
$previousValue = libxml_use_internal_errors(true); | ||
$isXml = simplexml_load_string($stream->getContents()); | ||
libxml_use_internal_errors($previousValue); | ||
|
||
return $isXml; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid content type detection if stream is not rewindable