// MediaTypesForSerializer returns a list of media and stream media types for the server. func MediaTypesForSerializer(ns runtime.NegotiatedSerializer) (mediaTypes, streamMediaTypes []string) { for _, info := range ns.SupportedMediaTypes() { mediaTypes = append(mediaTypes, info.MediaType) if info.StreamSerializer != nil { // stream=watch is the existing mime-type parameter for watch streamMediaTypes = append(streamMediaTypes, info.MediaType+";stream=watch") } } return mediaTypes, streamMediaTypes }
// WriteObjectNegotiated renders an object in the content type negotiated by the client func WriteObjectNegotiated(s runtime.NegotiatedSerializer, gv schema.GroupVersion, w http.ResponseWriter, req *http.Request, statusCode int, object runtime.Object) { serializer, err := negotiation.NegotiateOutputSerializer(req, s) if err != nil { status := apiStatus(err) WriteRawJSON(int(status.Code), status, w) return } w.Header().Set("Content-Type", serializer.MediaType) w.WriteHeader(statusCode) encoder := s.EncoderForVersion(serializer.Serializer, gv) if err := encoder.Encode(object, w); err != nil { errorJSONFatal(err, encoder, w) } }
// acceptedMediaTypesForEndpoint returns an array of structs that are used to efficiently check which // allowed media types the server exposes. func acceptedMediaTypesForEndpoint(ns runtime.NegotiatedSerializer) []acceptedMediaType { var acceptedMediaTypes []acceptedMediaType for _, info := range ns.SupportedMediaTypes() { segments := strings.SplitN(info.MediaType, "/", 2) if len(segments) == 1 { segments = append(segments, "*") } t := acceptedMediaType{ Type: segments[0], SubType: segments[1], Serializer: info, } acceptedMediaTypes = append(acceptedMediaTypes, t) } return acceptedMediaTypes }
func NegotiateInputSerializer(req *http.Request, ns runtime.NegotiatedSerializer) (runtime.SerializerInfo, error) { mediaTypes := ns.SupportedMediaTypes() mediaType := req.Header.Get("Content-Type") if len(mediaType) == 0 { mediaType = mediaTypes[0].MediaType } mediaType, _, err := mime.ParseMediaType(mediaType) if err != nil { _, supported := MediaTypesForSerializer(ns) return runtime.SerializerInfo{}, errUnsupportedMediaType{supported} } for _, info := range mediaTypes { if info.MediaType != mediaType { continue } return info, nil } _, supported := MediaTypesForSerializer(ns) return runtime.SerializerInfo{}, errUnsupportedMediaType{supported} }