Quick links: Content - sections - sub sections
EN FR
Quick Search Advanced search
 
Page

  [Opened] A basic gzip response

Posted by tudorilisoi on 11/11/2009 09:49

Hi, here's a basic gzip response Here's an example usage:

 $rep = $this->getResponse('gzip');
 $path=JELIX_APP_WWW_PATH.'some_file';
 $rep->mimeType=files::getMimeType($path);
 $rep->content=file_get_contents($path);
 return $rep;

, and here's the gzipResponse.class.php code:

 <?php
 class gzipResponse extends jResponse {
     /**
     * @var string
     */
     protected $_type = 'gzip';
 
     /**
      * text content
      * @var string
      */
     public $content = '';
     public $mimeType = '';
 
     /**
      * output the content with the text/plain mime type
      * @return boolean    true si it's ok
      */
     public function output(){
         global $gJConfig;
         if($this->hasErrors()) return false;
         if ((substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))) {
         	$this->addHttpHeader('Content-Encoding','gzip');
         	$this->content=ob_gzhandler($this->content,5);
         } else {
         	# code...
         }
         
         $this->addHttpHeader('Content-Type',$this->mimeType.';charset='.$gJConfig->charset,false);
         $this->_httpHeaders['Content-length']=strlen($this->content);
         $this->sendHttpHeaders();
         echo $this->content;
         return true;
     }
 
     /**
      * output errors
      */
     public function outputErrors(){
         global $gJConfig;
         header("HTTP/1.0 500 Internal Server Error");
         header('Content-Type: text/plain;charset='.$gJConfig->charset);
         if($this->hasErrors()){
             foreach( $GLOBALS['gJCoord']->errorMessages  as $e){
                echo '['.$e[0].' '.$e[1].'] '.$e[2]." \t".$e[3]." \t".$e[4]."\n";
             }
         }else{
             echo "[unknow error]\n";
         }
     }
 }

Of course, as I've also seen in ticket requests, the best way to gzip would be having an 'isGzip' flag in all the http response types

  [Opened] Re: A basic gzip response

Reply #1 Posted by laurentj on 11/12/2009 00:04

thanks for this contribution, but as you said, "the best way to gzip would be having an 'isGzip' flag in all the http response types" (so in the jResponse class)

 
Page
  1. A basic gzip response