response

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandlerErrorMessage added in v0.5.0

func HandlerErrorMessage(err error, status int) string

HandlerErrorMessage returns the client-facing message for a generated handler error. Ordinary 5xx failures use generic status text so internal details stay out of HTTP responses. HandlerError.Message remains an explicit application-owned override.

func HandlerStatus

func HandlerStatus(err error, fallback int) int

HandlerStatus returns a handler error status, or fallback for ordinary errors.

func NewHandlerError

func NewHandlerError(status int, message string, cause error) error

NewHandlerError creates an error suitable for generated handlers.

func ValidateLocalRedirect added in v0.5.0

func ValidateLocalRedirect(url string) error

ValidateLocalRedirect reports whether url is a safe same-origin redirect target. Only local absolute paths are allowed; protocol-relative URLs, backslash tricks browsers normalize to "//", and CRLF header-injection attempts are rejected. It is the single redirect-safety contract shared by the response, guard, and SSR lanes so an attacker-influenced "next"/"return_to" value cannot become an open redirect.

func ValidationHTML added in v0.1.5

func ValidationHTML(result validation.Result) string

ValidationHTML renders a small escaped validation message block.

func WriteHTML added in v0.1.5

func WriteHTML(writer http.ResponseWriter, request *http.Request, body string, cacheControl string) error

WriteHTML writes an HTML response with an explicit Cache-Control policy and suppresses the body for HEAD requests. An empty cache policy falls back to no-store for request-time safety.

func WriteHTTP

func WriteHTTP(writer http.ResponseWriter, result Response) error

WriteHTTP writes a runtime response envelope to net/http.

func WriteNoStoreError

func WriteNoStoreError(writer http.ResponseWriter, status int, message string)

WriteNoStoreError writes an HTTP error that must not be cached.

func WriteNoStoreHTML

func WriteNoStoreHTML(writer http.ResponseWriter, request *http.Request, body string) error

WriteNoStoreHTML writes a no-store HTML response and suppresses the body for HEAD requests.

func WriteNoStoreHTTP

func WriteNoStoreHTTP(writer http.ResponseWriter, result Response) error

WriteNoStoreHTTP writes a response envelope that must not be cached.

func WriteNoStoreHandlerError added in v0.5.0

func WriteNoStoreHandlerError(writer http.ResponseWriter, err error, fallbackStatus int)

WriteNoStoreHandlerError writes a generated handler error using the client-safe HandlerErrorMessage policy.

func WriteNoStoreHandlerJSONError added in v0.5.0

func WriteNoStoreHandlerJSONError(writer http.ResponseWriter, err error, fallbackStatus int)

WriteNoStoreHandlerJSONError writes a generated handler error as the stable JSON shape used by contract web adapters.

func WriteNoStoreJSONError added in v0.5.0

func WriteNoStoreJSONError(writer http.ResponseWriter, status int, message string)

WriteNoStoreJSONError writes the stable generated JSON error shape.

Types

type HandlerError

type HandlerError struct {
	Status  int
	Message string
	Cause   error
}

HandlerError wraps failures raised by generated handlers.

func (HandlerError) Error

func (err HandlerError) Error() string

func (HandlerError) Unwrap

func (err HandlerError) Unwrap() error

type Kind

type Kind string

Kind identifies the response shape produced by actions, fragments, APIs, or full-page rendering.

const (
	HTML     Kind = "html"
	Redirect Kind = "redirect"
	Fragment Kind = "fragment"
	JSON     Kind = "json"
	Reload   Kind = "reload"
)

type Response

type Response struct {
	Kind    Kind
	Status  int
	Body    string
	Target  string
	Swap    SwapMode
	URL     string
	Cookies []http.Cookie
}

Response is the generated runtime response envelope.

func FragmentFor

func FragmentFor(target, body string) Response

FragmentFor creates a partial fragment response for a DOM target.

func FragmentSwap

func FragmentSwap(target string, swap SwapMode, body string) (Response, error)

FragmentSwap creates a partial fragment response with an explicit swap mode.

func HTMLBody

func HTMLBody(status int, body string) Response

HTMLBody creates a full HTML response.

func JSONBody

func JSONBody(status int, body string) Response

JSONBody creates a JSON response from an already-encoded body.

func JSONValue

func JSONValue(status int, value any) (Response, error)

JSONValue marshals a value into a JSON response body.

func RedirectTo

func RedirectTo(url string) Response

RedirectTo creates a redirect response.

func ReloadPage added in v0.1.5

func ReloadPage() Response

ReloadPage asks enhanced clients to reload the current page. Non-enhanced forms should still use normal POST/redirect/get behavior.

func ValidationFragment added in v0.1.5

func ValidationFragment(target string, result validation.Result) Response

ValidationFragment creates a fragment response with escaped validation messages. It uses HTTP 200 so progressively enhanced partial forms can swap the returned fragment with the current client runtime.

func ValidationJSON added in v0.1.5

func ValidationJSON(result validation.Result) (Response, error)

ValidationJSON creates a structured validation error response.

func WithCookie added in v0.1.5

func WithCookie(result Response, cookie http.Cookie) Response

WithCookie returns a copy of result that sets cookie when written to HTTP.

type SwapMode

type SwapMode string

SwapMode identifies how a fragment response should update its target.

const (
	SwapInnerHTML SwapMode = "innerHTML"
	SwapOuterHTML SwapMode = "outerHTML"
)

Jump to

Keyboard shortcuts

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