1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-24 18:30:54 +00:00
Beego/server/web/filter.go

135 lines
3.6 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
2020-07-22 14:50:08 +00:00
2020-07-31 13:43:11 +00:00
import (
"strings"
"github.com/astaxie/beego/server/web/context"
2020-07-31 13:43:11 +00:00
)
// FilterChain is different from pure FilterFunc
// when you use this, you must invoke next(ctx) inside the FilterFunc which is returned
// And all those FilterChain will be invoked before other FilterFunc
type FilterChain func(next FilterFunc) FilterFunc
2020-07-22 14:50:08 +00:00
// FilterFunc defines a filter function which is invoked before the controller handler is executed.
2020-07-31 13:43:11 +00:00
type FilterFunc func(ctx *context.Context)
2020-07-22 14:50:08 +00:00
// FilterRouter defines a filter operation which is invoked before the controller handler is executed.
// It can match the URL against a pattern, and execute a filter function
// when a request with a matching URL arrives.
type FilterRouter struct {
filterFunc FilterFunc
2020-08-10 15:04:57 +00:00
next *FilterRouter
2020-07-22 14:50:08 +00:00
tree *Tree
pattern string
returnOnOutput bool
resetParams bool
}
2020-07-31 13:43:11 +00:00
// params is for:
// 1. setting the returnOnOutput value (false allows multiple filters to execute)
// 2. determining whether or not params need to be reset.
2020-09-02 14:44:31 +00:00
func newFilterRouter(pattern string, filter FilterFunc, opts ...FilterOpt) *FilterRouter {
2020-07-31 13:43:11 +00:00
mr := &FilterRouter{
2020-09-06 07:21:07 +00:00
tree: NewTree(),
pattern: pattern,
filterFunc: filter,
2020-07-31 13:43:11 +00:00
}
2020-09-06 07:21:07 +00:00
fos := &filterOpts{
returnOnOutput: true,
}
2020-09-02 14:44:31 +00:00
for _, o := range opts {
o(fos)
2020-07-31 13:43:11 +00:00
}
2020-09-02 14:44:31 +00:00
if !fos.routerCaseSensitive {
mr.pattern = strings.ToLower(pattern)
2020-07-31 13:43:11 +00:00
}
2020-09-02 14:44:31 +00:00
mr.returnOnOutput = fos.returnOnOutput
mr.resetParams = fos.resetParams
2020-07-31 13:43:11 +00:00
mr.tree.AddRouter(pattern, true)
return mr
}
// filter will check whether we need to execute the filter logic
// return (started, done)
func (f *FilterRouter) filter(ctx *context.Context, urlPath string, preFilterParams map[string]string) (bool, bool) {
if f.returnOnOutput && ctx.ResponseWriter.Started {
return true, true
}
if f.resetParams {
preFilterParams = ctx.Input.Params()
}
if ok := f.ValidRouter(urlPath, ctx); ok {
f.filterFunc(ctx)
if f.resetParams {
ctx.Input.ResetParams()
for k, v := range preFilterParams {
ctx.Input.SetParam(k, v)
}
}
2020-08-10 15:04:57 +00:00
} else if f.next != nil {
return f.next.filter(ctx, urlPath, preFilterParams)
2020-07-31 13:43:11 +00:00
}
if f.returnOnOutput && ctx.ResponseWriter.Started {
return true, true
}
return false, false
}
2020-07-22 14:50:08 +00:00
// ValidRouter checks if the current request is matched by this filter.
// If the request is matched, the values of the URL parameters defined
// by the filter pattern are also returned.
func (f *FilterRouter) ValidRouter(url string, ctx *context.Context) bool {
isOk := f.tree.Match(url, ctx)
if isOk != nil {
if b, ok := isOk.(bool); ok {
return b
}
}
return false
}
2020-09-02 14:44:31 +00:00
type filterOpts struct {
returnOnOutput bool
resetParams bool
routerCaseSensitive bool
}
type FilterOpt func(opts *filterOpts)
func WithReturnOnOutput(ret bool) FilterOpt {
return func(opts *filterOpts) {
opts.returnOnOutput = ret
}
}
func WithResetParams(reset bool) FilterOpt {
return func(opts *filterOpts) {
opts.resetParams = reset
}
}
func WithCaseSensitive(sensitive bool) FilterOpt {
return func(opts *filterOpts) {
opts.routerCaseSensitive = sensitive
}
}