Documentation ¶
Overview ¶
Package util provides utility methods for the notebookkernelsmixer codebase.
Index ¶
- func CheckXSRF(r *http.Request) error
- func HTTPStatusCode(err error) int
- func IfHeaderIsURLThenChangeHost(r *http.Request, name, targetHost string) error
- func IsUserError(err error) bool
- func Log(r *http.Request, msg any)
- func ModifyProxiedRequestForHost(r *http.Request, targetHost string) []error
- func NewLoggingResponseWriter(w http.ResponseWriter, r *http.Request, responseBuffer *bytes.Buffer) http.ResponseWriter
- type HTTPError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckXSRF ¶
CheckXSRF checks whether or not the given request includes XSRF headers if required.
func HTTPStatusCode ¶
HTTPStatusCode returns the HTTP status code corresponding to the given error.
If the supplied error does not have a known status code we fallback to 500.
func IfHeaderIsURLThenChangeHost ¶
IfHeaderIsURLThenChangeHost updates a request header containing a URL to specify the given host.
More specifically, if the given header is present, then its value is parsed as a URL, the host of that URL is updated to the given target, and then the updated URL is written back to the request header.
This is useful when forwarding requests via a reverse proxy, in order to ensure that certain headers, like `Origin` and `Referer`, match the hostname of the backend server.
Those values need to be changed before the request is forwarded to the backend as the URL for the proxy will be different from the URL for each respective backend server.
func IsUserError ¶
IsUserError reports whether or not the given error represents a mistake by the user.
func ModifyProxiedRequestForHost ¶
ModifyProxiedRequestForHost modifies a request so that it matches the given host.
This is a helper method for reverse proxies that need to translate a request specific to the proxy into one that is specific to a backend host.
func NewLoggingResponseWriter ¶
func NewLoggingResponseWriter(w http.ResponseWriter, r *http.Request, responseBuffer *bytes.Buffer) http.ResponseWriter
NewLoggingResponseWriter returns an http.ResponseWriter that wraps the given one and logs the response status.
Types ¶
type HTTPError ¶
type HTTPError int
HTTPError implements the error type for an HTTP response status code.
This is meant to be used as the base error that other errors wrap.
Example Usage:
if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("Some message: %w", util.HttpError(resp.StatusCode)) }
func (HTTPError) StatusCode ¶
StatusCode returns the HTTP status code for the given error.