From 920dbbef5e4baeefc1cb26ce5ddbb96b8b48161b Mon Sep 17 00:00:00 2001 From: houseme Date: Wed, 9 Mar 2022 17:42:56 +0800 Subject: [PATCH 1/7] fix: server access logs contain the protocol used between the server and the load balancer, but not the protocol used between the client and the load balancer --- net/ghttp/ghttp_request.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index be5935f41..5c546d164 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -225,7 +225,8 @@ func (r *Request) GetRemoteIp() string { // GetUrl returns current URL of this request. func (r *Request) GetUrl() string { scheme := "http" - if r.TLS != nil { + proto := r.Header.Get("X-Forwarded-Proto") + if r.TLS != nil || (proto != "" && strings.ToLower(proto) == "https") { scheme = "https" } return fmt.Sprintf(`%s://%s%s`, scheme, r.Host, r.URL.String()) From 553793021081a1c8dbe0520d59d5258929f38bb2 Mon Sep 17 00:00:00 2001 From: houseme Date: Wed, 9 Mar 2022 18:30:32 +0800 Subject: [PATCH 2/7] fix --- net/ghttp/ghttp_server_swagger.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/net/ghttp/ghttp_server_swagger.go b/net/ghttp/ghttp_server_swagger.go index 846993c9a..5071a72b6 100644 --- a/net/ghttp/ghttp_server_swagger.go +++ b/net/ghttp/ghttp_server_swagger.go @@ -7,6 +7,8 @@ package ghttp import ( + "fmt" + "github.com/gogf/gf/v2/text/gstr" ) @@ -45,7 +47,7 @@ func (s *Server) swaggerUI(r *Request) { if r.StaticFile != nil && r.StaticFile.File != nil && r.StaticFile.IsDir { content := gstr.ReplaceByMap(swaggerUITemplate, map[string]string{ swaggerUIDocURLPlaceHolder: s.config.OpenApiPath, - swaggerUIDocNamePlaceHolder: gstr.TrimRight(r.GetUrl(), "/") + "/" + swaggerUIDocName, + swaggerUIDocNamePlaceHolder: gstr.TrimRight(fmt.Sprintf(`//%s%s`, r.Host, r.URL.String()), "/") + "/" + swaggerUIDocName, }) r.Response.Write(content) r.ExitAll() From f6054ab37fa3bebac98556ff29d0ff50efa57d1b Mon Sep 17 00:00:00 2001 From: houseme Date: Wed, 9 Mar 2022 18:32:13 +0800 Subject: [PATCH 3/7] improve code --- net/ghttp/ghttp_request.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index 5c546d164..cba973756 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -224,8 +224,11 @@ func (r *Request) GetRemoteIp() string { // GetUrl returns current URL of this request. func (r *Request) GetUrl() string { - scheme := "http" - proto := r.Header.Get("X-Forwarded-Proto") + var ( + scheme = "http" + proto = r.Header.Get("X-Forwarded-Proto") + ) + if r.TLS != nil || (proto != "" && strings.ToLower(proto) == "https") { scheme = "https" } From be4720373255fe74391e6287c91ea8119c1e8486 Mon Sep 17 00:00:00 2001 From: houseme Date: Wed, 9 Mar 2022 21:24:57 +0800 Subject: [PATCH 4/7] improve code --- net/ghttp/ghttp_request.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index cba973756..725b1bb53 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -19,6 +19,7 @@ import ( "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/os/gview" "github.com/gogf/gf/v2/text/gregex" + "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/guid" ) @@ -229,7 +230,7 @@ func (r *Request) GetUrl() string { proto = r.Header.Get("X-Forwarded-Proto") ) - if r.TLS != nil || (proto != "" && strings.ToLower(proto) == "https") { + if r.TLS != nil || gstr.Equal(proto, "https") { scheme = "https" } return fmt.Sprintf(`%s://%s%s`, scheme, r.Host, r.URL.String()) From 3d5ff3b2505a7fa8ed0de268f36aad2226b9b45c Mon Sep 17 00:00:00 2001 From: houseme Date: Wed, 9 Mar 2022 21:27:02 +0800 Subject: [PATCH 5/7] fix --- net/ghttp/ghttp_server_log.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/net/ghttp/ghttp_server_log.go b/net/ghttp/ghttp_server_log.go index 7743f1f23..b27f8b6bf 100644 --- a/net/ghttp/ghttp_server_log.go +++ b/net/ghttp/ghttp_server_log.go @@ -18,8 +18,12 @@ func (s *Server) handleAccessLog(r *Request) { if !s.IsAccessLogEnabled() { return } - scheme := "http" - if r.TLS != nil { + var ( + scheme = "http" + proto = r.Header.Get("X-Forwarded-Proto") + ) + + if r.TLS != nil || gstr.Equal(proto, "https") { scheme = "https" } s.Logger().File(s.config.AccessLogPattern). From 3d6867c32155d63640eb5cfea5e1cdbc73bad3de Mon Sep 17 00:00:00 2001 From: houseme Date: Wed, 9 Mar 2022 21:29:49 +0800 Subject: [PATCH 6/7] fix --- net/ghttp/ghttp_server_log.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/net/ghttp/ghttp_server_log.go b/net/ghttp/ghttp_server_log.go index b27f8b6bf..bcf350521 100644 --- a/net/ghttp/ghttp_server_log.go +++ b/net/ghttp/ghttp_server_log.go @@ -47,9 +47,10 @@ func (s *Server) handleErrorLog(err error, r *Request) { code = gerror.Code(err) scheme = "http" codeDetail = code.Detail() + proto = r.Header.Get("X-Forwarded-Proto") codeDetailStr string ) - if r.TLS != nil { + if r.TLS != nil || gstr.Equal(proto, "https") { scheme = "https" } if codeDetail != nil { From c200177af43d87df4b39d436bb8dfff6e201b3b0 Mon Sep 17 00:00:00 2001 From: houseme Date: Wed, 9 Mar 2022 23:36:30 +0800 Subject: [PATCH 7/7] fix: js link err --- net/ghttp/ghttp_server_swagger.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ghttp/ghttp_server_swagger.go b/net/ghttp/ghttp_server_swagger.go index 5071a72b6..9f67f3758 100644 --- a/net/ghttp/ghttp_server_swagger.go +++ b/net/ghttp/ghttp_server_swagger.go @@ -47,7 +47,7 @@ func (s *Server) swaggerUI(r *Request) { if r.StaticFile != nil && r.StaticFile.File != nil && r.StaticFile.IsDir { content := gstr.ReplaceByMap(swaggerUITemplate, map[string]string{ swaggerUIDocURLPlaceHolder: s.config.OpenApiPath, - swaggerUIDocNamePlaceHolder: gstr.TrimRight(fmt.Sprintf(`//%s%s`, r.Host, r.URL.String()), "/") + "/" + swaggerUIDocName, + swaggerUIDocNamePlaceHolder: gstr.TrimRight(fmt.Sprintf(`//%s%s`, r.Host, r.Server.config.SwaggerPath), "/") + "/" + swaggerUIDocName, }) r.Response.Write(content) r.ExitAll()