public function body($content = NULL) { if ($content === NULL) { if ($this->_body === '' && in_array($this->_status, [200, 403, 404])) { $this->_body = $this->view->render(); } return $this->_body; } return parent::body($content); }
/** * Prepares the Kohana_Reponse object for * caching to nginx. This requires the object * to be flattened to a standard HTTP response * including full headers. * * // Creates a full HTTP valid response * $this->_create_nginx_cache($response); * * @param Kohana_Response response * @return string */ protected function _create_nginx_cache(Kohana_Response $response) { // Create empty cache buffer $cache = ''; // Generate HTTP header foreach ($response->headers as $key => $value) { $cache .= "{$key}: {$value}\n"; } // Check for HTTPS and secure cookie setting if (empty($_SERVER['HTTPS']) and !Cookie::$secure) { // Get the response cookies $cookies = $response->get_cookies(); // Generate cookies foreach ($cookies as $name => $value) { $cache .= 'Set-Cookie: ' . $name . '=' . Cookie::salt($name, $value['value']) . '~' . $value['value'] . '; expires: ' . gmdate('D, d M Y H:i:s T', $value['expiration']) . '; path: ' . Cookie::$path . '; domain: ' . Cookie::$domain . (Cookie::$httponly ? '; httpOnly' : '') . "\n"; } } // Create HTTP body $cache .= "\n" . $response->body; return $cache; }
/** * @param null $data * @param int $httpCode * @param null|array $headers * * @throws HTTP_Exception_Redirect */ static function json($data = NULL, $httpCode = 200, $headers = NULL) { if ($data instanceof HTTP_Exception_Redirect) { throw $data; } $response = Kohana_Response::factory(); try { $response->headers(['cache-control' => 'no-cache, no-store, max-age=0, must-revalidate', 'content-type' => 'application/json; charset=utf-8']); if (Kohana_Arr::is_array($headers)) { $response->headers($headers); } $response->status($httpCode); } catch (Exception $e) { $response->status($httpCode = 500); $data = $e; } if ($data instanceof Exception) { if ($data instanceof HTTP_Exception) { $response->status($httpCode = $data->getCode()); } elseif ($httpCode < 400) { $response->status($httpCode = 500); } $data = Helpers_Arr::exception($data); } if (NULL === $data && $httpCode == 200) { $response->status(204); } elseif (NULL !== $data) { try { $response->body(json_encode($data, JSON_UNESCAPED_UNICODE)); } catch (Exception $e) { $response->body(json_encode(Helpers_Arr::exception($e), JSON_UNESCAPED_UNICODE), 500); } } $response->send_headers(TRUE); exit($response); }