forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
writer_client.go
133 lines (112 loc) · 3.22 KB
/
writer_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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gresponsewriter
import (
"bufio"
"context"
"net"
"net/http"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/MetalBlockchain/metalgo/vms/rpcchainvm/ghttp/gconn"
"github.com/MetalBlockchain/metalgo/vms/rpcchainvm/ghttp/greader"
"github.com/MetalBlockchain/metalgo/vms/rpcchainvm/ghttp/gwriter"
"github.com/MetalBlockchain/metalgo/vms/rpcchainvm/grpcutils"
responsewriterpb "github.com/MetalBlockchain/metalgo/proto/pb/http/responsewriter"
readerpb "github.com/MetalBlockchain/metalgo/proto/pb/io/reader"
writerpb "github.com/MetalBlockchain/metalgo/proto/pb/io/writer"
connpb "github.com/MetalBlockchain/metalgo/proto/pb/net/conn"
)
var (
_ http.ResponseWriter = (*Client)(nil)
_ http.Flusher = (*Client)(nil)
_ http.Hijacker = (*Client)(nil)
)
// Client is an http.ResponseWriter that talks over RPC.
type Client struct {
client responsewriterpb.WriterClient
header http.Header
}
// NewClient returns a response writer connected to a remote response writer
func NewClient(header http.Header, client responsewriterpb.WriterClient) *Client {
return &Client{
client: client,
header: header,
}
}
func (c *Client) Header() http.Header {
return c.header
}
func (c *Client) Write(payload []byte) (int, error) {
req := &responsewriterpb.WriteRequest{
Headers: make([]*responsewriterpb.Header, 0, len(c.header)),
Payload: payload,
}
for key, values := range c.header {
req.Headers = append(req.Headers, &responsewriterpb.Header{
Key: key,
Values: values,
})
}
resp, err := c.client.Write(context.Background(), req)
if err != nil {
return 0, err
}
return int(resp.Written), nil
}
func (c *Client) WriteHeader(statusCode int) {
req := &responsewriterpb.WriteHeaderRequest{
Headers: make([]*responsewriterpb.Header, 0, len(c.header)),
StatusCode: int32(statusCode),
}
for key, values := range c.header {
req.Headers = append(req.Headers, &responsewriterpb.Header{
Key: key,
Values: values,
})
}
// TODO: Is there a way to handle the error here?
_, _ = c.client.WriteHeader(context.Background(), req)
}
func (c *Client) Flush() {
// TODO: is there a way to handle the error here?
_, _ = c.client.Flush(context.Background(), &emptypb.Empty{})
}
type addr struct {
network string
str string
}
func (a *addr) Network() string {
return a.network
}
func (a *addr) String() string {
return a.str
}
func (c *Client) Hijack() (net.Conn, *bufio.ReadWriter, error) {
resp, err := c.client.Hijack(context.Background(), &emptypb.Empty{})
if err != nil {
return nil, nil, err
}
clientConn, err := grpcutils.Dial(resp.ServerAddr)
if err != nil {
return nil, nil, err
}
conn := gconn.NewClient(
connpb.NewConnClient(clientConn),
&addr{
network: resp.LocalNetwork,
str: resp.LocalString,
},
&addr{
network: resp.RemoteNetwork,
str: resp.RemoteString,
},
clientConn,
)
reader := greader.NewClient(readerpb.NewReaderClient(clientConn))
writer := gwriter.NewClient(writerpb.NewWriterClient(clientConn))
readWriter := bufio.NewReadWriter(
bufio.NewReader(reader),
bufio.NewWriter(writer),
)
return conn, readWriter, nil
}