Merge pull request #4701 from sunxingboo/develop

Add a custom option for whether to escape HTML special characters when processing http request parameters.
This commit is contained in:
jianzhiyao 2021-07-22 17:23:09 +08:00 committed by GitHub
commit cb5fd74a56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 1 deletions

View File

@ -1,5 +1,6 @@
# developing # developing
- Add a custom option for whether to escape HTML special characters when processing http request parameters. [4701](https://github.com/beego/beego/pull/4701)
- Always set the response status in the CustomAbort function. [4686](https://github.com/beego/beego/pull/4686) - Always set the response status in the CustomAbort function. [4686](https://github.com/beego/beego/pull/4686)
- Add template functions eq,lt to support uint and int compare. [4607](https://github.com/beego/beego/pull/4607) - Add template functions eq,lt to support uint and int compare. [4607](https://github.com/beego/beego/pull/4607)
- Migrate tests to GitHub Actions. [4663](https://github.com/beego/beego/issues/4663) - Migrate tests to GitHub Actions. [4663](https://github.com/beego/beego/issues/4663)

View File

@ -258,6 +258,12 @@ func (b *BeegoHTTPRequest) AddFilters(fcs ...FilterChain) *BeegoHTTPRequest {
return b return b
} }
// SetEscapeHTML is used to set the flag whether escape HTML special characters during processing
func (b *BeegoHTTPRequest) SetEscapeHTML(isEscape bool) *BeegoHTTPRequest {
b.setting.EscapeHTML = isEscape
return b
}
// Param adds query param in to request. // Param adds query param in to request.
// params build query string as ?key1=value1&key2=value2... // params build query string as ?key1=value1&key2=value2...
func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest { func (b *BeegoHTTPRequest) Param(key, value string) *BeegoHTTPRequest {
@ -334,7 +340,7 @@ func (b *BeegoHTTPRequest) YAMLBody(obj interface{}) (*BeegoHTTPRequest, error)
// JSONBody adds the request raw body encoded in JSON. // JSONBody adds the request raw body encoded in JSON.
func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) { func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error) {
if b.req.Body == nil && obj != nil { if b.req.Body == nil && obj != nil {
byts, err := json.Marshal(obj) byts, err := b.JSONMarshal(obj)
if err != nil { if err != nil {
return b, berror.Wrap(err, InvalidJSONBody, "obj could not be converted to JSON body") return b, berror.Wrap(err, InvalidJSONBody, "obj could not be converted to JSON body")
} }
@ -345,6 +351,17 @@ func (b *BeegoHTTPRequest) JSONBody(obj interface{}) (*BeegoHTTPRequest, error)
return b, nil return b, nil
} }
func (b *BeegoHTTPRequest) JSONMarshal(obj interface{}) ([]byte, error) {
bf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(bf)
jsonEncoder.SetEscapeHTML(b.setting.EscapeHTML)
err := jsonEncoder.Encode(obj)
if err != nil {
return nil, err
}
return bf.Bytes(), nil
}
func (b *BeegoHTTPRequest) buildURL(paramBody string) { func (b *BeegoHTTPRequest) buildURL(paramBody string) {
// build GET url with query string // build GET url with query string
if b.req.Method == "GET" && len(paramBody) > 0 { if b.req.Method == "GET" && len(paramBody) > 0 {

View File

@ -18,6 +18,7 @@ import (
"bytes" "bytes"
"context" "context"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
@ -435,3 +436,13 @@ func TestBeegoHTTPRequestXMLBody(t *testing.T) {
// TODO // TODO
func TestBeegoHTTPRequestResponseForValue(t *testing.T) { func TestBeegoHTTPRequestResponseForValue(t *testing.T) {
} }
func TestBeegoHTTPRequestJSONMarshal(t *testing.T) {
req := Post("http://beego.me")
req.SetEscapeHTML(false)
body := map[string]interface{}{
"escape": "left&right",
}
b, _ := req.JSONMarshal(body)
assert.Equal(t, fmt.Sprintf(`{"escape":"left&right"}%s`, "\n"), string(b))
}

View File

@ -37,6 +37,7 @@ type BeegoHTTPSettings struct {
Retries int // if set to -1 means will retry forever Retries int // if set to -1 means will retry forever
RetryDelay time.Duration RetryDelay time.Duration
FilterChains []FilterChain FilterChains []FilterChain
EscapeHTML bool // if set to false means will not escape escape HTML special characters during processing, default true
} }
// createDefaultCookie creates a global cookiejar to store cookies. // createDefaultCookie creates a global cookiejar to store cookies.
@ -66,6 +67,7 @@ var defaultSetting = BeegoHTTPSettings{
ReadWriteTimeout: 60 * time.Second, ReadWriteTimeout: 60 * time.Second,
Gzip: true, Gzip: true,
FilterChains: make([]FilterChain, 0, 4), FilterChains: make([]FilterChain, 0, 4),
EscapeHTML: true,
} }
var ( var (