MediaWiki master
HttpAcceptParser.php
Go to the documentation of this file.
1<?php
2
11namespace Wikimedia\Http;
12
14
29 public function parseAccept( $accept ): array {
30 // FIXME: Allow commas in quotes
31 $accepts = explode( ',', $accept );
32 $ret = [];
33
34 foreach ( $accepts as $i => $a ) {
35 if ( !preg_match( '!^([^\s/;]+)/([^;\s]+)\s*(?:;(.*))?$!D', trim( $a ), $matches ) ) {
36 continue;
37 }
38
39 $q = 1;
40 $params = [];
41 if ( isset( $matches[3] ) ) {
42 // FIXME: Allow semi-colon in quotes
43 $kvps = explode( ';', $matches[3] );
44 foreach ( $kvps as $kv ) {
45 $kvArray = explode( '=', trim( $kv ), 2 );
46 if ( count( $kvArray ) != 2 ) {
47 continue;
48 }
49 [ $key, $val ] = $kvArray;
50 $key = strtolower( trim( $key ) );
51 $val = trim( $val );
52 if ( $key === 'q' ) {
53 // FIXME: Spec is stricter about this
54 $q = (float)$val;
55 } else {
56 if ( $val && $val[0] === '"' && $val[ strlen( $val ) - 1 ] === '"' ) {
57 $val = substr( $val, 1, strlen( $val ) - 2 );
58 }
59 $params[$key] = $val;
60 }
61 }
62 }
63 $ret[] = [
64 'type' => $matches[1],
65 'subtype' => $matches[2],
66 'q' => $q,
67 'i' => $i,
68 'params' => $params,
69 ];
70 }
71
72 // Sort list. First by q values, then by order
73 usort( $ret, static function ( $a, $b ) {
74 if ( $b['q'] > $a['q'] ) {
75 return 1;
76 } elseif ( $b['q'] === $a['q'] ) {
77 return $a['i'] - $b['i'];
78 } else {
79 return -1;
80 }
81 } );
82
83 return $ret;
84 }
85
100 public function parseWeights( $rawHeader ) {
101 // first, strip header name
102 $rawHeader = preg_replace( '/^[-\w]+:\s*/', '', $rawHeader );
103
104 // Return values in lower case
105 $rawHeader = strtolower( $rawHeader );
106
107 $accepts = $this->parseAccept( $rawHeader );
108
109 // Create a list like "en" => 0.8
110 return array_reduce( $accepts, static function ( $prev, $next ) {
111 $type = "{$next['type']}/{$next['subtype']}";
112 $prev[$type] = $next['q'];
113 return $prev;
114 }, [] );
115 }
116
117}
parseAccept( $accept)
Parse media types from an Accept header and sort them by q-factor.
parseWeights( $rawHeader)
Parses an HTTP header into a weight map, that is an associative array mapping values to their respect...
Utility for parsing a HTTP Accept header value into a weight map.