/
httppbthrift_codec.go
171 lines (152 loc) · 4.37 KB
/
httppbthrift_codec.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
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package generic
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"sync/atomic"
"github.com/jhump/protoreflect/desc"
"github.com/cloudwego/kitex/pkg/generic/descriptor"
"github.com/cloudwego/kitex/pkg/generic/proto"
"github.com/cloudwego/kitex/pkg/generic/thrift"
"github.com/cloudwego/kitex/pkg/remote"
"github.com/cloudwego/kitex/pkg/remote/codec"
"github.com/cloudwego/kitex/pkg/serviceinfo"
)
type httpPbThriftCodec struct {
svcDsc atomic.Value // *idl
pbSvcDsc atomic.Value // *pbIdl
provider DescriptorProvider
pbProvider PbDescriptorProvider
codec remote.PayloadCodec
}
func newHTTPPbThriftCodec(p DescriptorProvider, pbp PbDescriptorProvider, codec remote.PayloadCodec) (*httpPbThriftCodec, error) {
svc := <-p.Provide()
pbSvc := <-pbp.Provide()
c := &httpPbThriftCodec{codec: codec, provider: p, pbProvider: pbp}
c.svcDsc.Store(svc)
c.pbSvcDsc.Store(pbSvc)
go c.update()
return c, nil
}
func (c *httpPbThriftCodec) update() {
for {
svc, ok := <-c.provider.Provide()
if !ok {
return
}
pbSvc, ok := <-c.pbProvider.Provide()
if !ok {
return
}
c.svcDsc.Store(svc)
c.pbSvcDsc.Store(pbSvc)
}
}
func (c *httpPbThriftCodec) getMethod(req interface{}) (*Method, error) {
svcDsc, ok := c.svcDsc.Load().(*descriptor.ServiceDescriptor)
if !ok {
return nil, errors.New("get method name failed, no ServiceDescriptor")
}
r, ok := req.(*HTTPRequest)
if !ok {
return nil, errors.New("req is invalid, need descriptor.HTTPRequest")
}
function, err := svcDsc.Router.Lookup(r)
if err != nil {
return nil, err
}
return &Method{function.Name, function.Oneway}, nil
}
func (c *httpPbThriftCodec) Marshal(ctx context.Context, msg remote.Message, out remote.ByteBuffer) error {
svcDsc, ok := c.svcDsc.Load().(*descriptor.ServiceDescriptor)
if !ok {
return fmt.Errorf("get parser ServiceDescriptor failed")
}
pbSvcDsc, ok := c.pbSvcDsc.Load().(*desc.ServiceDescriptor)
if !ok {
return fmt.Errorf("get parser PbServiceDescriptor failed")
}
inner := thrift.NewWriteHTTPPbRequest(svcDsc, pbSvcDsc)
msg.Data().(WithCodec).SetCodec(inner)
return c.codec.Marshal(ctx, msg, out)
}
func (c *httpPbThriftCodec) Unmarshal(ctx context.Context, msg remote.Message, in remote.ByteBuffer) error {
if err := codec.NewDataIfNeeded(serviceinfo.GenericMethod, msg); err != nil {
return err
}
svcDsc, ok := c.svcDsc.Load().(*descriptor.ServiceDescriptor)
if !ok {
return fmt.Errorf("get parser ServiceDescriptor failed")
}
pbSvcDsc, ok := c.pbSvcDsc.Load().(proto.ServiceDescriptor)
if !ok {
return fmt.Errorf("get parser PbServiceDescriptor failed")
}
inner := thrift.NewReadHTTPPbResponse(svcDsc, pbSvcDsc)
msg.Data().(WithCodec).SetCodec(inner)
return c.codec.Unmarshal(ctx, msg, in)
}
func (c *httpPbThriftCodec) Name() string {
return "HttpPbThrift"
}
func (c *httpPbThriftCodec) Close() error {
var errs []string
if err := c.provider.Close(); err != nil {
errs = append(errs, err.Error())
}
if err := c.pbProvider.Close(); err != nil {
errs = append(errs, err.Error())
}
if len(errs) == 0 {
return nil
} else {
return errors.New(strings.Join(errs, ";"))
}
}
// FromHTTPPbRequest parse HTTPRequest from http.Request
func FromHTTPPbRequest(req *http.Request) (*HTTPRequest, error) {
customReq := &HTTPRequest{
Request: req,
ContentType: descriptor.MIMEApplicationProtobuf,
}
var b io.ReadCloser
var err error
if req.GetBody != nil {
// req from ServerHTTP or create by http.NewRequest
if b, err = req.GetBody(); err != nil {
return nil, err
}
} else {
b = req.Body
}
if b == nil {
// body == nil if from Get request
return customReq, nil
}
if customReq.RawBody, err = ioutil.ReadAll(b); err != nil {
return nil, err
}
if len(customReq.RawBody) == 0 {
return customReq, nil
}
return customReq, nil
}