response

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderContentType name of Content-Type header
	HeaderContentType = "content-type"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Response

type Response struct {
	StatusCode    int         // status code of response
	Headers       http.Header // headers for response
	ContentLength int64       // if buffered response contains length of buffer, for streams it equal to -1
	// contains filtered or unexported fields
}

Response is helper struct for wrapping different storage response

func New

func New(statusCode int, body io.ReadCloser) *Response

New create response object with io.ReadCloser

func NewBuf

func NewBuf(statusCode int, body []byte) *Response

NewBuf create response object from []byte

func NewError

func NewError(statusCode int, err error) *Response

NewError create response object from error

func NewNoContent

func NewNoContent(statusCode int) *Response

NewNoContent create response object without content

func NewString

func NewString(statusCode int, body string) *Response

NewString create response object from string

func (*Response) Body added in v0.15.0

func (r *Response) Body() ([]byte, error)

Body reads all content of response and returns []byte Content of the response is changed Such response shouldn't be Send to client

func (*Response) BodyTransformer added in v0.11.0

func (r *Response) BodyTransformer(w bodyTransformFnc)

BodyTransformer add function that will transform body before send to client

func (*Response) Close

func (r *Response) Close()

Close response reader

func (*Response) Copy

func (r *Response) Copy() (*Response, error)

Copy create complete response copy with headers and body

func (*Response) CopyBody

func (r *Response) CopyBody() ([]byte, error)

CopyBody returns a copy of Body in []byte

func (*Response) CreateView added in v0.30.0

func (r *Response) CreateView() (*Response, error)

CreateView returns a new Response that shares the body buffer with the original. This is a zero-copy operation that allows multiple consumers to read the same response data without duplicating memory.

The returned Response shares the underlying body buffer (read-only) and has cloned headers. This is safe because the buffer is immutable after buffering.

IMPORTANT: This method only works with buffered responses. Returns an error if the response is not buffered. Always check IsBuffered() first or call Body() to force buffering before creating a view.

Use case: Cache can store the original response and create views for each cache hit, eliminating the need to copy the full response body.

func (*Response) DecodeMsgpack added in v0.13.0

func (r *Response) DecodeMsgpack(dec *msgpack.Decoder) error

func (*Response) EncodeMsgpack added in v0.13.0

func (r *Response) EncodeMsgpack(enc *msgpack.Encoder) error

func (*Response) Error

func (r *Response) Error() error

Error returns error instance

func (*Response) GetTTL added in v0.13.0

func (r *Response) GetTTL() int

func (*Response) HasError

func (r *Response) HasError() bool

HasError check if response contains error

func (*Response) IsBuffered

func (r *Response) IsBuffered() bool

IsBuffered check if response has access to original buffer

func (*Response) IsCacheable added in v0.13.0

func (r *Response) IsCacheable() bool

func (*Response) IsFromCache added in v0.24.0

func (r *Response) IsFromCache() bool

func (*Response) IsImage added in v0.11.0

func (r *Response) IsImage() bool

IsImage check if response is image

func (*Response) Send

func (r *Response) Send(w http.ResponseWriter) error

Send write response to client using streaming

func (*Response) SendContent added in v0.3.0

func (r *Response) SendContent(req *http.Request, w http.ResponseWriter) error

SendContent use http.ServeContent to return response to client It can handle range and condition requests In this function we don't need to use transformer because it don't serve whole body It is used for range and condition requests

func (*Response) Set

func (r *Response) Set(headerName string, headerValue string)

Set update response headers

func (*Response) SetCacheHit added in v0.24.0

func (r *Response) SetCacheHit()

func (*Response) SetContentType

func (r *Response) SetContentType(contentType string) *Response

SetContentType update content type header of response

func (*Response) SetDebug

func (r *Response) SetDebug(obj *object.FileObject) *Response

SetDebug set flag indicating that response can including debug information

func (*Response) SetTransforms added in v0.15.0

func (r *Response) SetTransforms(trans []transforms.Transforms)

func (*Response) Stream

func (r *Response) Stream() io.ReadCloser

Stream return io.Reader interface from correct response content

type SharedResponse added in v0.30.0

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

SharedResponse wraps a Response with reference counting for safe sharing across multiple goroutines. This eliminates the need to create full copies of responses when distributing them to multiple consumers (e.g., in request collapsing).

func NewSharedResponse added in v0.30.0

func NewSharedResponse(resp *Response) (*SharedResponse, error)

NewSharedResponse creates a shareable response from a buffered response. The response must be fully buffered (IsBuffered() == true) before creating a SharedResponse. Returns an error if the response is not buffered or if buffering fails.

func (*SharedResponse) Acquire added in v0.30.0

func (sr *SharedResponse) Acquire() *Response

Acquire increments the reference count and returns a Response view that shares the underlying buffer. The returned Response is safe to use for reading but should not be modified.

Each call to Acquire() must be matched with a corresponding Release() call.

func (*SharedResponse) RefCount added in v0.30.0

func (sr *SharedResponse) RefCount() int32

RefCount returns the current reference count. This method is primarily useful for testing and debugging.

func (*SharedResponse) Release added in v0.30.0

func (sr *SharedResponse) Release()

Release decrements the reference count. When the reference count reaches zero, the underlying resources are released.

This method is safe to call from multiple goroutines and should always be called to prevent resource leaks. Use defer to ensure Release is called:

view := sharedResp.Acquire()
defer sharedResp.Release()

Jump to

Keyboard shortcuts

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