-
Notifications
You must be signed in to change notification settings - Fork 671
/
http_client.go
159 lines (142 loc) · 4.81 KB
/
http_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ghttp
import (
"net/http"
"google.golang.org/grpc"
"github.com/hashicorp/go-plugin"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/ghttp/ghttpproto"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/ghttp/greadcloser"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/ghttp/greadcloser/greadcloserproto"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/ghttp/gresponsewriter"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/ghttp/gresponsewriter/gresponsewriterproto"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/grpcutils"
)
var _ http.Handler = &Client{}
// Client is an http.Handler that talks over RPC.
type Client struct {
client ghttpproto.HTTPClient
broker *plugin.GRPCBroker
}
// NewClient returns an HTTP handler database instance connected to a remote
// HTTP handler instance
func NewClient(client ghttpproto.HTTPClient, broker *plugin.GRPCBroker) *Client {
return &Client{
client: client,
broker: broker,
}
}
func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
closer := grpcutils.ServerCloser{}
defer closer.GracefulStop()
readerID := c.broker.NextId()
go c.broker.AcceptAndServe(readerID, func(opts []grpc.ServerOption) *grpc.Server {
reader := grpc.NewServer(opts...)
closer.Add(reader)
greadcloserproto.RegisterReaderServer(reader, greadcloser.NewServer(r.Body))
return reader
})
writerID := c.broker.NextId()
go c.broker.AcceptAndServe(writerID, func(opts []grpc.ServerOption) *grpc.Server {
writer := grpc.NewServer(opts...)
closer.Add(writer)
gresponsewriterproto.RegisterWriterServer(writer, gresponsewriter.NewServer(w, c.broker))
return writer
})
req := &ghttpproto.HTTPRequest{
ResponseWriter: &ghttpproto.ResponseWriter{
Id: writerID,
Header: make([]*ghttpproto.Element, 0, len(r.Header)),
},
Request: &ghttpproto.Request{
Method: r.Method,
Proto: r.Proto,
ProtoMajor: int32(r.ProtoMajor),
ProtoMinor: int32(r.ProtoMinor),
Header: make([]*ghttpproto.Element, 0, len(r.Header)),
Body: readerID,
ContentLength: r.ContentLength,
TransferEncoding: r.TransferEncoding,
Host: r.Host,
Form: make([]*ghttpproto.Element, 0, len(r.Form)),
PostForm: make([]*ghttpproto.Element, 0, len(r.PostForm)),
RemoteAddr: r.RemoteAddr,
RequestURI: r.RequestURI,
},
}
for key, values := range w.Header() {
req.ResponseWriter.Header = append(req.ResponseWriter.Header, &ghttpproto.Element{
Key: key,
Values: values,
})
}
for key, values := range r.Header {
req.Request.Header = append(req.Request.Header, &ghttpproto.Element{
Key: key,
Values: values,
})
}
for key, values := range r.Form {
req.Request.Form = append(req.Request.Form, &ghttpproto.Element{
Key: key,
Values: values,
})
}
for key, values := range r.PostForm {
req.Request.PostForm = append(req.Request.PostForm, &ghttpproto.Element{
Key: key,
Values: values,
})
}
if r.URL != nil {
req.Request.Url = &ghttpproto.URL{
Scheme: r.URL.Scheme,
Opaque: r.URL.Opaque,
Host: r.URL.Host,
Path: r.URL.Path,
RawPath: r.URL.RawPath,
ForceQuery: r.URL.ForceQuery,
RawQuery: r.URL.RawQuery,
Fragment: r.URL.Fragment,
}
if r.URL.User != nil {
pwd, set := r.URL.User.Password()
req.Request.Url.User = &ghttpproto.Userinfo{
Username: r.URL.User.Username(),
Password: pwd,
PasswordSet: set,
}
}
}
if r.TLS != nil {
req.Request.Tls = &ghttpproto.ConnectionState{
Version: uint32(r.TLS.Version),
HandshakeComplete: r.TLS.HandshakeComplete,
DidResume: r.TLS.DidResume,
CipherSuite: uint32(r.TLS.CipherSuite),
NegotiatedProtocol: r.TLS.NegotiatedProtocol,
NegotiatedProtocolIsMutual: r.TLS.NegotiatedProtocolIsMutual,
ServerName: r.TLS.ServerName,
PeerCertificates: &ghttpproto.Certificates{
Cert: make([][]byte, len(r.TLS.PeerCertificates)),
},
VerifiedChains: make([]*ghttpproto.Certificates, len(r.TLS.VerifiedChains)),
SignedCertificateTimestamps: r.TLS.SignedCertificateTimestamps,
OcspResponse: r.TLS.OCSPResponse,
TlsUnique: r.TLS.TLSUnique,
}
for i, cert := range r.TLS.PeerCertificates {
req.Request.Tls.PeerCertificates.Cert[i] = cert.Raw
}
for i, chain := range r.TLS.VerifiedChains {
req.Request.Tls.VerifiedChains[i] = &ghttpproto.Certificates{
Cert: make([][]byte, len(chain)),
}
for j, cert := range chain {
req.Request.Tls.VerifiedChains[i].Cert[j] = cert.Raw
}
}
}
// TODO: is there a better way to handle this error?
_, _ = c.client.Handle(r.Context(), req)
}