Skip to content

Commit

Permalink
Backport ctx.String() to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Jan 26, 2025
1 parent 8b9db05 commit df1f20b
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit df1f20b

Please sign in to comment.