From ce723d2c125014758acb8d6d5d0b1d0498191276 Mon Sep 17 00:00:00 2001 From: guonaihong Date: Sun, 23 Jun 2019 14:49:53 +0800 Subject: [PATCH] Modify the code to get the http header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- binding/form_mapping.go | 15 ++++++++++++++- context_test.go | 6 ++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 7b84e2ea..3549a9bc 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -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 } diff --git a/context_test.go b/context_test.go index a893ef25..439e8ee6 100644 --- a/context_test.go +++ b/context_test.go @@ -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()) }