matchFormat checks if a parsed accept clause matches a given Format.
(ac goautoneg.Accept, f Format)
| 173 | |
| 174 | // matchFormat checks if a parsed accept clause matches a given Format. |
| 175 | func matchFormat(ac goautoneg.Accept, f Format) bool { |
| 176 | target, ok := formatToAccept[f] |
| 177 | if !ok { |
| 178 | parsed := goautoneg.ParseAccept(string(f)) |
| 179 | if len(parsed) == 0 { |
| 180 | return false |
| 181 | } |
| 182 | target = parsed[0] |
| 183 | } |
| 184 | |
| 185 | if ac.Type != "*" && ac.Type != target.Type { |
| 186 | return false |
| 187 | } |
| 188 | if ac.SubType != "*" && ac.SubType != target.SubType { |
| 189 | return false |
| 190 | } |
| 191 | |
| 192 | // Default OpenMetrics version to OpenMetricsVersion_0_0_1. |
| 193 | acVersion := ac.Params["version"] |
| 194 | if acVersion == "" && ac.Type+"/"+ac.SubType == OpenMetricsType { |
| 195 | acVersion = OpenMetricsVersion_0_0_1 |
| 196 | } |
| 197 | if acVersion == "" && ac.Type == "text" && ac.SubType == "plain" { |
| 198 | acVersion = TextVersion |
| 199 | } |
| 200 | |
| 201 | // General param matching. |
| 202 | for k, v := range target.Params { |
| 203 | if k == "charset" { |
| 204 | continue |
| 205 | } |
| 206 | acVal := ac.Params[k] |
| 207 | if k == "version" { |
| 208 | acVal = acVersion |
| 209 | } |
| 210 | if acVal != v { |
| 211 | return false |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return true |
| 216 | } |
| 217 | |
| 218 | // NewEncoder returns a new encoder based on content type negotiation. All |
| 219 | // Encoder implementations returned by NewEncoder also implement Closer, and |