Merge c2268830431c0d13fb2c0d8ec46de6f375e31e1f into 3010cbd7f4eccdbb610c510274895e083b8c058c

This commit is contained in:
Yuki Igarashi 2023-01-10 19:05:30 +09:00 committed by GitHub
commit 63df1c75a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 10 deletions

View File

@ -1143,19 +1143,27 @@ func (c *Context) NegotiateFormat(offered ...string) string {
return offered[0] return offered[0]
} }
for _, accepted := range c.Accepted { 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 { for _, offer := range offered {
// According to RFC 2616 and RFC 2396, non-ASCII characters are not allowed in headers, // 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 // therefore we can handle the string without casting it into []rune
i := 0
for ; i < len(accepted); i++ { offerMediaRange := strings.Split(offer, "/")
if accepted[i] == '*' || offer[i] == '*' { if len(offerMediaRange) != 2 {
continue
}
if acceptedMediaRange[0] == "*" || offerMediaRange[0] == "*" {
return offer return offer
} }
if accepted[i] != offer[i] {
break if acceptedMediaRange[0] == offerMediaRange[0] &&
} (acceptedMediaRange[1] == "*" || offerMediaRange[1] == "*" || acceptedMediaRange[1] == offerMediaRange[1]) {
}
if i == len(accepted) {
return offer return offer
} }
} }

View File

@ -1298,6 +1298,32 @@ func TestContextNegotiationFormatWithWildcardAccept(t *testing.T) {
assert.Equal(t, c.NegotiateFormat(MIMEHTML), MIMEHTML) 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) { func TestContextNegotiationFormatCustom(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", nil) c.Request, _ = http.NewRequest("POST", "/", nil)