Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 chore: Backport ctx.String() from v3 #3294

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1518,9 +1518,9 @@
i++
}

return c.Redirect(location+"?"+queryText.String(), status...)

Check warning

Code scanning / CodeQL

Open URL redirect Medium

This path to an untrusted URL redirection depends on a
user-provided value
.
This path to an untrusted URL redirection depends on a
user-provided value
.
}
return c.Redirect(location, status...)

Check warning

Code scanning / CodeQL

Open URL redirect Medium

This path to an untrusted URL redirection depends on a
user-provided value
.
This path to an untrusted URL redirection depends on a
user-provided value
.
}

// RedirectBack to the URL to referer
Expand Down Expand Up @@ -1834,14 +1834,39 @@
//
// 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('#') //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') //nolint:errcheck // Not needed here
}
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()) //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()) //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()

// 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
Loading