| 14 | } |
| 15 | |
| 16 | func contentNegociateBestHeaderValue(header http.Header, headerName string, serverPreferences []string) (string, error) { |
| 17 | clientPreferences, err := headerChoices(headerName, header[headerName]) |
| 18 | if err != nil { |
| 19 | return "", err |
| 20 | } |
| 21 | |
| 22 | if len(clientPreferences) > 0 && |
| 23 | clientPreferences[0].Value == "*/*" { |
| 24 | // If the first type is a catch all and there is a second, we'll select the second |
| 25 | if len(clientPreferences) > 1 { |
| 26 | clientPreferences = clientPreferences[1:] |
| 27 | } else { |
| 28 | // Otherwise, we just declare we have no preferences as we accept `*/*` |
| 29 | clientPreferences = []headerChoice{} |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | if len(clientPreferences) == 0 { |
| 34 | // check in request Content-Type |
| 35 | headerName := "Content-Type" |
| 36 | clientPreferences, err = headerChoices(headerName, header[headerName]) |
| 37 | if err != nil { |
| 38 | return "", err |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | best, ok := matchHeaderValue(clientPreferences, serverPreferences) |
| 43 | if ok { |
| 44 | return best, nil |
| 45 | } |
| 46 | |
| 47 | return "", nil |
| 48 | } |
| 49 | |
| 50 | func matchHeaderValue(clientPreferences []headerChoice, serverPreferences []string) (string, bool) { |
| 51 | // if client has no preferences and server has at least 1, we select the 1st server preference. |