mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 13:22:09 +08:00
Merge c2268830431c0d13fb2c0d8ec46de6f375e31e1f into 3010cbd7f4eccdbb610c510274895e083b8c058c
This commit is contained in:
commit
63df1c75a0
28
context.go
28
context.go
@ -1143,19 +1143,27 @@ func (c *Context) NegotiateFormat(offered ...string) string {
|
||||
return offered[0]
|
||||
}
|
||||
for _, accepted := range c.Accepted {
|
||||
// According to RFC 2616, media-range = ( "*/*" | ( type "/" "*" ) | ( type "/" subtype ) )
|
||||
acceptedMediaRange := strings.Split(accepted, "/")
|
||||
if len(acceptedMediaRange) != 2 || (acceptedMediaRange[0] == "*" && acceptedMediaRange[1] != "*") {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, offer := range offered {
|
||||
// According to RFC 2616 and RFC 2396, non-ASCII characters are not allowed in headers,
|
||||
// therefore we can just iterate over the string without casting it into []rune
|
||||
i := 0
|
||||
for ; i < len(accepted); i++ {
|
||||
if accepted[i] == '*' || offer[i] == '*' {
|
||||
return offer
|
||||
}
|
||||
if accepted[i] != offer[i] {
|
||||
break
|
||||
}
|
||||
// therefore we can handle the string without casting it into []rune
|
||||
|
||||
offerMediaRange := strings.Split(offer, "/")
|
||||
if len(offerMediaRange) != 2 {
|
||||
continue
|
||||
}
|
||||
if i == len(accepted) {
|
||||
|
||||
if acceptedMediaRange[0] == "*" || offerMediaRange[0] == "*" {
|
||||
return offer
|
||||
}
|
||||
|
||||
if acceptedMediaRange[0] == offerMediaRange[0] &&
|
||||
(acceptedMediaRange[1] == "*" || offerMediaRange[1] == "*" || acceptedMediaRange[1] == offerMediaRange[1]) {
|
||||
return offer
|
||||
}
|
||||
}
|
||||
|
@ -1298,6 +1298,32 @@ func TestContextNegotiationFormatWithWildcardAccept(t *testing.T) {
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEHTML), MIMEHTML)
|
||||
}
|
||||
|
||||
func TestContextNegotiationFormatWithInvalidAccept(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Add("Accept", "text*,*/html")
|
||||
|
||||
assert.Equal(t, c.NegotiateFormat("*/*"), "")
|
||||
assert.Equal(t, c.NegotiateFormat("text/*"), "")
|
||||
assert.Equal(t, c.NegotiateFormat("text/html"), "")
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEJSON), "")
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEXML), "")
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEHTML), "")
|
||||
|
||||
c, _ = CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Add("Accept", "text*/*,application/j*,application/jso,application/json2")
|
||||
|
||||
assert.Equal(t, c.NegotiateFormat("*/*"), "*/*")
|
||||
assert.Equal(t, c.NegotiateFormat("text/*"), "")
|
||||
assert.Equal(t, c.NegotiateFormat("text/html"), "")
|
||||
assert.Equal(t, c.NegotiateFormat("application/*"), "application/*")
|
||||
assert.Equal(t, c.NegotiateFormat("application/json"), "")
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEJSON), "")
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEXML), "")
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEHTML), "")
|
||||
}
|
||||
|
||||
func TestContextNegotiationFormatCustom(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
|
Loading…
x
Reference in New Issue
Block a user