diff --git a/CHANGELOG.md b/CHANGELOG.md index 7676349..1cf58e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Change Log +## Unreleased + +### Fixed + +- Fix CurlCommandFormatter for binary request payloads + + ## 1.6.0 - 2017-07-05 ### Added diff --git a/spec/Formatter/CurlCommandFormatterSpec.php b/spec/Formatter/CurlCommandFormatterSpec.php index 4352827..5340b62 100644 --- a/spec/Formatter/CurlCommandFormatterSpec.php +++ b/spec/Formatter/CurlCommandFormatterSpec.php @@ -72,4 +72,40 @@ function it_formats_the_request_with_user_agent(RequestInterface $request, UriIn $this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' -A 'foobar-browser'"); } + + function it_formats_requests_with_null_bytes(RequestInterface $request, UriInterface $uri, StreamInterface $body) + { + $request->getUri()->willReturn($uri); + $request->getBody()->willReturn($body); + + $body->__toString()->willReturn("\0"); + $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 '[binary stream omitted]'"); + } + + function it_formats_requests_with_nonseekable_body(RequestInterface $request, UriInterface $uri, StreamInterface $body) + { + $request->getUri()->willReturn($uri); + $request->getBody()->willReturn($body); + + $body->getSize()->willReturn(1); + $body->isSeekable()->willReturn(false); + $body->__toString()->shouldNotBeCalled(); + $body->rewind()->shouldNotBeCalled(); + + $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 '[non-seekable stream omitted]'"); + } } diff --git a/src/Formatter/CurlCommandFormatter.php b/src/Formatter/CurlCommandFormatter.php index f0375fd..5cdd427 100644 --- a/src/Formatter/CurlCommandFormatter.php +++ b/src/Formatter/CurlCommandFormatter.php @@ -36,11 +36,16 @@ public function formatRequest(RequestInterface $request) $body = $request->getBody(); if ($body->getSize() > 0) { - if (!$body->isSeekable()) { - return 'Cant format Request as cUrl command if body stream is not seekable.'; + if ($body->isSeekable()) { + $data = $body->__toString(); + $body->rewind(); + if (preg_match('/[\x00-\x1F\x7F]/', $data)) { + $data = '[binary stream omitted]'; + } + } else { + $data = '[non-seekable stream omitted]'; } - $command .= sprintf(' --data %s', escapeshellarg($body->__toString())); - $body->rewind(); + $command .= sprintf(' --data %s', escapeshellarg($data)); } return $command;