Skip to content

Commit

Permalink
Merge pull request #1 from adduc/20-large-request-bodies
Browse files Browse the repository at this point in the history
Add support for formatting large request bodies
  • Loading branch information
wadjeroudi authored Oct 18, 2019
2 parents 119f5b4 + e1a7105 commit e1c8018
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.0",
"psr/log": "^1.0"
"psr/log": "^1.0",
"symfony/process": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.2.2"
Expand Down
18 changes: 13 additions & 5 deletions src/Formatter/CurlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Cookie\CookieJarInterface;
use GuzzleHttp\Cookie\SetCookie;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\Process\Process;

/**
* Class CurlFormatter it formats a Guzzle request to a cURL shell command
Expand Down Expand Up @@ -134,7 +135,7 @@ protected function extractBodyArgument(RequestInterface $request)
if ($contents) {
// clean input of null bytes
$contents = str_replace(chr(0), '', $contents);
$this->addOption('d', escapeshellarg($contents));
$this->addOption('d', $this->escapeShellArgument($contents));
}

//if get request has data Add G otherwise curl will make a post request
Expand Down Expand Up @@ -168,7 +169,7 @@ protected function extractCookiesArgument(RequestInterface $request, array $opti
}

if ($values) {
$this->addOption('b', escapeshellarg(implode('; ', $values)));
$this->addOption('b', $this->escapeShellArgument(implode('; ', $values)));
}
}

Expand All @@ -183,12 +184,12 @@ protected function extractHeadersArgument(RequestInterface $request)
}

if ('user-agent' === strtolower($name)) {
$this->addOption('A', escapeshellarg($header[0]));
$this->addOption('A', $this->escapeShellArgument($header[0]));
continue;
}

foreach ((array)$header as $headerValue) {
$this->addOption('H', escapeshellarg("{$name}: {$headerValue}"));
$this->addOption('H', $this->escapeShellArgument("{$name}: {$headerValue}"));
}
}
}
Expand Down Expand Up @@ -228,6 +229,13 @@ protected function extractArguments(RequestInterface $request, array $options)
*/
protected function extractUrlArgument(RequestInterface $request)
{
$this->addCommandPart(escapeshellarg((string)$request->getUri()->withFragment('')));
$this->addCommandPart($this->escapeShellArgument((string)$request->getUri()->withFragment('')));
}

protected function escapeShellArgument($argument)
{
$process = new Process([$argument]);
$escaped = $process->getCommandLine();
return $escaped;
}
}
9 changes: 9 additions & 0 deletions tests/Formatter/CurlFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,13 @@ public function getHeadersAndBodyData()
],
];
}

public function testLongArgument()
{
ini_set('memory_limit', -1);
$body = str_repeat('A', 1024*1024*64);
$request = new Request('POST', 'http://example.local', [], \GuzzleHttp\Psr7\stream_for($body));

$curl = $this->curlFormatter->format($request);
}
}

0 comments on commit e1c8018

Please sign in to comment.