From df1f20b1e57cdfb044a0a7444a986869167c727b Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez Date: Sun, 26 Jan 2025 13:48:02 -0500 Subject: [PATCH 1/2] Backport ctx.String() to v2 --- ctx.go | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/ctx.go b/ctx.go index c0a4413ab0..cb0af4512d 100644 --- a/ctx.go +++ b/ctx.go @@ -1834,14 +1834,39 @@ func (c *Ctx) Status(status int) *Ctx { // // The returned value may be useful for logging. func (c *Ctx) String() string { - return fmt.Sprintf( - "#%016X - %s <-> %s - %s %s", - c.fasthttp.ID(), - c.fasthttp.LocalAddr(), - c.fasthttp.RemoteAddr(), - c.fasthttp.Request.Header.Method(), - c.fasthttp.URI().FullURI(), - ) + // Get buffer from pool + buf := bytebufferpool.Get() + + // Start with the ID, converting it to a hex string without fmt.Sprintf + buf.WriteByte('#') + // Convert ID to hexadecimal + id := strconv.FormatUint(c.fasthttp.ID(), 16) + // Pad with leading zeros to ensure 16 characters + for i := 0; i < (16 - len(id)); i++ { + buf.WriteByte('0') + } + buf.WriteString(id) + buf.WriteString(" - ") + + // Add local and remote addresses directly + buf.WriteString(c.fasthttp.LocalAddr().String()) + buf.WriteString(" <-> ") + buf.WriteString(c.fasthttp.RemoteAddr().String()) + buf.WriteString(" - ") + + // Add method and URI + buf.Write(c.fasthttp.Request.Header.Method()) + buf.WriteByte(' ') + buf.Write(c.fasthttp.URI().FullURI()) + + // Allocate string + str := buf.String() + + // Reset buffer + buf.Reset() + bytebufferpool.Put(buf) + + return str } // Type sets the Content-Type HTTP header to the MIME type specified by the file extension. From 3824772f7e0d5fe56cd98f3d07761efccea80d92 Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez Date: Mon, 27 Jan 2025 00:03:46 -0500 Subject: [PATCH 2/2] Fix lint issues --- ctx.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ctx.go b/ctx.go index cb0af4512d..b418dc8e90 100644 --- a/ctx.go +++ b/ctx.go @@ -1838,26 +1838,26 @@ func (c *Ctx) String() string { buf := bytebufferpool.Get() // Start with the ID, converting it to a hex string without fmt.Sprintf - buf.WriteByte('#') + buf.WriteByte('#') //nolint:errcheck // Not needed here // Convert ID to hexadecimal id := strconv.FormatUint(c.fasthttp.ID(), 16) // Pad with leading zeros to ensure 16 characters for i := 0; i < (16 - len(id)); i++ { - buf.WriteByte('0') + buf.WriteByte('0') //nolint:errcheck // Not needed here } - buf.WriteString(id) - buf.WriteString(" - ") + buf.WriteString(id) //nolint:errcheck // Not needed here + buf.WriteString(" - ") //nolint:errcheck // Not needed here // Add local and remote addresses directly - buf.WriteString(c.fasthttp.LocalAddr().String()) - buf.WriteString(" <-> ") - buf.WriteString(c.fasthttp.RemoteAddr().String()) - buf.WriteString(" - ") + buf.WriteString(c.fasthttp.LocalAddr().String()) //nolint:errcheck // Not needed here + buf.WriteString(" <-> ") //nolint:errcheck // Not needed here + buf.WriteString(c.fasthttp.RemoteAddr().String()) //nolint:errcheck // Not needed here + buf.WriteString(" - ") //nolint:errcheck // Not needed here // Add method and URI - buf.Write(c.fasthttp.Request.Header.Method()) - buf.WriteByte(' ') - buf.Write(c.fasthttp.URI().FullURI()) + buf.Write(c.fasthttp.Request.Header.Method()) //nolint:errcheck // Not needed here + buf.WriteByte(' ') //nolint:errcheck // Not needed here + buf.Write(c.fasthttp.URI().FullURI()) //nolint:errcheck // Not needed here // Allocate string str := buf.String()