-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathmessage.go
175 lines (152 loc) · 4.31 KB
/
message.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
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package http
import (
"context"
"io"
nethttp "net/http"
"net/textproto"
"strings"
"unicode"
"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/binding/format"
"github.com/cloudevents/sdk-go/v2/binding/spec"
)
const prefix = "Ce-"
var specs = spec.WithPrefixMatchExact(
func(s string) string {
if s == "datacontenttype" {
return "Content-Type"
} else {
return textproto.CanonicalMIMEHeaderKey("Ce-" + s)
}
},
"Ce-",
)
const ContentType = "Content-Type"
const ContentLength = "Content-Length"
// Message holds the Header and Body of a HTTP Request or Response.
// The Message instance *must* be constructed from NewMessage function.
// This message *cannot* be read several times. In order to read it more times, buffer it using binding/buffering methods
type Message struct {
Header nethttp.Header
BodyReader io.ReadCloser
OnFinish func(error) error
ctx context.Context
format format.Format
version spec.Version
}
// Check if http.Message implements binding.Message
var _ binding.Message = (*Message)(nil)
var _ binding.MessageContext = (*Message)(nil)
var _ binding.MessageMetadataReader = (*Message)(nil)
// NewMessage returns a binding.Message with header and data.
// The returned binding.Message *cannot* be read several times. In order to read it more times, buffer it using binding/buffering methods
func NewMessage(header nethttp.Header, body io.ReadCloser) *Message {
m := Message{Header: header}
if body != nil {
m.BodyReader = body
}
if m.format = format.Lookup(header.Get(ContentType)); m.format == nil {
m.version = specs.Version(m.Header.Get(specs.PrefixedSpecVersionName()))
}
return &m
}
// NewMessageFromHttpRequest returns a binding.Message with header and data.
// The returned binding.Message *cannot* be read several times. In order to read it more times, buffer it using binding/buffering methods
func NewMessageFromHttpRequest(req *nethttp.Request) *Message {
if req == nil {
return nil
}
message := NewMessage(req.Header, req.Body)
message.ctx = req.Context()
return message
}
// NewMessageFromHttpResponse returns a binding.Message with header and data.
// The returned binding.Message *cannot* be read several times. In order to read it more times, buffer it using binding/buffering methods
func NewMessageFromHttpResponse(resp *nethttp.Response) *Message {
if resp == nil {
return nil
}
msg := NewMessage(resp.Header, resp.Body)
return msg
}
func (m *Message) ReadEncoding() binding.Encoding {
if m.version != nil {
return binding.EncodingBinary
}
if m.format != nil {
if m.format == format.JSONBatch {
return binding.EncodingBatch
}
return binding.EncodingStructured
}
return binding.EncodingUnknown
}
func (m *Message) ReadStructured(ctx context.Context, encoder binding.StructuredWriter) error {
if m.format == nil {
return binding.ErrNotStructured
} else {
return encoder.SetStructuredEvent(ctx, m.format, m.BodyReader)
}
}
func (m *Message) ReadBinary(ctx context.Context, encoder binding.BinaryWriter) (err error) {
if m.version == nil {
return binding.ErrNotBinary
}
for k, v := range m.Header {
attr := m.version.Attribute(k)
if attr != nil {
err = encoder.SetAttribute(attr, v[0])
} else if strings.HasPrefix(k, prefix) {
// Trim Prefix + To lower
var b strings.Builder
b.Grow(len(k) - len(prefix))
b.WriteRune(unicode.ToLower(rune(k[len(prefix)])))
b.WriteString(k[len(prefix)+1:])
err = encoder.SetExtension(b.String(), v[0])
}
if err != nil {
return err
}
}
if m.BodyReader != nil {
err = encoder.SetData(m.BodyReader)
if err != nil {
return err
}
}
return
}
func (m *Message) GetAttribute(k spec.Kind) (spec.Attribute, interface{}) {
attr := m.version.AttributeFromKind(k)
if attr != nil {
h := m.Header[attributeHeadersMapping[attr.Name()]]
if h != nil {
return attr, h[0]
}
return attr, nil
}
return nil, nil
}
func (m *Message) GetExtension(name string) interface{} {
h := m.Header[extNameToHeaderName(name)]
if h != nil {
return h[0]
}
return nil
}
func (m *Message) Context() context.Context {
return m.ctx
}
func (m *Message) Finish(err error) error {
if m.BodyReader != nil {
_ = m.BodyReader.Close()
}
if m.OnFinish != nil {
return m.OnFinish(err)
}
return nil
}