// RoundTrip implements the http.RoundTripper interface
517123
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
	// Add reverse proxy headers.
	forwardedURI := path.Join(t.PathPrepend, req.URL.Path)
	if strings.HasSuffix(req.URL.Path, "/") {
		forwardedURI = forwardedURI + "/"
	}
	req.Header.Set("X-Forwarded-Uri", forwardedURI)
	if len(t.Host) > 0 {
		req.Header.Set("X-Forwarded-Host", t.Host)
	}
	if len(t.Scheme) > 0 {
		req.Header.Set("X-Forwarded-Proto", t.Scheme)
	}

	rt := t.RoundTripper
	if rt == nil {
		rt = http.DefaultTransport
	}
	resp, err := rt.RoundTrip(req)

	if err != nil {
		message := fmt.Sprintf("Error: '%s'\nTrying to reach: '%v'", err.Error(), req.URL.String())
		resp = &http.Response{
			StatusCode: http.StatusServiceUnavailable,
			Body:       ioutil.NopCloser(strings.NewReader(message)),
		}
		return resp, nil
	}

	if redirect := resp.Header.Get("Location"); redirect != "" {
		resp.Header.Set("Location", t.rewriteURL(redirect, req.URL, req.Host))
		return resp, nil
	}

	cType := resp.Header.Get("Content-Type")
	cType = strings.TrimSpace(strings.SplitN(cType, ";", 2)[0])
	if cType != "text/html" {
		// Do nothing, simply pass through
		return resp, nil
	}

	return t.rewriteResponse(req, resp)
}
647924
func (binder FastBinder) RoundTrip(stdreq *http.Request) (*http.Response, error) {
	fastreq := std2fast(stdreq)

	var conn net.Conn
	if stdreq.URL != nil && stdreq.URL.Scheme == "https" && binder.TLS != nil {
		conn = connTLS{state: binder.TLS}
	} else {
		conn = connNonTLS{}
	}

	ctx := fasthttp.RequestCtx{}
	ctx.Init2(conn, fastLogger{}, true)
	fastreq.CopyTo(&ctx.Request)

	if stdreq.ContentLength >= 0 {
		ctx.Request.Header.SetContentLength(int(stdreq.ContentLength))
	} else {
		ctx.Request.Header.Add("Transfer-Encoding", "chunked")
	}

	if stdreq.Body != nil {
		b, err := ioutil.ReadAll(stdreq.Body)
		if err == nil {
			ctx.Request.SetBody(b)
		}
	}

	binder.Handler(&ctx)

	return fast2std(stdreq, &ctx.Response), nil
}
786195
func (t RoundTripTrace) RoundTrip(req *http.Request) (res *http.Response, err error) {
	timeStamp := time.Now()

	if t.Transport == nil {
		return nil, errors.New("Invalid Argument")
	}

	res, err = t.Transport.RoundTrip(req)
	if err != nil {
		return res, err
	}

	if t.Trace != nil {
		err = t.Trace.Request(req)
		if err != nil {
			return nil, err
		}

		err = t.Trace.Response(res)
		if err != nil {
			return nil, err
		}
		console.Debugln("Response Time: ", time.Since(timeStamp).String()+"\n")
	}
	return res, err
}
337517
func RoundTripper(r http.RoundTripper) ClientOpt {
	return func(c *Client) {
		c.HTTPClient.Transport = r
	}
}
46228
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
	if t.Source == nil {
		return nil, errors.New("oauth2: Transport's Source is nil")
	}
	token, err := t.Source.Token()
	if err != nil {
		return nil, err
	}

	req2 := cloneRequest(req) // per RoundTripper contract
	token.SetAuthHeader(req2)
	t.setModReq(req, req2)
	res, err := t.base().RoundTrip(req2)
	if err != nil {
		t.setModReq(req, nil)
		return nil, err
	}
	res.Body = &onEOFReader{
		rc: res.Body,
		fn: func() { t.setModReq(req, nil) },
	}
	return res, nil
}
398639
func (conn *Conn) RoundTripSingleResponse(req []byte) (resp []byte, err error) {
	if err = conn.SendMessage(req); err != nil {
		return nil, err
	}

	if _, err = conn.ReadStatus(string(req)); err != nil {
		return nil, err
	}

	return conn.ReadMessage()
}
901793
func (limiters *RateLimiters) RoundTripper(rt http.RoundTripper, host string) http.RoundTripper {
	limiters.mu.Lock()
	defer limiters.mu.Unlock()

	if limiters.perHost == nil {
		limiters.perHost = map[string]*rate.Limiter{}
	}
	if _, ok := limiters.perHost[host]; !ok {
		rl := rate.NewLimiter(rate.Limit(limiters.RPS), limiters.Burst)
		limiters.perHost[host] = rl
	}
	var reduceOnce sync.Once
	return &RoundTripRateLimiter{
		rl: limiters.perHost[host],
		tx: rt,
		slowDown: func() {
			reduceOnce.Do(func() { limiters.BackOff(host) })
		},
	}
}
669675
func (t *dcosRoundtripper) RoundTrip(req *http.Request) (*http.Response, error) {
	// Set `User-Agent` header, defaulting to `dcos-go`
	userAgent := "dcos-go"
	if t.userAgent != "" {
		userAgent = t.userAgent
	}
	req.Header.Set("User-Agent", userAgent)

	// helper function to update `Authorization` header.
	addAuthToken := func() {
		if token := t.CurrentToken(); token != "" {
			req.Header.Set("Authorization", "token="+token)
		}
	}

	var (
		resp *http.Response
		err  error
	)

	// try first time
	addAuthToken()
	resp, err = t.transport.RoundTrip(req)
	if err != nil {
		return resp, err
	}

	// if request returned 401 retry one more time.
	if resp.StatusCode == http.StatusUnauthorized {
		if err := t.GenerateToken(); err != nil {
			return resp, err
		}

		addAuthToken()
		resp, err = t.transport.RoundTrip(req)
		if err != nil {
			return nil, err
		}
	}

	return resp, nil
}
1131327
func (s *SpdyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	header := utilnet.CloneHeader(req.Header)
	header.Add(httpstream.HeaderConnection, httpstream.HeaderUpgrade)
	header.Add(httpstream.HeaderUpgrade, HeaderSpdy31)

	var (
		conn        net.Conn
		rawResponse []byte
		err         error
	)

	if s.followRedirects {
		conn, rawResponse, err = utilnet.ConnectWithRedirects(req.Method, req.URL, header, req.Body, s, s.requireSameHostRedirects)
	} else {
		clone := utilnet.CloneRequest(req)
		clone.Header = header
		conn, err = s.Dial(clone)
	}
	if err != nil {
		return nil, err
	}

	responseReader := bufio.NewReader(
		io.MultiReader(
			bytes.NewBuffer(rawResponse),
			conn,
		),
	)

	resp, err := http.ReadResponse(responseReader, nil)
	if err != nil {
		if conn != nil {
			conn.Close()
		}
		return nil, err
	}

	s.conn = conn

	return resp, nil
}
1196362
func (t *UnauthenticatedRateLimitedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	if t.ClientID == "" {
		return nil, errors.New("t.ClientID is empty")
	}
	if t.ClientSecret == "" {
		return nil, errors.New("t.ClientSecret is empty")
	}

	// To set extra querystring params, we must make a copy of the Request so
	// that we don't modify the Request we were given. This is required by the
	// specification of http.RoundTripper.
	//
	// Since we are going to modify only req.URL here, we only need a deep copy
	// of req.URL.
	req2 := new(http.Request)
	*req2 = *req
	req2.URL = new(url.URL)
	*req2.URL = *req.URL

	q := req2.URL.Query()
	q.Set("client_id", t.ClientID)
	q.Set("client_secret", t.ClientSecret)
	req2.URL.RawQuery = q.Encode()

	// Make the HTTP request.
	return t.transport().RoundTrip(req2)
}
369596
func (a AWSTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	awsauth.Sign4(req, a.Credentials)
	return a.transport.RoundTrip(req)
}
1055973
func (r *RoundTripper) RoundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Response, error) {
	if req.URL == nil {
		closeRequestBody(req)
		return nil, errors.New("http3: nil Request.URL")
	}
	if req.URL.Host == "" {
		closeRequestBody(req)
		return nil, errors.New("http3: no Host in request URL")
	}
	if req.Header == nil {
		closeRequestBody(req)
		return nil, errors.New("http3: nil Request.Header")
	}

	if req.URL.Scheme == "https" {
		for k, vv := range req.Header {
			if !httpguts.ValidHeaderFieldName(k) {
				return nil, fmt.Errorf("http3: invalid http header field name %q", k)
			}
			for _, v := range vv {
				if !httpguts.ValidHeaderFieldValue(v) {
					return nil, fmt.Errorf("http3: invalid http header field value %q for key %v", v, k)
				}
			}
		}
	} else {
		closeRequestBody(req)
		return nil, fmt.Errorf("http3: unsupported protocol scheme: %s", req.URL.Scheme)
	}

	if req.Method != "" && !validMethod(req.Method) {
		closeRequestBody(req)
		return nil, fmt.Errorf("http3: invalid method %q", req.Method)
	}

	hostname := authorityAddr("https", hostnameFromRequest(req))
	cl, err := r.getClient(hostname, opt.OnlyCachedConn)
	if err != nil {
		return nil, err
	}
	return cl.RoundTrip(req)
}
636966
func WithRoundTripper(r http.RoundTripper) HTTPClientOption {
	return func(client *http.Client) {
		client.Transport = r
	}
}
390032
func (c *contextAwareUrlFetch) RoundTrip(r *http.Request) (*http.Response, error) {
	ctx := r.Context()
	// We assume context.Background() always returns exact same object.
	if ctx == background {
		ctx = c.c
	}
	return urlfetch.Get(ctx).RoundTrip(r)
}
554960
func Roundtrip(addr string) (string, error) {
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return "", err
	}
	defer conn.Close()
	return RoundtripWithConn(conn)
}
1267228
func (rt *FTPRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
	if request.URL.Scheme != "ftp" {
		return nil, fmt.Errorf("only ftp protocol is supported, got %s", request.Proto)
	}

	if request.Method != "GET" {
		return nil, fmt.Errorf("only GET method is supported, got %s", request.Method)
	}

	hostport := request.URL.Host
	if strings.Index(hostport, ":") == -1 {
		hostport = hostport + ":21"
	}

	connection, err := rt.getConnection(hostport)
	if err != nil {
		return nil, err
	}

	var body io.ReadCloser
	body, err = connection.Retr(request.URL.Path)

	if err != nil {
		if te, ok := err.(*textproto.Error); ok {
			rt.putConnection(hostport, connection)

			if te.Code == ftp.StatusFileUnavailable {
				return &http.Response{
					Status:     "404 Not Found",
					StatusCode: 404,
					Proto:      "FTP/1.0",
					ProtoMajor: 1,
					ProtoMinor: 0,
					Request:    request,
				}, nil
			}
		}

		return nil, err
	}

	return &http.Response{
		Status:     "200 OK",
		StatusCode: 200,
		Proto:      "FTP/1.0",
		ProtoMajor: 1,
		ProtoMinor: 0,
		Body: &readCloserWrapper{
			body:     body,
			rt:       rt,
			hostport: hostport,
			conn:     connection,
		},
		ContentLength: -1,
		Request:       request,
	}, nil
}
1223722
func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	resp, err := t.Transport.RoundTrip(req)
	if err != nil {
		return resp, err
	}

	authService, err := isTokenDemand(resp)
	if err != nil {
		resp.Body.Close()
		return nil, err
	}

	if authService == nil {
		return resp, nil
	}

	resp.Body.Close()

	return t.authAndRetry(authService, req)
}