1
0
mirror of https://github.com/mrclay/minify.git synced 2025-03-14 09:29:38 +01:00

Optimized header sniffing in Encoder.php; no more regex's needed for most browsers. Updated README w/r/t test suite update.

This commit is contained in:
Steve Clay 2008-03-05 14:56:23 +00:00
parent 98530fcd57
commit a0d48f16fb
3 changed files with 30 additions and 33 deletions

6
README
View File

@ -24,9 +24,9 @@ TESTING
4. Visit http://yourdomain.com/path/to/web/examples/1/ 4. Visit http://yourdomain.com/path/to/web/examples/1/
This page should contain 3 "PASS"es. It may take a few seconds to minify jQuery! This page should contain 3 "PASS"es. It may take a few seconds to minify jQuery!
5. Call each "test_*.php" file in http://yourdomain.com/path/to/web/test/ 5. Visit http://yourdomain.com/path/to/web/test/test_all.php
Each test should "PASS", except test_Minify.php. This script should output a This will run all unit tests. You can call the tests individually to see
single minified Javascript file sent as "application/x-javascript". more details.
MINIFY OVERVIEW MINIFY OVERVIEW

View File

@ -244,9 +244,7 @@ class HTTP_ConditionalGet {
if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
return false; return false;
} }
$ifModifiedSince = get_magic_quotes_gpc() $ifModifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE'])
: $_SERVER['HTTP_IF_MODIFIED_SINCE'];
if (false !== ($semicolon = strrpos($ifModifiedSince, ';'))) { if (false !== ($semicolon = strrpos($ifModifiedSince, ';'))) {
// IE has tacked on extra data to this header, strip it // IE has tacked on extra data to this header, strip it
$ifModifiedSince = substr($ifModifiedSince, 0, $semicolon); $ifModifiedSince = substr($ifModifiedSince, 0, $semicolon);

View File

@ -141,33 +141,35 @@ class HTTP_Encoder {
* call gzip "x-gzip" etc.) * call gzip "x-gzip" etc.)
*/ */
public static function getAcceptedEncoding() { public static function getAcceptedEncoding() {
// @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
if (! isset($_SERVER['HTTP_ACCEPT_ENCODING']) if (! isset($_SERVER['HTTP_ACCEPT_ENCODING'])
|| self::_isBuggyIe()) || self::_isBuggyIe())
{ {
return array('', ''); return array('', '');
} }
// @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html $ae = $_SERVER['HTTP_ACCEPT_ENCODING'];
// test for (x-)gzip, if q is specified, can't be "0" $aeRev = strrev($ae);
// faster test for most common "gzip, deflate" // Fast tests for common AEs. If these don't pass we have to do
if (preg_match('@(?:,| )deflate$@', $_SERVER['HTTP_ACCEPT_ENCODING']) // slow regex parsing
if (0 === strpos($aeRev, 'etalfed ,') // ie, webkit
|| 0 === strpos($aeRev, 'etalfed,') // gecko
|| 0 === strpos($ae, 'deflate,') // opera 9.5b
// slow parsing
|| preg_match( || preg_match(
'@(?:^|,)\\s*deflate\\s*(?:$|,|;\\s*q=(?:0\\.|1))@' '@(?:^|,)\\s*deflate\\s*(?:$|,|;\\s*q=(?:0\\.|1))@', $ae)) {
,$_SERVER['HTTP_ACCEPT_ENCODING'])
) {
return array('deflate', 'deflate'); return array('deflate', 'deflate');
} }
if (preg_match( if (preg_match(
'@(?:^|,)\\s*((?:x-)?gzip)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@' '@(?:^|,)\\s*((?:x-)?gzip)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@'
,$_SERVER['HTTP_ACCEPT_ENCODING'] ,$ae
,$m) ,$m)) {
) {
return array('gzip', $m[1]); return array('gzip', $m[1]);
} }
if (preg_match( if (preg_match(
'@(?:^|,)\\s*((?:x-)?compress)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@' '@(?:^|,)\\s*((?:x-)?compress)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@'
,$_SERVER['HTTP_ACCEPT_ENCODING'] ,$ae
,$m) ,$m)) {
) {
return array('compress', $m[1]); return array('compress', $m[1]);
} }
return array('', ''); return array('', '');
@ -226,20 +228,17 @@ class HTTP_Encoder {
*/ */
protected static function _isBuggyIe() protected static function _isBuggyIe()
{ {
if (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') $ua = $_SERVER['HTTP_USER_AGENT'];
|| !preg_match( // quick escape for non-IEs
'/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i' if (0 !== strpos($ua, 'Mozilla/4.0 (compatible; MSIE ')
,$_SERVER['HTTP_USER_AGENT'] || false !== strpos($ua, 'Opera')) {
,$m
)
) {
return false; return false;
} }
$version = floatval($m[1]); // no regex = faaast
if ($version < 6) return true; $version = (float)substr($ua, 30);
if ($version == 6 && false === strpos($_SERVER['HTTP_USER_AGENT'], 'SV1')) { return (
return true; $version < 6
} || ($version == 6 && false === strpos($ua, 'SV1'))
return false; );
} }
} }