Skip to content

Commit

Permalink
Allow disabling of cubex headers
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Sep 15, 2015
1 parent 971a6e8 commit 85f7cdf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,32 @@
class Response extends \Symfony\Component\HttpFoundation\Response
{
protected $_callTime;
protected $_sendCubexHeaders = true;

public function __construct($content = '', $status = 200, $headers = [])
{
parent::__construct('', $status, $headers);
$this->from($content);
}

/**
* @return $this
*/
public function disableCubexHeaders()
{
$this->_sendCubexHeaders = false;
return $this;
}

/**
* @return $this
*/
public function enableCubexHeaders()
{
$this->_sendCubexHeaders = true;
return $this;
}

/**
* Set the microtime(true) value when the call started
*
Expand Down Expand Up @@ -76,7 +95,7 @@ public function from($source)
public function fromJson($object)
{
$this->_originalSource = $object;
$response = \json_encode($object);
$response = \json_encode($object);

// Prevent content sniffing attacks by encoding "<" and ">", so browsers
// won't try to execute the document as HTML
Expand Down Expand Up @@ -104,8 +123,8 @@ public function fromJson($object)
public function fromJsonp($responseKey, $object)
{
$this->_originalSource = $object;
$responseObject = \json_encode($object);
$response = "{$responseKey}({$responseObject})";
$responseObject = \json_encode($object);
$response = "{$responseKey}({$responseObject})";

// Prevent content sniffing attacks by encoding "<" and ">", so browsers
// won't try to execute the document as HTML
Expand Down Expand Up @@ -157,21 +176,24 @@ public function send()
*/
public function setCubexHeaders()
{
//Add the exec time as a header if PHP_START has been defined by the project
if(defined('PHP_START'))
if($this->_sendCubexHeaders)
{
$this->headers->set(
"X-Execution-Time",
number_format((microtime(true) - PHP_START) * 1000, 3) . ' ms'
);
}
//Add the exec time as a header if PHP_START has been defined by the project
if(defined('PHP_START'))
{
$this->headers->set(
"X-Execution-Time",
number_format((microtime(true) - PHP_START) * 1000, 3) . ' ms'
);
}

if($this->_callTime > 0)
{
$this->headers->set(
'X-Call-Time',
number_format((microtime(true) - $this->_callTime) * 1000, 3) . ' ms'
);
if($this->_callTime > 0)
{
$this->headers->set(
'X-Call-Time',
number_format((microtime(true) - $this->_callTime) * 1000, 3) . ' ms'
);
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Cubex/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public function testCubexHeaders()
$response = new Response();
$response->setCubexHeaders();
$this->assertContains('X-Execution-Time', (string)$response);

$response = new Response();
$response->disableCubexHeaders();
$response->setCubexHeaders();
$this->assertNotContains('X-Execution-Time', (string)$response);

$response = new Response();
$response->disableCubexHeaders();
$response->enableCubexHeaders();
$response->setCubexHeaders();
$this->assertContains('X-Execution-Time', (string)$response);
}
}

Expand Down

0 comments on commit 85f7cdf

Please sign in to comment.