mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
feat: code adjust
This commit is contained in:
parent
696d37e030
commit
2ae40d5d4d
@ -84,7 +84,7 @@ var (
|
|||||||
// Default returns the appropriate Binding instance based on the HTTP method
|
// Default returns the appropriate Binding instance based on the HTTP method
|
||||||
// and the content type.
|
// and the content type.
|
||||||
func Default(method, contentType string) Binding {
|
func Default(method, contentType string) Binding {
|
||||||
if method == "GET" {
|
if method == http.MethodGet {
|
||||||
return Form
|
return Form
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errUnknownType = errors.New("unknown type")
|
// ErrUnknownType unknown request type
|
||||||
|
ErrUnknownType = errors.New("unknown type")
|
||||||
|
|
||||||
// ErrConvertMapStringSlice can not covert to map[string][]string
|
// ErrConvertMapStringSlice can not covert to map[string][]string
|
||||||
ErrConvertMapStringSlice = errors.New("can not convert to map slices of strings")
|
ErrConvertMapStringSlice = errors.New("can not convert to map slices of strings")
|
||||||
@ -240,7 +241,7 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
|
|||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
|
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
|
||||||
default:
|
default:
|
||||||
return errUnknownType
|
return ErrUnknownType
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -124,7 +124,7 @@ func TestMappingUnknownFieldType(t *testing.T) {
|
|||||||
|
|
||||||
err := mappingByPtr(&s, formSource{"U": {"unknown"}}, "form")
|
err := mappingByPtr(&s, formSource{"U": {"unknown"}}, "form")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Equal(t, errUnknownType, err)
|
assert.Equal(t, ErrUnknownType, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMappingURI(t *testing.T) {
|
func TestMappingURI(t *testing.T) {
|
||||||
|
@ -13,16 +13,21 @@ import (
|
|||||||
"github.com/gin-gonic/gin/internal/json"
|
"github.com/gin-gonic/gin/internal/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EnableDecoderUseNumber is used to call the UseNumber method on the JSON
|
var (
|
||||||
// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
|
// EnableDecoderUseNumber is used to call the UseNumber method on the JSON
|
||||||
// interface{} as a Number instead of as a float64.
|
// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
|
||||||
var EnableDecoderUseNumber = false
|
// interface{} as a Number instead of as a float64.
|
||||||
|
EnableDecoderUseNumber = false
|
||||||
|
|
||||||
// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method
|
// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method
|
||||||
// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to
|
// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to
|
||||||
// return an error when the destination is a struct and the input contains object
|
// return an error when the destination is a struct and the input contains object
|
||||||
// keys which do not match any non-ignored, exported fields in the destination.
|
// keys which do not match any non-ignored, exported fields in the destination.
|
||||||
var EnableDecoderDisallowUnknownFields = false
|
EnableDecoderDisallowUnknownFields = false
|
||||||
|
|
||||||
|
// ErrInvalidRequest request is nil or request body is nil.
|
||||||
|
ErrInvalidRequest = errors.New("invalid request")
|
||||||
|
)
|
||||||
|
|
||||||
type jsonBinding struct{}
|
type jsonBinding struct{}
|
||||||
|
|
||||||
@ -32,8 +37,9 @@ func (jsonBinding) Name() string {
|
|||||||
|
|
||||||
func (jsonBinding) Bind(req *http.Request, obj any) error {
|
func (jsonBinding) Bind(req *http.Request, obj any) error {
|
||||||
if req == nil || req.Body == nil {
|
if req == nil || req.Body == nil {
|
||||||
return errors.New("invalid request")
|
return ErrInvalidRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
return decodeJSON(req.Body, obj)
|
return decodeJSON(req.Body, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -792,11 +792,8 @@ func (c *Context) ContentType() string {
|
|||||||
// IsWebsocket returns true if the request headers indicate that a websocket
|
// IsWebsocket returns true if the request headers indicate that a websocket
|
||||||
// handshake is being initiated by the client.
|
// handshake is being initiated by the client.
|
||||||
func (c *Context) IsWebsocket() bool {
|
func (c *Context) IsWebsocket() bool {
|
||||||
if strings.Contains(strings.ToLower(c.requestHeader("Connection")), "upgrade") &&
|
return strings.Contains(strings.ToLower(c.requestHeader("Connection")), "upgrade") &&
|
||||||
strings.EqualFold(c.requestHeader("Upgrade"), "websocket") {
|
strings.EqualFold(c.requestHeader("Upgrade"), "websocket")
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) requestHeader(key string) string {
|
func (c *Context) requestHeader(key string) string {
|
||||||
|
24
recovery.go
24
recovery.go
@ -48,6 +48,20 @@ func RecoveryWithWriter(out io.Writer, recovery ...RecoveryFunc) HandlerFunc {
|
|||||||
return CustomRecoveryWithWriter(out, defaultHandleRecovery)
|
return CustomRecoveryWithWriter(out, defaultHandleRecovery)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsBroken Check error for a broken connection
|
||||||
|
func IsBroken(err interface{}) bool {
|
||||||
|
if ne, ok := err.(*net.OpError); ok {
|
||||||
|
if se, ok := ne.Err.(*os.SyscallError); ok {
|
||||||
|
if errMsg := strings.ToLower(se.Error()); strings.Contains(errMsg, "broken pipe") ||
|
||||||
|
strings.Contains(errMsg, "connection reset by peer") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// CustomRecoveryWithWriter returns a middleware for a given writer that recovers from any panics and calls the provided handle func to handle it.
|
// CustomRecoveryWithWriter returns a middleware for a given writer that recovers from any panics and calls the provided handle func to handle it.
|
||||||
func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
||||||
var logger *log.Logger
|
var logger *log.Logger
|
||||||
@ -59,15 +73,7 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
|||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
// Check for a broken connection, as it is not really a
|
// Check for a broken connection, as it is not really a
|
||||||
// condition that warrants a panic stack trace.
|
// condition that warrants a panic stack trace.
|
||||||
var brokenPipe bool
|
var brokenPipe = IsBroken(err)
|
||||||
if ne, ok := err.(*net.OpError); ok {
|
|
||||||
var se *os.SyscallError
|
|
||||||
if errors.As(ne, &se) {
|
|
||||||
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
|
|
||||||
brokenPipe = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if logger != nil {
|
if logger != nil {
|
||||||
stack := stack(3)
|
stack := stack(3)
|
||||||
httpRequest, _ := httputil.DumpRequest(c.Request, false)
|
httpRequest, _ := httputil.DumpRequest(c.Request, false)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user