Documentation
¶
Overview ¶
Package response provides thin, explicit HTTP response helpers.
The Response type wraps http.ResponseWriter and centralizes common response patterns (JSON, text, HTML, redirects, status/error helpers).
The Proxy type buffers response intent (headers, cookies, status, redirects) for handlers that run in parallel or lack direct ResponseWriter access. Proxy instances are NOT thread-safe — create one per goroutine/task, then merge on a single goroutine via MergeProxyResponses before applying.
Index ¶
- Constants
- func GetClientRedirectURL(w http.ResponseWriter) string
- type Proxy
- func (p *Proxy) AddHeadEls(els *headels.HeadEls)
- func (p *Proxy) AddHeader(key, value string)
- func (p *Proxy) ApplyToResponseWriter(w http.ResponseWriter, r *http.Request)
- func (p *Proxy) Cookies() []*http.Cookie
- func (p *Proxy) HeadEls() *headels.HeadEls
- func (p *Proxy) Header(key string) string
- func (p *Proxy) Headers(key string) []string
- func (p *Proxy) IsError() bool
- func (p *Proxy) IsRedirect() bool
- func (p *Proxy) IsSuccess() bool
- func (p *Proxy) Location() string
- func (p *Proxy) Redirect(r *http.Request, url string, code ...int) (bool, error)
- func (p *Proxy) SetCookie(cookie *http.Cookie)
- func (p *Proxy) SetHeader(key, value string)
- func (p *Proxy) SetStatus(status int, error_text ...string)
- func (p *Proxy) Status() (int, string)
- type Response
- func (res *Response) AddHeader(key, value string)
- func (res *Response) BadRequest(reasons ...string)
- func (res *Response) ClientRedirect(url string) error
- func (res *Response) Error(status int, reasons ...string)
- func (res *Response) Forbidden(reasons ...string)
- func (res *Response) HTML(html string)
- func (res *Response) HTMLBytes(bytes []byte)
- func (res *Response) InternalServerError(reasons ...string)
- func (res *Response) IsCommitted() bool
- func (res *Response) JSON(v any)
- func (res *Response) JSONBytes(bytes []byte)
- func (res *Response) MethodNotAllowed(reasons ...string)
- func (res *Response) NotFound()
- func (res *Response) NotModified()
- func (res *Response) OK()
- func (res *Response) OKText()
- func (res *Response) Redirect(r *http.Request, url string, code ...int) (used_client_redirect bool, err error)
- func (res *Response) ServerRedirect(r *http.Request, url string, code ...int)
- func (res *Response) SetHeader(key, value string)
- func (res *Response) SetStatus(status int)
- func (res *Response) Text(text string)
- func (res *Response) TooManyRequests(reasons ...string)
- func (res *Response) Unauthorized(reasons ...string)
Constants ¶
const ( // ClientRedirectHeader carries a client-consumed redirect target URL. ClientRedirectHeader = "X-Client-Redirect" // ClientAcceptsRedirectHeader opts-in to client redirect responses. ClientAcceptsRedirectHeader = "X-Accepts-Client-Redirect" )
Variables ¶
This section is empty.
Functions ¶
func GetClientRedirectURL ¶
func GetClientRedirectURL(w http.ResponseWriter) string
GetClientRedirectURL reads the client redirect target header from w.
Types ¶
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy buffers response intent for deferred or parallel handlers. Do not instantiate directly — use NewProxy().
func MergeProxyResponses ¶
MergeProxyResponses merges multiple proxies into one. Rules:
- Head elements, headers, cookies: merged in order (later cookies dedupe by name).
- Status: first error wins, otherwise last success wins.
- Redirect: first redirect wins (assuming no error).
func (*Proxy) AddHeadEls ¶ added in v0.83.0
AddHeadEls merges head elements into the proxy.
func (*Proxy) ApplyToResponseWriter ¶
func (p *Proxy) ApplyToResponseWriter(w http.ResponseWriter, r *http.Request)
ApplyToResponseWriter writes all buffered proxy state to the real writer.
func (*Proxy) IsRedirect ¶
IsRedirect reports whether any redirect (server or client) is set.
func (*Proxy) Redirect ¶
Redirect sets a redirect on the proxy. Returns whether a client redirect was used and any error from URL validation.
type Response ¶
type Response struct {
Writer http.ResponseWriter
// contains filtered or unexported fields
}
Response is a convenience wrapper for writing HTTP responses.
func (*Response) AddHeader ¶
AddHeader appends a response header value without committing the response.
func (*Response) BadRequest ¶
func (*Response) ClientRedirect ¶
ClientRedirect sets a client-side redirect header and status 200.
func (*Response) InternalServerError ¶
func (*Response) IsCommitted ¶
IsCommitted reports whether a status or body has already been written.
func (*Response) MethodNotAllowed ¶
func (*Response) NotModified ¶
func (res *Response) NotModified()
func (*Response) Redirect ¶
func (res *Response) Redirect( r *http.Request, url string, code ...int, ) (used_client_redirect bool, err error)
Redirect chooses client or server redirect based on request headers.
func (*Response) ServerRedirect ¶
ServerRedirect writes an HTTP redirect response.