HTTP
extends Kohana_HTTP
Contains the most low-level helpers methods in Kohana:
- Environment initialization
- Locating files within the cascading filesystem
- Auto-loading and transparent extension of classes
- Variable and path debugging
Information
This class is a transparent base class for Kohana_HTTP
Constants
- None
Properties
Properties
-
public static The $protocol
-
default protocol to use if it cannot be detected
-
string(8) "HTTP/1.1"
Methods
public static check_cache(Request $request , Response $response [, string $etag = NULL ] ) (defined in Kohana_HTTP)
Checks the browser cache to see the response needs to be returned, execution will halt and a 304 Not Modified will be sent if the browser cache is up to date.
Parameters
- Request $request required - Request
- Response $response required - Response
- string $etag = NULL - Resource ETag
Tags
Return Values
- Response
Source Code
public static function check_cache(Request $request, Response $response, $etag = NULL)
{
// Generate an etag if necessary
if ($etag == NULL)
{
$etag = $response->generate_etag();
}
// Set the ETag header
$response->headers('etag', $etag);
// Add the Cache-Control header if it is not already set
// This allows etags to be used with max-age, etc
if ($response->headers('cache-control'))
{
$response->headers('cache-control', $response->headers('cache-control').', must-revalidate');
}
else
{
$response->headers('cache-control', 'must-revalidate');
}
// Check if we have a matching etag
if ($request->headers('if-none-match') AND (string) $request->headers('if-none-match') === $etag)
{
// No need to send data again
throw HTTP_Exception::factory(304)->headers('etag', $etag);
}
return $response;
}
public static parse_header_string(string $header_string ) (defined in Kohana_HTTP)
Parses a HTTP header string into an associative array
Parameters
- string $header_string required - Header string to parse
Return Values
- HTTP_Header
Source Code
public static function parse_header_string($header_string)
{
// If the PECL HTTP extension is loaded
if (extension_loaded('http'))
{
// Use the fast method to parse header string
$headers = version_compare(phpversion('http'), '2.0.0', '>=') ?
\http\Header::parse($header_string) :
http_parse_headers($header_string);
return new HTTP_Header($headers);
}
// Otherwise we use the slower PHP parsing
$headers = [];
// Match all HTTP headers
if (preg_match_all('/(\w[^\s:]*):[ ]*([^\r\n]*(?:\r\n[ \t][^\r\n]*)*)/', $header_string, $matches))
{
// Parse each matched header
foreach ($matches[0] as $key => $value)
{
// If the header has not already been set
if ( ! isset($headers[$matches[1][$key]]))
{
// Apply the header directly
$headers[$matches[1][$key]] = $matches[2][$key];
}
// Otherwise there is an existing entry
else
{
// If the entry is an array
if (is_array($headers[$matches[1][$key]]))
{
// Apply the new entry to the array
$headers[$matches[1][$key]][] = $matches[2][$key];
}
// Otherwise create a new array with the entries
else
{
$headers[$matches[1][$key]] = [
$headers[$matches[1][$key]],
$matches[2][$key],
];
}
}
}
}
// Return the headers
return new HTTP_Header($headers);
}
public static redirect([ string $uri = string(0) "" , int $code = integer 302 ] ) (defined in Kohana_HTTP)
Issues a HTTP redirect.
Parameters
- string $uri = string(0) "" - URI to redirect to
- int $code = integer 302 - HTTP Status code to use for the redirect
Tags
Source Code
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 request_headers() (defined in Kohana_HTTP)
Parses the the HTTP request headers and returns an array containing key value pairs. This method is slow, but provides an accurate representation of the HTTP request.
// Get http headers into the request
$request->headers = HTTP::request_headers();
Return Values
- HTTP_Header
Source Code
public static function request_headers()
{
// If running on apache server
if (function_exists('apache_request_headers'))
{
// Return the much faster method
return new HTTP_Header(apache_request_headers());
}
// If the PECL HTTP tools are installed
elseif (extension_loaded('http'))
{
// Return the much faster method
$headers = version_compare(phpversion('http'), '2.0.0', '>=') ?
\http\Env::getRequestHeader() :
http_get_request_headers();
return new HTTP_Header($headers);
}
// Setup the output
$headers = [];
// Parse the content type
if ( ! empty($_SERVER['CONTENT_TYPE']))
{
$headers['content-type'] = $_SERVER['CONTENT_TYPE'];
}
// Parse the content length
if ( ! empty($_SERVER['CONTENT_LENGTH']))
{
$headers['content-length'] = $_SERVER['CONTENT_LENGTH'];
}
foreach ($_SERVER as $key => $value)
{
// If there is no HTTP header here, skip
if (strpos($key, 'HTTP_') !== 0)
{
continue;
}
// This is a dirty hack to ensure HTTP_X_FOO_BAR becomes X-FOO-BAR
$headers[str_replace('_', '-', substr($key, 5))] = $value;
}
return new HTTP_Header($headers);
}
public static www_form_urlencode([ array $params = array(0) ] ) (defined in Kohana_HTTP)
Processes an array of key value pairs and encodes the values to meet RFC 3986
Parameters
- array $params = array(0) - Params
Return Values
- string
Source Code
public static function www_form_urlencode(array $params = [])
{
if ( ! $params)
return;
$encoded = [];
foreach ($params as $key => $value)
{
$encoded[] = $key.'='.rawurlencode($value);
}
return implode('&', $encoded);
}