@@ -43,13 +43,13 @@ func MediaTypesForSerializer(ns runtime.NegotiatedSerializer) (mediaTypes, strea
43
43
// NegotiateOutputMediaType negotiates the output structured media type and a serializer, or
44
44
// returns an error.
45
45
func NegotiateOutputMediaType (req * http.Request , ns runtime.NegotiatedSerializer , restrictions EndpointRestrictions ) (MediaTypeOptions , runtime.SerializerInfo , error ) {
46
- mediaType , ok := NegotiateMediaTypeOptions (req .Header .Get ("Accept" ), AcceptedMediaTypesForEndpoint ( ns ), restrictions )
46
+ mediaType , ok := NegotiateMediaTypeOptions (req .Header .Get ("Accept" ), ns . SupportedMediaTypes ( ), restrictions )
47
47
if ! ok {
48
48
supported , _ := MediaTypesForSerializer (ns )
49
49
return mediaType , runtime.SerializerInfo {}, NewNotAcceptableError (supported )
50
50
}
51
51
// TODO: move into resthandler
52
- info := mediaType .Accepted . Serializer
52
+ info := mediaType .Accepted
53
53
if (mediaType .Pretty || isPrettyPrint (req )) && info .PrettySerializer != nil {
54
54
info .Serializer = info .PrettySerializer
55
55
}
@@ -58,12 +58,12 @@ func NegotiateOutputMediaType(req *http.Request, ns runtime.NegotiatedSerializer
58
58
59
59
// NegotiateOutputMediaTypeStream returns a stream serializer for the given request.
60
60
func NegotiateOutputMediaTypeStream (req * http.Request , ns runtime.NegotiatedSerializer , restrictions EndpointRestrictions ) (runtime.SerializerInfo , error ) {
61
- mediaType , ok := NegotiateMediaTypeOptions (req .Header .Get ("Accept" ), AcceptedMediaTypesForEndpoint ( ns ), restrictions )
62
- if ! ok || mediaType .Accepted .Serializer . StreamSerializer == nil {
61
+ mediaType , ok := NegotiateMediaTypeOptions (req .Header .Get ("Accept" ), ns . SupportedMediaTypes ( ), restrictions )
62
+ if ! ok || mediaType .Accepted .StreamSerializer == nil {
63
63
_ , supported := MediaTypesForSerializer (ns )
64
64
return runtime.SerializerInfo {}, NewNotAcceptableError (supported )
65
65
}
66
- return mediaType .Accepted . Serializer , nil
66
+ return mediaType .Accepted , nil
67
67
}
68
68
69
69
// NegotiateInputSerializer returns the input serializer for the provided request.
@@ -99,10 +99,13 @@ func NegotiateInputSerializerForMediaType(mediaType string, streaming bool, ns r
99
99
func isPrettyPrint (req * http.Request ) bool {
100
100
// DEPRECATED: should be part of the content type
101
101
if req .URL != nil {
102
- pp := req .URL .Query ().Get ("pretty" )
103
- if len (pp ) > 0 {
104
- pretty , _ := strconv .ParseBool (pp )
105
- return pretty
102
+ // avoid an allocation caused by parsing the URL query
103
+ if strings .Contains (req .URL .RawQuery , "pretty" ) {
104
+ pp := req .URL .Query ().Get ("pretty" )
105
+ if len (pp ) > 0 {
106
+ pretty , _ := strconv .ParseBool (pp )
107
+ return pretty
108
+ }
106
109
}
107
110
}
108
111
userAgent := req .UserAgent ()
@@ -139,17 +142,6 @@ func (emptyEndpointRestrictions) AllowsConversion(schema.GroupVersionKind, strin
139
142
func (emptyEndpointRestrictions ) AllowsServerVersion (string ) bool { return false }
140
143
func (emptyEndpointRestrictions ) AllowsStreamSchema (s string ) bool { return s == "watch" }
141
144
142
- // AcceptedMediaType contains information about a valid media type that the
143
- // server can serialize.
144
- type AcceptedMediaType struct {
145
- // Type is the first part of the media type ("application")
146
- Type string
147
- // SubType is the second part of the media type ("json")
148
- SubType string
149
- // Serializer is the serialization info this object accepts
150
- Serializer runtime.SerializerInfo
151
- }
152
-
153
145
// MediaTypeOptions describes information for a given media type that may alter
154
146
// the server response
155
147
type MediaTypeOptions struct {
@@ -176,13 +168,13 @@ type MediaTypeOptions struct {
176
168
Unrecognized []string
177
169
178
170
// the accepted media type from the client
179
- Accepted * AcceptedMediaType
171
+ Accepted runtime. SerializerInfo
180
172
}
181
173
182
174
// acceptMediaTypeOptions returns an options object that matches the provided media type params. If
183
175
// it returns false, the provided options are not allowed and the media type must be skipped. These
184
176
// parameters are unversioned and may not be changed.
185
- func acceptMediaTypeOptions (params map [string ]string , accepts * AcceptedMediaType , endpoint EndpointRestrictions ) (MediaTypeOptions , bool ) {
177
+ func acceptMediaTypeOptions (params map [string ]string , accepts * runtime. SerializerInfo , endpoint EndpointRestrictions ) (MediaTypeOptions , bool ) {
186
178
var options MediaTypeOptions
187
179
188
180
// extract all known parameters
@@ -208,7 +200,7 @@ func acceptMediaTypeOptions(params map[string]string, accepts *AcceptedMediaType
208
200
209
201
// controls the streaming schema
210
202
case "stream" :
211
- if len (v ) > 0 && (accepts .Serializer . StreamSerializer == nil || ! endpoint .AllowsStreamSchema (v )) {
203
+ if len (v ) > 0 && (accepts .StreamSerializer == nil || ! endpoint .AllowsStreamSchema (v )) {
212
204
return MediaTypeOptions {}, false
213
205
}
214
206
options .Stream = v
@@ -236,27 +228,27 @@ func acceptMediaTypeOptions(params map[string]string, accepts *AcceptedMediaType
236
228
}
237
229
}
238
230
239
- if options .Convert != nil && ! endpoint .AllowsConversion (* options .Convert , accepts .Type , accepts .SubType ) {
231
+ if options .Convert != nil && ! endpoint .AllowsConversion (* options .Convert , accepts .MediaTypeType , accepts .MediaTypeSubType ) {
240
232
return MediaTypeOptions {}, false
241
233
}
242
234
243
- options .Accepted = accepts
235
+ options .Accepted = * accepts
244
236
return options , true
245
237
}
246
238
247
239
type candidateMediaType struct {
248
- accepted * AcceptedMediaType
240
+ accepted * runtime. SerializerInfo
249
241
clauses goautoneg.Accept
250
242
}
251
243
252
244
type candidateMediaTypeSlice []candidateMediaType
253
245
254
246
// NegotiateMediaTypeOptions returns the most appropriate content type given the accept header and
255
247
// a list of alternatives along with the accepted media type parameters.
256
- func NegotiateMediaTypeOptions (header string , accepted []AcceptedMediaType , endpoint EndpointRestrictions ) (MediaTypeOptions , bool ) {
248
+ func NegotiateMediaTypeOptions (header string , accepted []runtime. SerializerInfo , endpoint EndpointRestrictions ) (MediaTypeOptions , bool ) {
257
249
if len (header ) == 0 && len (accepted ) > 0 {
258
250
return MediaTypeOptions {
259
- Accepted : & accepted [0 ],
251
+ Accepted : accepted [0 ],
260
252
}, true
261
253
}
262
254
@@ -266,8 +258,8 @@ func NegotiateMediaTypeOptions(header string, accepted []AcceptedMediaType, endp
266
258
for i := range accepted {
267
259
accepts := & accepted [i ]
268
260
switch {
269
- case clause .Type == accepts .Type && clause .SubType == accepts .SubType ,
270
- clause .Type == accepts .Type && clause .SubType == "*" ,
261
+ case clause .Type == accepts .MediaTypeType && clause .SubType == accepts .MediaTypeSubType ,
262
+ clause .Type == accepts .MediaTypeType && clause .SubType == "*" ,
271
263
clause .Type == "*" && clause .SubType == "*" :
272
264
candidates = append (candidates , candidateMediaType {accepted : accepts , clauses : clause })
273
265
}
@@ -282,22 +274,3 @@ func NegotiateMediaTypeOptions(header string, accepted []AcceptedMediaType, endp
282
274
283
275
return MediaTypeOptions {}, false
284
276
}
285
-
286
- // AcceptedMediaTypesForEndpoint returns an array of structs that are used to efficiently check which
287
- // allowed media types the server exposes.
288
- func AcceptedMediaTypesForEndpoint (ns runtime.NegotiatedSerializer ) []AcceptedMediaType {
289
- var acceptedMediaTypes []AcceptedMediaType
290
- for _ , info := range ns .SupportedMediaTypes () {
291
- segments := strings .SplitN (info .MediaType , "/" , 2 )
292
- if len (segments ) == 1 {
293
- segments = append (segments , "*" )
294
- }
295
- t := AcceptedMediaType {
296
- Type : segments [0 ],
297
- SubType : segments [1 ],
298
- Serializer : info ,
299
- }
300
- acceptedMediaTypes = append (acceptedMediaTypes , t )
301
- }
302
- return acceptedMediaTypes
303
- }
0 commit comments