From c4b9144d45218c0cdd7ec56cc5ec4a4330d59cc7 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 9 Apr 2018 18:51:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bghttp=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/http_server_auto.go | 4 ++-- g/net/ghttp/http_server_handler.go | 13 ++++++------- geg/net/ghttp/events.go | 18 ++++++++---------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/g/net/ghttp/http_server_auto.go b/g/net/ghttp/http_server_auto.go index 3904816ff..13149b552 100644 --- a/g/net/ghttp/http_server_auto.go +++ b/g/net/ghttp/http_server_auto.go @@ -12,14 +12,14 @@ func (s *Server) startCloseQueueLoop() { for { if v := s.closeQueue.PopFront(); v != nil { r := v.(*Request) - s.callHookHandler(r, "BeforeRequestClose") + s.callHookHandler(r, "BeforeClose") // 关闭当前会话的Cookie r.Cookie.Close() // 更新Session会话超时时间 r.Session.UpdateExpire() - s.callHookHandler(r, "AfterRequestClose") + s.callHookHandler(r, "AfterClose") } } }() diff --git a/g/net/ghttp/http_server_handler.go b/g/net/ghttp/http_server_handler.go index 48652c9b6..6fd23fc81 100644 --- a/g/net/ghttp/http_server_handler.go +++ b/g/net/ghttp/http_server_handler.go @@ -78,22 +78,21 @@ func (s *Server)callHandler(h *HandlerItem, r *Request) { s.callHookHandler(r, "AfterServe") // 路由规则打包 - s.callHookHandler(r, "BeforeRouterPatch") + s.callHookHandler(r, "BeforePatch") if buffer, err := s.Router.Patch(r.Response.Buffer()); err == nil { r.Response.ClearBuffer() r.Response.Write(buffer) } - s.callHookHandler(r, "AfterRouterPatch") + s.callHookHandler(r, "AfterPatch") + + s.callHookHandler(r, "BeforeOutput") // 输出Cookie - s.callHookHandler(r, "BeforeCookieOutput") r.Cookie.Output() - s.callHookHandler(r, "AfterCookieOutput") - // 输出缓冲区 - s.callHookHandler(r, "BeforeBufferOutput") r.Response.OutputBuffer() - s.callHookHandler(r, "AfterBufferOutput") + + s.callHookHandler(r, "AfterOutput") // 将Request对象指针丢到队列中异步处理 s.closeQueue.PushBack(r) diff --git a/geg/net/ghttp/events.go b/geg/net/ghttp/events.go index b37c0ad10..30d8a65f0 100644 --- a/geg/net/ghttp/events.go +++ b/geg/net/ghttp/events.go @@ -8,16 +8,14 @@ import ( func main() { pattern := "/" ghttp.GetServer().BindHookHandlerByMap(pattern, map[string]ghttp.HandlerFunc{ - "BeforeServe" : func(r *ghttp.Request){ fmt.Println("BeforeServe") }, - "AfterServe" : func(r *ghttp.Request){ fmt.Println("AfterServe") }, - "BeforeRouterPatch" : func(r *ghttp.Request){ fmt.Println("BeforeRouterPatch") }, - "AfterRouterPatch" : func(r *ghttp.Request){ fmt.Println("AfterRouterPatch") }, - "BeforeCookieOutput" : func(r *ghttp.Request){ fmt.Println("BeforeCookieOutput") }, - "AfterCookieOutput" : func(r *ghttp.Request){ fmt.Println("AfterCookieOutput") }, - "BeforeBufferOutput" : func(r *ghttp.Request){ fmt.Println("BeforeBufferOutput") }, - "AfterBufferOutput" : func(r *ghttp.Request){ fmt.Println("AfterBufferOutput") }, - "BeforeRequestClose" : func(r *ghttp.Request){ fmt.Println("BeforeRequestClose") }, - "AfterRequestClose" : func(r *ghttp.Request){ fmt.Println("AfterRequestClose") }, + "BeforeServe" : func(r *ghttp.Request){ fmt.Println("BeforeServe") }, + "AfterServe" : func(r *ghttp.Request){ fmt.Println("AfterServe") }, + "BeforePatch" : func(r *ghttp.Request){ fmt.Println("BeforePatch") }, + "AfterPatch" : func(r *ghttp.Request){ fmt.Println("AfterPatch") }, + "BeforeOutput" : func(r *ghttp.Request){ fmt.Println("BeforeOutput") }, + "AfterOutput" : func(r *ghttp.Request){ fmt.Println("AfterOutput") }, + "BeforeClose" : func(r *ghttp.Request){ fmt.Println("BeforeClose") }, + "AfterClose" : func(r *ghttp.Request){ fmt.Println("AfterClose") }, }) ghttp.GetServer().BindHandler(pattern, func(r *ghttp.Request) { r.Response.WriteString("Hello World!")