MCPcopy Create free account
hub / github.com/dolanor/rip / contentNegociateBestHeaderValue

Function contentNegociateBestHeaderValue

header.go:16–48  ·  view source on GitHub ↗
(header http.Header, headerName string, serverPreferences []string)

Source from the content-addressed store, hash-verified

14}
15
16func 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
50func 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.

Callers 6

TestChooseHeaderValueFunction · 0.85
TestBadHeaderQArgValueFunction · 0.85
handleCreateFunction · 0.85
HandleFunction · 0.85
badMethodHandlerFunction · 0.85
preprocessRequestFunction · 0.85

Calls 2

headerChoicesFunction · 0.85
matchHeaderValueFunction · 0.85

Tested by 2

TestChooseHeaderValueFunction · 0.68
TestBadHeaderQArgValueFunction · 0.68