Skip to content

Commit 220f67a

Browse files
committed
Merge pull request arangodb#89 from F21/enhanced_tracer
Enhanced tracer
2 parents 4316ba5 + 4e58212 commit 220f67a

File tree

7 files changed

+439
-20
lines changed

7 files changed

+439
-20
lines changed

lib/triagens/ArangoDb/Connection.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,14 @@ private function executeRequest($method, $url, $data)
335335

336336
$traceFunc = $this->_options[ConnectionOptions::OPTION_TRACE];
337337
if ($traceFunc) {
338+
338339
// call tracer func
339-
$traceFunc('send', $request);
340+
if($this->_options[ConnectionOptions::OPTION_ENHANCED_TRACE]){
341+
$parsed = HttpHelper::parseHttpMessage($request);
342+
$traceFunc(new TraceRequest(HttpHelper::parseHeaders($parsed['header']), $method, $url, $data));
343+
}else{
344+
$traceFunc('send', $request);
345+
}
340346
}
341347

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

364370
$scope->leave();
365371

366-
if ($traceFunc) {
367-
// call tracer func
368-
$traceFunc('receive', $result);
372+
if ($status['timed_out']) {
373+
throw new ClientException('Got a timeout when waiting on the server\'s response');
369374
}
370375

371-
if ($status['timed_out']) {
372-
throw new ClientException('Got a timeout when waiting on the server\'s response');
376+
$response = new HttpResponse($result);
377+
378+
if ($traceFunc) {
379+
// call tracer func
380+
if($this->_options[ConnectionOptions::OPTION_ENHANCED_TRACE]){
381+
$traceFunc(new TraceResponse($response->getHeaders(), $response->getHttpCode(), $response->getBody()));
382+
}else{
383+
$traceFunc('receive', $result);
384+
}
373385
}
374386

375-
return new HttpResponse($result);
387+
return $response;
376388
}
377389

378390
$scope->leave();

lib/triagens/ArangoDb/ConnectionOptions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class ConnectionOptions implements
6060
*/
6161
const OPTION_TRACE = 'trace';
6262

63+
/**
64+
* Enhanced trace
65+
*/
66+
const OPTION_ENHANCED_TRACE = 'enhancedTrace';
67+
6368
/**
6469
* "Create collections if they don't exist" index constant
6570
*/
@@ -291,6 +296,7 @@ private static function getDefaults()
291296
self::OPTION_IS_VOLATILE => DefaultValues::DEFAULT_IS_VOLATILE,
292297
self::OPTION_CONNECTION => DefaultValues::DEFAULT_CONNECTION,
293298
self::OPTION_TRACE => null,
299+
self::OPTION_ENHANCED_TRACE => false,
294300
self::OPTION_AUTH_USER => null,
295301
self::OPTION_AUTH_PASSWD => null,
296302
self::OPTION_AUTH_TYPE => null,

lib/triagens/ArangoDb/HttpHelper.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,54 @@ public static function createConnection(ConnectionOptions $options)
235235

236236
return $fp;
237237
}
238+
239+
/**
240+
* Splits a http message into its header and body.
241+
* @param string $httpMessage The http message string.
242+
* @throws ClientException
243+
* @return array
244+
*/
245+
public static function parseHttpMessage($httpMessage)
246+
{
247+
assert(is_string($httpMessage));
248+
249+
$barrier = HttpHelper::EOL . HttpHelper::EOL;
250+
$border = strpos($httpMessage, $barrier);
251+
252+
if ($border === false) {
253+
throw new ClientException('Got an invalid response from the server');
254+
}
255+
256+
$result = array();
257+
258+
$result['header'] = substr($httpMessage, 0, $border);
259+
$result['body'] = substr($httpMessage, $border + strlen($barrier));
260+
261+
return $result;
262+
}
263+
264+
/**
265+
* Process a string of HTTP headers into an array of header => values.
266+
* @param string $headers - the headers string
267+
* @return array
268+
*/
269+
public static function parseHeaders($headers)
270+
{
271+
$processed = array();
272+
273+
foreach (explode(HttpHelper::EOL, $headers) as $lineNumber => $line) {
274+
$line = trim($line);
275+
276+
if ($lineNumber == 0) {
277+
// first line of result is special, so discard it.
278+
continue;
279+
} else {
280+
// other lines contain key:value-like headers
281+
list($key, $value) = explode(':', $line, 2);
282+
$processed[strtolower(trim($key))] = trim($value);
283+
}
284+
}
285+
286+
return $processed;
287+
}
238288
}

