Skip to content

Commit 74c199d

Browse files
committed
####Version 1.7.15
* refactor: update *HttpContext to Context interface in middleware & request
1 parent 49e19ff commit 74c199d

File tree

4 files changed

+56
-28
lines changed

4 files changed

+56
-28
lines changed

context.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ type (
9595
WriteJsonBlobC(code int, b []byte) error
9696
WriteJsonp(callback string, i interface{}) error
9797
WriteJsonpBlob(callback string, b []byte) error
98+
99+
//inner func
100+
getMiddlewareStep() string
101+
setMiddlewareStep(step string)
102+
release()
103+
reset(res *Response, r *Request, server *HttpServer, node RouterNode, params Params, handler HttpHandle)
104+
setSessionID(id string)
98105
}
99106

100107
HttpContext struct {
@@ -132,6 +139,8 @@ type (
132139
}
133140
)
134141

142+
//************* HttpContext public func **********************
143+
135144
// reset response attr
136145
func (ctx *HttpContext) reset(res *Response, r *Request, server *HttpServer, node RouterNode, params Params, handler HttpHandle) {
137146
ctx.request = r
@@ -639,6 +648,25 @@ func (ctx *HttpContext) WriteJsonpBlob(callback string, b []byte) error {
639648
return err
640649
}
641650

651+
//**************** HttpContext inner func ************************
652+
653+
// setMiddlewareStep
654+
func (ctx *HttpContext) setMiddlewareStep(step string) {
655+
ctx.middlewareStep = step
656+
}
657+
658+
// getMiddlewareStep
659+
func (ctx *HttpContext) getMiddlewareStep() string {
660+
return ctx.middlewareStep
661+
}
662+
663+
// setSessionID
664+
func (ctx *HttpContext) setSessionID(id string) {
665+
ctx.sessionID = id
666+
}
667+
668+
//************* WebSocket public func **********************
669+
642670
// Request get http request
643671
func (ws *WebSocket) Request() *http.Request {
644672
return ws.Conn.Request()
@@ -656,6 +684,8 @@ func (ws *WebSocket) ReadMessage() (string, error) {
656684
return str, err
657685
}
658686

687+
//************* HijackConn public func **********************
688+
659689
// WriteString hjiack conn write string
660690
func (hj *HijackConn) WriteString(content string) (int, error) {
661691
n, err := hj.ReadWriter.WriteString(hj.header + "\r\n" + content)

middleware.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,25 @@ func (bm *BaseMiddleware) SetNext(m Middleware) {
6060
}
6161

6262
func (bm *BaseMiddleware) Next(ctx Context) error {
63-
httpCtx := ctx.(*HttpContext)
64-
if httpCtx.middlewareStep == "" {
65-
httpCtx.middlewareStep = middleware_App
63+
if ctx.getMiddlewareStep() == "" {
64+
ctx.setMiddlewareStep(middleware_App)
6665
}
6766
if bm.next == nil {
68-
if httpCtx.middlewareStep == middleware_App {
69-
httpCtx.middlewareStep = middleware_Group
70-
if len(httpCtx.RouterNode().GroupMiddlewares()) > 0 {
71-
return httpCtx.RouterNode().GroupMiddlewares()[0].Handle(ctx)
67+
if ctx.getMiddlewareStep() == middleware_App {
68+
ctx.setMiddlewareStep(middleware_Group)
69+
if len(ctx.RouterNode().GroupMiddlewares()) > 0 {
70+
return ctx.RouterNode().GroupMiddlewares()[0].Handle(ctx)
7271
}
7372
}
74-
if httpCtx.middlewareStep == middleware_Group {
75-
httpCtx.middlewareStep = middleware_Router
76-
if len(httpCtx.RouterNode().Middlewares()) > 0 {
77-
return httpCtx.RouterNode().Middlewares()[0].Handle(ctx)
73+
if ctx.getMiddlewareStep() == middleware_Group {
74+
ctx.setMiddlewareStep(middleware_Router)
75+
if len(ctx.RouterNode().Middlewares()) > 0 {
76+
return ctx.RouterNode().Middlewares()[0].Handle(ctx)
7877
}
7978
}
8079

81-
if httpCtx.middlewareStep == middleware_Router {
82-
return httpCtx.Handler()(ctx)
80+
if ctx.getMiddlewareStep() == middleware_Router {
81+
return ctx.Handler()(ctx)
8382
}
8483
} else {
8584
// check exclude config
@@ -136,12 +135,11 @@ func getIgnoreFaviconModule() *HttpModule {
136135
}
137136

138137
func (x *xMiddleware) Handle(ctx Context) error {
139-
httpCtx := ctx.(*HttpContext)
140-
if httpCtx.middlewareStep == "" {
141-
httpCtx.middlewareStep = middleware_App
138+
if ctx.getMiddlewareStep() == "" {
139+
ctx.setMiddlewareStep(middleware_App)
142140
}
143141
if x.IsEnd {
144-
return httpCtx.Handler()(ctx)
142+
return ctx.Handler()(ctx)
145143
}
146144
return x.Next(ctx)
147145
}

request.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ var maxBodySize int64 = 32 << 20 // 32 MB
1212

1313
type Request struct {
1414
*http.Request
15-
httpCtx *HttpContext
15+
httpCtx Context
1616
postBody []byte
1717
realUrl string
1818
isReadBody bool
1919
requestID string
2020
}
2121

2222
// reset response attr
23-
func (req *Request) reset(r *http.Request, ctx *HttpContext) {
23+
func (req *Request) reset(r *http.Request, ctx Context) {
2424
req.httpCtx = ctx
2525
req.Request = r
2626
req.isReadBody = false
2727
if ctx.HttpServer().ServerConfig().EnabledRequestID {
2828
req.requestID = ctx.HttpServer().DotApp.IDGenerater()
29-
ctx.response.SetHeader(HeaderRequestID, req.requestID)
29+
ctx.Response().SetHeader(HeaderRequestID, req.requestID)
3030
} else {
3131
req.requestID = ""
3232
}
@@ -151,14 +151,14 @@ func (req *Request) PostString(key string) string {
151151
func (req *Request) PostBody() []byte {
152152
if !req.isReadBody {
153153
if req.httpCtx != nil {
154-
switch req.httpCtx.httpServer.DotApp.Config.Server.MaxBodySize {
154+
switch req.httpCtx.HttpServer().DotApp.Config.Server.MaxBodySize {
155155
case -1:
156156
break
157157
case 0:
158-
req.Body = http.MaxBytesReader(req.httpCtx.response.Writer(), req.Body, maxBodySize)
158+
req.Body = http.MaxBytesReader(req.httpCtx.Response().Writer(), req.Body, maxBodySize)
159159
break
160160
default:
161-
req.Body = http.MaxBytesReader(req.httpCtx.response.Writer(), req.Body, req.httpApp().Config.Server.MaxBodySize)
161+
req.Body = http.MaxBytesReader(req.httpCtx.Response().Writer(), req.Body, req.httpApp().Config.Server.MaxBodySize)
162162
break
163163
}
164164
}

server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ func prepareHttpContext(server *HttpServer, w http.ResponseWriter, req *http.Req
498498
// get from pool
499499
response := server.pool.response.Get().(*Response)
500500
request := server.pool.request.Get().(*Request)
501-
httpCtx := server.pool.context.Get().(*HttpContext)
501+
httpCtx := server.pool.context.Get().(Context)
502502
httpCtx.reset(response, request, server, nil, nil, nil)
503503
response.reset(w)
504504
request.reset(req, httpCtx)
@@ -509,9 +509,9 @@ func prepareHttpContext(server *HttpServer, w http.ResponseWriter, req *http.Req
509509
if httpCtx.HttpServer().SessionConfig().EnabledSession {
510510
sessionId, err := httpCtx.HttpServer().GetSessionManager().GetClientSessionID(httpCtx.Request().Request)
511511
if err == nil && sessionId != "" {
512-
httpCtx.sessionID = sessionId
512+
httpCtx.setSessionID(sessionId)
513513
} else {
514-
httpCtx.sessionID = httpCtx.HttpServer().GetSessionManager().NewSessionID()
514+
httpCtx.setSessionID(httpCtx.HttpServer().GetSessionManager().NewSessionID())
515515
cookie := &http.Cookie{
516516
Name: httpCtx.HttpServer().sessionManager.StoreConfig().CookieName,
517517
Value: url.QueryEscape(httpCtx.SessionID()),
@@ -531,11 +531,11 @@ func prepareHttpContext(server *HttpServer, w http.ResponseWriter, req *http.Req
531531
httpCtx.Response().SetHeader(HeaderContentEncoding, gzipScheme)
532532
}
533533

534-
return httpCtx
534+
return httpCtx.(*HttpContext)
535535
}
536536

537537
// releaseHttpContext release HttpContext, release gzip writer
538-
func releaseHttpContext(server *HttpServer, httpCtx *HttpContext) {
538+
func releaseHttpContext(server *HttpServer, httpCtx Context) {
539539
if server.ServerConfig().EnabledGzip {
540540
var w io.Writer
541541
w = httpCtx.Response().Writer().(*gzipResponseWriter).Writer

0 commit comments

Comments
 (0)