Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions go/http/echo/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ func (a *EchoAdapter) GetPath() string {
func (a *EchoAdapter) GetURL() string {
req := a.ctx.Request()
scheme := "http"
if req.TLS != nil {
if xForwardedProto := req.Header.Get("X-Forwarded-Proto"); xForwardedProto != "" {
scheme = xForwardedProto
} else if req.TLS != nil {
scheme = "https"
}
host := req.Host
if host == "" {
if xForwardedHost := req.Header.Get("X-Forwarded-Host"); xForwardedHost != "" {
host = xForwardedHost
} else if host == "" {
host = req.Header.Get("Host")
}
return fmt.Sprintf("%s://%s%s", scheme, host, req.RequestURI)
Expand Down
9 changes: 9 additions & 0 deletions go/http/echo/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ func TestEchoAdapter_GetURL(t *testing.T) {
target: "/api/test?id=1&foo=bar",
expected: "http://example.com/api/test?id=1&foo=bar",
},
{
name: "x forwarded for",
target: "/api/test",
expected: "https://example.com/api/test",
},
}

for _, tt := range tests {
Expand All @@ -226,6 +231,10 @@ func TestEchoAdapter_GetURL(t *testing.T) {

req := httptest.NewRequest("GET", tt.target, nil)
req.Host = "example.com"
if tt.name == "x forwarded for" {
req.Header.Set("X-Forwarded-Proto", "https")
req.Header.Set("X-Forwarded-Host", "example.com")
}
w := httptest.NewRecorder()
e.ServeHTTP(w, req)

Expand Down
8 changes: 6 additions & 2 deletions go/http/gin/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ func (a *GinAdapter) GetPath() string {
// GetURL gets the full request URL
func (a *GinAdapter) GetURL() string {
scheme := "http"
if a.ctx.Request.TLS != nil {
if xForwardedProto := a.ctx.GetHeader("X-Forwarded-Proto"); xForwardedProto != "" {
scheme = xForwardedProto
} else if a.ctx.Request.TLS != nil {
scheme = "https"
}
host := a.ctx.Request.Host
if host == "" {
if xForwardedHost := a.ctx.GetHeader("X-Forwarded-Host"); xForwardedHost != "" {
host = xForwardedHost
} else if host == "" {
host = a.ctx.GetHeader("Host")
}
return fmt.Sprintf("%s://%s%s", scheme, host, a.ctx.Request.URL.RequestURI())
Expand Down
9 changes: 9 additions & 0 deletions go/http/gin/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ func TestGinAdapter_GetURL(t *testing.T) {
target: "/api/test?id=1&foo=bar",
expected: "http://example.com/api/test?id=1&foo=bar",
},
{
name: "x forwarded for",
target: "/api/test",
expected: "https://example.com/api/test",
},
}

for _, tt := range tests {
Expand All @@ -233,6 +238,10 @@ func TestGinAdapter_GetURL(t *testing.T) {

req := httptest.NewRequest("GET", tt.target, nil)
req.Host = "example.com"
if tt.name == "x forwarded for" {
req.Header.Set("X-Forwarded-Proto", "https")
req.Header.Set("X-Forwarded-Host", "example.com")
}
w := httptest.NewRecorder()
router.ServeHTTP(w, req)

Expand Down
8 changes: 6 additions & 2 deletions go/http/nethttp/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ func (a *NetHTTPAdapter) GetPath() string {
// GetURL gets the full request URL.
func (a *NetHTTPAdapter) GetURL() string {
scheme := "http"
if a.r.TLS != nil {
if xForwardedProto := a.r.Header.Get("X-Forwarded-Proto"); xForwardedProto != "" {
scheme = xForwardedProto
} else if a.r.TLS != nil {
scheme = "https"
}
host := a.r.Host
if host == "" {
if xForwardedHost := a.r.Header.Get("X-Forwarded-Host"); xForwardedHost != "" {
host = xForwardedHost
} else if host == "" {
host = a.r.Header.Get("Host")
}
return fmt.Sprintf("%s://%s%s", scheme, host, a.r.URL.RequestURI())
Expand Down
10 changes: 10 additions & 0 deletions go/http/nethttp/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ func TestNetHTTPAdapter_GetURL(t *testing.T) {
target: "/api/test?id=1&foo=bar",
expected: "http://example.com/api/test?id=1&foo=bar",
},
{
name: "x forwarded for",
target: "/api/test",
expected: "https://example.com/api/test",
},
}

for _, tt := range tests {
Expand All @@ -199,6 +204,11 @@ func TestNetHTTPAdapter_GetURL(t *testing.T) {
req.Host = "example.com"
adapter := NewNetHTTPAdapter(req)

if tt.name == "x forwarded for" {
req.Header.Set("X-Forwarded-Proto", "https")
req.Header.Set("X-Forwarded-Host", "example.com")
}

if adapter.GetURL() != tt.expected {
t.Errorf("Expected URL '%s', got '%s'", tt.expected, adapter.GetURL())
}
Expand Down
Loading