lib/triagens/ArangoDb/HttpResponse.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,10 @@ class HttpResponse
6868
*/
6969
public function __construct($responseString)
7070
{
71-
assert(is_string($responseString));
71+
$parsed = HttpHelper::parseHttpMessage($responseString);
7272

73-
$barrier = HttpHelper::EOL . HttpHelper::EOL;
74-
$border = strpos($responseString, $barrier);
75-
if ($border === false) {
76-
throw new ClientException('Got an invalid response from the server');
77-
}
78-
79-
$this->_header = substr($responseString, 0, $border);
80-
$this->_body = substr($responseString, $border + strlen($barrier));
73+
$this->_header = $parsed['header'];
74+
$this->_body = $parsed['body'];
8175

8276
$this->setupHeaders();
8377
}
@@ -178,6 +172,7 @@ public function getJson()
178172
*/
179173
private function setupHeaders()
180174
{
175+
//Get the http status code from the first line
181176
foreach (explode(HttpHelper::EOL, $this->_header) as $lineNumber => $line) {
182177
$line = trim($line);
183178

@@ -187,11 +182,12 @@ private function setupHeaders()
187182
if (preg_match("/^HTTP\/\d+\.\d+\s+(\d+)/", $line, $matches)) {
188183
$this->_httpCode = (int) $matches[1];
189184
}
190-
} else {
191-
// other lines contain key:value-like headers
192-
list($key, $value) = explode(':', $line, 2);
193-
$this->_headers[strtolower(trim($key))] = trim($value);
185+
186+
break;
194187
}
195188
}
189+
190+
//Process the rest of the headers
191+
$this->_headers = HttpHelper::parseHeaders($this->_header);
196192
}
197193
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* ArangoDB PHP client: connection
4+
*
5+
* @package ArangoDbPhpClient
6+
* @author Jan Steemann
7+
* @author Francis Chuang
8+
* @copyright Copyright 2012, triagens GmbH, Cologne, Germany
9+
*/
10+
11+
namespace triagens\ArangoDb;
12+
13+
class TraceRequest
14+
{
15+
/**
16+
* Stores each header as an array (key => value) element
17+
* @var array
18+
*/
19+
private $_headers = array();
20+
21+
/**
22+
* Stores the http method
23+
* @var string
24+
*/
25+
private $_method;
26+
27+
/**
28+
* Stores the request url
29+
* @var string
30+
*/
31+
private $_requestUrl;
32+
33+
/**
34+
* Store the string of the body
35+
* @var string
36+
*/
37+
private $_body;
38+
39+
/**
40+
* The http message type
41+
* @var string
42+
*/
43+
private $_type = "request";
44+
45+
/**
46+
* Set up the request trace
47+
* @param array $headers - the array of http headers
48+
* @param string $method - the request method
49+
* @param string $requestUrl - the request url
50+
* @param string $body - the string of http body
51+
*/
52+
public function __construct($headers, $method, $requestUrl, $body)
53+
{
54+
$this->_headers = $headers;
55+
$this->_method = $method;
56+
$this->_requestUrl = $requestUrl;
57+
$this->_body = $body;
58+
}
59+
60+
/**
61+
* Get an array of the request headers
62+
* @return array
63+
*/
64+
public function getHeaders()
65+
{
66+
return $this->_headers;
67+
}
68+
69+
/**
70+
* Get the request method
71+
* @return string
72+
*/
73+
public function getMethod()
74+
{
75+
return $this->_method;
76+
}
77+
78+
/**
79+
* Get the request url
80+
* @return string
81+
*/
82+
public function getRequestUrl()
83+
{
84+
return $this->_requestUrl;
85+
}
86+
87+
/**
88+
* Get the body of the request
89+
* @return string
90+
*/
91+
public function getBody()
92+
{
93+
return $this->_body;
94+
}
95+
96+
/**
97+
* Get the http message type
98+
* @return string
99+
*/
100+
public function getType()
101+
{
102+
return $this->_type;
103+
}
104+
}

0 commit comments

Comments
 (0)