response

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

nolint: mnd

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBodyWrapper

func NewBodyWrapper(body io.ReadCloser) io.ReadCloser

NewBodyWrapper creates a new BodyWrapper that wraps the provided io.ReadCloser. When the body is Read from, it counts the number of bytes read. When Close is called, it closes the underlying body.

func NewResponseWrapper

func NewResponseWrapper(responseWriter http.ResponseWriter) http.ResponseWriter

NewResponseWrapper creates a new Wrapper that wraps the provided http.ResponseWriter. The Wrapper allows for tracking the number of bytes written, the status code, and provides access to the request context. It also implements the http.Flusher interface to allow for flushing the response when needed.

Types

type BodyWrapper

type BodyWrapper struct {
	// contains filtered or unexported fields
}

func (*BodyWrapper) Close

func (bw *BodyWrapper) Close() error

Close the underlying body and returns any error encountered during closing.

func (*BodyWrapper) NumBytes

func (bw *BodyWrapper) NumBytes() int64

NumBytes returns the total number of bytes read from the body.

func (*BodyWrapper) Read

func (bw *BodyWrapper) Read(p []byte) (int, error)

Read reads data from the underlying body into the provided byte slice. It counts the number of bytes read and returns the count along with any error encountered during reading.

type Wrapper

type Wrapper struct {
	// contains filtered or unexported fields
}

func (*Wrapper) Flush

func (r *Wrapper) Flush()

func (*Wrapper) GetRequestContext

func (r *Wrapper) GetRequestContext() context.Context

GetRequestContext retrieves the request context associated with the Wrapper. If no context has been set, it returns a background context.

func (*Wrapper) Header

func (r *Wrapper) Header() http.Header

Header returns the header map that will be sent by WriteHeader. The Header map also is the mechanism with which Handlers can set HTTP trailers. By default, the returned map is initialized to be empty, and HTTP handlers can add key-value pairs to it as needed. The Header map is not thread-safe, so it should only be modified by the handler that is processing the request.

func (*Wrapper) NumBytes

func (r *Wrapper) NumBytes() int64

NumBytes returns the total number of bytes written to the response.

func (*Wrapper) SetRequestContext

func (r *Wrapper) SetRequestContext(ctx context.Context)

SetRequestContext sets the request context for the Wrapper. This context can be used to store and retrieve information related to the request being processed, such as tracing information or other metadata.

func (*Wrapper) SpanStatus

func (r *Wrapper) SpanStatus() (codes.Code, string)

SpanStatus returns the OpenTelemetry span status code and description based on the HTTP status code of the response. If WriteHeader has not been called, it returns codes.Error with a description indicating that no status code is available. If the HTTP status code is 400 or higher, it returns codes.Error with the corresponding HTTP status text as the description. For status codes below 400, it returns codes.Ok with the corresponding HTTP status text as the description.

func (*Wrapper) StatusCode

func (r *Wrapper) StatusCode() int

StatusCode returns the HTTP status code that was sent in the response. If WriteHeader has not been called, it returns 0.

func (*Wrapper) Write

func (r *Wrapper) Write(p []byte) (int, error)

Write writes the data to the connection as part of an HTTP reply. It counts the number of bytes written and returns the count along with any error encountered during writing. If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data.

func (*Wrapper) WriteHeader

func (r *Wrapper) WriteHeader(statusCode int)

WriteHeader sends an HTTP response header with the provided status code. If WriteHeader is called multiple times, only the first call will have an effect, and subsequent calls will be ignored. The status code is stored in the Wrapper for later retrieval, and the header is sent to the client using the underlying http.ResponseWriter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL