Skip to content

Commit c7f6a70

Browse files
authored
Merge pull request #92 from z38/binary-body
Improve formatting of requests with binary streams
2 parents 977edb5 + 3b410c5 commit c7f6a70

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Change Log
22

33

4+
## Unreleased
5+
6+
### Fixed
7+
8+
- Fix CurlCommandFormatter for binary request payloads
9+
10+
411
## 1.6.0 - 2017-07-05
512

613
### Added

spec/Formatter/CurlCommandFormatterSpec.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,40 @@ function it_formats_the_request_with_user_agent(RequestInterface $request, UriIn
7272

7373
$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' -A 'foobar-browser'");
7474
}
75+
76+
function it_formats_requests_with_null_bytes(RequestInterface $request, UriInterface $uri, StreamInterface $body)
77+
{
78+
$request->getUri()->willReturn($uri);
79+
$request->getBody()->willReturn($body);
80+
81+
$body->__toString()->willReturn("\0");
82+
$body->getSize()->willReturn(1);
83+
$body->isSeekable()->willReturn(true);
84+
$body->rewind()->willReturn(true);
85+
86+
$uri->withFragment('')->willReturn('http://foo.com/bar');
87+
$request->getMethod()->willReturn('POST');
88+
$request->getProtocolVersion()->willReturn('1.1');
89+
$request->getHeaders()->willReturn([]);
90+
91+
$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data '[binary stream omitted]'");
92+
}
93+
94+
function it_formats_requests_with_nonseekable_body(RequestInterface $request, UriInterface $uri, StreamInterface $body)
95+
{
96+
$request->getUri()->willReturn($uri);
97+
$request->getBody()->willReturn($body);
98+
99+
$body->getSize()->willReturn(1);
100+
$body->isSeekable()->willReturn(false);
101+
$body->__toString()->shouldNotBeCalled();
102+
$body->rewind()->shouldNotBeCalled();
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 '[non-seekable stream omitted]'");
110+
}
75111
}

src/Formatter/CurlCommandFormatter.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ public function formatRequest(RequestInterface $request)
3636

3737
$body = $request->getBody();
3838
if ($body->getSize() > 0) {
39-
if (!$body->isSeekable()) {
40-
return 'Cant format Request as cUrl command if body stream is not seekable.';
39+
if ($body->isSeekable()) {
40+
$data = $body->__toString();
41+
$body->rewind();
42+
if (preg_match('/[\x00-\x1F\x7F]/', $data)) {
43+
$data = '[binary stream omitted]';
44+
}
45+
} else {
46+
$data = '[non-seekable stream omitted]';
4147
}
42-
$command .= sprintf(' --data %s', escapeshellarg($body->__toString()));
43-
$body->rewind();
48+
$command .= sprintf(' --data %s', escapeshellarg($data));
4449
}
4550

4651
return $command;

0 commit comments

Comments
 (0)