util

package
v0.0.0-...-94e1d31 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package util provides utility methods for the notebookkernelsmixer codebase.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckXSRF

func CheckXSRF(r *http.Request) error

CheckXSRF checks whether or not the given request includes XSRF headers if required.

func HTTPStatusCode

func HTTPStatusCode(err error) int

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

func IfHeaderIsURLThenChangeHost(r *http.Request, name, targetHost string) error

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

func IsUserError(err error) bool

IsUserError reports whether or not the given error represents a mistake by the user.

func Log

func Log(r *http.Request, msg any)

Log writes a log message using a consistent output format.

func ModifyProxiedRequestForHost

func ModifyProxiedRequestForHost(r *http.Request, targetHost string) []error

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) Error

func (err HTTPError) Error() string

func (HTTPError) StatusCode

func (err HTTPError) StatusCode() int

StatusCode returns the HTTP status code for the given error.

Jump to

Keyboard shortcuts

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