* * Copyright 2017 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http:www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *

package grpc

import (
	
	
	
	
	
	
	
	
	
	
)

const proxyAuthHeaderKey = "Proxy-Authorization"

errDisabled indicates that proxy is disabled for the address.
The following variable will be overwritten in the tests.
	httpProxyFromEnvironment = http.ProxyFromEnvironment
)

func ( context.Context,  string) (*url.URL, error) {
	 := &http.Request{
		URL: &url.URL{
			Scheme: "https",
			Host:   ,
		},
	}
	,  := httpProxyFromEnvironment()
	if  != nil {
		return nil, 
	}
	if  == nil {
		return nil, errDisabled
	}
	return , nil
}
To read a response from a net.Conn, http.ReadResponse() takes a bufio.Reader. It's possible that this reader reads more than what's need for the response and stores those bytes in the buffer. bufConn wraps the original net.Conn and the bufio.Reader to make sure we don't lose the bytes in the buffer.
type bufConn struct {
	net.Conn
	r io.Reader
}

func ( *bufConn) ( []byte) (int, error) {
	return .r.Read()
}

func (,  string) string {
	 :=  + ":" + 
	return base64.StdEncoding.EncodeToString([]byte())
}

func ( context.Context,  net.Conn,  string,  *url.URL) ( net.Conn,  error) {
	defer func() {
		if  != nil {
			.Close()
		}
	}()

	 := &http.Request{
		Method: http.MethodConnect,
		URL:    &url.URL{Host: },
		Header: map[string][]string{"User-Agent": {grpcUA}},
	}
	if  := .User;  != nil {
		 := .Username()
		,  := .Password()
		.Header.Add(proxyAuthHeaderKey, "Basic "+basicAuth(, ))
	}

	if  := sendHTTPRequest(, , );  != nil {
		return nil, fmt.Errorf("failed to write the HTTP request: %v", )
	}

	 := bufio.NewReader()
	,  := http.ReadResponse(, )
	if  != nil {
		return nil, fmt.Errorf("reading server HTTP response: %v", )
	}
	defer .Body.Close()
	if .StatusCode != http.StatusOK {
		,  := httputil.DumpResponse(, true)
		if  != nil {
			return nil, fmt.Errorf("failed to do connect handshake, status code: %s", .Status)
		}
		return nil, fmt.Errorf("failed to do connect handshake, response: %q", )
	}

	return &bufConn{Conn: , r: }, nil
}
newProxyDialer returns a dialer that connects to proxy first if necessary. The returned dialer checks if a proxy is necessary, dial to the proxy with the provided dialer, does HTTP CONNECT handshake and returns the connection.
func ( func(context.Context, string) (net.Conn, error)) func(context.Context, string) (net.Conn, error) {
	return func( context.Context,  string) ( net.Conn,  error) {
		var  string
		,  := mapAddress(, )
		if  != nil {
			if  != errDisabled {
				return nil, 
			}
			 = 
		} else {
			 = .Host
		}

		,  = (, )
		if  != nil {
			return
		}
proxy is disabled if proxyURL is nil.
			,  = doHTTPConnectHandshake(, , , )
		}
		return
	}
}

func ( context.Context,  *http.Request,  net.Conn) error {
	 = .WithContext()
	if  := .Write();  != nil {
		return fmt.Errorf("failed to write the HTTP request: %v", )
	}
	return nil