ParseAccept parses an "Accept-Encoding" header. Reports a sorted list of encodings and a map of disallowed encodings. Reports nil if no selections were present.
(h string)
| 65 | // Reports a sorted list of encodings and a map of disallowed encodings. |
| 66 | // Reports nil if no selections were present. |
| 67 | func parseAccept(h string) ([]accept, map[string]struct{}) { |
| 68 | segs := strings.Split(h, ",") |
| 69 | ret := make([]accept, 0, len(segs)) |
| 70 | nok := make(map[string]struct{}) |
| 71 | for _, s := range segs { |
| 72 | a := accept{} |
| 73 | t, param, err := mime.ParseMediaType(s) |
| 74 | if err != nil { |
| 75 | continue |
| 76 | } |
| 77 | a.Type = t |
| 78 | if q, ok := param["q"]; ok { |
| 79 | if q == "0" { |
| 80 | nok[t] = struct{}{} |
| 81 | continue |
| 82 | } |
| 83 | qv, err := strconv.ParseFloat(param["q"], 64) |
| 84 | if err != nil { |
| 85 | nok[t] = struct{}{} |
| 86 | continue |
| 87 | } |
| 88 | a.Q = qv |
| 89 | } |
| 90 | ret = append(ret, a) |
| 91 | } |
| 92 | |
| 93 | sort.SliceStable(ret, func(i, j int) bool { |
| 94 | return ret[i].Q > ret[j].Q |
| 95 | }) |
| 96 | return ret, nok |
| 97 | } |
| 98 | |
| 99 | type accept struct { |
| 100 | Type string |