Newer
Older
package http
import (
"context"
"encoding/base64"
"errors"
"io"
"net/http"
"net/url"
"strings"
"gfx.cafe/open/jrpc/pkg/jsonrpc"
"gfx.cafe/open/jrpc/pkg/serverutil"
)
var _ jsonrpc.ReaderWriter = (*HttpCodec)(nil)
type HttpCodec struct {
ctx context.Context
cn context.CancelFunc
r *http.Request
w http.ResponseWriter
i jsonrpc.PeerInfo
f http.Flusher
msgs *serverutil.Bundle
}
func NewCodec(w http.ResponseWriter, r *http.Request) (jsonrpc.ReaderWriter, error) {
return NewGetCodec(w, r), nil
case http.MethodPost:
return NewPostCodec(w, r)
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
default:
http.Error(w, "method not supported", http.StatusMethodNotAllowed)
return nil, errors.New("method not allowed")
}
}
func NewGetCodec(w http.ResponseWriter, r *http.Request) *HttpCodec {
c := &HttpCodec{
r: r,
w: w,
i: jsonrpc.PeerInfo{
Transport: "http",
RemoteAddr: r.RemoteAddr,
HTTP: r.Clone(r.Context()),
},
}
c.ctx, c.cn = context.WithCancel(r.Context())
flusher, ok := w.(http.Flusher)
if ok {
c.f = flusher
}
method_up := r.URL.Query().Get("method")
if method_up == "" {
method_up = strings.TrimPrefix(r.URL.Path, "/")
}
params, _ := url.QueryUnescape(r.URL.Query().Get("params"))
var param []byte
// try to read params as base64
if pb, err := base64.URLEncoding.DecodeString(params); err == nil {
param = pb
} else {
// otherwise just take them raw
param = []byte(params)
}
id := r.URL.Query().Get("id")
if id == "" {
id = "1"
}
c.msgs = &serverutil.Bundle{
Messages: []*jsonrpc.Message{{
ID: jsonrpc.NewId(id),
Method: method_up,
Params: param,
}},
Batch: false,
}
return c
}
func NewPostCodec(w http.ResponseWriter, r *http.Request) (*HttpCodec, error) {
c := &HttpCodec{
r: r,
w: w,
i: jsonrpc.PeerInfo{
Transport: "http",
RemoteAddr: r.RemoteAddr,
HTTP: r.Clone(r.Context()),
},
}
c.ctx, c.cn = context.WithCancel(r.Context())
flusher, ok := w.(http.Flusher)
if ok {
c.f = flusher
}
data, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
c.msgs = serverutil.ParseBundle(data)
pathMethod := strings.TrimPrefix(r.URL.Path, "/")
for _, v := range c.msgs.Messages {
if v != nil {
if v.Method == "" {
v.Method = pathMethod
}
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
}
}
return c, nil
}
func NewJrpcCodec(w http.ResponseWriter, r *http.Request) (*HttpCodec, error) {
c := &HttpCodec{
r: r,
w: w,
i: jsonrpc.PeerInfo{
Transport: "http",
RemoteAddr: r.RemoteAddr,
HTTP: r.Clone(r.Context()),
},
}
c.ctx, c.cn = context.WithCancel(r.Context())
flusher, ok := w.(http.Flusher)
if ok {
c.f = flusher
}
data, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
method := strings.TrimPrefix(r.URL.Path, "/")
id := r.Header.Get("id")
if id == "" {
id = "none"
}
c.msgs = &serverutil.Bundle{
Messages: []*jsonrpc.Message{{
ID: jsonrpc.NewId(id),
Method: method,
Params: data,
}},
Batch: false,
}
return c, nil
}
// gets the peer info
func (c *HttpCodec) PeerInfo() jsonrpc.PeerInfo {
return c.i
}
func (c *HttpCodec) ReadBatch(ctx context.Context) ([]*jsonrpc.Message, bool, error) {
if c.msgs == nil {
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
208
209
210
return c.msgs.Messages, c.msgs.Batch, nil
}
// closes the connection
func (c *HttpCodec) Write(p []byte) (n int, err error) {
return c.w.Write(p)
}
func (c *HttpCodec) Flush() error {
c.w.Write([]byte{'\n'})
if c.f != nil {
c.f.Flush()
}
return nil
}
func (c *HttpCodec) Close() error {
if c.f != nil {
c.f.Flush()
}
c.cn()
return nil
}
// Closed returns a channel which is closed when the connection is closed.
func (c *HttpCodec) Closed() <-chan struct{} {
return c.ctx.Done()
}
// RemoteAddr returns the peer address of the connection.
func (c *HttpCodec) RemoteAddr() string {
return c.r.RemoteAddr
}