Пример #1
0
func (f *Forwarder) copyRequest(req *http.Request, u *url.URL) *http.Request {
	outReq := new(http.Request)
	*outReq = *req // includes shallow copies of maps, but we handle this below

	outReq.URL = utils.CopyURL(req.URL)

	outReq.RequestURI = ""

	outReq.URL.Scheme = u.Scheme
	outReq.URL.Host = u.Host
	outReq.URL.Opaque = req.RequestURI
	// raw query is already included in RequestURI, so ignore it to avoid dupes
	outReq.URL.RawQuery = ""
	// Do not pass client Host header unless optsetter PassHostHeader is set.
	if f.passHost != true {
		outReq.Host = u.Host
	}
	outReq.Proto = "HTTP/1.1"
	outReq.ProtoMajor = 1
	outReq.ProtoMinor = 1

	// Overwrite close flag so we can keep persistent connection for the backend servers
	outReq.Close = false

	outReq.Header = make(http.Header)
	utils.CopyHeaders(outReq.Header, req.Header)

	if f.rewriter != nil {
		f.rewriter.Rewrite(outReq)
	}
	return outReq
}
Пример #2
0
func (f *Forwarder) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	start := time.Now().UTC()
	res, err := f.httpClient.Do(f.copyRequest(req, req.URL))
	if err != nil {
		f.log.Errorf("Error forwarding to %v, err: %v", req.URL, err)
		f.errHandler.ServeHTTP(w, req, err)
		return
	}

	utils.CopyHeaders(w.Header(), res.Header)
	w.WriteHeader(res.StatusCode)
	written, _ := io.Copy(w, res.Body)
	contentLength := strconv.FormatInt(written, 10)
	if written != 0 {
		w.Header().Set(ContentLength, contentLength)
	}

	remoteIp := req.RemoteAddr[:strings.Index(req.RemoteAddr, ":")]

	if req.TLS != nil {
		f.log.Infof("%v , %v, method: %v , code: %v, duration: %v tls:version: %x, tls:resume:%t, tls:csuite:%x, tls:server:%v",
			req.URL, req.RequestURI, req.Method, res.StatusCode, time.Now().UTC().Sub(start),
			req.TLS.Version,
			req.TLS.DidResume,
			req.TLS.CipherSuite,
			req.TLS.ServerName)
	} else {
		f.log.Infof("%v \"%v %v %v\" %d %v %s %v %v",
			remoteIp, req.Method, req.RequestURI, req.Proto, res.StatusCode, contentLength,
			req.Header["User-Agent"][0],
			req.URL.Host,
			time.Now().UTC().Sub(start))
	}

	res.Body.Close()
}