3 namespace Wikimedia\Http
;
6 * Utility for negotiating a value from a set of supported values using a preference list.
7 * This is intended for use with HTTP headers like Accept, Accept-Language, Accept-Encoding, etc.
8 * See RFC 2616 section 14 for details.
10 * To use this with a request header, first parse the header value into an array of weights
11 * using HttpAcceptParser, then call getBestSupportedKey.
13 * @license GPL-2.0-or-later
14 * @author Daniel Kinzler
15 * @author Thiemo Kreuz
17 class HttpAcceptNegotiator
{
22 private $supportedValues;
27 private $defaultValue;
30 * @param string[] $supported A list of supported values.
32 public function __construct( array $supported ) {
33 $this->supportedValues
= $supported;
34 $this->defaultValue
= reset( $supported );
38 * Returns the best supported key from the given weight map. Of the keys from the
39 * $weights parameter that are also in the list of supported values supplied to
40 * the constructor, this returns the key that has the highest weight associated
41 * with it. If two keys have the same weight, the more specific key is preferred,
42 * as required by RFC2616 section 14. Keys that map to 0 or false are ignored.
43 * If no matching key is found, $default is returned.
45 * @param float[] $weights An associative array mapping accepted values to their
48 * @param null|string $default The value to return if non of the keys in $weights
49 * is supported (null per default).
51 * @return null|string The best supported key from the $weights parameter.
53 public function getBestSupportedKey( array $weights, $default = null ) {
54 // Make sure we correctly bias against wildcards and ranges, see RFC2616, section 14.
55 foreach ( $weights as $name => &$weight ) {
56 if ( $name === '*' ||
$name === '*/*' ) {
58 } elseif ( substr( $name, -2 ) === '/*' ) {
63 // Sort $weights by value and...
66 // remove any keys with values equal to 0 or false (HTTP/1.1 section 3.9)
67 $weights = array_filter( $weights );
69 // ...use the ordered list of keys
70 $preferences = array_reverse( array_keys( $weights ) );
72 $value = $this->getFirstSupportedValue( $preferences, $default );
77 * Returns the first supported value from the given preference list. Of the values from
78 * the $preferences parameter that are also in the list of supported values supplied
79 * to the constructor, this returns the value that has the lowest index in the list.
80 * If no such value is found, $default is returned.
82 * @param string[] $preferences A list of acceptable values, in order of preference.
84 * @param null|string $default The value to return if non of the keys in $weights
85 * is supported (null per default).
87 * @return null|string The best supported key from the $weights parameter.
89 public function getFirstSupportedValue( array $preferences, $default = null ) {
90 foreach ( $preferences as $value ) {
91 foreach ( $this->supportedValues
as $supported ) {
92 if ( $this->valueMatches( $value, $supported ) ) {
102 * Returns true if the given acceptable value matches the given supported value,
103 * according to the HTTP specification. The following rules are used:
105 * - comparison is case-insensitive
106 * - if $accepted and $supported are equal, they match
107 * - if $accepted is `*` or `*` followed by `/*`, it matches any $supported value.
108 * - if both $accepted and $supported contain a `/`, and $accepted ends with `/*`,
109 * they match if the part before the first `/` is equal.
111 * @param string $accepted An accepted value (may contain wildcards)
112 * @param string $supported A supported value.
114 * @return bool Whether the given supported value matches the given accepted value.
116 private function valueMatches( $accepted, $supported ) {
117 // RDF 2045: MIME types are case insensitive.
119 if ( strcasecmp( $accepted, $supported ) === 0 ) {
123 // wildcard match (HTTP/1.1 section 14.1, 14.2, 14.3)
124 if ( $accepted === '*' ||
$accepted === '*/*' ) {
128 // wildcard match (HTTP/1.1 section 14.1)
129 if ( substr( $accepted, -2 ) === '/*'
130 && strncasecmp( $accepted, $supported, strlen( $accepted ) - 2 ) === 0