Skip to content
Closed
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/labstack/echo/v4
module github.com/binhnguyenduc/echo/v4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to be removed


go 1.15

Expand Down
10 changes: 10 additions & 0 deletions middleware/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type (
// Examples: If custom TLS certificates are required.
Transport http.RoundTripper

// ModifyRequest defines function to modify request before passing to ReverseProxy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Transport field provides similar functionality as you can create your own http.RoundTripper which modifies request:

type MyRoundTripper struct {
	accessToken string
	r           http.RoundTripper
}

func (mrt MyRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
	r.Header.Add("Authorization", "Bearer: "+mrt.accessToken)
	return mrt.r.RoundTrip(r)
}

func main() {
	e := echo.New()

	e.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
		Transport: MyRoundTripper{},
	}))

	log.Fatal(e.Start(":8080"))
}

https://stackoverflow.com/questions/51325704/adding-a-default-http-header-in-go

ModifyRequest func(*http.Request) error

// ModifyResponse defines function to modify response from ProxyTarget.
ModifyResponse func(*http.Response) error
}
Expand Down Expand Up @@ -230,6 +233,13 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
}

req := c.Request()

if config.ModifyRequest != nil {
err := config.ModifyRequest(req)
if err != nil {
return err
}
}
res := c.Response()
tgt := config.Balancer.Next(c)
c.Set(config.ContextKey, tgt)
Expand Down
22 changes: 22 additions & 0 deletions middleware/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,25 @@ func TestClientCancelConnectionResultsHTTPCode499(t *testing.T) {
timeoutStop.Done()
assert.Equal(t, 499, rec.Code)
}

func TestProxyModifyRequestHostHeader(t *testing.T) {
// Setup
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer upstream.Close()
url, _ := url.Parse(upstream.URL)
rrb := NewRoundRobinBalancer([]*ProxyTarget{{Name: "upstream", URL: url}})
e := echo.New()

config := DefaultProxyConfig
config.Balancer = rrb
config.ModifyRequest = func(req *http.Request) error {
req.Header.Set("Host", "abc.com")
return nil
}
e.Use(ProxyWithConfig(config))
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()

e.ServeHTTP(rec, req)
assert.Equal(t, "abc.com", req.Header.Get("Host"), "expected: abc.com / actual: %s", req.Header.Get("Host"))
}