Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
54fb49ed
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
54fb49ed
编写于
11月 26, 2013
作者:
A
astaxie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix #315
上级
fbd1c3f8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
159 addition
and
63 deletion
+159
-63
context/input.go
context/input.go
+1
-0
filter.go
filter.go
+120
-13
fiter_test.go
fiter_test.go
+24
-0
router.go
router.go
+14
-50
未找到文件。
context/input.go
浏览文件 @
54fb49ed
...
...
@@ -139,6 +139,7 @@ func (input *BeegoInput) Param(key string) string {
}
func
(
input
*
BeegoInput
)
Query
(
key
string
)
string
{
input
.
req
.
ParseForm
()
return
input
.
req
.
Form
.
Get
(
key
)
}
...
...
filter.go
浏览文件 @
54fb49ed
...
...
@@ -2,36 +2,143 @@ package beego
import
(
"regexp"
"strings"
)
type
FilterRouter
struct
{
pattern
string
regex
*
regexp
.
Regexp
filterFunc
FilterFunc
hasregex
bool
params
map
[
int
]
string
pattern
string
regex
*
regexp
.
Regexp
filterFunc
FilterFunc
hasregex
bool
params
map
[
int
]
string
parseParams
map
[
string
]
string
}
func
(
mr
*
FilterRouter
)
ValidRouter
(
router
string
)
bool
{
func
(
mr
*
FilterRouter
)
ValidRouter
(
router
string
)
(
bool
,
map
[
string
]
string
)
{
if
mr
.
pattern
==
""
{
return
true
return
true
,
nil
}
if
mr
.
pattern
==
"*"
{
return
true
return
true
,
nil
}
if
router
==
mr
.
pattern
{
return
true
return
true
,
nil
}
if
mr
.
hasregex
{
if
mr
.
regex
.
MatchString
(
router
)
{
return
true
if
!
mr
.
regex
.
MatchString
(
router
)
{
return
false
,
nil
}
matches
:=
mr
.
regex
.
FindStringSubmatch
(
router
)
if
len
(
matches
)
>
0
{
if
len
(
matches
[
0
])
==
len
(
router
)
{
return
true
params
:=
make
(
map
[
string
]
string
)
for
i
,
match
:=
range
matches
[
1
:
]
{
params
[
mr
.
params
[
i
]]
=
match
}
return
true
,
params
}
}
}
return
false
return
false
,
nil
}
func
buildFilter
(
pattern
string
,
filter
FilterFunc
)
*
FilterRouter
{
mr
:=
new
(
FilterRouter
)
mr
.
params
=
make
(
map
[
int
]
string
)
mr
.
filterFunc
=
filter
parts
:=
strings
.
Split
(
pattern
,
"/"
)
j
:=
0
for
i
,
part
:=
range
parts
{
if
strings
.
HasPrefix
(
part
,
":"
)
{
expr
:=
"(.+)"
//a user may choose to override the default expression
// similar to expressjs: ‘/user/:id([0-9]+)’
if
index
:=
strings
.
Index
(
part
,
"("
);
index
!=
-
1
{
expr
=
part
[
index
:
]
part
=
part
[
:
index
]
//match /user/:id:int ([0-9]+)
//match /post/:username:string ([\w]+)
}
else
if
lindex
:=
strings
.
LastIndex
(
part
,
":"
);
lindex
!=
0
{
switch
part
[
lindex
:
]
{
case
":int"
:
expr
=
"([0-9]+)"
part
=
part
[
:
lindex
]
case
":string"
:
expr
=
`([\w]+)`
part
=
part
[
:
lindex
]
}
}
mr
.
params
[
j
]
=
part
parts
[
i
]
=
expr
j
++
}
if
strings
.
HasPrefix
(
part
,
"*"
)
{
expr
:=
"(.+)"
if
part
==
"*.*"
{
mr
.
params
[
j
]
=
":path"
parts
[
i
]
=
"([^.]+).([^.]+)"
j
++
mr
.
params
[
j
]
=
":ext"
j
++
}
else
{
mr
.
params
[
j
]
=
":splat"
parts
[
i
]
=
expr
j
++
}
}
//url like someprefix:id(xxx).html
if
strings
.
Contains
(
part
,
":"
)
&&
strings
.
Contains
(
part
,
"("
)
&&
strings
.
Contains
(
part
,
")"
)
{
var
out
[]
rune
var
start
bool
var
startexp
bool
var
param
[]
rune
var
expt
[]
rune
for
_
,
v
:=
range
part
{
if
start
{
if
v
!=
'('
{
param
=
append
(
param
,
v
)
continue
}
}
if
startexp
{
if
v
!=
')'
{
expt
=
append
(
expt
,
v
)
continue
}
}
if
v
==
':'
{
param
=
make
([]
rune
,
0
)
param
=
append
(
param
,
':'
)
start
=
true
}
else
if
v
==
'('
{
startexp
=
true
start
=
false
mr
.
params
[
j
]
=
string
(
param
)
j
++
expt
=
make
([]
rune
,
0
)
expt
=
append
(
expt
,
'('
)
}
else
if
v
==
')'
{
startexp
=
false
expt
=
append
(
expt
,
')'
)
out
=
append
(
out
,
expt
...
)
}
else
{
out
=
append
(
out
,
v
)
}
}
parts
[
i
]
=
string
(
out
)
}
}
if
j
!=
0
{
pattern
=
strings
.
Join
(
parts
,
"/"
)
regex
,
regexErr
:=
regexp
.
Compile
(
pattern
)
if
regexErr
!=
nil
{
//TODO add error handling here to avoid panic
panic
(
regexErr
)
}
mr
.
regex
=
regex
mr
.
hasregex
=
true
}
mr
.
pattern
=
pattern
return
mr
}
fiter_test.go
0 → 100644
浏览文件 @
54fb49ed
package
beego
import
(
"github.com/astaxie/beego/context"
"net/http"
"net/http/httptest"
"testing"
)
var
FilterUser
=
func
(
ctx
*
context
.
Context
)
{
ctx
.
Output
.
Body
([]
byte
(
"i am "
+
ctx
.
Input
.
Params
[
":last"
]
+
ctx
.
Input
.
Params
[
":first"
]))
}
func
TestFilter
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/person/asta/Xie"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
:=
NewControllerRegistor
()
handler
.
AddFilter
(
"/person/:last/:first"
,
"AfterStatic"
,
FilterUser
)
handler
.
Add
(
"/person/:last/:first"
,
&
TestController
{})
handler
.
ServeHTTP
(
w
,
r
)
if
w
.
Body
.
String
()
!=
"i am astaXie"
{
t
.
Errorf
(
"user define func can't run"
)
}
}
router.go
浏览文件 @
54fb49ed
...
...
@@ -222,49 +222,6 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
}
}
func
buildFilter
(
pattern
string
,
filter
FilterFunc
)
*
FilterRouter
{
mr
:=
new
(
FilterRouter
)
mr
.
filterFunc
=
filter
parts
:=
strings
.
Split
(
pattern
,
"/"
)
j
:=
0
for
i
,
part
:=
range
parts
{
if
strings
.
HasPrefix
(
part
,
":"
)
{
expr
:=
"(.+)"
//a user may choose to override the default expression
// similar to expressjs: ‘/user/:id([0-9]+)’
if
index
:=
strings
.
Index
(
part
,
"("
);
index
!=
-
1
{
expr
=
part
[
index
:
]
part
=
part
[
:
index
]
//match /user/:id:int ([0-9]+)
//match /post/:username:string ([\w]+)
}
else
if
lindex
:=
strings
.
LastIndex
(
part
,
":"
);
lindex
!=
0
{
switch
part
[
lindex
:
]
{
case
":int"
:
expr
=
"([0-9]+)"
part
=
part
[
:
lindex
]
case
":string"
:
expr
=
`([\w]+)`
part
=
part
[
:
lindex
]
}
}
parts
[
i
]
=
expr
j
++
}
}
if
j
!=
0
{
pattern
=
strings
.
Join
(
parts
,
"/"
)
regex
,
regexErr
:=
regexp
.
Compile
(
pattern
)
if
regexErr
!=
nil
{
//TODO add error handling here to avoid panic
panic
(
regexErr
)
}
mr
.
regex
=
regex
mr
.
hasregex
=
true
}
mr
.
pattern
=
pattern
return
mr
}
func
(
p
*
ControllerRegistor
)
AddFilter
(
pattern
,
action
string
,
filter
FilterFunc
)
{
mr
:=
buildFilter
(
pattern
,
filter
)
switch
action
{
...
...
@@ -469,7 +426,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
p
.
enableFilter
{
if
l
,
ok
:=
p
.
filters
[
BeforeRouter
];
ok
{
for
_
,
filterR
:=
range
l
{
if
filterR
.
ValidRouter
(
r
.
URL
.
Path
)
{
if
ok
,
p
:=
filterR
.
ValidRouter
(
r
.
URL
.
Path
);
ok
{
context
.
Input
.
Params
=
p
filterR
.
filterFunc
(
context
)
if
w
.
started
{
goto
Admin
...
...
@@ -516,7 +474,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
p
.
enableFilter
{
if
l
,
ok
:=
p
.
filters
[
AfterStatic
];
ok
{
for
_
,
filterR
:=
range
l
{
if
filterR
.
ValidRouter
(
r
.
URL
.
Path
)
{
if
ok
,
p
:=
filterR
.
ValidRouter
(
r
.
URL
.
Path
);
ok
{
context
.
Input
.
Params
=
p
filterR
.
filterFunc
(
context
)
if
w
.
started
{
goto
Admin
...
...
@@ -591,7 +550,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
p
.
enableFilter
{
if
l
,
ok
:=
p
.
filters
[
BeforeExec
];
ok
{
for
_
,
filterR
:=
range
l
{
if
filterR
.
ValidRouter
(
r
.
URL
.
Path
)
{
if
ok
,
p
:=
filterR
.
ValidRouter
(
r
.
URL
.
Path
);
ok
{
context
.
Input
.
Params
=
p
filterR
.
filterFunc
(
context
)
if
w
.
started
{
goto
Admin
...
...
@@ -746,7 +706,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
p
.
enableFilter
{
if
l
,
ok
:=
p
.
filters
[
AfterExec
];
ok
{
for
_
,
filterR
:=
range
l
{
if
filterR
.
ValidRouter
(
r
.
URL
.
Path
)
{
if
ok
,
p
:=
filterR
.
ValidRouter
(
r
.
URL
.
Path
);
ok
{
context
.
Input
.
Params
=
p
filterR
.
filterFunc
(
context
)
if
w
.
started
{
goto
Admin
...
...
@@ -795,7 +756,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
p
.
enableFilter
{
if
l
,
ok
:=
p
.
filters
[
BeforeExec
];
ok
{
for
_
,
filterR
:=
range
l
{
if
filterR
.
ValidRouter
(
r
.
URL
.
Path
)
{
if
ok
,
p
:=
filterR
.
ValidRouter
(
r
.
URL
.
Path
);
ok
{
context
.
Input
.
Params
=
p
filterR
.
filterFunc
(
context
)
if
w
.
started
{
goto
Admin
...
...
@@ -850,7 +812,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
p
.
enableFilter
{
if
l
,
ok
:=
p
.
filters
[
AfterExec
];
ok
{
for
_
,
filterR
:=
range
l
{
if
filterR
.
ValidRouter
(
r
.
URL
.
Path
)
{
if
ok
,
p
:=
filterR
.
ValidRouter
(
r
.
URL
.
Path
);
ok
{
context
.
Input
.
Params
=
p
filterR
.
filterFunc
(
context
)
if
w
.
started
{
goto
Admin
...
...
@@ -878,7 +841,8 @@ Admin:
if
p
.
enableFilter
{
if
l
,
ok
:=
p
.
filters
[
FinishRouter
];
ok
{
for
_
,
filterR
:=
range
l
{
if
filterR
.
ValidRouter
(
r
.
URL
.
Path
)
{
if
ok
,
p
:=
filterR
.
ValidRouter
(
r
.
URL
.
Path
);
ok
{
context
.
Input
.
Params
=
p
filterR
.
filterFunc
(
context
)
if
w
.
started
{
break
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录