-
Notifications
You must be signed in to change notification settings - Fork 664
/
http_server.go
207 lines (177 loc) · 6.06 KB
/
http_server.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ghttp
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"net/http"
"net/url"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/ghttp/gresponsewriter"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/grpcutils"
httppb "github.com/ava-labs/avalanchego/proto/pb/http"
responsewriterpb "github.com/ava-labs/avalanchego/proto/pb/http/responsewriter"
)
var (
_ httppb.HTTPServer = (*Server)(nil)
_ http.ResponseWriter = (*ResponseWriter)(nil)
)
// Server is an http.Handler that is managed over RPC.
type Server struct {
httppb.UnsafeHTTPServer
handler http.Handler
}
// NewServer returns an http.Handler instance managed remotely
func NewServer(handler http.Handler) *Server {
return &Server{
handler: handler,
}
}
func (s *Server) Handle(ctx context.Context, req *httppb.HTTPRequest) (*emptypb.Empty, error) {
clientConn, err := grpcutils.Dial(req.ResponseWriter.ServerAddr)
if err != nil {
return nil, err
}
writerHeaders := make(http.Header)
for _, elem := range req.ResponseWriter.Header {
writerHeaders[elem.Key] = elem.Values
}
writer := gresponsewriter.NewClient(writerHeaders, responsewriterpb.NewWriterClient(clientConn))
// create the request with the current context
request, err := http.NewRequestWithContext(
ctx,
req.Request.Method,
req.Request.RequestUri,
bytes.NewBuffer(req.Request.Body),
)
if err != nil {
return nil, err
}
if req.Request.Url != nil {
request.URL = &url.URL{
Scheme: req.Request.Url.Scheme,
Opaque: req.Request.Url.Opaque,
Host: req.Request.Url.Host,
Path: req.Request.Url.Path,
RawPath: req.Request.Url.RawPath,
ForceQuery: req.Request.Url.ForceQuery,
RawQuery: req.Request.Url.RawQuery,
Fragment: req.Request.Url.Fragment,
}
if req.Request.Url.User != nil {
if req.Request.Url.User.PasswordSet {
request.URL.User = url.UserPassword(req.Request.Url.User.Username, req.Request.Url.User.Password)
} else {
request.URL.User = url.User(req.Request.Url.User.Username)
}
}
}
request.Proto = req.Request.Proto
request.ProtoMajor = int(req.Request.ProtoMajor)
request.ProtoMinor = int(req.Request.ProtoMinor)
request.Header = make(http.Header, len(req.Request.Header))
for _, elem := range req.Request.Header {
request.Header[elem.Key] = elem.Values
}
request.ContentLength = req.Request.ContentLength
request.TransferEncoding = req.Request.TransferEncoding
request.Host = req.Request.Host
request.Form = make(url.Values, len(req.Request.Form))
for _, elem := range req.Request.Form {
request.Form[elem.Key] = elem.Values
}
request.PostForm = make(url.Values, len(req.Request.PostForm))
for _, elem := range req.Request.PostForm {
request.PostForm[elem.Key] = elem.Values
}
request.Trailer = make(http.Header)
request.RemoteAddr = req.Request.RemoteAddr
request.RequestURI = req.Request.RequestUri
if req.Request.Tls != nil {
request.TLS = &tls.ConnectionState{
Version: uint16(req.Request.Tls.Version),
HandshakeComplete: req.Request.Tls.HandshakeComplete,
DidResume: req.Request.Tls.DidResume,
CipherSuite: uint16(req.Request.Tls.CipherSuite),
NegotiatedProtocol: req.Request.Tls.NegotiatedProtocol,
NegotiatedProtocolIsMutual: true, // always true per https://pkg.go.dev/crypto/tls#ConnectionState
ServerName: req.Request.Tls.ServerName,
PeerCertificates: make([]*x509.Certificate, len(req.Request.Tls.PeerCertificates.Cert)),
VerifiedChains: make([][]*x509.Certificate, len(req.Request.Tls.VerifiedChains)),
SignedCertificateTimestamps: req.Request.Tls.SignedCertificateTimestamps,
OCSPResponse: req.Request.Tls.OcspResponse,
}
for i, certBytes := range req.Request.Tls.PeerCertificates.Cert {
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
return nil, err
}
request.TLS.PeerCertificates[i] = cert
}
for i, chain := range req.Request.Tls.VerifiedChains {
request.TLS.VerifiedChains[i] = make([]*x509.Certificate, len(chain.Cert))
for j, certBytes := range chain.Cert {
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
return nil, err
}
request.TLS.VerifiedChains[i][j] = cert
}
}
}
s.handler.ServeHTTP(writer, request)
return &emptypb.Empty{}, clientConn.Close()
}
// HandleSimple handles http requests over http2 using a simple request response model.
// Websockets are not supported. Based on https://www.weave.works/blog/turtles-way-http-grpc/
func (s *Server) HandleSimple(ctx context.Context, r *httppb.HandleSimpleHTTPRequest) (*httppb.HandleSimpleHTTPResponse, error) {
req, err := http.NewRequest(r.Method, r.Url, bytes.NewBuffer(r.Body))
if err != nil {
return nil, err
}
grpcutils.MergeHTTPHeader(r.Headers, req.Header)
req = req.WithContext(ctx)
req.RequestURI = r.Url
req.ContentLength = int64(len(r.Body))
w := newResponseWriter()
s.handler.ServeHTTP(w, req)
resp := &httppb.HandleSimpleHTTPResponse{
Code: int32(w.statusCode),
Headers: grpcutils.GetHTTPHeader(w.Header()),
Body: w.body.Bytes(),
}
if w.statusCode == http.StatusInternalServerError {
return nil, grpcutils.GetGRPCErrorFromHTTPResponse(resp)
}
return resp, nil
}
type ResponseWriter struct {
body *bytes.Buffer
header http.Header
statusCode int
}
// newResponseWriter returns very basic implementation of the http.ResponseWriter
func newResponseWriter() *ResponseWriter {
return &ResponseWriter{
body: new(bytes.Buffer),
header: make(http.Header),
statusCode: http.StatusOK,
}
}
func (w *ResponseWriter) Header() http.Header {
return w.header
}
func (w *ResponseWriter) Write(buf []byte) (int, error) {
return w.body.Write(buf)
}
func (w *ResponseWriter) WriteHeader(code int) {
w.statusCode = code
}
func (w *ResponseWriter) StatusCode() int {
return w.statusCode
}
func (w *ResponseWriter) Body() *bytes.Buffer {
return w.body
}