From 31e71dbd72828120787f3522b6a4230b1bfdbe5d Mon Sep 17 00:00:00 2001 From: John Date: Wed, 25 Apr 2018 17:11:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dghttp=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/http_request.go | 11 +++++- g/net/ghttp/http_server_hooks.go | 4 +-- g/net/ghttp/http_server_router.go | 9 ++--- geg/net/ghttp/domain.go | 5 +-- geg/other/test.go | 59 ++++++++++++++++++++++++++++--- 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/g/net/ghttp/http_request.go b/g/net/ghttp/http_request.go index be13f696f..4c15947b1 100644 --- a/g/net/ghttp/http_request.go +++ b/g/net/ghttp/http_request.go @@ -299,7 +299,16 @@ func (r *Request) IsExited() bool { return r.exit.Val() } -// 获取请求的客户端Ip地址 +// 获取请求的服务端IP/域名 +func (r *Request) GetHost() string { + array, _ := gregx.MatchString(`(.+):(\d+)`, r.Host) + if len(array) > 1 { + return array[1] + } + return r.Host +} + +// 获取请求的客户端IP地址 func (r *Request) GetClientIp() string { array, _ := gregx.MatchString(`(.+):(\d+)`, r.RemoteAddr) if len(array) > 1 { diff --git a/g/net/ghttp/http_server_hooks.go b/g/net/ghttp/http_server_hooks.go index 102a0072f..4e0c826e6 100644 --- a/g/net/ghttp/http_server_hooks.go +++ b/g/net/ghttp/http_server_hooks.go @@ -96,7 +96,7 @@ func (s *Server) callHookHandler(r *Request, hook string) { defer s.hhcmu.RUnlock() var hookItems []*hookCacheItem - cacheKey := hook + "^" + r.URL.Path + cacheKey := s.handlerHookKey(r.GetHost(), r.Method, r.URL.Path, hook) if v := s.hooksCache.Get(cacheKey); v == nil { hookItems = s.searchHookHandler(r, hook) if hookItems != nil { @@ -124,7 +124,7 @@ func (s *Server) searchHookHandler(r *Request, hook string) []*hookCacheItem { s.hhmu.RLock() defer s.hhmu.RUnlock() hookItems := make([]*hookCacheItem, 0) - domains := []string{gDEFAULT_DOMAIN, strings.Split(r.Host, ":")[0]} + domains := []string{gDEFAULT_DOMAIN, r.GetHost()} array := strings.Split(r.URL.Path[1:], "/") for _, domain := range domains { p, ok := s.hooksTree[domain] diff --git a/g/net/ghttp/http_server_router.go b/g/net/ghttp/http_server_router.go index 8d661a190..680fcfaf6 100644 --- a/g/net/ghttp/http_server_router.go +++ b/g/net/ghttp/http_server_router.go @@ -28,10 +28,11 @@ func (s *Server) getHandler(r *Request) *HandlerItem { defer s.hmcmu.RUnlock() var handlerItem *handlerCacheItem - if v := s.handlerCache.Get(r.URL.Path); v == nil { + cacheKey := s.handlerKey(r.GetHost(), r.Method, r.URL.Path) + if v := s.handlerCache.Get(cacheKey); v == nil { handlerItem = s.searchHandler(r) if handlerItem != nil { - s.handlerCache.Set(r.URL.Path, handlerItem, 0) + s.handlerCache.Set(cacheKey, handlerItem, 0) } } else { handlerItem = v.(*handlerCacheItem) @@ -176,7 +177,7 @@ func (s *Server) searchHandler(r *Request) *handlerCacheItem { func (s *Server) searchHandlerStatic(r *Request) *handlerCacheItem { s.hmmu.RLock() defer s.hmmu.RUnlock() - domains := []string{gDEFAULT_DOMAIN, strings.Split(r.Host, ":")[0]} + domains := []string{gDEFAULT_DOMAIN, r.GetHost()} // 首先进行静态匹配 for _, domain := range domains { if f, ok := s.handlerMap[s.handlerKey(domain, r.Method, r.URL.Path)]; ok { @@ -190,7 +191,7 @@ func (s *Server) searchHandlerStatic(r *Request) *handlerCacheItem { func (s *Server) searchHandlerDynamic(r *Request) *handlerCacheItem { s.hmmu.RLock() defer s.hmmu.RUnlock() - domains := []string{gDEFAULT_DOMAIN, strings.Split(r.Host, ":")[0]} + domains := []string{gDEFAULT_DOMAIN, r.GetHost()} array := strings.Split(r.URL.Path[1:], "/") for _, domain := range domains { p, ok := s.handlerTree[domain] diff --git a/geg/net/ghttp/domain.go b/geg/net/ghttp/domain.go index 10d37a5c3..89de7ddb9 100644 --- a/geg/net/ghttp/domain.go +++ b/geg/net/ghttp/domain.go @@ -3,16 +3,17 @@ package main import "gitee.com/johng/gf/g/net/ghttp" func Hello1(r *ghttp.Request) { - r.Response.Write("Hello World1!") + r.Response.Write("127.0.0.1: Hello World1!") } func Hello2(r *ghttp.Request) { - r.Response.Write("Hello World2!") + r.Response.Write("localhost: Hello World2!") } func main() { s := ghttp.GetServer() s.Domain("127.0.0.1").BindHandler("/", Hello1) s.Domain("localhost").BindHandler("/", Hello2) + s.SetPort(8199) s.Run() } diff --git a/geg/other/test.go b/geg/other/test.go index c6e2c58c7..87ab4ea8f 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,11 +2,62 @@ package main import ( "fmt" - "gitee.com/johng/gf/g" ) -func main() { - fmt.Println(g.Map{}) +var arabicNumber2RomanSymbolMap = map[int]string{ + 1 : "I", + 5 : "V", + 10 : "X", + 50 : "L", + 100 : "C", + 500 : "D", + 1000 : "M", +} + +var romanSymbol2ArabicNumberMap = map[int]string{ + 4000 : "MMMCMC", + 1000 : "M", + 900 : "CM", + 500 : "D", + 400 : "CD", + 100 : "C", + 90 : "XC", + 50 : "L", + 40 : "XL", + 10 : "X", + 9 : "IX", + 5 : "V", + 4 : "IV", + 1 : "I", +} + +var array = []int{4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1} + + +// Arabic numbers to Roman symbols +func romanSymbols2ArabicNumber(s string) int { + +} + +// Arabic numbers to Roman symbols +func arabicNumberToRomanSymbols(i int) string { + r := "" + for i > 0 { + for _, v := range array { + if i >= v { + r += romans[v] + i -= v + break + } + } + } + return r +} + +func main() { + fmt.Println(intToRomans(1944)) + //for i := 0; i < 10; i++ { + // fmt.Println(intToRomans(i)) + //} - select {} } \ No newline at end of file