1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-26 15:24:48 +02:00

HTTP_ConditionalGet::check()

HTTP_Encoder::output()
This commit is contained in:
Steve Clay
2009-02-04 22:03:29 +00:00
parent 5686c0576d
commit b553442816
2 changed files with 86 additions and 11 deletions

View File

@@ -13,6 +13,7 @@
* list($updateTime, $content) = getDbUpdateAndContent(); * list($updateTime, $content) = getDbUpdateAndContent();
* $cg = new HTTP_ConditionalGet(array( * $cg = new HTTP_ConditionalGet(array(
* 'lastModifiedTime' => $updateTime * 'lastModifiedTime' => $updateTime
* ,'isPublic' => true
* )); * ));
* $cg->sendHeaders(); * $cg->sendHeaders();
* if ($cg->cacheIsValid) { * if ($cg->cacheIsValid) {
@@ -21,6 +22,12 @@
* echo $content; * echo $content;
* </code> * </code>
* *
* E.g. Shortcut for the above
* <code>
* HTTP_ConditionalGet::check($updateTime, true); // exits if client has cache
* echo $content;
* </code>
*
* E.g. Content from DB with no update time: * E.g. Content from DB with no update time:
* <code> * <code>
* $content = getContentFromDB(); * $content = getContentFromDB();
@@ -69,8 +76,8 @@ class HTTP_ConditionalGet {
* @param array $spec options * @param array $spec options
* *
* 'isPublic': (bool) if true, the Cache-Control header will contain * 'isPublic': (bool) if true, the Cache-Control header will contain
* "public", allowing proxy caches to cache the content. Otherwise * "public", allowing proxies to cache the content. Otherwise "private" will
* "private" will be sent, allowing only browsers to cache. (default false) * be sent, allowing only browser caching. (default false)
* *
* 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers * 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers
* will be sent with content. This is recommended. * will be sent with content. This is recommended.
@@ -95,7 +102,8 @@ class HTTP_ConditionalGet {
* *
* @return null * @return null
*/ */
public function __construct($spec) { public function __construct($spec)
{
$scope = (isset($spec['isPublic']) && $spec['isPublic']) $scope = (isset($spec['isPublic']) && $spec['isPublic'])
? 'public' ? 'public'
: 'private'; : 'private';
@@ -147,7 +155,8 @@ class HTTP_ConditionalGet {
* *
* @return array * @return array
*/ */
public function getHeaders() { public function getHeaders()
{
return $this->_headers; return $this->_headers;
} }
@@ -162,7 +171,8 @@ class HTTP_ConditionalGet {
* *
* @return int copy of input $bytes * @return int copy of input $bytes
*/ */
public function setContentLength($bytes) { public function setContentLength($bytes)
{
return $this->_headers['Content-Length'] = $bytes; return $this->_headers['Content-Length'] = $bytes;
} }
@@ -177,7 +187,8 @@ class HTTP_ConditionalGet {
* *
* @return null * @return null
*/ */
public function sendHeaders() { public function sendHeaders()
{
$headers = $this->_headers; $headers = $this->_headers;
if (array_key_exists('_responseCode', $headers)) { if (array_key_exists('_responseCode', $headers)) {
header($headers['_responseCode']); header($headers['_responseCode']);
@@ -188,6 +199,36 @@ class HTTP_ConditionalGet {
} }
} }
/**
* Exit if the client's cache is valid for this resource
*
* This is a convenience method for common use of the class
*
* @param int $lastModifiedTime if given, both ETag AND Last-Modified headers
* will be sent with content. This is recommended.
*
* @param bool $isPublic (default false) if true, the Cache-Control header
* will contain "public", allowing proxies to cache the content. Otherwise
* "private" will be sent, allowing only browser caching.
*
* @param array $options (default empty) additional options for constructor
*
* @return null
*/
public static function check($lastModifiedTime = null, $isPublic = false, $options = array())
{
if (null !== $lastModifiedTime) {
$options['lastModifiedTime'] = (int)$lastModifiedTime;
}
$options['isPublic'] = (bool)$isPublic;
$cg = new HTTP_ConditionalGet($options);
$cg->sendHeaders();
if ($cg->cacheIsValid) {
exit();
}
}
/** /**
* Get a GMT formatted date for use in HTTP headers * Get a GMT formatted date for use in HTTP headers
* *
@@ -199,7 +240,8 @@ class HTTP_ConditionalGet {
* *
* @return string * @return string
*/ */
public static function gmtDate($time) { public static function gmtDate($time)
{
return gmdate('D, d M Y H:i:s \G\M\T', $time); return gmdate('D, d M Y H:i:s \G\M\T', $time);
} }
@@ -207,14 +249,16 @@ class HTTP_ConditionalGet {
protected $_lmTime = null; protected $_lmTime = null;
protected $_etag = null; protected $_etag = null;
protected function _setEtag($hash, $scope) { protected function _setEtag($hash, $scope)
{
$this->_etag = '"' . $hash $this->_etag = '"' . $hash
. substr($scope, 0, 3) . substr($scope, 0, 3)
. '"'; . '"';
$this->_headers['ETag'] = $this->_etag; $this->_headers['ETag'] = $this->_etag;
} }
protected function _setLastModified($time) { protected function _setLastModified($time)
{
$this->_lmTime = (int)$time; $this->_lmTime = (int)$time;
$this->_headers['Last-Modified'] = self::gmtDate($time); $this->_headers['Last-Modified'] = self::gmtDate($time);
} }
@@ -237,7 +281,8 @@ class HTTP_ConditionalGet {
return $isValid; return $isValid;
} }
protected function resourceMatchedEtag() { protected function resourceMatchedEtag()
{
if (!isset($_SERVER['HTTP_IF_NONE_MATCH'])) { if (!isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
return false; return false;
} }
@@ -253,7 +298,8 @@ class HTTP_ConditionalGet {
return false; return false;
} }
protected function resourceNotModified() { protected function resourceNotModified()
{
if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
return false; return false;
} }

View File

@@ -19,6 +19,12 @@
* </code> * </code>
* *
* <code> * <code>
* // Shortcut to encoding output
* header('Content-Type: text/css'); // needed if not HTML
* HTTP_Encoder::output($css);
* </code>
*
* <code>
* // Just sniff for the accepted encoding * // Just sniff for the accepted encoding
* $encoding = HTTP_Encoder::getAcceptedEncoding(); * $encoding = HTTP_Encoder::getAcceptedEncoding();
* </code> * </code>
@@ -259,6 +265,29 @@ class HTTP_Encoder {
return true; return true;
} }
/**
* Encode and send appropriate headers and content
*
* This is a convenience method for common use of the class
*
* @param string $content
*
* @param int $compressionLevel given to zlib functions. If not given, the
* class default will be used.
*
* @return bool success true if the content was actually compressed
*/
public static function output($content, $compressionLevel = null)
{
if (null === $compressionLevel) {
$compressionLevel = self::$compressionLevel;
}
$he = new HTTP_Encoder(array('content' => $content));
$ret = $he->encode($compressionLevel);
$he->sendAll();
return $ret;
}
protected $_content = ''; protected $_content = '';
protected $_headers = array(); protected $_headers = array();
protected $_encodeMethod = array('', ''); protected $_encodeMethod = array('', '');