httputil

package module
v0.0.0-...-e9b977e Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2016 License: MIT Imports: 5 Imported by: 1

README

httputil: Make better web apps without frameworks

This library provides utilities for building better web tools, but without buying into a framework.

Resolver

The Resolver is a tool to assist HTTP applications resolve from a request to a route.

For example, you may wish to define a route like this:

GET /foo/*/bar

That route will handle any requests that come in and match the given pattern. When a request comes in for GET /foo/123/bar, it should be matched to the route above.

The httputil.Resolver provides this capability.

Notes

Parts of this library were extracted from Cookoo and refactored.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRouteNotFound = errors.New("route not found")

Functions

This section is empty.

Types

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver resolves a request or path to a route.

Given a registered set of route strings, this will take an *http.Request or a verb-path string and resolve it to the best match.

For example, given the registered path "GET /foo/*", the request "GET /foo/bar" will resolve to "GET /foo/*", while "GET /foo/bar/baz" will not.

The path matching syntax follows the one in the `path` package of Go.

HTTP Verbs: This resolver also allows you to specify verbs at the beginning of a path:

  • "GET /foo" and "POST /foo" are separate (but legal) paths. "* /foo" will allow any verb.
  • There are no constraints on verb name. Thus, verbs like WebDAV's PROPSET are fine, too. Or you can make up your own.

IMPORTANT! When it comes to matching route patterns against paths, ORDER IS IMPORTANT. Routes are evaluated in order. So if two rules (GET /a/b* and GET /a/bc) are both defined, the incomming request GET /a/bc will match whichever route is defined first. See the unit tests for examples.

The `**` and `/**` Wildcards: =============================

In addition to the paths described in the `path` package of Go's core, two extra wildcard sequences are defined:

- `**`: Match everything. - `/**`: a suffix that matches any sub-path.

The `**` wildcard works in ONLY ONE WAY: If the path is declared as `**`, with nothing else, then any path will match.

VALID: `**`, `GET /foo/**`, `GET /**` NOT VALID: `GET **`, `**/foo`, `foo/**/bar`

The `/**` suffix can only be added to the end of a path, and says "Match any subpath under this".

Examples:

  • URI paths "POST /foo", "GET /a/b/c", and "hello" all match "**". (The ** rule can be very dangerous for this reason.)
  • URI path "POST /assets/images/foo/bar/baz.jpg" matches "POST /assets/**"

The behavior for rules that contain `/**` anywhere other than the end have undefined behavior.

The list of paths is not modifed after the resolver is constructed, so this can safely be used concurrently.

func NewResolver

func NewResolver(paths []string) *Resolver

NewResolver creates a new Resolver.

This creates a resolver that knows about the given paths. Paths are evaluated in order.

Paths may contain HTTP verbs and wildcards as described above.

func (*Resolver) Resolve

func (r *Resolver) Resolve(req *http.Request) (string, error)

Resolve takes an HTTP request and attempts to resolve it.

When successful, it will return the path that it matched.

When no path is found, an ErrRouteNotFound error is returned.

Errors in path format, regexp compilation, and so on may also be returned.

func (*Resolver) ResolvePath

func (r *Resolver) ResolvePath(pathName string) (string, error)

Resolve a path name based using path patterns.

For general usage, this is intended to be used with verbed paths (e.g. "GET /foo"). If the verb is omitted, it will only match paths that do not have a verb. It should be noted that the Resolve() method will ONLY match verbed paths.

The resolver will take a path and attempt to match it against one of the paths the Resolver.

This resolver is designed to match path-like strings to path patterns. For example, the path `GET /foo/bar/baz` may match routes like `* /foo/*/baz` or `GET /foo/bar/*`

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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