Skip to content

Commit

Permalink
add UseFullPath option to use full path when true
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHlt committed Mar 1, 2019
1 parent c44a577 commit a5ebb78
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions proxy_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type ProxyRoute struct {
MiddlewareParams interface{} `json:"middleware_params" yaml:"middleware_params"`
// Set to true to see errors on web page when there is a panic error on gobis
ShowError bool `json:"show_error" yaml:"show_error"`
// Set to true to use full path
// e.g.: path=/metrics/** and request=/metrics/foo this will be redirected to /metrics/foo on upstream instead of /foo
UseFullPath bool `json:"use_full_path" yaml:"use_full_path"`
// Chain others routes in a routes
Routes []ProxyRoute `json:"routes" yaml:"routes"`
// Set an handler to use to forward request to this handler when using gobis programmatically
Expand Down Expand Up @@ -139,22 +142,28 @@ func (r ProxyRoute) RequestPath(req *http.Request) string {
}

func (r ProxyRoute) UpstreamUrl(req *http.Request) *url.URL {
origPath := ""
if r.UseFullPath {
origPath = r.PathAsStartPath() + "/"
}
if r.ForwardHandler != nil {
req.URL.Path = ""
req.URL.Path = origPath
return req.URL
}
var upstreamUrl *url.URL
if r.ForwardedHeader == "" {
upstreamUrl, _ = url.Parse(r.Url)
upstreamUrl.Path = origPath + upstreamUrl.Path
return upstreamUrl
}
upstream := req.Header.Get(r.ForwardedHeader)
if upstream == "" {
upstreamUrl, _ = url.Parse(r.Url)
upstreamUrl.Path = origPath + upstreamUrl.Path
return upstreamUrl
}
upstreamUrl, _ = url.Parse(upstream)
upstreamUrl.Path = ""
upstreamUrl.Path = origPath
return upstreamUrl
}

Expand Down
10 changes: 10 additions & 0 deletions router_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ var _ = Describe("RouterFactory", func() {
Expect(request.Header.Get("Authorization")).ShouldNot(BeEmpty())
})
})
Context("when route have option UseFullPath", func() {
It("should set request url with start path from path option", func() {
ForwardRequest(ProxyRoute{
Path: NewPathMatcher("/root"),
Url: "http://my.proxified.api",
UseFullPath: true,
}, request, "")
Expect(request.URL.String()).Should(Equal("http://my.proxified.api/root"))
})
})
Context("when route have option ForwardedHeader", func() {
It("should set request url to upstream url", func() {
req, _ := http.NewRequest("GET", "http://localhost/path", nil)
Expand Down

0 comments on commit a5ebb78

Please sign in to comment.