@@ -67,6 +67,52 @@ type topicEventJSON struct {
67
67
PubsubName string `json:"pubsubname"`
68
68
}
69
69
70
+ func (in topicEventJSON ) getData () (data any , rawData []byte ) {
71
+ var (
72
+ err error
73
+ v any
74
+ )
75
+ if len (in .Data ) > 0 {
76
+ rawData = []byte (in .Data )
77
+ data = rawData
78
+ // We can assume that rawData is valid JSON
79
+ // without checking in.DataContentType == "application/json".
80
+ if err = json .Unmarshal (rawData , & v ); err == nil {
81
+ data = v
82
+ // Handling of JSON base64 encoded or escaped in a string.
83
+ if str , ok := v .(string ); ok {
84
+ // This is the path that will most likely succeed.
85
+ var (
86
+ vString any
87
+ decoded []byte
88
+ )
89
+ if err = json .Unmarshal ([]byte (str ), & vString ); err == nil {
90
+ data = vString
91
+ } else if decoded , err = base64 .StdEncoding .DecodeString (str ); err == nil {
92
+ // Decoded Base64 encoded JSON does not seem to be in the spec
93
+ // but it is in existing unit tests so this handles that case.
94
+ var vBase64 any
95
+ if err = json .Unmarshal (decoded , & vBase64 ); err == nil {
96
+ data = vBase64
97
+ }
98
+ }
99
+ }
100
+ }
101
+ } else if in .DataBase64 != "" {
102
+ rawData , err = base64 .StdEncoding .DecodeString (in .DataBase64 )
103
+ if err == nil {
104
+ data = rawData
105
+ if in .DataContentType == "application/json" {
106
+ if err = json .Unmarshal (rawData , & v ); err == nil {
107
+ data = v
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ return data , rawData
114
+ }
115
+
70
116
func (s * Server ) registerBaseHandler () {
71
117
// register subscribe handler
72
118
f := func (w http.ResponseWriter , r * http.Request ) {
@@ -189,14 +235,25 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn common.TopicE
189
235
s .mux .Handle (sub .Route , optionsHandler (http .HandlerFunc (
190
236
func (w http.ResponseWriter , r * http.Request ) {
191
237
// check for post with no data
192
- if r .ContentLength == 0 {
238
+ var (
239
+ body []byte
240
+ err error
241
+ )
242
+ if r .Body != nil {
243
+ body , err = io .ReadAll (r .Body )
244
+ if err != nil {
245
+ http .Error (w , err .Error (), PubSubHandlerDropStatusCode )
246
+ return
247
+ }
248
+ }
249
+ if len (body ) == 0 {
193
250
http .Error (w , "nil content" , PubSubHandlerDropStatusCode )
194
251
return
195
252
}
196
253
197
254
// deserialize the event
198
255
var in topicEventJSON
199
- if err : = json .NewDecoder ( r . Body ). Decode ( & in ); err != nil {
256
+ if err = json .Unmarshal ( body , & in ); err != nil {
200
257
http .Error (w , err .Error (), PubSubHandlerDropStatusCode )
201
258
return
202
259
}
@@ -208,46 +265,7 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn common.TopicE
208
265
in .Topic = sub .Topic
209
266
}
210
267
211
- var data interface {}
212
- var rawData []byte
213
- if len (in .Data ) > 0 {
214
- rawData = []byte (in .Data )
215
- data = rawData
216
- var v interface {}
217
- // We can assume that rawData is valid JSON
218
- // without checking in.DataContentType == "application/json".
219
- if err := json .Unmarshal (rawData , & v ); err == nil {
220
- data = v
221
- // Handling of JSON base64 encoded or escaped in a string.
222
- if str , ok := v .(string ); ok {
223
- // This is the path that will most likely succeed.
224
- var vString interface {}
225
- if err := json .Unmarshal ([]byte (str ), & vString ); err == nil {
226
- data = vString
227
- } else if decoded , err := base64 .StdEncoding .DecodeString (str ); err == nil {
228
- // Decoded Base64 encoded JSON does not seem to be in the spec
229
- // but it is in existing unit tests so this handles that case.
230
- var vBase64 interface {}
231
- if err := json .Unmarshal (decoded , & vBase64 ); err == nil {
232
- data = vBase64
233
- }
234
- }
235
- }
236
- }
237
- } else if in .DataBase64 != "" {
238
- var err error
239
- rawData , err = base64 .StdEncoding .DecodeString (in .DataBase64 )
240
- if err == nil {
241
- data = rawData
242
- if in .DataContentType == "application/json" {
243
- var v interface {}
244
- if err := json .Unmarshal (rawData , & v ); err == nil {
245
- data = v
246
- }
247
- }
248
- }
249
- }
250
-
268
+ data , rawData := in .getData ()
251
269
te := common.TopicEvent {
252
270
ID : in .ID ,
253
271
SpecVersion : in .SpecVersion ,
0 commit comments