Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
80bc372f
Commit
80bc372f
authored
Dec 10, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pool.Put
parent
f70f3380
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
66 deletions
+66
-66
context.go
context/context.go
+26
-2
router.go
router.go
+40
-64
No files found.
context/context.go
View file @
80bc372f
...
...
@@ -49,14 +49,14 @@ type Context struct {
Input
*
BeegoInput
Output
*
BeegoOutput
Request
*
http
.
Request
ResponseWriter
http
.
ResponseWriter
ResponseWriter
*
Response
_xsrfToken
string
}
// Reset init Context, BeegoInput and BeegoOutput
func
(
ctx
*
Context
)
Reset
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
ctx
.
Request
=
r
ctx
.
ResponseWriter
=
rw
ctx
.
ResponseWriter
=
&
Response
{
rw
,
false
,
0
}
ctx
.
Input
.
Reset
(
ctx
)
ctx
.
Output
.
Reset
(
ctx
)
}
...
...
@@ -164,3 +164,27 @@ func (ctx *Context) CheckXSRFCookie() bool {
}
return
true
}
//Response is a wrapper for the http.ResponseWriter
//started set to true if response was written to then don't execute other handler
type
Response
struct
{
http
.
ResponseWriter
Started
bool
Status
int
}
// Write writes the data to the connection as part of an HTTP reply,
// and sets `started` to true.
// started means the response has sent out.
func
(
w
*
Response
)
Write
(
p
[]
byte
)
(
int
,
error
)
{
w
.
Started
=
true
return
w
.
ResponseWriter
.
Write
(
p
)
}
// WriteHeader sends an HTTP response header with status code,
// and sets `started` to true.
func
(
w
*
Response
)
WriteHeader
(
code
int
)
{
w
.
Status
=
code
w
.
Started
=
true
w
.
ResponseWriter
.
WriteHeader
(
code
)
}
router.go
View file @
80bc372f
...
...
@@ -576,36 +576,11 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin
return
false
,
""
}
// Implement http.Handler interface.
func
(
p
*
ControllerRegister
)
ServeHTTP
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
starttime
:=
time
.
Now
()
var
(
runrouter
reflect
.
Type
findrouter
bool
runMethod
string
routerInfo
*
controllerInfo
w
=
&
responseWriter
{
rw
,
false
,
0
}
)
if
BConfig
.
RunMode
==
"dev"
{
w
.
Header
()
.
Set
(
"Server"
,
BConfig
.
ServerName
)
}
context
:=
p
.
pool
.
Get
()
.
(
*
beecontext
.
Context
)
context
.
Reset
(
w
,
r
)
defer
p
.
recoverPanic
(
context
)
var
urlPath
string
if
!
BConfig
.
RouterCaseSensitive
{
urlPath
=
strings
.
ToLower
(
r
.
URL
.
Path
)
}
else
{
urlPath
=
r
.
URL
.
Path
}
// defined filter function
doFilter
:=
func
(
pos
int
)
(
started
bool
)
{
func
(
p
*
ControllerRegister
)
execFilter
(
context
*
beecontext
.
Context
,
pos
int
,
urlPath
string
)
(
started
bool
)
{
if
p
.
enableFilter
{
if
l
,
ok
:=
p
.
filters
[
pos
];
ok
{
for
_
,
filterR
:=
range
l
{
if
filterR
.
returnOnOutput
&&
w
.
s
tarted
{
if
filterR
.
returnOnOutput
&&
context
.
ResponseWriter
.
S
tarted
{
return
true
}
if
ok
,
params
:=
filterR
.
ValidRouter
(
urlPath
);
ok
{
...
...
@@ -617,28 +592,53 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
filterR
.
filterFunc
(
context
)
}
if
filterR
.
returnOnOutput
&&
w
.
s
tarted
{
if
filterR
.
returnOnOutput
&&
context
.
ResponseWriter
.
S
tarted
{
return
true
}
}
}
}
return
false
}
// Implement http.Handler interface.
func
(
p
*
ControllerRegister
)
ServeHTTP
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
starttime
:=
time
.
Now
()
var
(
runrouter
reflect
.
Type
findrouter
bool
runMethod
string
routerInfo
*
controllerInfo
)
context
:=
p
.
pool
.
Get
()
.
(
*
beecontext
.
Context
)
context
.
Reset
(
rw
,
r
)
defer
p
.
pool
.
Put
(
context
)
defer
p
.
recoverPanic
(
context
)
if
BConfig
.
RunMode
==
"dev"
{
context
.
Output
.
Header
(
"Server"
,
BConfig
.
ServerName
)
}
var
urlPath
string
if
!
BConfig
.
RouterCaseSensitive
{
urlPath
=
strings
.
ToLower
(
r
.
URL
.
Path
)
}
else
{
urlPath
=
r
.
URL
.
Path
}
// filter wrong httpmethod
if
_
,
ok
:=
HTTPMETHOD
[
r
.
Method
];
!
ok
{
http
.
Error
(
w
,
"Method Not Allowed"
,
405
)
http
.
Error
(
r
w
,
"Method Not Allowed"
,
405
)
goto
Admin
}
// filter for static file
if
doFilter
(
BeforeStatic
)
{
if
p
.
execFilter
(
context
,
BeforeStatic
,
urlPath
)
{
goto
Admin
}
serverStaticRouter
(
context
)
if
w
.
s
tarted
{
if
context
.
ResponseWriter
.
S
tarted
{
findrouter
=
true
goto
Admin
}
...
...
@@ -646,14 +646,14 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
// session init
if
BConfig
.
WebConfig
.
Session
.
SessionOn
{
var
err
error
context
.
Input
.
CruSession
,
err
=
GlobalSessions
.
SessionStart
(
w
,
r
)
context
.
Input
.
CruSession
,
err
=
GlobalSessions
.
SessionStart
(
r
w
,
r
)
if
err
!=
nil
{
Error
(
err
)
exception
(
"503"
,
context
)
return
}
defer
func
()
{
context
.
Input
.
CruSession
.
SessionRelease
(
w
)
context
.
Input
.
CruSession
.
SessionRelease
(
r
w
)
}()
}
...
...
@@ -664,7 +664,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
context
.
Input
.
ParseFormOrMulitForm
(
BConfig
.
MaxMemory
)
}
if
doFilter
(
BeforeRouter
)
{
if
p
.
execFilter
(
context
,
BeforeRouter
,
urlPath
)
{
goto
Admin
}
...
...
@@ -703,7 +703,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if
findrouter
{
//execute middleware filters
if
doFilter
(
BeforeExec
)
{
if
p
.
execFilter
(
context
,
BeforeExec
,
urlPath
)
{
goto
Admin
}
isRunnable
:=
false
...
...
@@ -764,7 +764,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
execController
.
URLMapping
()
if
!
w
.
s
tarted
{
if
!
context
.
ResponseWriter
.
S
tarted
{
//exec main logic
switch
runMethod
{
case
"GET"
:
...
...
@@ -790,7 +790,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
//render template
if
!
w
.
s
tarted
&&
context
.
Output
.
Status
==
0
{
if
!
context
.
ResponseWriter
.
S
tarted
&&
context
.
Output
.
Status
==
0
{
if
BConfig
.
WebConfig
.
AutoRender
{
if
err
:=
execController
.
Render
();
err
!=
nil
{
panic
(
err
)
...
...
@@ -804,12 +804,12 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
//execute middleware filters
if
doFilter
(
AfterExec
)
{
if
p
.
execFilter
(
context
,
AfterExec
,
urlPath
)
{
goto
Admin
}
}
doFilter
(
FinishRouter
)
p
.
execFilter
(
context
,
FinishRouter
,
urlPath
)
Admin
:
timeend
:=
time
.
Since
(
starttime
)
...
...
@@ -842,7 +842,7 @@ Admin:
// Call WriteHeader if status code has been set changed
if
context
.
Output
.
Status
!=
0
{
w
.
WriteHeader
(
context
.
Output
.
Status
)
context
.
ResponseWriter
.
WriteHeader
(
context
.
Output
.
Status
)
}
}
...
...
@@ -878,30 +878,6 @@ func (p *ControllerRegister) recoverPanic(context *beecontext.Context) {
}
}
//responseWriter is a wrapper for the http.ResponseWriter
//started set to true if response was written to then don't execute other handler
type
responseWriter
struct
{
http
.
ResponseWriter
started
bool
status
int
}
// Write writes the data to the connection as part of an HTTP reply,
// and sets `started` to true.
// started means the response has sent out.
func
(
w
*
responseWriter
)
Write
(
p
[]
byte
)
(
int
,
error
)
{
w
.
started
=
true
return
w
.
ResponseWriter
.
Write
(
p
)
}
// WriteHeader sends an HTTP response header with status code,
// and sets `started` to true.
func
(
w
*
responseWriter
)
WriteHeader
(
code
int
)
{
w
.
status
=
code
w
.
started
=
true
w
.
ResponseWriter
.
WriteHeader
(
code
)
}
func
tourl
(
params
map
[
string
]
string
)
string
{
if
len
(
params
)
==
0
{
return
""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment