Skip to content
Prev Previous commit
Next Next commit
Make sure to rewind stream.
  • Loading branch information
Nyholm committed Aug 1, 2016
commit ebe52028dc13a85813107f5ef8d71d5b29e1fa63
10 changes: 7 additions & 3 deletions src/Formatter/CurlCommandFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public function formatRequest(RequestInterface $request)
$command .= sprintf(' -H \'%s: %s\'', $name, $request->getHeaderLine($name));
}

$body = $request->getBody()->__toString();
if (!empty($body)) {
$command .= sprintf(' --data \'%s\'', escapeshellarg($body));
$body = $request->getBody();
if ($body->getSize() > 0) {
if (!$body->isSeekable()) {
throw new \RuntimeException('Cannot take data from a stream that is not seekable');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sending an exception here is horrible, i.e. some requests can randomly fail only on debugging (not on prod) in a symfony env with this formatter in the debug toolbar. Can we set a specific exception like : CannotFormatException (or something else) that we can catch in class using this formatter to not show something ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was planning to remove this code when your request cloner is merged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this handled outside? or should we just return something in the text that says there would be the body but we can't read it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
$command .= sprintf(' --data %s', escapeshellarg($body->__toString()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we should have some sort of sanity check to not put huge files in here? say if the body size > 5000 we truncate or just do something like [too large body] in the command? if we use this formatter eg in the symfony debug toolbar, it could break on large files

$body->rewind();
}

return $command;
Expand Down