func (f *forwarder) Apply(w http.ResponseWriter, req *http.Request, next filters.Next) error { op := ops.Begin("proxy_http") defer op.End() // Create a copy of the request suitable for our needs reqClone, err := f.cloneRequest(req, req.URL) if err != nil { return op.FailIf(filters.Fail("Error forwarding from %v to %v: %v", req.RemoteAddr, req.Host, err)) } f.Rewriter.Rewrite(reqClone) if log.IsTraceEnabled() { reqStr, _ := httputil.DumpRequest(req, false) log.Tracef("Forwarder Middleware received request:\n%s", reqStr) reqStr2, _ := httputil.DumpRequest(reqClone, false) log.Tracef("Forwarder Middleware forwarding rewritten request:\n%s", reqStr2) } // Forward the request and get a response start := time.Now().UTC() response, err := f.RoundTripper.RoundTrip(reqClone) if err != nil { return op.FailIf(filters.Fail("Error forwarding from %v to %v: %v", req.RemoteAddr, req.Host, err)) } log.Debugf("Round trip: %v, code: %v, duration: %v", reqClone.URL, response.StatusCode, time.Now().UTC().Sub(start)) if log.IsTraceEnabled() { respStr, _ := httputil.DumpResponse(response, true) log.Tracef("Forward Middleware received response:\n%s", respStr) } // Forward the response to the origin copyHeadersForForwarding(w.Header(), response.Header) w.WriteHeader(response.StatusCode) // It became nil in a Co-Advisor test though the doc says it will never be nil if response.Body != nil { buf := buffers.Get() defer buffers.Put(buf) _, err = io.CopyBuffer(w, response.Body, buf) if err != nil { log.Debug(err) } response.Body.Close() } return filters.Stop() }
func (f *commonFilter) Apply(w http.ResponseWriter, req *http.Request, next filters.Next) error { if !f.AllowLocalhost && !f.isException(req.URL.Host) { reqAddr, err := net.ResolveTCPAddr("tcp", req.Host) // If there was an error resolving is probably because it wasn't an address // in the form localhost:port if err == nil { if reqAddr.IP.IsLoopback() { return filters.Fail("%v requested loopback address %v (%v)", req.RemoteAddr, req.Host, reqAddr) } for _, ip := range f.localIPs { if reqAddr.IP.Equal(ip) { return filters.Fail("%v requested local address %v (%v)", req.RemoteAddr, req.Host, reqAddr) } } } } return next() }