diff options
author | Andrey Andreev <narf@bofh.bg> | 2012-11-29 16:21:43 +0100 |
---|---|---|
committer | Andrey Andreev <narf@bofh.bg> | 2012-11-29 16:21:43 +0100 |
commit | cc4b00347c9ff061488bc88194002be701f4190f (patch) | |
tree | 899984be1b01268cff965aa30d31e5f56a8701a3 | |
parent | e1d6c46b7d5e3d82dc6953af29f0b02ccb03e2dc (diff) |
Added CI_Output::get_header()
(an improved version of PR #645)
Also fixed get_content_type() to only return the MIME value and created
Output library unit tests for both of these methods.
-rw-r--r-- | system/core/Output.php | 35 | ||||
-rw-r--r-- | tests/codeigniter/core/Output_test.php | 35 | ||||
-rw-r--r-- | user_guide_src/source/changelog.rst | 3 | ||||
-rw-r--r-- | user_guide_src/source/libraries/output.rst | 30 |
4 files changed, 96 insertions, 7 deletions
diff --git a/system/core/Output.php b/system/core/Output.php index 6312ccd86..7bfde07c7 100644 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -256,7 +256,7 @@ class CI_Output { { 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; } @@ -268,6 +268,39 @@ class CI_Output { // -------------------------------------------------------------------- /** + * 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 * * As of version 1.7.2, this is an alias for common function diff --git a/tests/codeigniter/core/Output_test.php b/tests/codeigniter/core/Output_test.php new file mode 100644 index 000000000..3384143e9 --- /dev/null +++ b/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 + ); + } + +}
\ No newline at end of file diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index ab1d99dab..09c425d0a 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -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. diff --git a/user_guide_src/source/libraries/output.rst b/user_guide_src/source/libraries/output.rst index 82b1a56a5..a3d67b847 100644 --- a/user_guide_src/source/libraries/output.rst +++ b/user_guide_src/source/libraries/output.rst @@ -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:: |