Skip to content

Commit 0e44789

Browse files
authored
Merge pull request #133 from driehle/bugfix/binary-detection
fixed detection of binary strings (#132)
2 parents 9af3759 + b2afb6b commit 0e44789

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

spec/Formatter/CurlCommandFormatterSpec.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ function it_formats_requests_with_null_bytes(RequestInterface $request, UriInter
9191
$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data '[binary stream omitted]'");
9292
}
9393

94+
function it_formats_requests_with_line_break(RequestInterface $request, UriInterface $uri, StreamInterface $body)
95+
{
96+
$request->getUri()->willReturn($uri);
97+
$request->getBody()->willReturn($body);
98+
99+
$body->__toString()->willReturn("foo\nbar");
100+
$body->getSize()->willReturn(1);
101+
$body->isSeekable()->willReturn(true);
102+
$body->rewind()->willReturn(true);
103+
104+
$uri->withFragment('')->willReturn('http://foo.com/bar');
105+
$request->getMethod()->willReturn('POST');
106+
$request->getProtocolVersion()->willReturn('1.1');
107+
$request->getHeaders()->willReturn([]);
108+
109+
$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data 'foo\nbar'");
110+
}
111+
94112
function it_formats_requests_with_nonseekable_body(RequestInterface $request, UriInterface $uri, StreamInterface $body)
95113
{
96114
$request->getUri()->willReturn($uri);

spec/Formatter/FullHttpMessageFormatterSpec.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@ function it_omits_body_with_null_bytes(RequestInterface $request, StreamInterfac
245245
GET /foo HTTP/1.1
246246
247247
[binary stream omitted]
248+
STR;
249+
$this->formatRequest($request)->shouldReturn($expectedMessage);
250+
}
251+
252+
function it_omits_body_with_line_break(RequestInterface $request, StreamInterface $stream)
253+
{
254+
$this->beConstructedWith(7);
255+
256+
$stream->isSeekable()->willReturn(true);
257+
$stream->rewind()->shouldBeCalled();
258+
$stream->__toString()->willReturn("foo\nbar");
259+
$request->getBody()->willReturn($stream);
260+
$request->getMethod()->willReturn('GET');
261+
$request->getRequestTarget()->willReturn('/foo');
262+
$request->getProtocolVersion()->willReturn('1.1');
263+
$request->getHeaders()->willReturn([]);
264+
265+
$expectedMessage = <<<STR
266+
GET /foo HTTP/1.1
267+
268+
foo
269+
bar
248270
STR;
249271
$this->formatRequest($request)->shouldReturn($expectedMessage);
250272
}

src/Formatter/CurlCommandFormatter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function formatRequest(RequestInterface $request)
4242
} elseif ($body->isSeekable()) {
4343
$data = $body->__toString();
4444
$body->rewind();
45-
if (preg_match('/[\x00-\x1F\x7F]/', $data)) {
45+
// all non-printable ASCII characters and <DEL> except for \t, \r, \n
46+
if (preg_match('/([\x00-\x09\x0C\x0E-\x1F\x7F])/', $data)) {
4647
$data = '[binary stream omitted]';
4748
}
4849
} else {

src/Formatter/FullHttpMessageFormatter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ private function addBody(MessageInterface $request, $message)
8686
$data = $stream->__toString();
8787
$stream->rewind();
8888

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

0 commit comments

Comments
 (0)