Skip to content

Commit

Permalink
Finetune Header and add HeaderTest.php.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsur committed Apr 5, 2017
1 parent 693d287 commit ce36f41
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 23 deletions.
63 changes: 40 additions & 23 deletions src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Header {
* @return array A dict containing code and message if
* $code is valid, otherwise 404 dict.
*/
public static function get_header_string($code) {
final public static function get_header_string($code) {
if (self::$header_string[$code] === null)
$code = 404;
return [
Expand All @@ -82,6 +82,16 @@ public static function get_header_string($code) {
];
}

public static function header($val) {
header($val);
}

public static function header_halt($str=null) {
if ($str)
die((string)$str);
die();
}

/**
* Send response headers and read a file if applicable.
*
Expand All @@ -96,7 +106,7 @@ public static function get_header_string($code) {
* directive for Apache or the equivalent for other webservers.
* May be set externally with webserver internal directive.
*/
public static function send_header(
final public static function send_header(
$fname=false, $cache=false, $echo=true,
$code=200, $disposition=false, $xsendfile_header=null
) {
Expand All @@ -106,20 +116,25 @@ public static function send_header(

extract(self::get_header_string($code));

header("HTTP/1.1 $code $msg");
static::header("HTTP/1.1 $code $msg");
if ($cache) {
$cache = intval($cache);
$expire = time() + $cache;
header("Expires: " . gmdate("D, d M Y H:i:s", $expire)." GMT");
header("Cache-Control: must-revalidate");
static::header(
"Expires: " . gmdate("D, d M Y H:i:s", $expire)." GMT");
static::header(
"Cache-Control: must-revalidate");
} else {
header("Expires: Mon, 27 Jul 1996 07:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
static::header(
"Expires: Mon, 27 Jul 1996 07:00:00 GMT");
static::header(
"Cache-Control: no-store, no-cache, must-revalidate");
static::header(
"Cache-Control: post-check=0, pre-check=0", false);
static::header("Pragma: no-cache");
}
header("Last-Modified: " . gmdate("D, d M Y H:i:s")." GMT");
header("X-Powered-By: Zap!");
static::header("Last-Modified: " . gmdate("D, d M Y H:i:s")." GMT");
static::header("X-Powered-By: Zap!");

if (!$echo)
return;
Expand All @@ -128,30 +143,32 @@ public static function send_header(
# Cannot echo anything if fname doesn't exist.
return;

if ($code != 200)
if ($code != 200) {
# Echoing error page, i.e. serving non-text as error page
# makes little sense. Error pages must always be generated
# and not cached.
die();
static::header_halt();
return;
}

@header('Content-Length: ' . filesize($fname));
static::header('Content-Length: ' . filesize($fname));

$mime = Common::get_mimetype($fname);
@header("Content-Type: $mime");
static::header("Content-Type: $mime");

if ($disposition) {
if ($disposition === true)
$disposition = basename($fname);
@header(sprintf(
static::header(sprintf(
'Content-Disposition: attachment; filename="%s"',
$disposition));
}

if ($xsendfile_header !== null)
@header($xsendfile_header);
static::header($xsendfile_header);
else
readfile($fname);
die();
static::header_halt();
}

/**
Expand All @@ -162,14 +179,14 @@ public static function send_header(
* @param int $http_code Valid HTTP response code.
* @param int $cache Cache duration in seconds. 0 for no cache.
*/
public static function print_json(
final public static function print_json(
$errno=0, $data=[], $http_code=200, $cache=0
) {
self::send_header(0, $cache, false, $http_code);
$js = json_encode(compact('errno', 'data'));
@header("Content-Length: " . strlen($js));
@header('Content-Type: application/json');
die($js);
static::header("Content-Length: " . strlen($js));
static::header('Content-Type: application/json');
static::header_halt($js);
}

/**
Expand All @@ -181,7 +198,7 @@ public static function print_json(
* this parameter, e.g. 403.
* @param int $cache Cache duration in seconds. 0 for no cache.
*/
public static function pj(
final public static function pj(
$retval, $forbidden_code=null, $cache=0
) {
if (count($retval) < 2)
Expand Down
66 changes: 66 additions & 0 deletions tests/HeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php


use PHPUnit\Framework\TestCase;
use BFITech\ZapCore\Header;


class HeaderPatched extends Header {
public static function header($val) {
echo "$val\n";
}
public static function header_halt($str=null) {
if ($str)
echo $str;
}
}


class HeaderTest extends TestCase {

public function test_send_header() {
ob_start();
HeaderPatched::send_header(false, 3600, false);
$rv = ob_get_clean();
$this->assertNotEquals(
strpos($rv, 'Cache-Control'), false);

ob_start();
HeaderPatched::send_header(false, 0, false);
$rv = ob_get_clean();
$this->assertNotEquals(
strpos($rv, 'Pragma'), false);

ob_start();
HeaderPatched::send_header(
__FILE__, true, true, 302, 'test.php');
$rv = ob_get_clean();
$this->assertNotEquals(
strpos($rv, 'Expire'), false);

ob_start();
HeaderPatched::send_header(
__FILE__, true, true, 200, 'test.php');
$rv = ob_get_clean();
$this->assertNotEquals(
strpos($rv, file_get_contents(__FILE__)), false);
}

public function test_print_json() {
ob_start();
HeaderPatched::print_json();
$rv = ob_get_clean();
$this->assertNotEquals(
strpos($rv, '"errno":0'), false);
}

public function test_pj() {
ob_start();
HeaderPatched::pj([1, 403]);
$rv = ob_get_clean();
$this->assertNotEquals(
strpos($rv, '"errno":1'), false);
}

}

0 comments on commit ce36f41

Please sign in to comment.