Beispiel #1
0
// NewRule creates new Rule.
func NewRule(basePath string, status int) *Rule {
	return &Rule{
		Base:           basePath,
		StatusCode:     status,
		RequestMatcher: httpserver.PathMatcher(basePath),
	}
}
Beispiel #2
0
// NewComplexRule creates a new RegexpRule. It returns an error if regexp
// pattern (pattern) or extensions (ext) are invalid.
func NewComplexRule(base, pattern, to string, status int, ext []string, matcher httpserver.RequestMatcher) (*ComplexRule, error) {
	// validate regexp if present
	var r *regexp.Regexp
	if pattern != "" {
		var err error
		r, err = regexp.Compile(pattern)
		if err != nil {
			return nil, err
		}
	}

	// validate extensions if present
	for _, v := range ext {
		if len(v) < 2 || (len(v) < 3 && v[0] == '!') {
			// check if no extension is specified
			if v != "/" && v != "!/" {
				return nil, fmt.Errorf("invalid extension %v", v)
			}
		}
	}

	// use both IfMatcher and PathMatcher
	matcher = httpserver.MergeRequestMatchers(
		// If condition matcher
		matcher,
		// Base path matcher
		httpserver.PathMatcher(base),
	)

	return &ComplexRule{
		Base:           base,
		To:             to,
		Status:         status,
		Exts:           ext,
		RequestMatcher: matcher,
		Regexp:         r,
	}, nil
}