1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-13 17:44:00 +02:00

Moved HTTP examples into web/test

This commit is contained in:
Steve Clay
2008-05-15 15:20:33 +00:00
parent 048c689663
commit fb4ecd2231
8 changed files with 14 additions and 6 deletions

View File

@@ -1,44 +0,0 @@
<?php
require '../../ConditionalGet.php';
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lastModified
));
if ($cg->cacheIsValid) {
$cg->sendHeaders();
// we're done
exit();
}
// generate content
$title = 'Last-Modified is known : add Content-Length';
$explain = '
<p>Here, like <a href="./">the first example</a>, we know the Last-Modified time,
but we also want to set the Content-Length to increase cacheability and allow
HTTP persistent connections. Instead of sending headers immediately, we first
generate our content, then use <code>setContentLength(strlen($content))</code>
to add the header. Then finally call <code>sendHeaders()</code> and send the
content.</p>
<p><strong>Note:</strong> This is not required if your PHP config buffers all
output and your script doesn\'t do any incremental flushing of the output
buffer. PHP will generally set Content-Length for you if it can.</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
$content = get_content(array(
'title' => $title
,'explain' => $explain
));
$cg->setContentLength(strlen($content));
$cg->sendHeaders();
send_slowly($content);
?>

View File

@@ -1,39 +0,0 @@
<?php
require '../../ConditionalGet.php';
// generate content first (not ideal)
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
$title = 'Last-Modified is unknown : use hash of content for ETag';
$explain = '
<p>When Last-Modified is unknown, you can still use ETags, but you need a short
string that is unique for that content. In the worst case, you have to generate
all the content first, <em>then</em> instantiate HTTP_ConditionalGet, setting
the array key <code>contentHash</code> to the output of a hash function of the
content. Since we have the full content, we might as well also use
<code>setContentLength(strlen($content))</code> in the case where we need to
send it.</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
$content = get_content(array(
'title' => $title
,'explain' => $explain
));
$cg = new HTTP_ConditionalGet(array(
'contentHash' => substr(md5($content), 7)
));
if ($cg->cacheIsValid) {
$cg->sendHeaders();
// we're done
exit();
}
$cg->setContentLength(strlen($content));
$cg->sendHeaders();
send_slowly($content);
?>

View File

@@ -1,46 +0,0 @@
<?php
require '../../ConditionalGet.php';
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lastModified
));
$cg->sendHeaders();
if ($cg->cacheIsValid) {
// we're done
exit();
}
// output encoded content
$title = 'ConditionalGet + Encoder';
$explain = '
<p>Using ConditionalGet and Encoder is straightforward. First impliment the
ConditionalGet, then if the cache is not valid, encode and send the content</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
$content = get_content(array(
'title' => $title
,'explain' => $explain
));
require '../../Encoder.php';
$he = new HTTP_Encoder(array(
'content' => get_content(array(
'title' => $title
,'explain' => $explain
))
));
$he->encode();
// usually you would just $he->sendAll(), but here we want to emulate slow
// connection
$he->sendHeaders();
send_slowly($he->getContent());
?>

View File

@@ -1,27 +0,0 @@
<?php
require '../../ConditionalGet.php';
// far expires
$cg = new HTTP_ConditionalGet(array(
'setExpires' => (time() + 86400 * 365) // 1 yr
));
$cg->sendHeaders();
// generate, send content
$title = 'Expires date is known';
$explain = '
<p>Here we set "setExpires" to a timestamp or GMT date string. This results in
<code>$cacheIsValid</code> always being false, so content is always served, but
with an Expires header.
<p><strong>Note:</strong> This isn\'t a conditional GET, but is useful if you\'re
used to the HTTP_ConditionalGet workflow already.</p>
';
require '_include.php';
echo get_content(array(
'title' => $title
,'explain' => $explain
));
?>

View File

