Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/triagens/ArangoDb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,14 @@ private function executeRequest($method, $url, $data)

$traceFunc = $this->_options[ConnectionOptions::OPTION_TRACE];
if ($traceFunc) {

// call tracer func
$traceFunc('send', $request);
if($this->_options[ConnectionOptions::OPTION_ENHANCED_TRACE]){
$parsed = HttpHelper::parseHttpMessage($request);
$traceFunc(new TraceRequest(HttpHelper::parseHeaders($parsed['header']), $method, $url, $data));
}else{
$traceFunc('send', $request);
}
}

// set socket timeout for this scope
Expand All @@ -363,16 +369,22 @@ private function executeRequest($method, $url, $data)

$scope->leave();

if ($traceFunc) {
// call tracer func
$traceFunc('receive', $result);
if ($status['timed_out']) {
throw new ClientException('Got a timeout when waiting on the server\'s response');
}

if ($status['timed_out']) {
throw new ClientException('Got a timeout when waiting on the server\'s response');
$response = new HttpResponse($result);

if ($traceFunc) {
// call tracer func
if($this->_options[ConnectionOptions::OPTION_ENHANCED_TRACE]){
$traceFunc(new TraceResponse($response->getHeaders(), $response->getHttpCode(), $response->getBody()));
}else{
$traceFunc('receive', $result);
}
}

return new HttpResponse($result);
return $response;
}

$scope->leave();
Expand Down
6 changes: 6 additions & 0 deletions lib/triagens/ArangoDb/ConnectionOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class ConnectionOptions implements
*/
const OPTION_TRACE = 'trace';

/**
* Enhanced trace
*/
const OPTION_ENHANCED_TRACE = 'enhancedTrace';

/**
* "Create collections if they don't exist" index constant
*/
Expand Down Expand Up @@ -291,6 +296,7 @@ private static function getDefaults()
self::OPTION_IS_VOLATILE => DefaultValues::DEFAULT_IS_VOLATILE,
self::OPTION_CONNECTION => DefaultValues::DEFAULT_CONNECTION,
self::OPTION_TRACE => null,
self::OPTION_ENHANCED_TRACE => false,
self::OPTION_AUTH_USER => null,
self::OPTION_AUTH_PASSWD => null,
self::OPTION_AUTH_TYPE => null,
Expand Down
50 changes: 50 additions & 0 deletions lib/triagens/ArangoDb/HttpHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,54 @@ public static function createConnection(ConnectionOptions $options)

return $fp;
}

/**
* Splits a http message into its header and body.
* @param string $httpMessage The http message string.
* @throws ClientException
* @return array
*/
public static function parseHttpMessage($httpMessage)
{
assert(is_string($httpMessage));

$barrier = HttpHelper::EOL . HttpHelper::EOL;
$border = strpos($httpMessage, $barrier);

if ($border === false) {
throw new ClientException('Got an invalid response from the server');
}

$result = array();

$result['header'] = substr($httpMessage, 0, $border);
$result['body'] = substr($httpMessage, $border + strlen($barrier));

return $result;
}

/**
* Process a string of HTTP headers into an array of header => values.
* @param string $headers - the headers string
* @return array
*/
public static function parseHeaders($headers)
{
$processed = array();

foreach (explode(HttpHelper::EOL, $headers) as $lineNumber => $line) {
$line = trim($line);

if ($lineNumber == 0) {
// first line of result is special, so discard it.
continue;
} else {
// other lines contain key:value-like headers
list($key, $value) = explode(':', $line, 2);
$processed[strtolower(trim($key))] = trim($value);
}
}

return $processed;
}
}
22 changes: 9 additions & 13 deletions lib/triagens/ArangoDb/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,10 @@ class HttpResponse
*/
public function __construct($responseString)
{
assert(is_string($responseString));
$parsed = HttpHelper::parseHttpMessage($responseString);

$barrier = HttpHelper::EOL . HttpHelper::EOL;
$border = strpos($responseString, $barrier);
if ($border === false) {
throw new ClientException('Got an invalid response from the server');
}

$this->_header = substr($responseString, 0, $border);
$this->_body = substr($responseString, $border + strlen($barrier));
$this->_header = $parsed['header'];
$this->_body = $parsed['body'];

$this->setupHeaders();
}
Expand Down Expand Up @@ -178,6 +172,7 @@ public function getJson()
*/
private function setupHeaders()
{
//Get the http status code from the first line
foreach (explode(HttpHelper::EOL, $this->_header) as $lineNumber => $line) {
$line = trim($line);

Expand All @@ -187,11 +182,12 @@ private function setupHeaders()
if (preg_match("/^HTTP\/\d+\.\d+\s+(\d+)/", $line, $matches)) {
$this->_httpCode = (int) $matches[1];
}
} else {
// other lines contain key:value-like headers
list($key, $value) = explode(':', $line, 2);
$this->_headers[strtolower(trim($key))] = trim($value);

break;
}
}

//Process the rest of the headers
$this->_headers = HttpHelper::parseHeaders($this->_header);
}
}
104 changes: 104 additions & 0 deletions lib/triagens/ArangoDb/TraceRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* ArangoDB PHP client: connection
*
* @package ArangoDbPhpClient
* @author Jan Steemann
* @author Francis Chuang
* @copyright Copyright 2012, triagens GmbH, Cologne, Germany
*/

namespace triagens\ArangoDb;

class TraceRequest
{
/**
* Stores each header as an array (key => value) element
* @var array
*/
private $_headers = array();

/**
* Stores the http method
* @var string
*/
private $_method;

/**
* Stores the request url
* @var string
*/
private $_requestUrl;

/**
* Store the string of the body
* @var string
*/
private $_body;

/**
* The http message type
* @var string
*/
private $_type = "request";

/**
* Set up the request trace
* @param array $headers - the array of http headers
* @param string $method - the request method
* @param string $requestUrl - the request url
* @param string $body - the string of http body
*/
public function __construct($headers, $method, $requestUrl, $body)
{
$this->_headers = $headers;
$this->_method = $method;
$this->_requestUrl = $requestUrl;
$this->_body = $body;
}

/**
* Get an array of the request headers
* @return array
*/
public function getHeaders()
{
return $this->_headers;
}

/**
* Get the request method
* @return string
*/
public function getMethod()
{
return $this->_method;
}

/**
* Get the request url
* @return string
*/
public function getRequestUrl()
{
return $this->_requestUrl;
}

/**
* Get the body of the request
* @return string
*/
public function getBody()
{
return $this->_body;
}

/**
* Get the http message type
* @return string
*/
public function getType()
{
return $this->_type;
}
}
Loading