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 *forwarder) Apply(w http.ResponseWriter, req *http.Request, next filters.Next) error { if !f.Force { persistentAllowed, _ := strconv.ParseBool(req.Header.Get(XLanternPersistent)) if !persistentAllowed { return next() } } op := ops.Begin("proxy_http") defer op.End() err := f.intercept(w, req) if err != nil { log.Error(op.FailIf(err)) } return filters.Stop() }
func (f *httpConnectHandler) Apply(w http.ResponseWriter, req *http.Request, next filters.Next) error { if req.Method != "CONNECT" { return next() } if log.IsTraceEnabled() { reqStr, _ := httputil.DumpRequest(req, true) log.Tracef("httpConnectHandler Middleware received request:\n%s", reqStr) } op := ops.Begin("proxy_https") defer op.End() if f.portAllowed(op, w, req) { err := f.intercept(w, req) if err != nil { log.Error(op.FailIf(err)) } } return filters.Stop() }
func fail(resp http.ResponseWriter, err error) error { resp.WriteHeader(http.StatusForbidden) fmt.Fprint(resp, err) return filters.Stop() }