mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 13:22:09 +08:00
This fixes #3241
This commit is contained in:
parent
b57163a0e4
commit
f175c5a82a
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@ count.out
|
||||
test
|
||||
profile.out
|
||||
tmp.out
|
||||
.vscode
|
10
context.go
10
context.go
@ -1130,6 +1130,14 @@ func (c *Context) NegotiateFormat(offered ...string) string {
|
||||
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
|
||||
|
||||
//When the lengths are not equal,
|
||||
//offer must end with '*'
|
||||
//in order to avoid getting index out of range errors
|
||||
if len(accepted) > len(offer) && offer[len(offer)-1] != '*' {
|
||||
continue
|
||||
}
|
||||
|
||||
i := 0
|
||||
for ; i < len(accepted); i++ {
|
||||
if accepted[i] == '*' || offer[i] == '*' {
|
||||
@ -1139,7 +1147,7 @@ func (c *Context) NegotiateFormat(offered ...string) string {
|
||||
break
|
||||
}
|
||||
}
|
||||
if i == len(accepted) {
|
||||
if i == len(accepted) && i == len(offer) {
|
||||
return offer
|
||||
}
|
||||
}
|
||||
|
@ -1296,6 +1296,15 @@ func TestContextNegotiationFormatWithWildcardAccept(t *testing.T) {
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEJSON), "")
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEXML), "")
|
||||
assert.Equal(t, c.NegotiateFormat(MIMEHTML), MIMEHTML)
|
||||
|
||||
c, _ = CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Add("Accept", "text/html")
|
||||
|
||||
assert.Equal(t, c.NegotiateFormat("text/htm"), "")
|
||||
assert.Equal(t, c.NegotiateFormat("text/htmll"), "")
|
||||
assert.Equal(t, c.NegotiateFormat("tex/*"), "")
|
||||
assert.Equal(t, c.NegotiateFormat("text/html"), MIMEHTML)
|
||||
}
|
||||
|
||||
func TestContextNegotiationFormatCustom(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user