Skip to content

Instantly share code, notes, and snippets.

@curiousdannii
Forked from jeresig/error.php
Created October 21, 2010 02:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curiousdannii/637837 to your computer and use it in GitHub Desktop.
Save curiousdannii/637837 to your computer and use it in GitHub Desktop.
Opera 304 tests
<?php header('HTTP/1.0 500 Internal Server Error'); exit; ?>
<!--
There is no way, in Opera, to figure out if an incoming page
is returning a 304 status.
Opera provides no indicator as to if a 304 occurred.
- xhr.status === 0
- xhr.statusText === ""
- xhr.getAllResponseHeader() === ""
- xhr.getResponseHeader( anything ) (not found)
All of the above is virtually identical to an aborted request in other browsers.
Example here: http://ejohn.org/files/bugs/304/
-->
<ul id="out"></ul>
<script>
window.onload = function() {
request("success.php", 200, 1);
request("error.php", 500);
request("404.php", 404);
request("not_modified.php", 304);
request("wait.php", 0);
};
function request(url, expect, stage) {
var xhr = window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject("Microsoft.XMLHTTP");
xhr.open( "GET", url, true );
if ( stage == 1 )
{
// Ensure it's not cached
// Does not appear to work in Opera?
xhr.setRequestHeader( 'If-None-Match', '"nocache"' );
}
var onreadystatechange = xhr.onreadystatechange = function() {
// If readyState === 0 then abort occurred, according to Opera
if ( xhr.readyState === 4 || xhr.readyState === 0 ) {
// Opera gives status of 0 for 500 errors
// Unfortunately this leads to ambiguity between errors and 304/Not Modified
// As not modified pages ALSO return a status of 0
if ( xhr.status === expect ) {
if ( stage )
{
url += ' ' + xhr.responseText;
}
log( "PASS: " + url );
} else {
log( "FAIL: " + url + " (" + xhr.status + ")" );
}
xhr.onreadystatechange = null;
if ( stage == 1 )
{
// Test with ETags now
request("success.php", 200, 2);
}
}
};
xhr.send( null );
if ( /wait/.test( url ) ) {
setTimeout(function(){
xhr.abort();
// Opera doesn't fire onreadystatechange on abort, fire it explicitly
if ( xhr.onreadystatechange ) {
onreadystatechange();
}
}, 13);
}
}
function log( msg ) {
document.getElementById("out").innerHTML += "<li>" + msg + "</li>";
}
</script>
<?php header('HTTP/1.0 304 Not Modified'); exit; ?>
<?php
if ( isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == '"304bugtest"' )
{
header('HTTP/1.0 304 Not Modified');
exit;
}
header('Etag: "304bugtest"');
echo 'yay';
?>
<?php sleep(10); exit; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment