Esempio n. 1
0
 /**
  * 处理压缩资源内容。调用父类APF_Resource_ResourcesController的handle_request方法,
  * 获取资源内容,在调用压缩服务实现压缩,最终给浏览器返回压缩后的资源内容。
  * @see APF_Resource_ResourcesController::handle_request()
  */
 public function handle_request()
 {
     // 开启缓冲区
     ob_start();
     parent::handle_request();
     $uri = $_SERVER['REQUEST_URI'];
     // 获取资源内容
     $content = ob_get_contents();
     ob_end_clean();
     // 读取压缩服务配置
     $mpf = MPF::get_instance();
     $host = @$mpf->get_config(self::CONFIG_N_YUICOMPRESSOR_HOST, parent::CONFIG_F_RESOURCE);
     $port = @$mpf->get_config(self::CONFIG_N_YUICOMPRESSOR_PORT, parent::CONFIG_F_RESOURCE);
     if (!$host || !$port) {
         // 为配置压缩服务则直接输出原始内容
         echo $content;
         return;
     }
     // 调用压缩服务,输出压缩结果
     $fp = @fsockopen($host, $port);
     if (!$fp) {
         // 连接失败则直接输出内容
         echo $content;
         return;
     }
     // 发送要压缩的内容
     fwrite($fp, "{$uri}\n");
     fwrite($fp, $content);
     fwrite($fp, "\n\n");
     // 读取并显示压缩后的内容
     while (!feof($fp)) {
         echo fread($fp, 8192);
     }
     fclose($fp);
 }
Esempio n. 2
0
 public function handle_request()
 {
     // TODO: to handling 304 header by an interceptor?
     $mpf = MPF::get_instance();
     $response = $mpf->get_response();
     $cache_control = $this->get_cache_control();
     if ($cache_control) {
         $response->set_header("Cache-Control", $cache_control);
     }
     $expires = @$mpf->get_config(self::CONFIG_N_EXPIRES, self::CONFIG_F_RESOURCE);
     $last_modified = @$mpf->get_config(self::CONFIG_N_LAST_MODIFIED, self::CONFIG_F_RESOURCE);
     if (isset($last_modified)) {
         $etag = '"' . dechex($last_modified) . '"';
     }
     if (isset($etag)) {
         $none_match = @$_SERVER['HTTP_IF_NONE_MATCH'];
         if ($none_match && $none_match == $etag) {
             $response->set_header("HTTP/1.1", "304 ETag Matched", "304");
             exit;
         }
     }
     if (isset($last_modified) && isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
         $tmp = explode(";", $_SERVER['HTTP_IF_MODIFIED_SINCE']);
         $modified_since = @strtotime($tmp[0]);
         if ($modified_since && $modified_since >= $last_modified) {
             $response->set_header("HTTP/1.1", "304 Not Modified", "304");
             exit;
         }
     }
     if (isset($expires)) {
         $response->set_header("Expires", gmdate("D, d M Y H:i:s", $expires) . " GMT");
     }
     if (isset($last_modified)) {
         $response->set_header("ETag", $etag);
         $response->set_header("Last-Modified", gmdate("D, d M Y H:i:s", $last_modified) . " GMT");
     }
     return parent::handle_request();
 }