-
Notifications
You must be signed in to change notification settings - Fork 217
/
codec_v02.go
261 lines (233 loc) · 6.24 KB
/
codec_v02.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package http
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/textproto"
"strings"
"github.com/cloudevents/sdk-go/pkg/cloudevents"
cecontext "github.com/cloudevents/sdk-go/pkg/cloudevents/context"
"github.com/cloudevents/sdk-go/pkg/cloudevents/observability"
"github.com/cloudevents/sdk-go/pkg/cloudevents/transport"
"github.com/cloudevents/sdk-go/pkg/cloudevents/types"
)
// CodecV02 represents a http transport codec that uses CloudEvents spec v0.2
type CodecV02 struct {
CodecStructured
DefaultEncoding Encoding
}
// Adheres to Codec
var _ transport.Codec = (*CodecV02)(nil)
// Encode implements Codec.Encode
func (v CodecV02) Encode(ctx context.Context, e cloudevents.Event) (transport.Message, error) {
encoding := v.DefaultEncoding
strEnc := cecontext.EncodingFrom(ctx)
if strEnc != "" {
switch strEnc {
case Binary:
encoding = BinaryV02
case Structured:
encoding = StructuredV02
}
}
_, r := observability.NewReporter(ctx, CodecObserved{o: reportEncode, c: encoding.Codec()})
m, err := v.obsEncode(ctx, e, encoding)
if err != nil {
r.Error()
} else {
r.OK()
}
return m, err
}
func (v CodecV02) obsEncode(ctx context.Context, e cloudevents.Event, encoding Encoding) (transport.Message, error) {
switch encoding {
case Default:
fallthrough
case BinaryV02:
return v.encodeBinary(ctx, e)
case StructuredV02:
return v.encodeStructured(ctx, e)
default:
return nil, fmt.Errorf("unknown encoding: %d", encoding)
}
}
// Decode implements Codec.Decode
func (v CodecV02) Decode(ctx context.Context, msg transport.Message) (*cloudevents.Event, error) {
_, r := observability.NewReporter(ctx, CodecObserved{o: reportDecode, c: v.inspectEncoding(ctx, msg).Codec()}) // TODO: inspectEncoding is not free.
e, err := v.obsDecode(ctx, msg)
if err != nil {
r.Error()
} else {
r.OK()
}
return e, err
}
func (v CodecV02) obsDecode(ctx context.Context, msg transport.Message) (*cloudevents.Event, error) {
switch v.inspectEncoding(ctx, msg) {
case BinaryV02:
return v.decodeBinary(ctx, msg)
case StructuredV02:
return v.decodeStructured(ctx, cloudevents.CloudEventsVersionV02, msg)
default:
return nil, transport.NewErrMessageEncodingUnknown("v02", TransportName)
}
}
func (v CodecV02) encodeBinary(ctx context.Context, e cloudevents.Event) (transport.Message, error) {
header, err := v.toHeaders(e.Context.AsV02())
if err != nil {
return nil, err
}
body, err := e.DataBytes()
if err != nil {
return nil, err
}
msg := &Message{
Header: header,
Body: body,
}
return msg, nil
}
func (v CodecV02) toHeaders(ec *cloudevents.EventContextV02) (http.Header, error) {
h := http.Header{}
h.Set("ce-specversion", ec.SpecVersion)
h.Set("ce-type", ec.Type)
h.Set("ce-source", ec.Source.String())
h.Set("ce-id", ec.ID)
if ec.Time != nil && !ec.Time.IsZero() {
h.Set("ce-time", ec.Time.String())
}
if ec.SchemaURL != nil {
h.Set("ce-schemaurl", ec.SchemaURL.String())
}
if ec.ContentType != nil && *ec.ContentType != "" {
h.Set("Content-Type", *ec.ContentType)
}
for k, v := range ec.Extensions {
// Per spec, map-valued extensions are converted to a list of headers as:
// CE-attrib-key
if mapVal, ok := v.(map[string]interface{}); ok {
for subkey, subval := range mapVal {
encoded, err := json.Marshal(subval)
if err != nil {
return nil, err
}
h.Set("ce-"+k+"-"+subkey, string(encoded))
}
continue
}
encoded, err := json.Marshal(v)
if err != nil {
return nil, err
}
h.Set("ce-"+k, string(encoded))
}
return h, nil
}
func (v CodecV02) decodeBinary(ctx context.Context, msg transport.Message) (*cloudevents.Event, error) {
m, ok := msg.(*Message)
if !ok {
return nil, fmt.Errorf("failed to convert transport.Message to http.Message")
}
ca, err := v.fromHeaders(m.Header)
if err != nil {
return nil, err
}
var body interface{}
if len(m.Body) > 0 {
body = m.Body
}
return &cloudevents.Event{
Context: &ca,
Data: body,
DataEncoded: body != nil,
}, nil
}
func (v CodecV02) fromHeaders(h http.Header) (cloudevents.EventContextV02, error) {
// Normalize headers.
for k, v := range h {
ck := textproto.CanonicalMIMEHeaderKey(k)
if k != ck {
delete(h, k)
h[ck] = v
}
}
ec := cloudevents.EventContextV02{}
ec.SpecVersion = h.Get("ce-specversion")
h.Del("ce-specversion")
ec.ID = h.Get("ce-id")
h.Del("ce-id")
ec.Type = h.Get("ce-type")
h.Del("ce-type")
source := types.ParseURLRef(h.Get("ce-source"))
if source != nil {
ec.Source = *source
}
h.Del("ce-source")
var err error
ec.Time, err = types.ParseTimestamp(h.Get("ce-time"))
if err != nil {
return ec, err
}
h.Del("ce-time")
ec.SchemaURL = types.ParseURLRef(h.Get("ce-schemaurl"))
h.Del("ce-schemaurl")
contentType := h.Get("Content-Type")
if contentType != "" {
ec.ContentType = &contentType
}
h.Del("Content-Type")
// At this point, we have deleted all the known headers.
// Everything left is assumed to be an extension.
extensions := make(map[string]interface{})
for k, v := range h {
if len(k) > len("ce-") && strings.EqualFold(k[:len("ce-")], "ce-") {
ak := strings.ToLower(k[len("ce-"):])
if i := strings.Index(ak, "-"); i > 0 {
// attrib-key
attrib := ak[:i]
key := ak[(i + 1):]
if xv, ok := extensions[attrib]; ok {
if m, ok := xv.(map[string]interface{}); ok {
m[key] = v
continue
}
// TODO: revisit how we want to bubble errors up.
return ec, fmt.Errorf("failed to process map type extension")
} else {
m := make(map[string]interface{})
m[key] = v
extensions[attrib] = m
}
} else {
// key
var tmp interface{}
if err := json.Unmarshal([]byte(v[0]), &tmp); err == nil {
extensions[ak] = tmp
} else {
// If we can't unmarshal the data, treat it as a string.
extensions[ak] = v[0]
}
}
}
}
if len(extensions) > 0 {
ec.Extensions = extensions
}
return ec, nil
}
func (v CodecV02) inspectEncoding(ctx context.Context, msg transport.Message) Encoding {
version := msg.CloudEventsVersion()
if version != cloudevents.CloudEventsVersionV02 {
return Unknown
}
m, ok := msg.(*Message)
if !ok {
return Unknown
}
contentType := m.Header.Get("Content-Type")
if contentType == cloudevents.ApplicationCloudEventsJSON {
return StructuredV02
}
return BinaryV02
}