123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- abstract class Kohana_HTTP {
-
- public static $protocol = 'HTTP/1.1';
-
- public static function redirect($uri = '', $code = 302)
- {
- $e = HTTP_Exception::factory($code);
- if ( ! $e instanceof HTTP_Exception_Redirect)
- throw new Kohana_Exception('Invalid redirect code \':code\'', [
- ':code' => $code
- ]);
- throw $e->location($uri);
- }
-
- public static function check_cache(Request $request, Response $response, $etag = NULL)
- {
-
- if ($etag == NULL)
- {
- $etag = $response->generate_etag();
- }
-
- $response->headers('etag', $etag);
-
-
- if ($response->headers('cache-control'))
- {
- $response->headers('cache-control', $response->headers('cache-control').', must-revalidate');
- }
- else
- {
- $response->headers('cache-control', 'must-revalidate');
- }
-
- if ($request->headers('if-none-match') AND (string) $request->headers('if-none-match') === $etag)
- {
-
- throw HTTP_Exception::factory(304)->headers('etag', $etag);
- }
- return $response;
- }
-
- public static function parse_header_string($header_string)
- {
-
- if (extension_loaded('http'))
- {
-
- $headers = version_compare(phpversion('http'), '2.0.0', '>=') ?
- \http\Header::parse($header_string) :
- http_parse_headers($header_string);
- return new HTTP_Header($headers);
- }
-
- $headers = [];
-
- if (preg_match_all('/(\w[^\s:]*):[ ]*([^\r\n]*(?:\r\n[ \t][^\r\n]*)*)/', $header_string, $matches))
- {
-
- foreach ($matches[0] as $key => $value)
- {
-
- if ( ! isset($headers[$matches[1][$key]]))
- {
-
- $headers[$matches[1][$key]] = $matches[2][$key];
- }
-
- else
- {
-
- if (is_array($headers[$matches[1][$key]]))
- {
-
- $headers[$matches[1][$key]][] = $matches[2][$key];
- }
-
- else
- {
- $headers[$matches[1][$key]] = [
- $headers[$matches[1][$key]],
- $matches[2][$key],
- ];
- }
- }
- }
- }
-
- return new HTTP_Header($headers);
- }
-
- public static function request_headers()
- {
-
- if (function_exists('apache_request_headers'))
- {
-
- return new HTTP_Header(apache_request_headers());
- }
-
- elseif (extension_loaded('http'))
- {
-
- $headers = version_compare(phpversion('http'), '2.0.0', '>=') ?
- \http\Env::getRequestHeader() :
- http_get_request_headers();
- return new HTTP_Header($headers);
- }
-
- $headers = [];
-
- if ( ! empty($_SERVER['CONTENT_TYPE']))
- {
- $headers['content-type'] = $_SERVER['CONTENT_TYPE'];
- }
-
- if ( ! empty($_SERVER['CONTENT_LENGTH']))
- {
- $headers['content-length'] = $_SERVER['CONTENT_LENGTH'];
- }
- foreach ($_SERVER as $key => $value)
- {
-
- if (strpos($key, 'HTTP_') !== 0)
- {
- continue;
- }
-
- $headers[str_replace('_', '-', substr($key, 5))] = $value;
- }
- return new HTTP_Header($headers);
- }
-
- public static function www_form_urlencode(array $params = [])
- {
- if ( ! $params)
- return;
- $encoded = [];
- foreach ($params as $key => $value)
- {
- $encoded[] = $key.'='.rawurlencode($value);
- }
- return implode('&', $encoded);
- }
- }
|