MCPcopy
hub / github.com/grpc/grpc-go / doHTTPConnectHandshake

Function doHTTPConnectHandshake

internal/transport/proxy.go:57–99  ·  view source on GitHub ↗
(ctx context.Context, conn net.Conn, grpcUA string, opts proxyattributes.Options)

Source from the content-addressed store, hash-verified

55}
56
57func doHTTPConnectHandshake(ctx context.Context, conn net.Conn, grpcUA string, opts proxyattributes.Options) (_ net.Conn, err error) {
58 defer func() {
59 if err != nil {
60 conn.Close()
61 }
62 }()
63
64 req := &http.Request{
65 Method: http.MethodConnect,
66 URL: &url.URL{Host: opts.ConnectAddr},
67 Header: map[string][]string{"User-Agent": {grpcUA}},
68 }
69 if user := opts.User; user != nil {
70 u := user.Username()
71 p, _ := user.Password()
72 req.Header.Add(proxyAuthHeaderKey, "Basic "+basicAuth(u, p))
73 }
74 if err := sendHTTPRequest(ctx, req, conn); err != nil {
75 return nil, fmt.Errorf("failed to write the HTTP request: %v", err)
76 }
77
78 r := bufio.NewReader(conn)
79 resp, err := http.ReadResponse(r, req)
80 if err != nil {
81 return nil, fmt.Errorf("reading server HTTP response: %v", err)
82 }
83 defer resp.Body.Close()
84 if resp.StatusCode != http.StatusOK {
85 dump, err := httputil.DumpResponse(resp, true)
86 if err != nil {
87 return nil, fmt.Errorf("failed to do connect handshake, status code: %s", resp.Status)
88 }
89 return nil, fmt.Errorf("failed to do connect handshake, response: %q", dump)
90 }
91 // The buffer could contain extra bytes from the target server, so we can't
92 // discard it. However, in many cases where the server waits for the client
93 // to send the first message (e.g. when TLS is being used), the buffer will
94 // be empty, so we can avoid the overhead of reading through this buffer.
95 if r.Buffered() != 0 {
96 return &bufConn{Conn: conn, r: r}, nil
97 }
98 return conn, nil
99}
100
101// proxyDial establishes a TCP connection to the specified address and performs an HTTP CONNECT handshake.
102func proxyDial(ctx context.Context, addr resolver.Address, grpcUA string, opts proxyattributes.Options) (net.Conn, error) {

Callers 1

proxyDialFunction · 0.85

Calls 5

basicAuthFunction · 0.85
sendHTTPRequestFunction · 0.85
CloseMethod · 0.65
AddMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected