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
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\HTTP;
/**
* HTTP Header field definitions.
*
* Instances of this class are used to collect and manipulate HTTP header field definitions.
* Header field instances are used to handle the definition of complex header fields such as
* `Content-Type` and `Cache-Control`. For instance a {@link Headers\CacheControl} instance
* is used to handle the directives of the `Cache-Control` header field.
*
* @see http://tools.ietf.org/html/rfc2616#section-14
*/
class Headers implements \ArrayAccess, \IteratorAggregate
{
static private $mapping = [
'Cache-Control' => Headers\CacheControl::class,
'Content-Disposition' => Headers\ContentDisposition::class,
'Content-Type' => Headers\ContentType::class,
'Date' => Headers\Date::class,
'Expires' => Headers\Date::class,
'If-Modified-Since' => Headers\Date::class,
'If-Unmodified-Since' => Headers\Date::class,
'Last-Modified' => Headers\Date::class
];
/**
* Normalizes field name.
*
* @param string $name
*
* @return string
*/
static private function normalize_field_name($name)
{
return mb_convert_case(strtr(substr($name, 5), '_', '-'), MB_CASE_TITLE);
}
/**
* Header fields.
*
* @var array
*/
protected $fields = [];
/**
* If the `REQUEST_URI` key is found in the header fields they are considered coming from the
* super global `$_SERVER` array in which case they are filtered to keep only keys
* starting with the `HTTP_` prefix. Also, header field names are normalized. For instance,
* `HTTP_CONTENT_TYPE` becomes `Content-Type`.
*
* @param array $fields The initial headers.
*/
public function __construct(array $fields = [])
{
if (isset($fields['REQUEST_URI']))
{
foreach ($fields as $field => $value)
{
if (strpos($field, 'HTTP_') !== 0)
{
continue;
}
$field = self::normalize_field_name($field);
$this[$field] = $value;
}
}
else
{
foreach ($fields as $field => $value)
{
if (strpos($field, 'HTTP_') === 0)
{
$field = self::normalize_field_name($field);
}
$this[$field] = $value;
}
}
}
/**
* Clone instantiated fields.
*/
public function __clone()
{
foreach ($this->fields as &$field)
{
if (!is_object($field))
{
continue;
}
$field = clone $field;
}
}
/**
* Returns the header as a string.
*
* Header fields with empty string values are discarded.
*
* @return string
*/
public function __toString()
{
$header = '';
foreach ($this->fields as $field => $value)
{
$value = (string) $value;
if ($value === '')
{
continue;
}
$header .= "$field: $value\r\n";
}
return $header;
}
/**
* Sends header fields using the {@link header()} function.
*
* Header fields with empty string values are discarded.
*/
public function __invoke()
{
foreach ($this->fields as $field => $value)
{
$value = (string) $value;
if ($value === '')
{
continue;
}
$this->send_header($field, $value);
}
}
/**
* Send header field.
*
* Note: The only reason for this method is testing.
*
* @param string $field
* @param string $value
*/
// @codeCoverageIgnoreStart
protected function send_header($field, $value)
{
header("$field: $value");
}
// @codeCoverageIgnoreEnd
/**
* Checks if a header field exists.
*
* @param mixed $field
*
* @return boolean
*/
public function offsetExists($field)
{
return isset($this->fields[(string) $field]);
}
/**
* Returns a header.
*
* @param mixed $field
*
* @return string|null The header field value or null if it is not defined.
*/
public function offsetGet($field)
{
if (isset(self::$mapping[$field]))
{
if (empty($this->fields[$field]))
{
$class = self::$mapping[$field];
$this->fields[$field] = call_user_func($class . '::from', null);
}
return $this->fields[$field];
}
return $this->offsetExists($field) ? $this->fields[$field] : null;
}
/**
* Sets a header field.
*
* Note: Setting a header field to `null` removes it, just like unset() would.
*
* ## Date, Expires, Last-Modified
*
* The `Date`, `Expires` and `Last-Modified` header fields can be provided as a Unix
* timestamp, a string or a {@link \DateTime} object.
*
* ## Cache-Control, Content-Disposition and Content-Type
*
* Instances of the {@link Headers\CacheControl}, {@link Headers\ContentDisposition} and
* {@link Headers\ContentType} are used to handle the values of the `Cache-Control`,
* `Content-Disposition` and `Content-Type` header fields.
*
* @param string $field The header field to set.
* @param mixed $value The value of the header field.
*/
public function offsetSet($field, $value)
{
if ($value === null)
{
unset($this[$field]);
return;
}
switch ($field)
{
# http://tools.ietf.org/html/rfc2616#section-14.25
case 'If-Modified-Since':
{
#
# Removes the ";length=xxx" string added by Internet Explorer.
# http://stackoverflow.com/questions/12626699/if-modified-since-http-header-passed-by-ie9-includes-length
#
if (is_string($value))
{
$pos = strpos($value, ';');
if ($pos)
{
$value = substr($value, 0, $pos);
}
}
}
break;
# http://tools.ietf.org/html/rfc2616#section-14.37
case 'Retry-After':
{
$value = is_numeric($value) ? $value : Headers\Date::from($value);
}
break;
}
if (isset(self::$mapping[$field]))
{
$value = call_user_func(self::$mapping[$field] . '::from', $value);
}
$this->fields[$field] = $value;
}
/**
* Removes a header field.
*
* @param mixed $field
*/
public function offsetUnset($field)
{
unset($this->fields[$field]);
}
/**
* Returns an iterator for the header fields.
*/
public function getIterator()
{
return new \ArrayIterator($this->fields);
}
}