mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-21 08:42:16 +08:00
Modify the code to get the http header
When the http header is obtained in the standard library, the key value will be modified by the CanonicalMIMEHeaderKey function, and finally the value of the http header will be obtained from the map. As follows. ```go func (h MIMEHeader) Get(key string) string { // ... v := h[CanonicalMIMEHeaderKey(key)] // ... } ``` This pr also follows this modification
This commit is contained in:
parent
f439a1b166
commit
ce723d2c12
@ -7,6 +7,7 @@ package binding
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/textproto"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -106,6 +107,7 @@ func mapping(value reflect.Value, field reflect.StructField, setter setter, tag
|
||||
|
||||
type setOptions struct {
|
||||
isDefaultExists bool
|
||||
isHeader bool
|
||||
defaultValue string
|
||||
}
|
||||
|
||||
@ -127,6 +129,7 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter
|
||||
}
|
||||
|
||||
var opt string
|
||||
setOpt.isHeader = tag == "header"
|
||||
for len(opts) > 0 {
|
||||
opt, opts = head(opts, ",")
|
||||
|
||||
@ -140,7 +143,17 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter
|
||||
}
|
||||
|
||||
func setByForm(value reflect.Value, field reflect.StructField, form map[string][]string, tagValue string, opt setOptions) (isSetted bool, err error) {
|
||||
vs, ok := form[tagValue]
|
||||
var (
|
||||
vs []string
|
||||
ok bool
|
||||
)
|
||||
|
||||
if opt.isHeader {
|
||||
vs, ok = form[textproto.CanonicalMIMEHeaderKey(tagValue)]
|
||||
} else {
|
||||
vs, ok = form[tagValue]
|
||||
}
|
||||
|
||||
if !ok && !opt.isDefaultExists {
|
||||
return false, nil
|
||||
}
|
||||
|
@ -1443,15 +1443,18 @@ func TestContextBindHeader(t *testing.T) {
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Add("rate", "8000")
|
||||
c.Request.Header.Add("domain", "music")
|
||||
c.Request.Header.Add("limit", "1000")
|
||||
|
||||
var testHeader struct {
|
||||
Rate int `header:"Rate"`
|
||||
Domain string `header:"Domain"`
|
||||
Limit int `header:"limit"`
|
||||
}
|
||||
|
||||
assert.NoError(t, c.BindHeader(&testHeader))
|
||||
assert.Equal(t, 8000, testHeader.Rate)
|
||||
assert.Equal(t, "music", testHeader.Domain)
|
||||
assert.Equal(t, 1000, testHeader.Limit)
|
||||
assert.Equal(t, 0, w.Body.Len())
|
||||
}
|
||||
|
||||
@ -1569,15 +1572,18 @@ func TestContextShouldBindHeader(t *testing.T) {
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c.Request.Header.Add("rate", "8000")
|
||||
c.Request.Header.Add("domain", "music")
|
||||
c.Request.Header.Add("limit", "1000")
|
||||
|
||||
var testHeader struct {
|
||||
Rate int `header:"Rate"`
|
||||
Domain string `header:"Domain"`
|
||||
Limit int `header:"limit"`
|
||||
}
|
||||
|
||||
assert.NoError(t, c.ShouldBindHeader(&testHeader))
|
||||
assert.Equal(t, 8000, testHeader.Rate)
|
||||
assert.Equal(t, "music", testHeader.Domain)
|
||||
assert.Equal(t, 1000, testHeader.Limit)
|
||||
assert.Equal(t, 0, w.Body.Len())
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user