-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make sure event body is a string #393
Conversation
router/router.go
Outdated
if contentType, ok := httpdata.Headers["Content-Type"]; ok && | ||
(strings.HasPrefix(contentType, mimeFormMultipart) || contentType == mimeFormURLEncoded) { | ||
if byteSlice, ok := httpdata.Body.([]byte); ok { | ||
httpdata.Body = string(byteSlice) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elsteelbrain I think this is generally the right approach -- stringifying the request Body -- but I don't know that this is the right place to do it. It would only fix it for http
events, rather than all events.
This method might be better. cc @mthenw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, you are right @alexdebrie. We discussed it earlier today.
fea34bc
to
f12f965
Compare
@mthenw @alexdebrie Changed as requested. |
@alexdebrie @mthenw Please take a look now |
router/event.go
Outdated
return errors.New("malformed JSON body") | ||
} | ||
break | ||
default: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two notes:
-
We check properties of the
body
at the beginning of this block, but then check the properties ofevent.Data
within thedefault
statement, though those should be the same thing since body is assigned to event.Data earlier. -
The first
case
statement checks the mimetype, and the default statement also checks the mimetype in anif
statement. How about we move thatif
statement into a secondcase
statement?
Combining these two, we'd have something like:
if len(body) > 0 {
switch mime {
case mimeJSON:
err := json.Unmarshal(body, &event.Data)
if err != nil {
return errors.New("malformed JSON body")
}
case mimeFormMultipart, mimeFormURLEncoded:
event.Data = string(body)
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexdebrie About the second note, mimeFormMultipart
mime will be a bit different based on what user agent is sending them. The user agent specifies the body boundary, which is mentioned in the mime header, so, we cannot add that into a case
statement. The other one we can, but then we'll have to duplicate the code for them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
About the first one, we cannot assign string
to a body
of type []byte
because that is not allowed. The type interface{}
of event.Data
allows us to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elsteelbrain Good point on the multipart/form-data
boundary. I hadn't thought of that.
About the first one, we cannot assign string to a body of type []byte because that is not allowed. The type interface{} of event.Data allows us to do that.
I don't understand this one. We're not assigning string to body
, we're assigning the stringified version of body
to event.Data
, right? Like this: event.Data = string(body)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna second what Alex said, those should be two case
statements. I don't understand why is it not possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about this:
if len(body) > 0 {
switch {
case mime == mimeJSON:
err = json.Unmarshal(body, &event.Data)
if err != nil {
return nil, "", errors.New("malformed JSON body")
}
case strings.HasPrefix(mime, mimeFormMultipart), mime == mimeFormURLEncoded:
event.Data = string(body)
}
}
This also gets the complexity down under the limit without needing an additional function.
cc @elsteelbrain @mthenw
router/event.go
Outdated
@@ -124,3 +124,23 @@ func transformHeaders(req http.Header) map[string]string { | |||
|
|||
return headers | |||
} | |||
|
|||
|
|||
func correctEventData(event *eventpkg.Event, body []byte, mime string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add comment explaining what this function does?
router/event.go
Outdated
return errors.New("malformed JSON body") | ||
} | ||
break | ||
default: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna second what Alex said, those should be two case
statements. I don't understand why is it not possible.
router/event.go
Outdated
break | ||
default: | ||
if byteSlice, ok := event.Data.([]byte); | ||
ok && (strings.HasPrefix(mime, mimeFormMultipart) || mime == mimeFormURLEncoded) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you check for prefix here, not the full header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the content type is different based on the user agent. For example, Insomnia send this as content type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
.
Codecov Report
@@ Coverage Diff @@
## master #393 +/- ##
==========================================
+ Coverage 56.55% 58.97% +2.41%
==========================================
Files 24 24
Lines 1434 1438 +4
==========================================
+ Hits 811 848 +37
+ Misses 577 543 -34
- Partials 46 47 +1
Continue to review full report at Codecov.
|
router/event.go
Outdated
if err != nil { | ||
return nil, "", errors.New("malformed JSON body") | ||
} | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elsteelbrain break
isn't needed in Golang case statements -- https://tour.golang.org/flowcontrol/9
router/event.go
Outdated
break | ||
case strings.HasPrefix(mime, mimeFormMultipart), mime == mimeFormURLEncoded: | ||
event.Data = string(body) | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elsteelbrain Same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, can you also add test case for multipart/form-data
?
No description provided.