HTTP.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Contains the most low-level helpers methods in Kohana:
  4. *
  5. * - Environment initialization
  6. * - Locating files within the cascading filesystem
  7. * - Auto-loading and transparent extension of classes
  8. * - Variable and path debugging
  9. *
  10. * @package Kohana
  11. * @category HTTP
  12. * @author Kohana Team
  13. * @since 3.1.0
  14. * @copyright (c) 2008-2012 Kohana Team
  15. * @license http://kohanaphp.com/license
  16. */
  17. abstract class Kohana_HTTP {
  18. /**
  19. * @var The default protocol to use if it cannot be detected
  20. */
  21. public static $protocol = 'HTTP/1.1';
  22. /**
  23. * Issues a HTTP redirect.
  24. *
  25. * @param string $uri URI to redirect to
  26. * @param int $code HTTP Status code to use for the redirect
  27. * @throws HTTP_Exception
  28. */
  29. public static function redirect($uri = '', $code = 302)
  30. {
  31. $e = HTTP_Exception::factory($code);
  32. if ( ! $e instanceof HTTP_Exception_Redirect)
  33. throw new Kohana_Exception('Invalid redirect code \':code\'', array(
  34. ':code' => $code
  35. ));
  36. throw $e->location($uri);
  37. }
  38. /**
  39. * Checks the browser cache to see the response needs to be returned,
  40. * execution will halt and a 304 Not Modified will be sent if the
  41. * browser cache is up to date.
  42. *
  43. * @param Request $request Request
  44. * @param Response $response Response
  45. * @param string $etag Resource ETag
  46. * @throws HTTP_Exception_304
  47. * @return Response
  48. */
  49. public static function check_cache(Request $request, Response $response, $etag = NULL)
  50. {
  51. // Generate an etag if necessary
  52. if ($etag == NULL)
  53. {
  54. $etag = $response->generate_etag();
  55. }
  56. // Set the ETag header
  57. $response->headers('etag', $etag);
  58. // Add the Cache-Control header if it is not already set
  59. // This allows etags to be used with max-age, etc
  60. if ($response->headers('cache-control'))
  61. {
  62. $response->headers('cache-control', $response->headers('cache-control').', must-revalidate');
  63. }
  64. else
  65. {
  66. $response->headers('cache-control', 'must-revalidate');
  67. }
  68. // Check if we have a matching etag
  69. if ($request->headers('if-none-match') AND (string) $request->headers('if-none-match') === $etag)
  70. {
  71. // No need to send data again
  72. throw HTTP_Exception::factory(304)->headers('etag', $etag);
  73. }
  74. return $response;
  75. }
  76. /**
  77. * Parses a HTTP header string into an associative array
  78. *
  79. * @param string $header_string Header string to parse
  80. * @return HTTP_Header
  81. */
  82. public static function parse_header_string($header_string)
  83. {
  84. // If the PECL HTTP extension is loaded
  85. if (extension_loaded('http'))
  86. {
  87. // Use the fast method to parse header string
  88. return new HTTP_Header(http_parse_headers($header_string));
  89. }
  90. // Otherwise we use the slower PHP parsing
  91. $headers = array();
  92. // Match all HTTP headers
  93. if (preg_match_all('/(\w[^\s:]*):[ ]*([^\r\n]*(?:\r\n[ \t][^\r\n]*)*)/', $header_string, $matches))
  94. {
  95. // Parse each matched header
  96. foreach ($matches[0] as $key => $value)
  97. {
  98. // If the header has not already been set
  99. if ( ! isset($headers[$matches[1][$key]]))
  100. {
  101. // Apply the header directly
  102. $headers[$matches[1][$key]] = $matches[2][$key];
  103. }
  104. // Otherwise there is an existing entry
  105. else
  106. {
  107. // If the entry is an array
  108. if (is_array($headers[$matches[1][$key]]))
  109. {
  110. // Apply the new entry to the array
  111. $headers[$matches[1][$key]][] = $matches[2][$key];
  112. }
  113. // Otherwise create a new array with the entries
  114. else
  115. {
  116. $headers[$matches[1][$key]] = array(
  117. $headers[$matches[1][$key]],
  118. $matches[2][$key],
  119. );
  120. }
  121. }
  122. }
  123. }
  124. // Return the headers
  125. return new HTTP_Header($headers);
  126. }
  127. /**
  128. * Parses the the HTTP request headers and returns an array containing
  129. * key value pairs. This method is slow, but provides an accurate
  130. * representation of the HTTP request.
  131. *
  132. * // Get http headers into the request
  133. * $request->headers = HTTP::request_headers();
  134. *
  135. * @return HTTP_Header
  136. */
  137. public static function request_headers()
  138. {
  139. // If running on apache server
  140. if (function_exists('apache_request_headers'))
  141. {
  142. // Return the much faster method
  143. return new HTTP_Header(apache_request_headers());
  144. }
  145. // If the PECL HTTP tools are installed
  146. elseif (extension_loaded('http'))
  147. {
  148. // Return the much faster method
  149. return new HTTP_Header(http_get_request_headers());
  150. }
  151. // Setup the output
  152. $headers = array();
  153. // Parse the content type
  154. if ( ! empty($_SERVER['CONTENT_TYPE']))
  155. {
  156. $headers['content-type'] = $_SERVER['CONTENT_TYPE'];
  157. }
  158. // Parse the content length
  159. if ( ! empty($_SERVER['CONTENT_LENGTH']))
  160. {
  161. $headers['content-length'] = $_SERVER['CONTENT_LENGTH'];
  162. }
  163. foreach ($_SERVER as $key => $value)
  164. {
  165. // If there is no HTTP header here, skip
  166. if (strpos($key, 'HTTP_') !== 0)
  167. {
  168. continue;
  169. }
  170. // This is a dirty hack to ensure HTTP_X_FOO_BAR becomes x-foo-bar
  171. $headers[str_replace(array('HTTP_', '_'), array('', '-'), $key)] = $value;
  172. }
  173. return new HTTP_Header($headers);
  174. }
  175. /**
  176. * Processes an array of key value pairs and encodes
  177. * the values to meet RFC 3986
  178. *
  179. * @param array $params Params
  180. * @return string
  181. */
  182. public static function www_form_urlencode(array $params = array())
  183. {
  184. if ( ! $params)
  185. return;
  186. $encoded = array();
  187. foreach ($params as $key => $value)
  188. {
  189. $encoded[] = $key.'='.rawurlencode($value);
  190. }
  191. return implode('&', $encoded);
  192. }
  193. } // End Kohana_HTTP