appurl

package
v2.10.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package appurl handles all parsing/validation/etc around application URLs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompileHostnamePattern

func CompileHostnamePattern(pattern string) (*regexp.Regexp, error)

CompileHostnamePattern compiles a hostname pattern into a regular expression. A hostname pattern is a string that may contain a single wildcard character at the beginning. The wildcard character matches any number of hostname-safe characters excluding periods. The pattern is case-insensitive.

The supplied pattern:

  • must not start or end with a period
  • must contain exactly one asterisk at the beginning
  • must not contain any other wildcard characters
  • must not contain any other characters that are not hostname-safe (including whitespace)
  • must contain at least two hostname labels/segments (i.e. "foo" or "*" are not valid patterns, but "foo.bar" and "*.bar" are).

The returned regular expression will match an entire hostname with optional trailing periods and whitespace. The first submatch will be the wildcard match.

func ExecuteHostnamePattern

func ExecuteHostnamePattern(pattern *regexp.Regexp, hostname string) (string, bool)

ExecuteHostnamePattern executes a pattern generated by CompileHostnamePattern and returns the wildcard match. If the pattern does not match the hostname, returns false.

func HostnamesMatch

func HostnamesMatch(a, b string) bool

HostnamesMatch returns true if the hostnames are equal, disregarding capitalization, extra leading or trailing periods, and ports.

func SubdomainAppHost

func SubdomainAppHost(apphost string, accessURL *url.URL) string

SubdomainAppHost returns the URL of the apphost for subdomain based apps. It will omit the scheme.

Arguments: apphost: Expected to contain a wildcard, example: "*.coder.com" accessURL: The access url for the deployment.

Returns: 'apphost:port'

For backwards compatibility and for "accessurl=localhost:0" purposes, we need to use the port from the accessurl if the apphost doesn't have a port. If the user specifies a port in the apphost, we will use that port instead.

Types

type ApplicationURL

type ApplicationURL struct {
	Prefix        string
	AppSlugOrPort string
	AgentName     string
	WorkspaceName string
	Username      string
}

ApplicationURL is a parsed application URL hostname.

func ParseSubdomainAppURL

func ParseSubdomainAppURL(subdomain string) (ApplicationURL, error)

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

Subdomains should be in the form:

	({PREFIX}---)?{PORT/APP_SLUG}--{AGENT_NAME}--{WORKSPACE_NAME}--{USERNAME}
	e.g.
     https://8080--main--dev--dean.hi.c8s.io
     https://app--main--dev--dean.hi.c8s.io
     https://prefix---8080--main--dev--dean.hi.c8s.io
     https://prefix---app--main--dev--dean.hi.c8s.io

The optional prefix is permitted to allow customers to put additional URL at the beginning of their application URL (i.e. if they want to simulate different subdomains on the same app/port).

Prefix requires three hyphens at the end to separate it from the rest of the URL so we can add/remove segments in the future from the parsing logic.

TODO(dean): make the agent name optional when using the app slug. This will reduce the character count for app URLs.

func (ApplicationURL) Path

func (a ApplicationURL) Path() string

Path is a helper function to get the url path of the app if it is not served on a subdomain. In practice this is not really used because we use the chi `{variable}` syntax to extract these parts. For testing purposes and for completeness of this package, we include it.

func (ApplicationURL) String

func (a ApplicationURL) String() string

String returns the application URL hostname without scheme. You will likely want to append a period and the base hostname.

Jump to

Keyboard shortcuts

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