Skip to content

Commit

Permalink
update proxy scheme based on incoming request
Browse files Browse the repository at this point in the history
  • Loading branch information
ins-tykgw committed Feb 10, 2021
1 parent a8c3f2c commit e88591c
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions gateway/handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"net/http/httputil"
"net/textproto"
"net/url"
"runtime/pprof"
"strconv"
"strings"
Expand Down Expand Up @@ -312,9 +311,9 @@ func (s *SuccessHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) *http
log.Debug("Check update_host_header")
if len(s.Spec.Proxy.UpdateHostHeader) > 0 {
//Normalize header
log.Debug("Detected UpdateHostHeader ", s.Spec.Proxy.UpdateHostHeader)
log.Debug("Handler_success - Detected UpdateHostHeader ", s.Spec.Proxy.UpdateHostHeader)
header := textproto.CanonicalMIMEHeaderKey(s.Spec.Proxy.UpdateHostHeader)
log.Debug("CanonicalMIMEHeaderKey form ", s.Spec.Proxy.UpdateHostHeader)
log.Debug("Handler_success - CanonicalMIMEHeaderKey form ", s.Spec.Proxy.UpdateHostHeader)
updateHost, ok := r.Header[header]
if ok {
//Create Reverse proxy
Expand All @@ -323,20 +322,15 @@ func (s *SuccessHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) *http

//Reset the rawquery assuming URLRewrite may have reset the path
if origURL := ctxGetOrigRequestURL(req); origURL != nil {
log.Debug("Original Request URL", origURL.String())
log.Debug("Handler_success - Original Request URL", origURL.String())
req.URL = origURL
}

//update the Host from header and scheme from target spec
log.Debug("Updating upstream host", updateHost[0])

//Set host
req.URL.Host = updateHost[0]

targetUrl, _ := url.Parse(s.Spec.Proxy.TargetURL)

//Set Scheme
switch targetUrl.Scheme {
//set scheme - read it from incoming request - ServeHTTP - closure
switch r.URL.Scheme {
case "http":
req.URL.Scheme = "http"
case "https":
Expand All @@ -346,6 +340,14 @@ func (s *SuccessHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) *http
case "wss":
req.URL.Scheme = "https"
}

log.Debug("Handler_success - request scheme", req.URL.Scheme)
log.Debug("Handler_success - request Host ", req.URL.Host)
log.Debug("Handler_success - request URL ", req.URL)

//update the Host from header and scheme from target spec
// targetUrl, _ := url.Parse(s.Spec.Proxy.TargetURL)

}

proxy := &httputil.ReverseProxy{Director: director}
Expand All @@ -355,6 +357,8 @@ func (s *SuccessHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) *http
proxy.ServeHTTP(w, r)
log.Debug("Done update_host_header proxy")
return nil
} else {
log.Debug("update_host_header header not found")
}
}

Expand Down

0 comments on commit e88591c

Please sign in to comment.