Skip to content
Permalink
Browse files

(proxy): Use the Proxy struct instance and methods

  • Loading branch information
erbesharat committed Aug 17, 2019
1 parent 61950fb commit 62e607afdc9d623b019847e45f457ef8acba3b9a
Showing with 27 additions and 8 deletions.
  1. +25 −7 proxy.go
  2. +2 −1 txtdirect.go
@@ -33,8 +33,26 @@ type ProxyResponse struct {
status int
}

func proxyRequest(w http.ResponseWriter, r *http.Request, rec record, c Config, code int) error {
to, _, err := getBaseTarget(rec, r)
// Proxy contains the data that are needed to proxy the request
type Proxy struct {
rw http.ResponseWriter
req *http.Request
c Config
rec record
}

func NewProxy(w http.ResponseWriter, r *http.Request, rec record, c Config) *Proxy {
return &Proxy{
rw: w,
req: r,
rec: rec,
c: c,
}
}

// Proxy proxies the request to the endpoint defined in the record
func (p *Proxy) Proxy() error {
to, _, err := getBaseTarget(p.rec, p.req)
if err != nil {
return err
}
@@ -45,25 +63,25 @@ func proxyRequest(w http.ResponseWriter, r *http.Request, rec record, c Config,
reverseProxy := proxy.NewSingleHostReverseProxy(u, "", proxyKeepalive, proxyTimeout, fallbackDelay)

tmpResponse := ProxyResponse{headers: make(http.Header)}
reverseProxy.ServeHTTP(&tmpResponse, r, nil)
reverseProxy.ServeHTTP(&tmpResponse, p.req, nil)

// Decompress the body based on "Content-Encoding" header and write to a writer buffer
if err := tmpResponse.WriteBody(); err != nil {
return fmt.Errorf("[txtdirect]: Couldn't write the response body: %s", err.Error())
}

// Replace the URL hosts with the request's host
if err := tmpResponse.ReplaceBody(u.Scheme, u.Host, r.Host); err != nil {
if err := tmpResponse.ReplaceBody(u.Scheme, u.Host, p.req.Host); err != nil {
return fmt.Errorf("[txtdirect]: Couldn't replace urls inside the response body: %s", err.Error())
}

copyHeader(w.Header(), tmpResponse.Header())
copyHeader(p.rw.Header(), tmpResponse.Header())

// Write the status from the temporary ResponseWriter to the main ResponseWriter
w.WriteHeader(tmpResponse.status)
p.rw.WriteHeader(tmpResponse.status)

// Write the final response from the temporary ResponseWriter to the main ResponseWriter
if _, err := w.Write(tmpResponse.Body()); err != nil {
if _, err := p.rw.Write(tmpResponse.Body()); err != nil {
return fmt.Errorf("[txtdirect]: Couldn't write the temporary response to main response body: %s", err.Error())
}
return nil
@@ -202,7 +202,8 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
RequestsCountBasedOnType.WithLabelValues(host, "proxy").Add(1)
log.Printf("[txtdirect]: %s > %s", rec.From, rec.To)

if err = proxyRequest(w, r, rec, c, rec.Code); err != nil {
proxy := NewProxy(w, r, rec, c)
if err = proxy.Proxy(); err != nil {
log.Print("Fallback is triggered because an error has occurred: ", err)
fallback(w, r, "to", rec.Code, c)
}

0 comments on commit 62e607a

Please sign in to comment.
You can’t perform that action at this time.