Changeset 952
- Timestamp:
- 07/25/08 04:21:51 (2 months ago)
- Files:
-
- branches/arnoschn/cache/lib/AkActionController/AkCacheHandler.php (modified) (7 diffs)
- branches/arnoschn/cache/lib/AkCache/AkCachedPage.php (modified) (3 diffs)
- branches/arnoschn/cache/lib/AkRequest.php (modified) (1 diff)
- branches/arnoschn/cache/lib/AkUnitTest/AkTestApplication.php (modified) (4 diffs)
- branches/arnoschn/cache/test/unit/lib/AkActionController/_page_caching.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/arnoschn/cache/lib/AkActionController/AkCacheHandler.php
r899 r952 195 195 $cacheId = $this->_buildCacheId($path, $language); 196 196 $cacheGroup = $this->_buildCacheGroup(); 197 return $this->_cache_store->remove($cacheId,$cacheGroup); 198 } 199 function cachePage($content, $path = null, $language = null) 197 $notGzippedRes=$this->_cache_store->remove($cacheId,$cacheGroup); 198 $gZippedCacheId = $this->_scopeWithGzip($cacheId); 199 $gzippedRes=$this->_cache_store->remove($gZippedCacheId,$cacheGroup); 200 return ($notGzippedRes || $gzippedRes); 201 } 202 function cachePage($content, $path = null, $language = null, $gzipped=false) 200 203 { 201 204 if (!($this->_cachingAllowed() && $this->_perform_caching)) return; 202 205 203 206 $cacheId = $this->_buildCacheId($path, $language); 207 if ($gzipped) { 208 $cacheId = $this->_scopeWithGzip($cacheId); 209 } 204 210 $cacheGroup = $this->_buildCacheGroup(); 205 211 $content = $this->_modifyCacheContent($content); … … 209 215 function _modifyCacheContent($content) 210 216 { 217 211 218 $headers = $this->_controller->Response->_headers_sent; 212 219 $headerString = serialize($headers); … … 277 284 return true; 278 285 } 279 286 287 function _scopeWithGzip($cacheId) 288 { 289 $cacheId = 'gzip' . DS . $cacheId; 290 return $cacheId; 291 } 280 292 function afterPageCache() 281 293 { 282 $this->_controller->handleResponse(); 283 $contents = ob_get_flush(); 284 285 $this->cachePage($contents); 294 $encodings = $this->_getAcceptedEncodings(); 295 $xgzip = false; 296 $gzip = false; 297 if (($gzip=in_array('gzip',$encodings)) || ($xgzip=in_array('x-gzip',$encodings))) { 298 $this->_controller->Response->addHeader('Content-Encoding',$xgzip?'x-gzip':'gzip'); 299 $gzip = $gzip || $xgzip; 300 $this->_controller->handleResponse(); 301 $contents = ob_get_flush(); 302 /** 303 * Caching unzipped content 304 */ 305 $this->cachePage($contents,null,null,false); 306 $contents = $this->_gzipCache($contents); 307 } else { 308 $this->_controller->handleResponse(); 309 $contents = ob_get_flush(); 310 /** 311 * Caching gzipped content 312 */ 313 $gzippedContents = $this->_gzipCache($contents); 314 $this->cachePage($gzippedContents,null,null,true); 315 } 316 $this->cachePage($contents,null,null,$gzip); 286 317 return true; 287 318 } 319 320 function _gzipCache($cache) 321 { 322 $pre ='\x1f\x8b\x08\x00\x00\x00\x00\x00'; 323 $size = strlen($cache); 324 $gzipped = gzcompress($cache, 9); 325 $gzipped = substr($gzipped, 0, $size); 326 $gzipped = $pre.$gzipped; 327 return $gzipped; 288 328 } 289 329 … … 320 360 return $this->_lastCacheId; 321 361 } 322 362 function _getAcceptedEncodings() 363 { 364 $encodings = isset($_SERVER['HTTP_ACCEPT_ENCODING'])?$_SERVER['HTTP_ACCEPT_ENCODING']:''; 365 $encodings = preg_split('/\s*,\s*/',$encodings); 366 return $encodings; 367 } 323 368 function &getCachedPage($path = null,$forcedLanguage = null) 324 369 { … … 331 376 } 332 377 $cacheId = $this->_buildCacheId($path, $forcedLanguage); 378 $encodings = $this->_getAcceptedEncodings(); 379 if (($gzip=in_array('gzip',$encodings)) || ($xgzip=in_array('x-gzip',$encodings))) { 380 $cacheId = $this->_scopeWithGzip($cacheId); 381 } 333 382 $cacheGroup = $this->_buildCacheGroup(); 334 383 $cache = $this->_cache_store->get($cacheId, $cacheGroup); … … 577 626 function _addExtension($path, $extension) 578 627 { 579 if (!empty($extension) ) {628 if (!empty($extension) && substr($path,-strlen($extension))!==$extension) { 580 629 $path = $path.'.'.$extension; 581 630 } … … 622 671 } 623 672 624 625 673 /** 626 674 * @access protected branches/arnoschn/cache/lib/AkCache/AkCachedPage.php
r849 r952 6 6 var $_headerSeparator; 7 7 var $_options; 8 var $_encodingAliases = array('gzip','x-gzip', 'compress', 'x-compress'); 8 9 function __construct(&$contents, $header_separator, $options = array()) 9 10 { … … 71 72 $headers = @unserialize($headersSerialized); 72 73 $headers = !is_array($headers)?array():$headers; 74 $acceptedEncodings = $this->_getAcceptedEncodings(); 73 75 if ($sendHeaders) { 74 76 foreach ($headers as $header) { 75 header($header); 77 78 header($this->_handleEncodingAliases($header, $acceptedEncodings)); 76 79 } 77 80 … … 86 89 } 87 90 $exit?exit:null; 91 } 92 function _handleEncodingAliases($header, $acceptedEncodings) 93 { 94 $parts = split(': ',$header); 95 if (strtolower($parts[0])=='content-encoding' && 96 isset($parts[1]) && 97 in_array($parts[1],$this->_encodingAliases)) { 98 $acceptedEncodings = array_intersect($acceptedEncodings,$this->_encodingAliases); 99 if (isset($acceptedEncodings[0])) { 100 $header =$parts[0].': '.$acceptedEncodings[0]; 101 } 102 } 103 return $header; 104 } 105 function _getAcceptedEncodings() 106 { 107 $encodings = isset($_SERVER['HTTP_ACCEPT_ENCODING'])?$_SERVER['HTTP_ACCEPT_ENCODING']:''; 108 $encodings = preg_split('/\s*,\s*/',$encodings); 109 return $encodings; 88 110 } 89 111 branches/arnoschn/cache/lib/AkRequest.php
r902 r952 696 696 if (preg_match('/^([^\.]+)\.(.+)$/', @$_REQUEST['ak'], $matches)) { 697 697 $this->_format = isset($matches[2])?$matches[2]:null; 698 $_REQUEST['ak'] = $matches[ 1];699 $this->_request['ak'] = $matches[ 1];698 $_REQUEST['ak'] = $matches[0]; 699 $this->_request['ak'] = $matches[0]; 700 700 } 701 701 } branches/arnoschn/cache/lib/AkUnitTest/AkTestApplication.php
r899 r952 98 98 $_SERVER['HTTP_X_REQUESTED_WITH']='xmlhttprequest'; 99 99 } 100 function setAcceptEncoding($encoding) 101 { 102 $_SERVER['HTTP_ACCEPT_ENCODING']=$encoding; 103 } 100 104 function &getHeader($name) 101 105 { … … 123 127 $_REQUEST = array(); 124 128 $_POST = array(); 129 125 130 } 126 131 … … 129 134 $this->_reset(); 130 135 $this->_response = null; 136 $this->_cacheHeaders = array(); 131 137 $this->_setConstants($constants); 132 138 $parts = parse_url($url); … … 165 171 $res=true; 166 172 } 167 return $res; 173 $this->_cleanUp(); 174 return $res; 175 } 176 function _cleanUp() 177 { 178 unset($_SERVER['HTTP_IF_MODIFIED_SINCE']); 179 unset($_SERVER['HTTP_X_REQUESTED_WITH']); 180 unset($_SERVER['HTTP_ACCEPT_ENCODING']); 168 181 } 169 182 function post($url, $data = null, $constants = array(), $controllerVars = array()) branches/arnoschn/cache/test/unit/lib/AkActionController/_page_caching.php
r897 r952 33 33 $this->assertText('/^\s*$/'); 34 34 $this->assertResponse(200); 35 $this-> _assertPageCached('/page_caching/ok');35 $this->assertTrue($this->_assertPageCached('/page_caching/ok')); 36 36 37 37 38 38 } 39 function test_should_cache_get_with_ok_status_gzipped_and_unzipped() 40 { 41 $this->_flushCache('www.example.com'); 42 43 $this->assertTrue($this->_assertPageNotCached('/page_caching/ok')); 44 $this->setAcceptEncoding('gzip'); 45 $this->assertTrue($this->_assertPageNotCached('/page_caching/ok')); 46 $this->setAcceptEncoding(''); 47 48 $this->setIp('212.121.121.121'); 49 $this->get('http://www.example.com/page_caching/ok'); 50 $this->assertFalse($this->getHeader('Content-Encoding')); 51 $this->assertResponse(200); 52 $this->assertTrue($this->_assertPageCached('/page_caching/ok')); 53 $this->setAcceptEncoding('gzip'); 54 $this->assertTrue($this->_assertPageCached('/page_caching/ok')); 55 } 56 57 function test_should_cache_get_with_ok_status_gzipped() 58 { 59 $this->_flushCache('www.example.com'); 60 $this->setIp('212.121.121.121'); 61 $this->setAcceptEncoding('gzip'); 62 $this->get('http://www.example.com/page_caching/ok'); 63 $this->assertHeader('Content-Encoding','gzip'); 64 $this->assertResponse(200); 65 $this->assertTrue($this->_assertPageCached('/page_caching/ok')); 66 $this->setAcceptEncoding('gzip'); 67 $this->assertTrue($this->_assertPageCached('/page_caching/ok')); 68 69 } 70 71 function test_should_cache_get_with_ok_status_xgzipped() 72 { 73 $this->_flushCache('www.example.com'); 74 $this->setIp('212.121.121.121'); 75 $this->setAcceptEncoding('x-gzip'); 76 $this->get('http://www.example.com/page_caching/ok'); 77 $this->assertHeader('Content-Encoding','x-gzip'); 78 $this->assertResponse(200); 79 $this->assertTrue($this->_assertPageCached('/page_caching/ok')); 80 $this->setAcceptEncoding('gzip'); 81 $this->assertTrue($this->_assertPageCached('/page_caching/ok')); 82 83 } 84 39 85 function _getCachedPage($path) 40 86 { … … 50 96 return $cachedPage; 51 97 } 98 52 99 function _assertPageCached($path, $message = false) 53 100 { … … 55 102 $this->assertTrue($cachedPage!=false,$message); 56 103 $this->assertIsA($cachedPage,'AkCachedPage'); 104 return $cachedPage!=false && is_a($cachedPage,'AkCachedPage'); 57 105 } 58 106 function _assertPageNotCached($path, $message = '%s') … … 60 108 $cachedPage = $this->_getCachedPage($path); 61 109 $this->assertTrue($cachedPage==false,$message); 110 return $cachedPage==false; 62 111 } 63 112 function test_last_modified() … … 85 134 $this->get('http://www.example.com/page_caching/custom_path'); 86 135 $this->assertText('Akelos rulez'); 87 $this-> _assertPageCached('/index.html');136 $this->assertTrue($this->_assertPageCached('/index.html')); 88 137 } 89 138 … … 91 140 { 92 141 $this->get('http://www.example.com/page_caching/custom_path'); 93 $this-> _assertPageCached('/index.html');142 $this->assertTrue($this->_assertPageCached('/index.html')); 94 143 95 144 $this->get('http://www.example.com/page_caching/expire_custom_path'); 96 $this-> _assertPageNotCached('/index.html');145 $this->assertTrue($this->_assertPageNotCached('/index.html')); 97 146 } 98 147 … … 101 150 $controller=$this->getController(); 102 151 $controller->cachePage('cached content', '/page_caching_test/trailing_slash'); 103 $this-> _assertPageCached('/page_caching_test/trailing_slash.html');152 $this->assertTrue($this->_assertPageCached('/page_caching_test/trailing_slash.html')); 104 153 } 105 154 … … 108 157 $controller=$this->getController(); 109 158 $controller->cachePage('cached content', '/page_caching_test/trailing_slash/'); 110 $this-> _assertPageCached('/page_caching_test/trailing_slash.html');159 $this->assertTrue($this->_assertPageCached('/page_caching_test/trailing_slash.html')); 111 160 } 112 161 … … 120 169 $this->$method($path); 121 170 if ($this->getHeader('Status') == 200 && $method=='get') { 122 $this-> _assertPageCached($path, 'action ok with GET request should be cached');171 $this->assertTrue($this->_assertPageCached($path, 'action ok with GET request should be cached')); 123 172 } else { 124 $this-> _assertPageNotCached($path,' action '.$action.' with '.strtoupper($method).' should not be cached');173 $this->assertTrue($this->_assertPageNotCached($path,' action '.$action.' with '.strtoupper($method).' should not be cached')); 125 174 } 126 175 }
