Skip to content

Commit

Permalink
Convert headers in HTTP Message to use a value object so that we can …
Browse files Browse the repository at this point in the history
…have more than one header with the same name.
  • Loading branch information
lonnieezell committed Mar 15, 2016
1 parent de4b728 commit 264c840
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 55 deletions.
5 changes: 3 additions & 2 deletions system/Debug/Toolbar/View/toolbar.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@
<table id="request_headers_table">
<tbody>
<?php foreach ($headers as $header => $value) : ?>
<?php if (empty($value)) continue; ?>
<tr>
<td><?= esc($header) ?></td>
<td><?= esc($request->getHeaderLine($header)) ?></td>
<td><?= esc($value->getName()) ?></td>
<td><?= esc($value->getValueLine()) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down
239 changes: 239 additions & 0 deletions system/HTTP/Header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
<?php namespace CodeIgniter\HTTP;

/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2016, British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
* @license http://opensource.org/licenses/MIT MIT License
* @link http://codeigniter.com
* @since Version 3.0.0
* @filesource
*/

/**
* Class Header
*
* Represents a single HTTP header.
*
* @package CodeIgniter\HTTP
*/
class Header
{
/**
* The name of the header.
*
* @var string
*/
protected $name;

/**
* The value of the header. May have more than one
* value. If so, will be an array of strings.
*
* @var string|array
*/
protected $value;

//--------------------------------------------------------------------

/**
* Header constructor. If a name or value is provided they will be set.
*
* @param string|null $name
* @param string|array|null $value
*/
public function __construct(string $name = null, $value = null)
{
$this->name = $name;
$this->value = $value;
}

//--------------------------------------------------------------------

/**
* Returns the name of the header, in the same case it was set.
*
* @return string
*/
public function getName()
{
return $this->name;
}

//--------------------------------------------------------------------

/**
* Gets the raw value of the header. This may return either a string
* of an array, depending on whether the header has mutliple values or not.
*
* @return array|null|string
*/
public function getValue()
{
return $this->value;
}

//--------------------------------------------------------------------

/**
* Sets the name of the header, overwriting any previous value.
*
* @param string $name
*
* @return $this
*/
public function setName(string $name)
{
$this->name = $name;

return $this;
}

//--------------------------------------------------------------------

/**
* Sets the value of the header, overwriting any previous value(s).
*
* @param null $value
*
* @return $this
*/
public function setValue($value = null)
{
$this->value = $value;

return $this;
}

//--------------------------------------------------------------------

/**
* Appends a value to the list of values for this header. If the
* header is a single value string, it will be converted to an array.
*
* @param null $value
*
* @return $this
*/
public function appendValue($value = null)
{
if (! is_array($this->value))
{
$this->value = [$this->value];
}

$this->value[] = $value;

return $this;
}

//--------------------------------------------------------------------

/**
* Prepends a value to the list of values for this header. If the
* header is a single value string, it will be converted to an array.
*
* @param null $value
*
* @return $this
*/
public function prependValue($value = null)
{
if (! is_array($this->value))
{
$this->value = [$this->value];
}

array_unshift($this->value, $value);

return $this;
}

//--------------------------------------------------------------------


/**
* Retrieves a comma-separated string of the values for a single header.
*
* NOTE: Not all header values may be appropriately represented using
* comma concatenation. For such headers, use getHeader() instead
* and supply your own delimiter when concatenating.
*
* @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
*/
public function getValueLine(): string
{
if (is_string($this->value))
{
return $this->value;
}
else if (! is_array($this->value))
{
return '';
}

$options = [];

foreach ($this->value as $key => $value)
{
if (is_string($key) && ! is_array($value))
{
$options[] = $key.'='.$value;
}
else if (is_array($value))
{
$key = key($value);
$options[] = $key.'='.$value[$key];
}
else if (is_numeric($key))
{
$options[] = $value;
}
}

return implode(', ', $options);
}

//--------------------------------------------------------------------

/**
* Returns a representation of the entire header string, including
* the header name and all values converted to the proper format.
*
* @return string
*/
public function __toString(): string
{
return $this->name.': '.$this->getValueLine();
}

//--------------------------------------------------------------------


}
81 changes: 46 additions & 35 deletions system/HTTP/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
class Message
{
/**
* List of all HTTP request headers
* List of all HTTP request headers.
*
* @var array
*/
Expand Down Expand Up @@ -167,12 +167,15 @@ public function getHeaders() : array
//--------------------------------------------------------------------

/**
* Returns a single header.
* Returns a single header object. If multiple headers with the same
* name exist, then will return an array of header objects.
*
* @param $index
* @param null $filter
*
* @return array|\CodeIgniter\HTTP\Header
*/
public function getHeader($name, $filter = null)
public function getHeader($name)
{
$orig_name = $this->getHeaderName($name);

Expand All @@ -181,14 +184,7 @@ public function getHeader($name, $filter = null)
return NULL;
}

if (is_null($filter))
{
$filter = FILTER_DEFAULT;
}

return is_array($this->headers[$orig_name])
? filter_var_array($this->headers[$orig_name], $filter)
: filter_var($this->headers[$orig_name], $filter);
return $this->headers[$orig_name];
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -234,26 +230,14 @@ public function getHeaderLine(string $name): string
return '';
}

if (is_array($this->headers[$orig_name]) || $this->headers[$orig_name] instanceof \ArrayAccess)
// If there are more than 1 headers with this name,
// then return the value of the first.
if (is_array($this->headers[$orig_name]))
{
$options = [];

foreach ($this->headers[$orig_name] as $key => $value)
{
if (is_numeric($key))
{
$options[] = $value;
}
else
{
$options[] = $key.'='.$value;
}
}

return implode(', ', $options);
return $this->headers[$orig_name][0]->getValueLine();
}

return (string)$this->headers[$orig_name];
return $this->headers[$orig_name]->getValueLine();
}

//--------------------------------------------------------------------
Expand All @@ -269,9 +253,21 @@ public function getHeaderLine(string $name): string
*/
public function setHeader(string $name, $value): self
{
$this->headers[$name] = $value;
if (! isset($this->headers[$name]))
{
$this->headers[$name] = new Header($name, $value);

$this->headerMap[strtolower($name)] = $name;

return $this;
}

if (! is_array($this->headers[$name]))
{
$this->headers[$name] = [$this->headers[$name]];
}

$this->headerMap[strtolower($name)] = $name;
$this->headers[$name][] = new Header($name, $value);

return $this;
}
Expand Down Expand Up @@ -310,12 +306,27 @@ public function appendHeader(string $name, $value): self
{
$orig_name = $this->getHeaderName($name);

if (! is_array($this->headers[$orig_name]) && ! ($this->headers[$orig_name] instanceof \ArrayAccess))
{
throw new \LogicException("Header '{$orig_name}' does not support multiple values.");
}
$this->headers[$orig_name]->appendValue($value);

return $this;
}

//--------------------------------------------------------------------

/**
* Adds an additional header value to any headers that accept
* multiple values (i.e. are an array or implement ArrayAccess)
*
* @param string $name
* @param $value
*
* @return string
*/
public function prependHeader(string $name, $value): self
{
$orig_name = $this->getHeaderName($name);

$this->headers[$orig_name][] = $value;
$this->headers[$orig_name]->prependValue($value);

return $this;
}
Expand Down
Loading

0 comments on commit 264c840

Please sign in to comment.