diff --git a/internal/configtypes/types.go b/internal/configtypes/types.go index 21cb73101..bb64ac3d7 100644 --- a/internal/configtypes/types.go +++ b/internal/configtypes/types.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "net" - "net/http" "github.com/centrifugal/centrifuge" ) @@ -117,38 +116,6 @@ type TransformedConnectErrorHttpResponse struct { Body string `mapstructure:"body" json:"body" envconfig:"body" yaml:"body" toml:"body"` } -func ConnectErrorToToHTTPResponse(err error, transforms []ConnectCodeToHTTPStatusTransform) (TransformedConnectErrorHttpResponse, bool) { - var code uint32 - var body string - switch t := err.(type) { - case *centrifuge.Disconnect: - code = t.Code - body = t.Reason - case centrifuge.Disconnect: - code = t.Code - body = t.Reason - case *centrifuge.Error: - code = t.Code - body = t.Message - default: - } - if code > 0 { - for _, t := range transforms { - if t.Code != code { - continue - } - if t.ToResponse.Body == "" { - t.ToResponse.Body = body - } - return t.ToResponse, true - } - } - return TransformedConnectErrorHttpResponse{ - StatusCode: http.StatusInternalServerError, - Body: http.StatusText(http.StatusInternalServerError), - }, false -} - type UniGRPC struct { Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"` Address string `mapstructure:"address" json:"address" envconfig:"address" yaml:"address" toml:"address"` diff --git a/internal/tools/ascii_test.go b/internal/tools/ascii_test.go new file mode 100644 index 000000000..539c73d37 --- /dev/null +++ b/internal/tools/ascii_test.go @@ -0,0 +1,35 @@ +package tools + +import "testing" + +func Test_IsASCII(t *testing.T) { + tests := []struct { + name string + input string + expected bool + }{ + { + name: "empty string", + input: "", + expected: true, + }, + { + name: "ascii string", + input: "hello", + expected: true, + }, + { + name: "non-ascii string", + input: "こんにちは", + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if IsASCII(tt.input) != tt.expected { + t.Errorf("expected %v, got %v", tt.expected, !tt.expected) + } + }) + } +} diff --git a/internal/tools/code_translate.go b/internal/tools/code_translate.go index 138db4cd6..de413a18f 100644 --- a/internal/tools/code_translate.go +++ b/internal/tools/code_translate.go @@ -3,25 +3,12 @@ package tools import ( "net/http" + "github.com/centrifugal/centrifugo/v5/internal/configtypes" + "github.com/centrifugal/centrifuge" ) -type ConnectCodeToHTTPStatus struct { - Enabled bool `mapstructure:"enabled" json:"enabled"` - Transforms []ConnectCodeToHTTPStatusTransform `mapstructure:"transforms" json:"transforms"` -} - -type ConnectCodeToHTTPStatusTransform struct { - Code uint32 `mapstructure:"code" json:"code"` - ToResponse TransformedConnectErrorHttpResponse `mapstructure:"to_response" json:"to_response"` -} - -type TransformedConnectErrorHttpResponse struct { - Status int `mapstructure:"status_code" json:"status_code"` - Body string `mapstructure:"body" json:"body"` -} - -func ConnectErrorToToHTTPResponse(err error, transforms []ConnectCodeToHTTPStatusTransform) (TransformedConnectErrorHttpResponse, bool) { +func ConnectErrorToToHTTPResponse(err error, transforms []configtypes.ConnectCodeToHTTPStatusTransform) (configtypes.TransformedConnectErrorHttpResponse, bool) { var code uint32 var body string switch t := err.(type) { @@ -47,8 +34,8 @@ func ConnectErrorToToHTTPResponse(err error, transforms []ConnectCodeToHTTPStatu return t.ToResponse, true } } - return TransformedConnectErrorHttpResponse{ - Status: http.StatusInternalServerError, - Body: http.StatusText(http.StatusInternalServerError), + return configtypes.TransformedConnectErrorHttpResponse{ + StatusCode: http.StatusInternalServerError, + Body: http.StatusText(http.StatusInternalServerError), }, false } diff --git a/internal/unihttpstream/handler.go b/internal/unihttpstream/handler.go index c90de262d..7defa9db7 100644 --- a/internal/unihttpstream/handler.go +++ b/internal/unihttpstream/handler.go @@ -6,8 +6,9 @@ import ( "net/http" "time" + "github.com/centrifugal/centrifugo/v5/internal/tools" + "github.com/centrifugal/centrifuge" - "github.com/centrifugal/centrifugo/v5/internal/configtypes" "github.com/centrifugal/protocol" ) @@ -95,7 +96,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if h.config.ConnectCodeToHTTPStatus.Enabled { err = c.ConnectNoErrorToDisconnect(connectRequest) if err != nil { - resp, ok := configtypes.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.Transforms) + resp, ok := tools.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.Transforms) if ok { w.WriteHeader(resp.StatusCode) _, _ = w.Write([]byte(resp.Body)) diff --git a/internal/unisse/handler.go b/internal/unisse/handler.go index 990427385..5a072f5d9 100644 --- a/internal/unisse/handler.go +++ b/internal/unisse/handler.go @@ -6,7 +6,7 @@ import ( "net/http" "time" - "github.com/centrifugal/centrifugo/v5/internal/configtypes" + "github.com/centrifugal/centrifugo/v5/internal/tools" "github.com/centrifugal/centrifuge" "github.com/centrifugal/protocol" @@ -106,7 +106,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if h.config.ConnectCodeToHTTPStatus.Enabled { err = c.ConnectNoErrorToDisconnect(connectRequest) if err != nil { - resp, ok := configtypes.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.Transforms) + resp, ok := tools.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.Transforms) if ok { w.WriteHeader(resp.StatusCode) _, _ = w.Write([]byte(resp.Body))