Skip to content

Commit

Permalink
Added HTTP header emition (with cache control).
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berstein committed Apr 17, 2011
1 parent 506073f commit 07dd77c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/Sfw/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static public function getRoot()
*
* @return string
*/
static public function route()
static public function route($emitHeaders = true)
{
if (array_key_exists('SCRIPT_URL', $_SERVER)) {
$uri = $_SERVER['SCRIPT_URL'];
Expand Down Expand Up @@ -74,6 +74,11 @@ static public function route()
$callback = array($node, $action);
if (in_array($action, get_class_methods($node)) && is_callable($callback)) {
call_user_func($callback);

if ($emitHeaders) {
$node->emitHeaders();
}

return (string) $node;
}

Expand All @@ -88,6 +93,11 @@ static public function route()
$callback = array($node, $alias['action']);
if (is_callable($callback)) {
call_user_func($callback);

if ($emitHeaders) {
$node->emitHeaders();
}

return (string) $node;
}

Expand All @@ -97,6 +107,10 @@ static public function route()
$node = new Sfw_Node_NotFound($request, $instance);
$node->notFound();

if ($emitHeaders) {
$node->emitHeaders();
}

return (string) $node;
}

Expand Down
41 changes: 40 additions & 1 deletion lib/Sfw/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Sfw_Node
protected $_controller;
protected $_content;
protected $_status = 200;
protected $_headers = array();

public function __construct(Sfw_Request_Interface $request, Sfw_Controller $controller)
{
Expand All @@ -29,14 +30,26 @@ protected function _add($content, $status = null)
return $this;
}

public function _addHeader($name, $value = null)
{
if (empty($value)) {
$this->_headers[] = $name;
} else {
$this->_headers[$name] = $value;
}

return $this;
}

protected function _status()
{
$proto = '1.0';
if (array_key_exists('SERVER_PROTOCOL', $_SERVER)) {
$proto = $_SERVER['SERVER_PROTOCOL'];
}

header("HTTP/{$proto} {$this->_status}");
$header = "HTTP/{$proto} {$this->_status}";
$this->_addHeader($header);

return $this;
}
Expand All @@ -49,6 +62,32 @@ protected function _unknownAction($name)
);
}

public function emitHeaders($withStatus = true)
{
if ($withStatus) {
$this->_status();
}

// Cache control headers
$this->_addHeader('Cache-Control', 'none');
$this->_addHeader('Expires', date('r', time() - (3600 * 24)));

// Informational headers
$this->_addHeader('X-Framework', 'Sfw-' . Sfw_Version::getVersion());

foreach ($this->_headers as $name => $value) {
if (empty($name) || ctype_digit($name)) {
header($value);
} else if (empty($value)) {
header($name);
} else {
header($name . ': ' . $value, true);
}
}

return $this;
}

public function __toString()
{
$this->_status();
Expand Down
11 changes: 11 additions & 0 deletions lib/Sfw/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class Sfw_Version
{
const VERSION = '0.1';

static public function getVersion()
{
return 'v' . self::VERSION;
}
}

0 comments on commit 07dd77c

Please sign in to comment.