MCPcopy
hub / github.com/gin-gonic/gin / NegotiateFormat

Method NegotiateFormat

context.go:1394–1422  ·  view source on GitHub ↗

NegotiateFormat returns an acceptable Accept format.

(offered ...string)

Source from the content-addressed store, hash-verified

1392
1393// NegotiateFormat returns an acceptable Accept format.
1394func (c *Context) NegotiateFormat(offered ...string) string {
1395 assert1(len(offered) > 0, "you must provide at least one offer")
1396
1397 if c.Accepted == nil {
1398 c.Accepted = parseAccept(c.requestHeader("Accept"))
1399 }
1400 if len(c.Accepted) == 0 {
1401 return offered[0]
1402 }
1403 for _, accepted := range c.Accepted {
1404 for _, offer := range offered {
1405 // According to RFC 2616 and RFC 2396, non-ASCII characters are not allowed in headers,
1406 // therefore we can just iterate over the string without casting it into []rune
1407 i := 0
1408 for ; i < len(accepted) && i < len(offer); i++ {
1409 if accepted[i] == '*' || offer[i] == '*' {
1410 return offer
1411 }
1412 if accepted[i] != offer[i] {
1413 break
1414 }
1415 }
1416 if i == len(accepted) {
1417 return offer
1418 }
1419 }
1420 }
1421 return ""
1422}
1423
1424// SetAccepted sets Accept header data.
1425func (c *Context) SetAccepted(formats ...string) {

Calls 3

requestHeaderMethod · 0.95
assert1Function · 0.85
parseAcceptFunction · 0.85