From 864ab64ede8000e54767a4166b7907eecd42ef8a Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 23 Apr 2018 14:57:33 +0200 Subject: [PATCH 1/2] Documented the HeaderUtils class --- components/http_foundation.rst | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/components/http_foundation.rst b/components/http_foundation.rst index d0d8963581b..7a9e8cf5233 100644 --- a/components/http_foundation.rst +++ b/components/http_foundation.rst @@ -241,6 +241,39 @@ the previous requests. an exception in Symfony 5.0 when the session is ``null``. Check for an existing session first by calling :method:`Symfony\\Component\\HttpFoundation\\Request::hasSession()`. +Processing HTTP Headers +~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 4.1 + The ``HeaderUtils`` class was introduced in Symfony 4.1. + +Processing HTTP headers is not a trivial task because of the escaping and white +space handling of their contents. Symfony provides a +:class:`Symfony\\Component\\HttpFoundation\\HeaderUtils` class that abstracts +this complexity and defines some methods for the most common tasks:: + + use Symfony\Component\HttpFoundation\HeaderUtils; + + // Splits an HTTP header by one or more separators + HeaderUtils::split('da, en-gb;q=0.8', ',;') + // => array(array('da'), array('en-gb'), array('q', '0.8')) + + // Combines an array of arrays into one associative array + HeaderUtils::combineParts(array(array('foo', 'abc'), array('bar'))) + // => array('foo' => 'abc', 'bar' => true) + + // Joins an associative array into a string for use in an HTTP header + HeaderUtils::joinAssoc(array('foo' => 'abc', 'bar' => true, 'baz' => 'a b c'), ',') + // => 'foo=bar, baz, baz="a b c"' + + // Encodes a string as a quoted string, if necessary + HeaderUtils::quote('foo "bar"') + // => 'foo \"bar\"' + + // Decodes a quoted string + HeaderUtils::unquote('foo \"bar\"') + // => 'foo "bar"' + Accessing ``Accept-*`` Headers Data ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 626aa96ef8a2f0f5c9b14c15dce89d757fdff1f3 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 30 Apr 2018 09:01:35 +0200 Subject: [PATCH 2/2] Renamed some methods --- components/http_foundation.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/http_foundation.rst b/components/http_foundation.rst index 7a9e8cf5233..7835f48f2f8 100644 --- a/components/http_foundation.rst +++ b/components/http_foundation.rst @@ -259,11 +259,11 @@ this complexity and defines some methods for the most common tasks:: // => array(array('da'), array('en-gb'), array('q', '0.8')) // Combines an array of arrays into one associative array - HeaderUtils::combineParts(array(array('foo', 'abc'), array('bar'))) + HeaderUtils::combine(array(array('foo', 'abc'), array('bar'))) // => array('foo' => 'abc', 'bar' => true) // Joins an associative array into a string for use in an HTTP header - HeaderUtils::joinAssoc(array('foo' => 'abc', 'bar' => true, 'baz' => 'a b c'), ',') + HeaderUtils::toString(array('foo' => 'abc', 'bar' => true, 'baz' => 'a b c'), ',') // => 'foo=bar, baz, baz="a b c"' // Encodes a string as a quoted string, if necessary