Пример #1
0
 public function process_response($request, $response)
 {
     // It's not worth compressing non-OK or really short responses.
     if (!$response || $response->status_code != 200 || is_string($response->content) === false || strlen($response->content) < 200) {
         return $response;
     }
     // We patch already because if we have IE being the first
     // asking through a proxy, the proxy could cache without
     // taking into account the Accept-Encoding as it may not be
     // applied for it and thus a good browser afterward would not
     // benefit from the gzip version.
     \photon\http\HeaderTool::updateVary($response, array('Accept-Encoding'));
     // Avoid gzipping if we've already got a content-encoding.
     if (isset($response->headers['Content-Encoding'])) {
         return $response;
     }
     $ctype = strtolower($response->headers['Content-Type']);
     // MSIE have issues with gzipped respones of various content types.
     if (false !== strpos(strtolower($request->getHeader('user-agent')), 'msie')) {
         if (0 !== strpos($ctype, 'text/') || false !== strpos($ctype, 'javascript')) {
             return $response;
         }
     }
     // We do not recompress zip files and compressed files
     if (false !== strpos($ctype, 'zip') || false !== strpos($ctype, 'compressed')) {
         return $response;
     }
     $accept = $request->getHeader('accept-encoding');
     // deflate is the fastest, so first
     $methods = array('deflate' => 'gzdeflate', 'gzip' => 'gzencode');
     foreach ($methods as $encoding => $encoder) {
         if (preg_match('/\\b' . $encoding . '\\b/i', $accept)) {
             $response->content = $encoder($response->content);
             $response->headers['Content-Encoding'] = $encoding;
             $response->headers['Content-Length'] = strlen($response->content);
             break;
         }
     }
     return $response;
 }
Пример #2
0
 public function process_response($request, $response)
 {
     if (!$response) {
         // The view took care of everything.
         return $response;
     }
     if (isset($request->session) == false) {
         // The request do not have execute process_request before
         // A more highly priority middleware have return a answer
         return $response;
     }
     if ($request->session->accessed) {
         // This view used session data to render, this means it
         // varies on the cookie information.
         \photon\http\HeaderTool::updateVary($response, array('Cookie'));
     }
     if ($request->session->modified || Conf::f('session_save_every_request', false)) {
         // Time to store
         $request->session->commit($response);
         $expire = Conf::f('session_cookie_expire', 1209600);
         if ($expire) {
             $expire += time();
         }
         $response->COOKIE->setCookie(Conf::f('session_cookie_name', 'sid'), $request->session->key, $expire, Conf::f('session_cookie_path', ''), Conf::f('session_cookie_domain', ''), Conf::f('session_cookie_secure', false), Conf::f('session_cookie_httponly', false));
     }
     return $response;
 }