@@ -1,65 +0,0 @@
<?php
function send_slowly($content)
{
$half = ceil(strlen($content) / 2);
$content = str_split($content, $half);
while ($chunk = array_shift($content)) {
sleep(1);
echo $chunk;
ob_flush();
flush();
}
}
function get_content($data)
{
ob_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HTTP_ConditionalGet : <?php echo $data['title']; ?></title>
</head>
<body>
<h1>HTTP_ConditionalGet</h1>
<h2><?php echo $data['title']; ?></h2>
<?php echo $data['explain']; ?>
<ul>
<li><a href="./">Last-Modified is known : simple usage</a></li>
<li><a href="2.php">Last-Modified is known : add Content-Length</a></li>
<li><a href="3.php">Last-Modified is unknown : use hash of content for ETag</a></li>
<li><a href="4.php">ConditionalGet + Encoder</a></li>
<li><a href="5.php">Expires date is known</a></li>
</ul>
<h2>Notes</h2>
<h3>How to distinguish 200 and 304 responses</h3>
<p>For these pages all 200 responses are sent in chunks a second apart, so you
should notice that 304 responses are quicker. You can also use HTTP sniffers
like <a href="http://www.fiddlertool.com/">Fiddler (win)</a> and
<a href="http://livehttpheaders.mozdev.org/">LiveHTTPHeaders (Firefox add-on)</a>
to verify headers and content being sent.</p>
<h3>Browser notes</h3>
<dl>
<dt>Opera</dt>
<dd>Opera has a couple behaviors against the HTTP spec: Manual refreshes (F5)
prevents the ETag/If-Modified-Since headers from being sent; it only sends
them when following a link or bookmark. Also, Opera will not honor the
<code>must-revalidate</code> Cache-Control value unless <code>max-age</code>
is set. To get Opera to follow the spec, ConditionalGet will send Opera max-age=0
(if one is not already set).</dd>
<dt>Safari</dt>
<dd>ETag validation is unsupported, but Safari supports HTTP/1.0 validation via
If-Modified-Since headers as long as the cache is explicitly marked
"public" or "private" ("private" is default in ConditionalGet).</dd>
</dl>
</body>
</html>
<?php
$content = ob_get_contents();
ob_end_clean();
return $content;
}
?>

View File

@@ -1,36 +0,0 @@
<?php
require '../../ConditionalGet.php';
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lastModified
));
$cg->sendHeaders();
if ($cg->cacheIsValid) {
// we're done
exit();
}
$title = 'Last-Modified is known : simple usage';
$explain = '
<p>If your content has not changed since a certain timestamp, set this via the
the <code>lastModifiedTime</code> array key when instantiating HTTP_ConditionalGet.
You can immediately call the method <code>sendHeaders()</code> to set the
Last-Modified, ETag, and Cache-Control headers. The, if <code>cacheIsValid</code>
property is false, you echo the content.</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
echo send_slowly(get_content(array(
'title' => $title
,'explain' => $explain
)));
?>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 202 B

View File

@@ -1,59 +0,0 @@
<?php
ini_set('display_errors', 'on');
require '../../Encoder.php';
if (!isset($_GET['test'])) {
$type = 'text/html';
ob_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>HTTP_Encoder Test</title>
<style type="text/css">
@import "?test=2";
#img {background:url("?test=1");}
.green {background:#0f0;}
p span {padding:0 .5em;}
</style>
</head>
<body>
<h1>HTTP_Encoder test</h1>
<p><span class="green"> HTML </span></p>
<p><span id="css"> CSS </span></p>
<p><span id="js"> Javascript </span></p>
<p><span id="img"> image </span></p>
<script src="?test=3" type="text/javascript"></script>
</body>
</html>
<?php
$content = ob_get_contents();
ob_end_clean();
} elseif ($_GET['test'] == '1') {
$content = file_get_contents(dirname(__FILE__) . '/green.png');
$type = 'image/png';
} elseif ($_GET['test'] == '2') {
$content = '#css {background:#0f0;}';
$type = 'text/css';
} else {
$content = '
window.onload = function(){
document.getElementById("js").className = "green";
};
';
$type = 'application/x-javascript';
}
$he = new HTTP_Encoder(array(
'content' => $content
,'type' => $type
));
$he->encode();
$he->sendAll();
?>