-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] Add HeaderUtils class #24699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation; | ||
|
||
/** | ||
* HTTP header utility functions. | ||
* | ||
* @author Christian Schmidt <github@chsc.dk> | ||
*/ | ||
class HeaderUtils | ||
{ | ||
/** | ||
* This class should not be instantiated. | ||
*/ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Splits an HTTP header by one or more separators. | ||
* | ||
* Example: | ||
* | ||
* HeaderUtils::split("da, en-gb;q=0.8", ",;") | ||
* // => array(array("da"), array("en-gb"), array("q", "0.8")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure about this example ? Why is the header split on |
||
* | ||
* @param string $header HTTP header value | ||
* @param string $separators List of characters to split on, ordered by | ||
* precedence, e.g. ",", ";=", or ",;=" | ||
* | ||
* @return array Nested array with as many levels as there are characters in | ||
* $separators | ||
*/ | ||
public static function split(string $header, string $separators): array | ||
{ | ||
$quotedSeparators = preg_quote($separators); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should include the regex delimiter ( |
||
|
||
preg_match_all(' | ||
/ | ||
(?!\s) | ||
(?: | ||
# quoted-string | ||
"(?:[^"\\\\]|\\\\.)*(?:"|\\\\|$) | ||
| | ||
# token | ||
[^"'.$quotedSeparators.']+ | ||
)+ | ||
(?<!\s) | ||
| | ||
# separator | ||
\s* | ||
(?<separator>['.$quotedSeparators.']) | ||
\s* | ||
/x', trim($header), $matches, PREG_SET_ORDER); | ||
|
||
return self::groupParts($matches, $separators); | ||
} | ||
|
||
/** | ||
* Combines an array of arrays into one associative array. | ||
* | ||
* Each of the nested arrays should have one or two elements. The first | ||
* value will be used as the keys in the associative array, and the second | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's not what is done. The key is the associative array is the lowercased first value, not the first value. |
||
* will be used as the values, or true if the nested array only contains one | ||
* element. | ||
* | ||
* Example: | ||
* | ||
* HeaderUtils::combineParts(array(array("foo", "abc"), array("bar"))) | ||
* // => array("foo" => "abc", "bar" => true) | ||
*/ | ||
public static function combineParts(array $parts): array | ||
{ | ||
$assoc = array(); | ||
foreach ($parts as $part) { | ||
$name = strtolower($part[0]); | ||
$value = $part[1] ?? true; | ||
$assoc[$name] = $value; | ||
} | ||
|
||
return $assoc; | ||
} | ||
|
||
/** | ||
* Joins an associative array into a string for use in an HTTP header. | ||
* | ||
* The key and value of each entry are joined with "=", and all entries | ||
* is joined with the specified separator and an additional space (for | ||
* readability). Values are quoted if necessary. | ||
* | ||
* Example: | ||
* | ||
* HeaderUtils::joinAssoc(array("foo" => "abc", "bar" => true, "baz" => "a b c"), ",") | ||
* // => 'foo=bar, baz, baz="a b c"' | ||
*/ | ||
public static function joinAssoc(array $assoc, string $separator): string | ||
{ | ||
$parts = array(); | ||
foreach ($assoc as $name => $value) { | ||
if (true === $value) { | ||
$parts[] = $name; | ||
} else { | ||
$parts[] = $name.'='.self::quote($value); | ||
} | ||
} | ||
|
||
return implode($separator.' ', $parts); | ||
} | ||
|
||
/** | ||
* Encodes a string as a quoted string, if necessary. | ||
* | ||
* If a string contains characters not allowed by the "token" construct in | ||
* the HTTP specification, it is backslash-escaped and enclosed in quotes | ||
* to match the "quoted-string" construct. | ||
*/ | ||
public static function quote(string $s): string | ||
{ | ||
if (preg_match('/^[a-z0-9!#$%&\'*.^_`|~-]+$/i', $s)) { | ||
return $s; | ||
} | ||
|
||
return '"'.addcslashes($s, '"\\"').'"'; | ||
} | ||
|
||
/** | ||
* Decodes a quoted string. | ||
* | ||
* If passed an unquoted string that matches the "token" construct (as | ||
* defined in the HTTP specification), it is passed through verbatimly. | ||
*/ | ||
public static function unquote(string $s): string | ||
{ | ||
return preg_replace('/\\\\(.)|"/', '$1', $s); | ||
} | ||
|
||
private static function groupParts(array $matches, string $separators): array | ||
{ | ||
$separator = $separators[0]; | ||
$partSeparators = substr($separators, 1); | ||
|
||
$i = 0; | ||
$partMatches = array(); | ||
foreach ($matches as $match) { | ||
if (isset($match['separator']) && $match['separator'] === $separator) { | ||
++$i; | ||
} else { | ||
$partMatches[$i][] = $match; | ||
} | ||
} | ||
|
||
$parts = array(); | ||
if ($partSeparators) { | ||
foreach ($partMatches as $matches) { | ||
$parts[] = self::groupParts($matches, $partSeparators); | ||
} | ||
} else { | ||
foreach ($partMatches as $matches) { | ||
$parts[] = self::unquote($matches[0][0]); | ||
} | ||
} | ||
|
||
return $parts; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall this class be marked
@internal
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. It is generally useful when parsing HTTP headers, including those not supported directly by HttpFoundation. It may even be used in other contexts than parsing incoming HTTP request headers.