rewrite

package
Version: v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2016 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package rewrite is middleware for rewriting requests internally to a different path.

Index

Constants

View Source
const (
	Is         = "is"
	Not        = "not"
	Has        = "has"
	NotHas     = "not_has"
	StartsWith = "starts_with"
	EndsWith   = "ends_with"
	Match      = "match"
	NotMatch   = "not_match"
)

Operators

Variables

This section is empty.

Functions

This section is empty.

Types

type ComplexRule added in v0.8.1

type ComplexRule struct {
	// Path base. Request to this path and subpaths will be rewritten
	Base string

	// Path to rewrite to
	To string

	// If set, neither performs rewrite nor proceeds
	// with request. Only returns code.
	Status int

	// Extensions to filter by
	Exts []string

	// Rewrite conditions
	Ifs []If

	*regexp.Regexp
}

ComplexRule is a rewrite rule based on a regular expression

func NewComplexRule added in v0.8.1

func NewComplexRule(base, pattern, to string, status int, ext []string, ifs []If) (*ComplexRule, error)

NewComplexRule creates a new RegexpRule. It returns an error if regexp pattern (pattern) or extensions (ext) are invalid.

func (*ComplexRule) Rewrite added in v0.8.1

func (r *ComplexRule) Rewrite(fs http.FileSystem, req *http.Request) (re Result)

Rewrite rewrites the internal location of the current request.

type If added in v0.8.1

type If struct {
	A        string
	Operator string
	B        string
}

If is statement for a rewrite condition.

func NewIf added in v0.8.1

func NewIf(a, operator, b string) (If, error)

NewIf creates a new If condition.

func (If) True added in v0.8.1

func (i If) True(r *http.Request) bool

True returns true if the condition is true and false otherwise. If r is not nil, it replaces placeholders before comparison.

type Result added in v0.8.1

type Result int

Result is the result of a rewrite

const (
	// RewriteIgnored is returned when rewrite is not done on request.
	RewriteIgnored Result = iota
	// RewriteDone is returned when rewrite is done on request.
	RewriteDone
	// RewriteStatus is returned when rewrite is not needed and status code should be set
	// for the request.
	RewriteStatus
)

func To added in v0.8.1

func To(fs http.FileSystem, r *http.Request, to string, replacer middleware.Replacer) Result

To attempts rewrite. It attempts to rewrite to first valid path or the last path if none of the paths are valid. Returns true if rewrite is successful and false otherwise.

type Rewrite

type Rewrite struct {
	Next    middleware.Handler
	FileSys http.FileSystem
	Rules   []Rule
}

Rewrite is middleware to rewrite request locations internally before being handled.

func (Rewrite) ServeHTTP

func (rw Rewrite) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)

ServeHTTP implements the middleware.Handler interface.

type Rule added in v0.6.0

type Rule interface {
	// Rewrite rewrites the internal location of the current request.
	Rewrite(http.FileSystem, *http.Request) Result
}

Rule describes an internal location rewrite rule.

type SimpleRule added in v0.7.0

type SimpleRule struct {
	From, To string
}

SimpleRule is a simple rewrite rule.

func NewSimpleRule added in v0.7.0

func NewSimpleRule(from, to string) SimpleRule

NewSimpleRule creates a new Simple Rule

func (SimpleRule) Rewrite added in v0.7.0

func (s SimpleRule) Rewrite(fs http.FileSystem, r *http.Request) Result

Rewrite rewrites the internal location of the current request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL