Skip to content

Commit

Permalink
Return correct content-type headers based off the template's file ext…
Browse files Browse the repository at this point in the history
…ension
  • Loading branch information
kolber committed Dec 11, 2009
1 parent d0716cd commit cecc7f3
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions app/stacey.inc.php
Expand Up @@ -25,17 +25,37 @@ function php_fixes() {
if(function_exists('date_default_timezone_set')) date_default_timezone_set('Australia/Melbourne');
}

function custom_headers() {
# set utf-8 charset header
header ("Content-type: text/html; charset=utf-8");
function set_content_type($template_file) {
# split by file extension
preg_match('/\.([\w\d]+?)$/u', $template_file, $split_path);

switch ($split_path[1]) {
case 'txt':
# set text/utf-8 charset header
header("Content-type: text/plain; charset=utf-8");
break;
case 'xml':
# set atom+xml/utf-8 charset header
header("Content-type: application/atom+xml; charset=utf-8");
break;
case 'json':
# set json/utf-8 charset header
header('Content-type: application/json; charset=utf-8');
break;
default:
# set html/utf-8 charset header
header("Content-type: text/html; charset=utf-8");
}


}

function etag_expired($cache) {
header ('Etag: "'.$cache->hash.'"');
header('Etag: "'.$cache->hash.'"');
if(isset($_SERVER['HTTP_IF_NONE_MATCH']) && stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"'.$cache->hash.'"') {
# local cache is still fresh, so return 304
header ("HTTP/1.0 304 Not Modified");
header ('Content-Length: 0');
header("HTTP/1.0 304 Not Modified");
header('Content-Length: 0');
return false;
} else {
return true;
Expand All @@ -45,7 +65,7 @@ function etag_expired($cache) {
function render($page) {
$cache = new Cache($page);
# set any custom headers
$this->custom_headers();
$this->set_content_type($page->template_file);
# if etag is still fresh, return 304 and don't render anything
if(!$this->etag_expired($cache)) return;
# if cache has expired
Expand Down

0 comments on commit cecc7f3

Please sign in to comment.