Skip to content

Commit

Permalink
Refactor reverse proxy setup and cache handling
Browse files Browse the repository at this point in the history
Simplified reverse proxy configuration and cache request handling by streamlining redundant code. Eliminated unnecessary request cloning for client modification, leading to cleaner and more efficient code maintenance.

Issue: #123
  • Loading branch information
mattmattox committed Mar 4, 2024
1 parent 6959974 commit 7c1f33c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 61 deletions.
54 changes: 3 additions & 51 deletions pkg/proxy/NewReverseProxy.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,19 @@
package proxy

import (
"log"
"net/http"
"net/http/httputil"
"net/url"

"github.com/supporttools/go-web-cache/pkg/cache"
"github.com/supporttools/go-web-cache/pkg/config"
)

// NewReverseProxy creates and configures a reverse proxy.
func NewReverseProxy(targetURL string, cacheManager cache.CacheManager) *httputil.ReverseProxy {
parsedURL, err := url.Parse(targetURL)
if err != nil {
log.Fatalf("Error parsing target URL '%s': %v", targetURL, err)
}
url, _ := url.Parse(targetURL)

log.Printf("Creating reverse proxy to %s", parsedURL.String())

proxy := httputil.NewSingleHostReverseProxy(parsedURL)
proxy.Transport = &Transport{
RoundTripper: http.DefaultTransport,
CacheManager: cacheManager,
}

// Modify proxy.Director for additional debugging and request customization
originalDirector := proxy.Director
proxy.Director = func(req *http.Request) {
log.Printf("Proxying request for %s", req.URL.String())

// Call the original director to preserve default behavior
originalDirector(req)

// Log the request URL to debug
if config.CFG.Debug {
log.Printf("Modified request URL: %s", req.URL.String())
}

// Ensure the request host is correctly set
req.URL.Scheme = "http" // or "https", as appropriate
req.URL.Host = parsedURL.Host
req.Host = parsedURL.Host

// Additional logging to confirm the host and scheme are correctly set
if config.CFG.Debug {
log.Printf("Request URL after modification: %s", req.URL.String())
log.Printf("Request Host after modification: %s", req.Host)
// Debugging: Log the request headers after modification by the director
log.Printf("Request headers after director modifications:")
for name, values := range req.Header {
for _, value := range values {
log.Printf("%s: %s", name, value)
}
}
}

}

// Optionally, customize the Transport to log errors or responses
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
log.Printf("Error handling request for %s: %v", r.URL.String(), err)
}
proxy := httputil.NewSingleHostReverseProxy(url)
proxy.Transport = &Transport{http.DefaultTransport, cacheManager}

return proxy
}
20 changes: 10 additions & 10 deletions pkg/proxy/RoundTrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.RoundTripper.RoundTrip(req)
}

modifiedReq := cloneRequestForClient(req)
log.Printf("Modified request for caching: URL Scheme: %s, Host: %s", modifiedReq.URL.Scheme, modifiedReq.Host)
//modifiedReq := cloneRequestForClient(req)
//log.Printf("Modified request for caching: URL Scheme: %s, Host: %s", modifiedReq.URL.Scheme, modifiedReq.Host)

if security.HasWordPressLoginCookie(req) {
log.Println("Request has WordPress login cookie, bypassing cache")
Expand Down Expand Up @@ -94,11 +94,11 @@ func cacheResponse(t *Transport, cacheKey string, resp *http.Response, body []by
return false
}

func cloneRequestForClient(req *http.Request) *http.Request {
urlCopy := *req.URL
clonedReq := req.WithContext(req.Context())
clonedReq.URL = &urlCopy
clonedReq.Host = req.Host
log.Printf("Request cloned for modification: %s", clonedReq.URL.String())
return clonedReq
}
// func cloneRequestForClient(req *http.Request) *http.Request {
// urlCopy := *req.URL
// clonedReq := req.WithContext(req.Context())
// clonedReq.URL = &urlCopy
// clonedReq.Host = req.Host
// log.Printf("Request cloned for modification: %s", clonedReq.URL.String())
// return clonedReq
// }

0 comments on commit 7c1f33c

Please sign in to comment.