Documentation
¶
Overview ¶
Package buffer provides a ResponseWriter wrapper that buffers request and response bodies. It is used for logging and metrics collection in HTTP middleware.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Buffer ¶
type Buffer struct {
http.ResponseWriter
// contains filtered or unexported fields
}
Buffer wraps http.ResponseWriter to capture request and response bodies. It is commonly used in middleware for logging and metrics collection.
func Acquire ¶
func Acquire(w http.ResponseWriter) *Buffer
Acquire retrieves a Buffer from the pool and initializes it with the given ResponseWriter. It is safe for concurrent use and should be paired with Release.
func New ¶
func New() *Buffer
New creates a new Buffer with initialized request and response buffers.
func (*Buffer) ReadRequestBody ¶
ReadRequestBody reads the entire request body into the buffer. It returns an error if the read operation fails.
func (*Buffer) RequestBody ¶
RequestBody returns the captured request body as a byte slice.
func (*Buffer) Reset ¶
func (m *Buffer) Reset(w http.ResponseWriter)
Reset reinitializes the buffer with a new ResponseWriter and clears internal state. It should be called before reusing a pooled Buffer.
func (*Buffer) ResponseBody ¶
ResponseBody returns the captured response body as a byte slice.
func (*Buffer) StatusCode ¶
StatusCode returns the captured status code, or http.StatusOK if not set.
func (*Buffer) Write ¶
Write writes data to both the underlying ResponseWriter and the response buffer. It returns the number of bytes written and any error encountered.
func (*Buffer) WriteHeader ¶
WriteHeader captures the status code and delegates to the underlying ResponseWriter.
type RequestBody ¶
type RequestBody struct {
// contains filtered or unexported fields
}
RequestBody wraps a bytes.Buffer to provide an io.ReadCloser for request body replay. It is used when the request body needs to be read multiple times (e.g., for logging).
func NewRequestBody ¶
func NewRequestBody(body []byte) RequestBody
NewRequestBody creates a new RequestBody from a byte slice.
func (RequestBody) Close ¶
func (r RequestBody) Close() error
Close is a no-op to satisfy the io.ReadCloser interface.