Skip to content

Commit

Permalink
Replaced copied guzzle/guzzle parsers with guzzlehttp/psr7 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-konshin committed Aug 5, 2015
1 parent 64b01fa commit 2743d30
Showing 1 changed file with 8 additions and 90 deletions.
98 changes: 8 additions & 90 deletions src/http/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace RingCentral\http;

use Exception;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use stdClass;
Expand Down Expand Up @@ -44,25 +43,17 @@ public function __construct(RequestInterface $request = null, $body = null, $sta
{

$this->request = $request;
$this->body = $body;
$this->raw = $body;

if (is_string($body)) {

// Make the HTTP message complete
if (substr($body, 0, 5) !== 'HTTP/') {
$body = "HTTP/1.1 " . $status . "\r\n" . $body;
}

$r = $this->parseResponse($body);

if (!$r) {
throw new \InvalidArgumentException('Message was empty');
}

$this->response = new Response($r['code'], $r['headers'], $r['body'], $r['version'], $r['reason_phrase']);
$body = (string)$body;

// Make the HTTP message complete
if (substr($body, 0, 5) !== 'HTTP/') {
$body = "HTTP/1.1 " . $status . " OK\r\n" . $body;
}

$this->response = \GuzzleHttp\Psr7\parse_response($body);

}

public function getText()
Expand Down Expand Up @@ -161,7 +152,7 @@ public function getMultipart()

// Step 2. Split by boundary and remove first and last parts if needed

$parts = explode('--' . $boundary . '', (string)$this->response->getBody());
$parts = explode('--' . $boundary . '', (string)$this->response->getBody()); //TODO Handle as stream

if (empty($parts[0])) {
array_shift($parts);
Expand Down Expand Up @@ -265,77 +256,4 @@ protected function getContentType()
return $this->response->getHeaderLine('content-type');
}

/**
* Ported from guzzle/guzzle
* @param $message
* @return array
*/
protected function parseMessage($message)
{
$startLine = null;
$headers = array();
$body = '';

// Iterate over each line in the message, accounting for line endings
$lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {

$line = $lines[$i];

// If two line breaks were encountered, then this is the end of body
if (empty($line)) {
if ($i < $totalLines - 1) {
$body = implode('', array_slice($lines, $i + 2));
}
break;
}

// Parse message headers
if (!$startLine) {
$startLine = explode(' ', $line, 3);
} elseif (strpos($line, ':')) {
$parts = explode(':', $line, 2);
$key = trim($parts[0]);
$value = isset($parts[1]) ? trim($parts[1]) : '';
if (!isset($headers[$key])) {
$headers[$key] = $value;
} elseif (!is_array($headers[$key])) {
$headers[$key] = array($headers[$key], $value);
} else {
$headers[$key][] = $value;
}
}
}

return array(
'start_line' => $startLine,
'headers' => $headers,
'body' => $body
);
}

/**
* Ported from guzzle/guzzle
* @param $message
* @return array|bool
*/
protected function parseResponse($message)
{
if (!$message) {
return false;
}

$parts = $this->parseMessage($message);
list($protocol, $version) = explode('/', trim($parts['start_line'][0]));

return array(
'protocol' => $protocol,
'version' => $version,
'code' => $parts['start_line'][1],
'reason_phrase' => isset($parts['start_line'][2]) ? $parts['start_line'][2] : '',
'headers' => $parts['headers'],
'body' => $parts['body']
);
}

}

0 comments on commit 2743d30

Please sign in to comment.