Skip to content

Commit 4afb831

Browse files
authored
Fixed: do not require a Content-Length header (#424)
* Fixed: do not require a Content-Length header Signed-off-by: ItalyPaleAle <[email protected]> * Make getData private Signed-off-by: ItalyPaleAle <[email protected]> --------- Signed-off-by: ItalyPaleAle <[email protected]>
1 parent effc2f0 commit 4afb831

File tree

4 files changed

+71
-53
lines changed

4 files changed

+71
-53
lines changed

service/http/binding.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ func (s *Server) AddBindingInvocationHandler(route string, fn common.BindingInvo
3737

3838
s.mux.Handle(route, optionsHandler(http.HandlerFunc(
3939
func(w http.ResponseWriter, r *http.Request) {
40-
var content []byte
41-
if r.ContentLength > 0 {
42-
body, err := io.ReadAll(r.Body)
40+
var (
41+
content []byte
42+
err error
43+
)
44+
if r.Body != nil {
45+
content, err = io.ReadAll(r.Body)
4346
if err != nil {
4447
http.Error(w, err.Error(), http.StatusBadRequest)
4548
return
4649
}
47-
content = body
4850
}
4951

5052
// assuming Dapr doesn't pass multiple values for key

service/http/invoke.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,13 @@ func (s *Server) AddServiceInvocationHandler(route string, fn common.ServiceInvo
5454
ContentType: r.Header.Get("Content-type"),
5555
}
5656

57-
// check for post with no data
58-
if r.ContentLength > 0 {
59-
content, err := io.ReadAll(r.Body)
57+
var err error
58+
if r.Body != nil {
59+
e.Data, err = io.ReadAll(r.Body)
6060
if err != nil {
6161
http.Error(w, err.Error(), http.StatusBadRequest)
6262
return
6363
}
64-
e.Data = content
6564
}
6665

6766
ctx := r.Context()

service/http/topic.go

+60-42
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,52 @@ type topicEventJSON struct {
6767
PubsubName string `json:"pubsubname"`
6868
}
6969

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+
70116
func (s *Server) registerBaseHandler() {
71117
// register subscribe handler
72118
f := func(w http.ResponseWriter, r *http.Request) {
@@ -189,14 +235,25 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn common.TopicE
189235
s.mux.Handle(sub.Route, optionsHandler(http.HandlerFunc(
190236
func(w http.ResponseWriter, r *http.Request) {
191237
// 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 {
193250
http.Error(w, "nil content", PubSubHandlerDropStatusCode)
194251
return
195252
}
196253

197254
// deserialize the event
198255
var in topicEventJSON
199-
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
256+
if err = json.Unmarshal(body, &in); err != nil {
200257
http.Error(w, err.Error(), PubSubHandlerDropStatusCode)
201258
return
202259
}
@@ -208,46 +265,7 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn common.TopicE
208265
in.Topic = sub.Topic
209266
}
210267

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()
251269
te := common.TopicEvent{
252270
ID: in.ID,
253271
SpecVersion: in.SpecVersion,

service/http/topic_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ import (
2525
"strings"
2626
"testing"
2727

28-
"github.com/dapr/go-sdk/actor/api"
29-
"github.com/dapr/go-sdk/actor/mock"
30-
3128
"github.com/stretchr/testify/assert"
3229
"github.com/stretchr/testify/require"
3330

31+
"github.com/dapr/go-sdk/actor/api"
32+
"github.com/dapr/go-sdk/actor/mock"
3433
"github.com/dapr/go-sdk/service/common"
3534
"github.com/dapr/go-sdk/service/internal"
3635
)

0 commit comments

Comments
 (0)