Skip to content

fixed detection of binary strings (#132) #133

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 1 commit into from
Oct 13, 2020
Merged
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
18 changes: 18 additions & 0 deletions spec/Formatter/CurlCommandFormatterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ function it_formats_requests_with_null_bytes(RequestInterface $request, UriInter
$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data '[binary stream omitted]'");
}

function it_formats_requests_with_line_break(RequestInterface $request, UriInterface $uri, StreamInterface $body)
{
$request->getUri()->willReturn($uri);
$request->getBody()->willReturn($body);

$body->__toString()->willReturn("foo\nbar");
$body->getSize()->willReturn(1);
$body->isSeekable()->willReturn(true);
$body->rewind()->willReturn(true);

$uri->withFragment('')->willReturn('http://foo.com/bar');
$request->getMethod()->willReturn('POST');
$request->getProtocolVersion()->willReturn('1.1');
$request->getHeaders()->willReturn([]);

$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data 'foo\nbar'");
}

function it_formats_requests_with_nonseekable_body(RequestInterface $request, UriInterface $uri, StreamInterface $body)
{
$request->getUri()->willReturn($uri);
Expand Down
22 changes: 22 additions & 0 deletions spec/Formatter/FullHttpMessageFormatterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,28 @@ function it_omits_body_with_null_bytes(RequestInterface $request, StreamInterfac
GET /foo HTTP/1.1

[binary stream omitted]
STR;
$this->formatRequest($request)->shouldReturn($expectedMessage);
}

function it_omits_body_with_line_break(RequestInterface $request, StreamInterface $stream)
{
$this->beConstructedWith(7);

$stream->isSeekable()->willReturn(true);
$stream->rewind()->shouldBeCalled();
$stream->__toString()->willReturn("foo\nbar");
$request->getBody()->willReturn($stream);
$request->getMethod()->willReturn('GET');
$request->getRequestTarget()->willReturn('/foo');
$request->getProtocolVersion()->willReturn('1.1');
$request->getHeaders()->willReturn([]);

$expectedMessage = <<<STR
GET /foo HTTP/1.1

foo
bar
STR;
$this->formatRequest($request)->shouldReturn($expectedMessage);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Formatter/CurlCommandFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function formatRequest(RequestInterface $request)
} elseif ($body->isSeekable()) {
$data = $body->__toString();
$body->rewind();
if (preg_match('/[\x00-\x1F\x7F]/', $data)) {
// all non-printable ASCII characters and <DEL> except for \t, \r, \n
if (preg_match('/([\x00-\x09\x0C\x0E-\x1F\x7F])/', $data)) {
$data = '[binary stream omitted]';
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/Formatter/FullHttpMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ private function addBody(MessageInterface $request, $message)
$data = $stream->__toString();
$stream->rewind();

if (preg_match('/[\x00-\x1F\x7F]/', $data)) {
// all non-printable ASCII characters and <DEL> except for \t, \r, \n
if (preg_match('/([\x00-\x09\x0C\x0E-\x1F\x7F])/', $data)) {
return $message.'[binary stream omitted]';
}

Expand Down