httpapi

package
v0.8.15 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// XForwardedHostHeader is a header used by proxies to indicate the
	// original host of the request.
	XForwardedHostHeader = "X-Forwarded-Host"
)

Variables

View Source
var (
	UsernameValidRegex = regexp.MustCompile("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$")
)

Functions

func AppNameOrPort added in v0.8.15

func AppNameOrPort(val string) (string, uint16)

AppNameOrPort takes a string and returns either the input string or a port number.

func Forbidden added in v0.5.10

func Forbidden(rw http.ResponseWriter)

func InternalServerError added in v0.8.10

func InternalServerError(rw http.ResponseWriter, err error)

func IsWebsocketUpgrade added in v0.8.10

func IsWebsocketUpgrade(r *http.Request) bool

func ParseCustom added in v0.7.5

func ParseCustom[T any](parser *QueryParamParser, vals url.Values, def T, queryParam string, parseFunc func(v string) (T, error)) T

ParseCustom has to be a function, not a method on QueryParamParser because generics cannot be used on struct methods.

func Read

func Read(rw http.ResponseWriter, r *http.Request, value interface{}) bool

Read decodes JSON from the HTTP request into the value provided. It uses go-validator to validate the incoming request body.

func RequestHost added in v0.8.10

func RequestHost(r *http.Request) string

RequestHost returns the name of the host from the request. It prioritizes 'X-Forwarded-Host' over r.Host since most requests are being proxied.

func ResourceNotFound added in v0.7.0

func ResourceNotFound(rw http.ResponseWriter)

ResourceNotFound is intentionally vague. All 404 responses should be identical to prevent leaking existence of resources.

func SplitSubdomain added in v0.8.15

func SplitSubdomain(hostname string) (subdomain string, rest string, err error)

SplitSubdomain splits a subdomain from the rest of the hostname. E.g.:

  • "foo.bar.com" becomes "foo", "bar.com"
  • "foo.bar.baz.com" becomes "foo", "bar.baz.com"

An error is returned if the string doesn't contain a period.

func StripCoderCookies added in v0.8.6

func StripCoderCookies(header string) string

StripCoderCookies removes the session token from the cookie header provided.

func UsernameFrom added in v0.8.2

func UsernameFrom(str string) string

UsernameFrom returns a best-effort username from the provided string.

It first attempts to validate the incoming string, which will be returned if it is valid. It then will attempt to extract the username from an email address. If no success happens during these steps, a random username will be returned.

func UsernameValid added in v0.8.2

func UsernameValid(str string) bool

UsernameValid returns whether the input string is a valid username.

func WebsocketCloseSprintf

func WebsocketCloseSprintf(format string, vars ...any) string

WebsocketCloseSprintf formats a websocket close message and ensures it is truncated to the maximum allowed length.

func Write

func Write(rw http.ResponseWriter, status int, response interface{})

Write outputs a standardized format to an HTTP response body.

Types

type ApplicationURL added in v0.8.15

type ApplicationURL struct {
	// Only one of AppName or Port will be set.
	AppName       string
	Port          uint16
	AgentName     string
	WorkspaceName string
	Username      string
	// BaseHostname is the rest of the hostname minus the application URL part
	// and the first dot.
	BaseHostname string
}

ApplicationURL is a parsed application URL hostname.

func ParseSubdomainAppURL added in v0.8.15

func ParseSubdomainAppURL(hostname string) (ApplicationURL, error)

ParseSubdomainAppURL parses an ApplicationURL from the given hostname. If the subdomain is not a valid application URL hostname, returns a non-nil error.

Subdomains should be in the form:

{PORT/APP_NAME}--{AGENT_NAME}--{WORKSPACE_NAME}--{USERNAME}
(eg. http://8080--main--dev--dean.hi.c8s.io)

func (ApplicationURL) String added in v0.8.15

func (a ApplicationURL) String() string

String returns the application URL hostname without scheme.

type QueryParamParser added in v0.7.0

type QueryParamParser struct {
	// Errors is the set of errors to return via the API. If the length
	// of this set is 0, there are no errors!.
	Errors []codersdk.ValidationError
}

QueryParamParser is a helper for parsing all query params and gathering all errors in 1 sweep. This means all invalid fields are returned at once, rather than only returning the first error

func NewQueryParamParser added in v0.7.0

func NewQueryParamParser() *QueryParamParser

func (*QueryParamParser) Int added in v0.7.0

func (p *QueryParamParser) Int(vals url.Values, def int, queryParam string) int

func (*QueryParamParser) String added in v0.7.0

func (*QueryParamParser) String(vals url.Values, def string, queryParam string) string

func (*QueryParamParser) Strings added in v0.7.5

func (*QueryParamParser) Strings(vals url.Values, def []string, queryParam string) []string

func (*QueryParamParser) UUID added in v0.7.0

func (p *QueryParamParser) UUID(vals url.Values, def uuid.UUID, queryParam string) uuid.UUID

func (*QueryParamParser) UUIDorMe added in v0.7.0

func (p *QueryParamParser) UUIDorMe(vals url.Values, def uuid.UUID, me uuid.UUID, queryParam string) uuid.UUID

func (*QueryParamParser) UUIDs added in v0.7.0

func (p *QueryParamParser) UUIDs(vals url.Values, def []uuid.UUID, queryParam string) []uuid.UUID

type StatusWriter added in v0.8.10

type StatusWriter struct {
	http.ResponseWriter
	Status   int
	Hijacked bool
	// contains filtered or unexported fields
}

StatusWriter intercepts the status of the request and the response body up to maxBodySize if Status >= 400. It is guaranteed to be the ResponseWriter directly downstream from Middleware.

func (*StatusWriter) Hijack added in v0.8.10

func (w *StatusWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*StatusWriter) ResponseBody added in v0.8.10

func (w *StatusWriter) ResponseBody() []byte

func (*StatusWriter) Write added in v0.8.10

func (w *StatusWriter) Write(b []byte) (int, error)

func (*StatusWriter) WriteHeader added in v0.8.10

func (w *StatusWriter) WriteHeader(status int)

Jump to

Keyboard shortcuts

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