<commit_before><?php

namespace Dolphiq\GzipAndCacheResponse\Http\Middleware;

use Closure;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Http\Response;
use Dolphiq\GzipAndCacheResponse\GzipAndCacheResponse;

class GzipAndCacheResponseMiddleware 
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
     
    protected $_cache_next_request = false;
    protected $_cache_file_name = '';
    protected $_cache_full_path_name = '';
    protected $_cache_hit = false;
    

    protected $_gzip_and_cache_response;



    public function __construct(GzipAndCacheResponse $_gzip_and_cache_response)
    {
	    
        $this->_gzip_and_cache_response = $_gzip_and_cache_response;
        
    }
  
     
    public function handle($request, Closure $next)
    {
	    

	    // check if the page should be filtered!

		$input = $request->all();
			
	    if($request->format()=='html' && $request->getMethod()=='GET' && count($input)==0) {
			    
		     
			// we also can use; $request->path(); .. 
			
		    $this->_cache_file_name = md5($request->url()).'.gz';
			$this->_cache_full_path_name = storage_path('app/dolphiq/htmlcache/' . $this->_cache_file_name);
			
			// default false
			$this->_cache_hit=false;
			
						
			if(file_exists($this->_cache_full_path_name)) {
				// check file last modification date
				$filetime = \File::lastModified($this->_cache_full_path_name);
				$curtime=date("U");
				$difference = round (($curtime - $filetime));
				//die($difference);
				if($difference>60*60*24) { // max 1 day old
					
					\File::delete($this->_cache_full_path_name);
				} else
				{
					$this->_cache_hit=true;
				}		
			
			}				
			
			if($this->_cache_hit===true) {
			
				// set headers
				header("Html-Cache-Hit: true");
				header("Html-Cache-File: ".$this->_cache_file_name);
				header("Html-Cache-Age: ".$difference);
				
				$encoded_html=\File::get($this->_cache_full_path_name);
				header('Content-Length: ' . strlen($encoded_html));
				header('Content-Encoding: gzip');
				echo $encoded_html;
				
			    flush();
			    ob_flush();
				
				
				// please flush to browser, and re-fetch if needed
				if($difference>60) { // more than 60 seconds old, please re fetch after sent to browser
				
					\File::delete($this->_cache_full_path_name);
					
					// todo, please use other method to fetch url
					file_get_contents($request->fullUrl());
					
					
				}
				
				die();
			} else 
			
			{
				$this->_cache_next_request=true;
					
				$response = $next($request)->header('Html-Cache-Hit', 'false');   
				
				if($this->_cache_next_request==true) {
					$response=$response->header('Html-Cache-Stored', 'true');   
					$response=$response->header('Html-Cache-File', $this->_cache_file_name);   
				}
				
				return $response;				
			}
			
	    } else  
	    {
		    // just perform the request without a change 
		    return $next($request);
	    }
	  
        
    }  
 
	// save the file in the terminate class (after all other middle ware
    public function terminate($request, $response)
    {
	    
	    $input = $request->all();
	    
        // Store the final response in a file
 	    if($request->format()=='html' && $request->getMethod()=='GET' && count($input)==0) {
			 
		     
			// we also can use; $request->path(); .. 
				    
		    $this->_cache_file_name = md5($request->url()).'.gz';
			$this->_cache_full_path_name = storage_path('app/dolphiq/htmlcache/' . $this->_cache_file_name);		
			{
				$this->_cache_next_request=true;
								
				if($this->_cache_next_request==true) {
 
					// \File::put($this->_cache_full_path_name,$response->content());
					\File::put($this->_cache_full_path_name,gzencode($response->content(),9));<commit_msg>Create cache folder if not exists<commit_after><?php

namespace Dolphiq\GzipAndCacheResponse\Http\Middleware;

use Closure;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Http\Response;
use Dolphiq\GzipAndCacheResponse\GzipAndCacheResponse;

class GzipAndCacheResponseMiddleware 
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
     
    protected $_cache_next_request = false;
    protected $_cache_file_name = '';
    protected $_cache_sub_path_name = 'app/dolphiq/htmlcache';
    protected $_cache_full_path_name = '';
    protected $_cache_hit = false;
    

    protected $_gzip_and_cache_response;



    public function __construct(GzipAndCacheResponse $_gzip_and_cache_response)
    {
	    
        $this->_gzip_and_cache_response = $_gzip_and_cache_response;
        
    }
  
     
    public function handle($request, Closure $next)
    {
	    

	    // check if the page should be filtered!

		$input = $request->all();
			
	    if($request->format()=='html' && $request->getMethod()=='GET' && count($input)==0) {
			    
		     
			// we also can use; $request->path(); .. 
			
		    $this->_cache_file_name = md5($request->url()).'.gz';
			$this->_cache_full_path_name = storage_path($this->_cache_sub_path_name .'/' . $this->_cache_file_name);
			
			// default false
			$this->_cache_hit=false;
			
						
			if(file_exists($this->_cache_full_path_name)) {
				// check file last modification date
				$filetime = \File::lastModified($this->_cache_full_path_name);
				$curtime=date("U");
				$difference = round (($curtime - $filetime));
				//die($difference);
				if($difference>60*60*24) { // max 1 day old
					
					\File::delete($this->_cache_full_path_name);
				} else
				{
					$this->_cache_hit=true;
				}		
			
			}				
			
			if($this->_cache_hit===true) {
			
				// set headers
				header("Html-Cache-Hit: true");
				header("Html-Cache-File: ".$this->_cache_file_name);
				header("Html-Cache-Age: ".$difference);
				
				$encoded_html=\File::get($this->_cache_full_path_name);
				header('Content-Length: ' . strlen($encoded_html));
				header('Content-Encoding: gzip');
				echo $encoded_html;
				
			    flush();
			    ob_flush();
				
				
				// please flush to browser, and re-fetch if needed
				if($difference>60) { // more than 60 seconds old, please re fetch after sent to browser
				
					\File::delete($this->_cache_full_path_name);
					
					// todo, please use other method to fetch url
					file_get_contents($request->fullUrl());
					
					
				}
				
				die();
			} else 
			
			{
				$this->_cache_next_request=true;
					
				$response = $next($request)->header('Html-Cache-Hit', 'false');   
				
				if($this->_cache_next_request==true) {
					$response=$response->header('Html-Cache-Stored', 'true');   
					$response=$response->header('Html-Cache-File', $this->_cache_file_name);   
				}
				
				return $response;				
			}
			
	    } else  
	    {
		    // just perform the request without a change 
		    return $next($request);
	    }
	  
        
    }  
 
	// save the file in the terminate class (after all other middle ware
    public function terminate($request, $response)
    {
	    
	    $input = $request->all();
	    
        // Store the final response in a file
 	    if($request->format()=='html' && $request->getMethod()=='GET' && count($input)==0) {
			 
		     
			// we also can use; $request->path(); .. 
				    
		    $this->_cache_file_name = md5($request->url()).'.gz';
			$this->_cache_full_path_name = storage_path($this->_cache_sub_path_name .'/'  . $this->_cache_file_name);		
			{
				$this->_cache_next_request=true;
								
				if($this->_cache_next_request==true) {
 
					// check if folder exists
					if(!\File::exists(storage_path($this->_cache_sub_path_name))) {
						\File::makeDirectory(storage_path($this->_cache_sub_path_name), $mode = 0777, true, true);
					}
					
					
					\File::put($this->_cache_full_path_name,gzencode($response->content(),9));
{'fraction_non_alphanumeric': 0.1475740390674228, 'fraction_numerical': 0.004536862003780718, 'mean_word_length': 2.6470588235294117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1688537', 'n_tokens_mistral': 3148, 'n_tokens_neox': 2495, 'n_words': 434}