Skip to content

Commit

Permalink
Added CI_Output::get_header()
Browse files Browse the repository at this point in the history
(an improved version of PR bcit-ci#645)

Also fixed get_content_type() to only return the MIME value and created
Output library unit tests for both of these methods.
  • Loading branch information
narfbg committed Nov 29, 2012
1 parent 5b9be19 commit 13801a2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
35 changes: 34 additions & 1 deletion system/core/Output.php
Expand Up @@ -256,7 +256,7 @@ public function get_content_type()
{
for ($i = 0, $c = count($this->headers); $i < $c; $i++)
{
if (sscanf($this->headers[$i][0], 'Content-Type: %s', $content_type) === 1)
if (sscanf($this->headers[$i][0], 'Content-Type: %[^;]', $content_type) === 1)
{
return $content_type;
}
Expand All @@ -267,6 +267,39 @@ public function get_content_type()

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

/**
* Get Header
*
* @param string $header_name
* @return string
*/
public function get_header($header)
{
// Combine headers already sent with our batched headers
$headers = array_merge(
// We only need [x][0] from our multi-dimensional array
array_map('array_shift', $this->headers),
headers_list()
);

if (empty($headers) OR empty($header))
{
return NULL;
}

for ($i = 0, $c = count($headers); $i < $c; $i++)
{
if (strncasecmp($header, $headers[$i], $l = strlen($header)) === 0)
{
return trim(substr($headers[$i], $l+1));
}
}

return NULL;
}

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

/**
* Set HTTP Status Header
*
Expand Down
35 changes: 35 additions & 0 deletions tests/codeigniter/core/Output_test.php
@@ -0,0 +1,35 @@
<?php

class Output_test extends CI_TestCase {

public function set_up()
{
$this->ci_set_config('charset', 'UTF-8');
$output = $this->ci_core_class('output');
$this->output = new $output();
}

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

public function test_get_content_type()
{
$this->assertEquals('text/html', $this->output->get_content_type());
}

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

public function test_get_header()
{
$this->assertNull($this->output->get_header('Non-Existent-Header'));

// TODO: Find a way to test header() values as well. Currently,
// PHPUnit prevents this by not using output buffering.

$this->output->set_content_type('text/plain', 'WINDOWS-1251');
$this->assertEquals(
'text/plain; charset=windows-1251', // Character set is converted to lowercase
$this->output->get_header('content-type') // Case-insensitive comparison
);
}

}
3 changes: 2 additions & 1 deletion user_guide_src/source/changelog.rst
Expand Up @@ -294,8 +294,9 @@ Release Date: Not Released
- Added support for HTTP-Only cookies with new config option *cookie_httponly* (default FALSE).
- Renamed method ``_call_hook()`` to ``call_hook()`` in the :doc:`Hooks Library <general/hooks>`.
- :doc:`Output Library <libraries/output>` changes include:
- Added method ``get_content_type()``.
- Added a second argument to method ``set_content_type()`` that allows setting the document charset as well.
- Added method ``get_content_type()``.
- Added method ``get_header()``.
- ``$config['time_reference']`` now supports all timezone strings supported by PHP.
- :doc:`Config Library <libraries/config>` changes include:
- Changed ``site_url()`` method to accept an array as well.
Expand Down
30 changes: 25 additions & 5 deletions user_guide_src/source/libraries/output.rst
Expand Up @@ -53,17 +53,37 @@ You can also set the character set of the document, by passing a second argument

$this->output->set_content_type('css', 'utf-8');

$this->output->get_content_type();
==========================================
$this->output->get_content_type()
=================================

Returns the Content-Type HTTP header that's currently in use.
Returns the Content-Type HTTP header that's currently in use,
excluding the character set value.

$mime = $this->output->get_content_type();

.. note:: If not set, the default return value is 'text/html'.

$this->output->get_output();
=============================
$this->output->get_header()
===========================

Gets the requested HTTP header value, if set.

If the header is not set, NULL will be returned.
If an empty value is passed to the method, it will return FALSE.

Example::

$this->output->set_content_type('text/plain', 'UTF-8');
echo $this->output->get_header('content-type');
// Outputs: text/plain; charset=utf-8

.. note:: The header name is compared in a case-insensitive manner.

.. note:: Raw headers sent via PHP's native ``header()`` function are
also detected.

$this->output->get_output()
===========================

Permits you to manually retrieve any output that has been sent for
storage in the output class. Usage example::
Expand Down

0 comments on commit 13801a2

Please sign in to comment.