MCPcopy
hub / github.com/ContainerSSH/ContainerSSH / findMarshaller

Method findMarshaller

http/handler_impl.go:91–130  ·  view source on GitHub ↗
(_ goHttp.ResponseWriter, request *goHttp.Request)

Source from the content-addressed store, hash-verified

89}
90
91func (h *handler) findMarshaller(_ goHttp.ResponseWriter, request *goHttp.Request) (responseMarshaller, string, int) {
92 acceptHeader := request.Header.Get("Accept")
93 if acceptHeader == "" {
94 return h.defaultResponseMarshaller, h.defaultResponseType, 200
95 }
96
97 accepted := strings.Split(acceptHeader, ",")
98 acceptMap := make(map[string]float64, len(accepted))
99 acceptList := make([]string, len(accepted))
100 for i, accept := range accepted {
101 acceptParts := strings.SplitN(strings.TrimSpace(accept), ";", 2)
102 q := 1.0
103 if len(acceptParts) == 2 {
104 acceptParts2 := strings.SplitN(acceptParts[1], "=", 2)
105 if acceptParts2[0] == "q" && len(acceptParts2) == 2 {
106 var err error
107 q, err = strconv.ParseFloat(acceptParts2[1], 64)
108 if err != nil {
109 return nil, h.defaultResponseType, 400
110 }
111 } else {
112 return nil, h.defaultResponseType, 400
113 }
114 }
115 acceptMap[acceptParts[0]] = q
116 acceptList[i] = acceptParts[0]
117 }
118 sort.SliceStable(acceptList, func(i, j int) bool {
119 return acceptMap[acceptList[i]] > acceptMap[acceptList[j]]
120 })
121
122 for _, a := range acceptList {
123 for _, marshaller := range h.responseMarshallers {
124 if marshaller.SupportsMIME(a) {
125 return marshaller, a, 200
126 }
127 }
128 }
129 return nil, h.defaultResponseType, 406
130}
131
132type internalRequest struct {
133 writer goHttp.ResponseWriter

Callers 1

ServeHTTPMethod · 0.95

Calls 2

GetMethod · 0.65
SupportsMIMEMethod · 0.65

Tested by

no test coverage detected