/** * @return $this * @throws \Exception */ public function doRequest() { $this->responseHeaders = []; $this->responseBody = []; $command = $this->getCurlCommand(); $result = $this->connection->exec($command); if ($result['returnVar'] != 0) { throw new \Exception('Curl error: ' . $this->getCurlError($result['returnVar'])); } if (!isset($result['output'])) { throw new \Exception('No output found'); } // the command FIRST returns the body and THEN the headers (I tried many different ways and no matter // what's redirected to what curl alwyas seems to dump the headers last $httpLine = false; do { $line = array_pop($result['output']); // yes, this is really ugly... // I wish we'd be able to separate header and body in a cleaner way // but we can't do this with exec(), and proc_open also doesn't make things easier if (preg_match('|HTTP/\\d\\.\\d\\s+(\\d+)\\s+.*|', $line, $matches)) { $this->setResponseCode($matches[0]); $httpLine = true; // put the rest back since it belongs to the response body $restOfThatLine = preg_replace('|HTTP/\\d\\.\\d\\s+(\\d+)\\s+.*|', '', $line); if ($restOfThatLine) { array_push($result['output'], $restOfThatLine); } } if (!$httpLine && !empty($line)) { $this->parseHeader($line); } } while (!$httpLine); $this->responseBody = implode("\n", $result['output']); return $this; }
<?php define('CWD', getcwd()); if (is_readable(CWD . DIRECTORY_SEPARATOR . '.env.default')) { $dotenv = new Dotenv\Dotenv(CWD, '.env.default'); $dotenv->overload(); } if (is_readable(CWD . DIRECTORY_SEPARATOR . '.env')) { $dotenv = new Dotenv\Dotenv(CWD); $dotenv->overload(); } register_shutdown_function(function () { \AwsInspector\Ssh\Connection::closeMuxConnections(); });
/** * @test */ public function withJumpHost() { $jumpHost = new Instance(['Tags' => [], 'PrivateIpAddress' => '4.5.7.6']); $connection = new Connection('TestUsername', '1.2.3.4', null, $jumpHost); $this->assertEquals('ssh -o ProxyCommand="ssh -o ConnectTimeout=5 -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ec2-user@4.5.7.6 \'nc %h %p\'" -o ConnectTimeout=5 -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null TestUsername@1.2.3.4', $connection->__toString()); }