nth try is the charm

This commit is contained in:
Jeremy Kauffman 2019-09-23 17:13:54 -04:00
parent 5238e52575
commit f95856f198

View file

@ -31,7 +31,6 @@ class Response
protected static $headersSent = false; protected static $headersSent = false;
protected static $content = ''; protected static $content = '';
protected static $contentSent = false; protected static $contentSent = false;
protected static $isHeadersOnly = false;
protected static $gzipResponseContent = true; protected static $gzipResponseContent = true;
protected static $metaImages = []; protected static $metaImages = [];
protected static $facebookAnalyticsType = "PageView"; protected static $facebookAnalyticsType = "PageView";
@ -145,9 +144,22 @@ class Response
public static function send() public static function send()
{ {
$status = static::getHeader(static::HEADER_STATUS);
$sendContent = true;
if ((!$status || $status === 200) &&
static::getHeader(static::HEADER_ETAG) &&
Request::getHttpHeader('If-None-Match', null) === static::getHeader(static::HEADER_ETAG)
) {
static::setHeader(static::HEADER_STATUS, 304);
$sendContent = false;
}
static::sendHeaders(); static::sendHeaders();
if ($sendContent) {
static::sendContent(); static::sendContent();
} }
}
public static function setContent(string $content) public static function setContent(string $content)
{ {
@ -165,18 +177,11 @@ class Response
throw new LogicException('Content has already been sent. It cannot be sent twice'); throw new LogicException('Content has already been sent. It cannot be sent twice');
} }
if (!static::$isHeadersOnly) {
echo static::$content; echo static::$content;
}
static::$contentSent = true; static::$contentSent = true;
} }
public static function setIsHeadersOnly(bool $isHeadersOnly = true)
{
static::$isHeadersOnly = $isHeadersOnly;
}
public static function setDownloadHttpHeaders($name, $type = null, $size = null, $noSniff = true) public static function setDownloadHttpHeaders($name, $type = null, $size = null, $noSniff = true)
{ {
static::setBinaryHttpHeaders($type, $size, $noSniff); static::setBinaryHttpHeaders($type, $size, $noSniff);
@ -202,7 +207,7 @@ class Response
//public mutable cache = soft-caching (requires at least one round trip for headers) as long as etag identifier matches //public mutable cache = soft-caching (requires at least one round trip for headers) as long as etag identifier matches
public static function enablePublicMutableCache(string $etag) public static function enablePublicMutableCache(string $etag)
{ {
static::setHeader(static::HEADER_CACHE_CONTROL, 'public, no-cache'); static::setHeader(static::HEADER_CACHE_CONTROL, 'public');
static::setHeader(static::HEADER_ETAG, $etag); static::setHeader(static::HEADER_ETAG, $etag);
} }