/usr/share/php/kohana3.1/system/classes/kohana/http/header.php is in libkohana3.1-core-php 3.1.5-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | <?php defined('SYSPATH') OR die('No direct script access.');
/**
* The Kohana_HTTP_Header class provides an Object-Orientated interface
* to HTTP headers. This can parse header arrays returned from the
* PHP functions `apache_request_headers()` or the `http_parse_headers()`
* function available within the PECL HTTP library.
*
* @package Kohana
* @category HTTP
* @author Kohana Team
* @since 3.1.0
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_HTTP_Header extends ArrayObject {
/**
* @var boolean Controls whether to automatically sort headers by quality value
*/
public static $sort_by_quality = FALSE;
/**
* @var array Default positive filter for sorting header values
*/
public static $default_sort_filter = array('accept','accept-charset','accept-encoding','accept-language');
/**
* Parses HTTP Header values and creating an appropriate object
* depending on type; i.e. accept-type, accept-char, cache-control etc.
*
* $header_values_array = HTTP_Header::parse_header_values(array('cache-control' => 'max-age=200; public'));
*
* @param array $header_values Values to parse
* @param array $header_commas_allowed Header values where commas are not delimiters (usually date)
* @return array
*/
public static function parse_header_values(array $header_values, array $header_commas_allowed = array('user-agent', 'date', 'expires', 'last-modified'))
{
/**
* @see http://www.w3.org/Protocols/rfc2616/rfc2616.html
*
* HTTP header declarations should be treated as case-insensitive
*/
$header_values = array_change_key_case($header_values, CASE_LOWER);
// Foreach of the header values applied
foreach ($header_values as $key => $value)
{
if (is_array($value))
{
$values = array();
if (Arr::is_assoc($value))
{
foreach ($value as $k => $v)
{
$values[] = HTTP_Header::parse_header_values($v);
}
}
else
{
// RFC 2616 allows multiple headers with same name if they can be
// concatinated using commas without altering the original message.
// This usually occurs with multiple Set-Cookie: headers
$array = array();
foreach ($value as $k => $v)
{
// Break value into component parts
$v = explode(';', $v);
// Do some nasty parsing to flattern the array into components,
// parsing key values
$array = Arr::flatten(array_map('HTTP_Header_Value::parse_key_value', $v));
// Get the K/V component and extract the first element
$key_value_component = array_slice($array, 0, 1, TRUE);
array_shift($array);
// Create the HTTP_Header_Value component array
$http_header['key'] = key($key_value_component);
$http_header['value'] = current($key_value_component);
$http_header['properties'] = $array;
// Create the HTTP_Header_Value
$values[] = new HTTP_Header_Value($http_header);
}
}
// Assign HTTP_Header_Value array to the header
$header_values[$key] = $values;
continue;
}
// If the key allows commas or no commas are found
if (in_array($key, $header_commas_allowed) or (strpos($value, ',') === FALSE))
{
// If the key is user-agent, we don't want to parse the string
if ($key === 'user-agent')
{
$header_values[$key] = new HTTP_Header_Value($value, TRUE);
}
// Else, behave normally
else
{
$header_values[$key] = new HTTP_Header_Value($value);
}
// Move to next header
continue;
}
// Create an array of the values and clear any whitespace
$value = array_map('trim', explode(',', $value));
$parsed_values = array();
// Foreach value
foreach ($value as $v)
{
$v = new HTTP_Header_Value($v);
// Convert the value string into an object
if ($v->key === NULL)
{
$parsed_values[] = $v;
}
else
{
$parsed_values[$v->key] = $v;
}
}
// Apply parsed value to the header
$header_values[$key] = $parsed_values;
}
// Return the parsed header values
return $header_values;
}
/**
* Constructor method for [Kohana_HTTP_Header]. Uses the standard constructor
* of the parent `ArrayObject` class.
*
* $header_object = new HTTP_Header(array('x-powered-by' => 'Kohana 3.1.x', 'expires' => '...'));
*
* @param mixed $input Input array
* @param int $flags Flags
* @param string $iterator_class The iterator class to use
*/
public function __construct($input, $flags = NULL, $iterator_class = 'ArrayIterator')
{
// Parse the values into [HTTP_Header_Values]
parent::__construct(HTTP_Header::parse_header_values($input), $flags, $iterator_class);
// If sort by quality is set, sort the fields by q=0.0 value
if (HTTP_Header::$sort_by_quality)
{
$this->sort_values_by_quality();
}
}
/**
* Returns the header object as a string, including
* the terminating new line
*
* // Return the header as a string
* echo (string) $request->headers();
*
* @return string
*/
public function __toString()
{
$header = '';
foreach ($this as $key => $value)
{
if (is_array($value))
{
$header .= $key.': '.(implode(', ', $value))."\r\n";
}
else
{
$header .= $key.': '.$value."\r\n";
}
}
return $header."\n";
}
/**
* Overloads the `ArrayObject::exchangeArray()` method to ensure all
* values passed are parsed correctly into a [Kohana_HTTP_Header_Value].
*
* // Input new headers
* $headers->exchangeArray(array(
* 'date' => 'Wed, 24 Nov 2010 21:09:23 GMT',
* 'cache-control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
* ));
*
* @param array $array Array to exchange
* @return array
*/
public function exchangeArray($array)
{
return parent::exchangeArray(HTTP_Header::parse_header_values($array));
}
/**
* Overloads the `ArrayObject::offsetSet` method to ensure any
* access is correctly converted to the correct object type.
*
* // Add a new header from encoded string
* $headers['cache-control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
*
* @param mixed $index Key
* @param mixed $newval Value
* @return void
*/
public function offsetSet($index, $newval)
{
if (is_array($newval) AND (current($newval) instanceof HTTP_Header_Value))
return parent::offsetSet(strtolower($index), $newval);
elseif ( ! $newval instanceof HTTP_Header_Value)
{
$newval = new HTTP_Header_Value((string) $newval);
}
parent::offsetSet(strtolower($index), (string) $newval);
}
/**
* Sort the headers by quality property if the header matches the
* [Kohana_HTTP_Header::$default_sort_filter] definition.
*
* #### Default sort values
*
* - Accept
* - Accept-Chars
* - Accept-Encoding
* - Accept-Lang
*
* @param array $filter Header fields to parse
* @return self
*/
public function sort_values_by_quality(array $filter = array())
{
// If a filter argument is supplied
if ($filter)
{
// Apply filter and store previous
$previous_filter = HTTP_Header::$default_sort_filter;
HTTP_Header::$default_sort_filter = $filter;
}
// Get a copy of this ArrayObject
$values = $this->getArrayCopy();
foreach ($values as $key => $value)
{
if ( ! is_array($value) or ! in_array($key, HTTP_Header::$default_sort_filter))
{
unset($values[$key]);
continue;
}
// Sort them by comparison
uasort($value, array($this, '_sort_by_comparison'));
$values[$key] = $value;
}
// Return filter to previous state if required
if ($filter)
{
HTTP_Header::$default_sort_filter = $previous_filter;
}
foreach ($values as $key => $value)
{
$this[$key] = $value;
}
// Return this
return $this;
}
protected function _sort_by_comparison($value_a, $value_b)
{
// Test for correct instance type
if ( ! $value_a instanceof HTTP_Header_Value OR ! $value_b instanceof HTTP_Header_Value)
{
// Return neutral if cannot test value
return 0;
}
// Extract the qualities
$a = (float) Arr::get($value_a->properties, 'q', HTTP_Header_Value::$default_quality);
$b = (float) Arr::get($value_b->properties, 'q', HTTP_Header_Value::$default_quality);
if ($a == $b)
return 0;
elseif ($a < $b)
return 1;
elseif ($a > $b)
return -1;
}
} // End Kohana_HTTP_Header
|