From 835d074ea79ffbc8e01f5d9babbc7ae4b2a1b8f2 Mon Sep 17 00:00:00 2001 From: Aden-Q Date: Fri, 26 Apr 2024 11:30:03 -0500 Subject: [PATCH] use raw string to bytes conversion --- auth.go | 6 ++---- binding/form_mapping.go | 5 ++--- render/json.go | 12 ++++++------ render/text.go | 4 +--- tree.go | 4 ++-- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/auth.go b/auth.go index 5d3222d5..7bc4f523 100644 --- a/auth.go +++ b/auth.go @@ -9,8 +9,6 @@ import ( "encoding/base64" "net/http" "strconv" - - "github.com/gin-gonic/gin/internal/bytesconv" ) // AuthUserKey is the cookie name for user credential in basic auth. @@ -34,7 +32,7 @@ func (a authPairs) searchCredential(authValue string) (string, bool) { return "", false } for _, pair := range a { - if subtle.ConstantTimeCompare(bytesconv.StringToBytes(pair.value), bytesconv.StringToBytes(authValue)) == 1 { + if subtle.ConstantTimeCompare([]byte(pair.value), []byte(authValue)) == 1 { return pair.user, true } } @@ -90,7 +88,7 @@ func processAccounts(accounts Accounts) authPairs { func authorizationHeader(user, password string) string { base := user + ":" + password - return "Basic " + base64.StdEncoding.EncodeToString(bytesconv.StringToBytes(base)) + return "Basic " + base64.StdEncoding.EncodeToString([]byte(base)) } // BasicAuthForProxy returns a Basic HTTP Proxy-Authorization middleware. diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 77a1bde6..65054831 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -13,7 +13,6 @@ import ( "strings" "time" - "github.com/gin-gonic/gin/internal/bytesconv" "github.com/gin-gonic/gin/internal/json" ) @@ -239,9 +238,9 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel case multipart.FileHeader: return nil } - return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface()) + return json.Unmarshal([]byte(val), value.Addr().Interface()) case reflect.Map: - return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface()) + return json.Unmarshal([]byte(val), value.Addr().Interface()) case reflect.Ptr: if !value.Elem().IsValid() { value.Set(reflect.New(value.Type().Elem())) diff --git a/render/json.go b/render/json.go index fc8dea45..5883b29a 100644 --- a/render/json.go +++ b/render/json.go @@ -97,9 +97,9 @@ func (r SecureJSON) Render(w http.ResponseWriter) error { return err } // if the jsonBytes is array values - if bytes.HasPrefix(jsonBytes, bytesconv.StringToBytes("[")) && bytes.HasSuffix(jsonBytes, - bytesconv.StringToBytes("]")) { - if _, err = w.Write(bytesconv.StringToBytes(r.Prefix)); err != nil { + if bytes.HasPrefix(jsonBytes, []byte("[")) && bytes.HasSuffix(jsonBytes, + []byte("]")) { + if _, err = w.Write([]byte(r.Prefix)); err != nil { return err } } @@ -126,11 +126,11 @@ func (r JsonpJSON) Render(w http.ResponseWriter) (err error) { } callback := template.JSEscapeString(r.Callback) - if _, err = w.Write(bytesconv.StringToBytes(callback)); err != nil { + if _, err = w.Write([]byte(callback)); err != nil { return err } - if _, err = w.Write(bytesconv.StringToBytes("(")); err != nil { + if _, err = w.Write([]byte("(")); err != nil { return err } @@ -138,7 +138,7 @@ func (r JsonpJSON) Render(w http.ResponseWriter) (err error) { return err } - if _, err = w.Write(bytesconv.StringToBytes(");")); err != nil { + if _, err = w.Write([]byte(");")); err != nil { return err } diff --git a/render/text.go b/render/text.go index 77eafdfd..af4cecf2 100644 --- a/render/text.go +++ b/render/text.go @@ -7,8 +7,6 @@ package render import ( "fmt" "net/http" - - "github.com/gin-gonic/gin/internal/bytesconv" ) // String contains the given interface object slice and its format. @@ -36,6 +34,6 @@ func WriteString(w http.ResponseWriter, format string, data []any) (err error) { _, err = fmt.Fprintf(w, format, data...) return } - _, err = w.Write(bytesconv.StringToBytes(format)) + _, err = w.Write([]byte(format)) return } diff --git a/tree.go b/tree.go index 878023d1..b956ea51 100644 --- a/tree.go +++ b/tree.go @@ -93,14 +93,14 @@ func (n *node) addChild(child *node) { func countParams(path string) uint16 { var n uint16 - s := bytesconv.StringToBytes(path) + s := []byte(path) n += uint16(bytes.Count(s, strColon)) n += uint16(bytes.Count(s, strStar)) return n } func countSections(path string) uint16 { - s := bytesconv.StringToBytes(path) + s := []byte(path) return uint16(bytes.Count(s, strSlash)) }