-
Notifications
You must be signed in to change notification settings - Fork 98
Make sure event body is a string #393
Changes from 2 commits
f12f965
5969a23
56cffd8
b4d20c7
74512a1
dbedb9e
1a4d8ab
4ef70a4
1e761d7
fcbbd29
7cce702
83f57ae
841fb1d
1c37d3d
14dae4e
6b11d76
9e63be3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ type HTTPResponse struct { | |
|
||
const ( | ||
mimeJSON = "application/json" | ||
mimeFormMultipart = "multipart/form-data" | ||
mimeFormURLEncoded = "application/x-www-form-urlencoded" | ||
) | ||
|
||
func isHTTPEvent(r *http.Request) bool { | ||
|
@@ -63,11 +65,9 @@ func (router *Router) eventFromRequest(r *http.Request) (*eventpkg.Event, string | |
} | ||
|
||
event := eventpkg.New(eventType, mime, body) | ||
if mime == mimeJSON && len(body) > 0 { | ||
err = json.Unmarshal(body, &event.Data) | ||
if err != nil { | ||
return nil, "", errors.New("malformed JSON body") | ||
} | ||
|
||
if err := correctEventData(event, body, mime); err != nil { | ||
return nil, "", err | ||
} | ||
|
||
if event.Type == eventpkg.TypeHTTP { | ||
|
@@ -124,3 +124,23 @@ func transformHeaders(req http.Header) map[string]string { | |
|
||
return headers | ||
} | ||
|
||
|
||
func correctEventData(event *eventpkg.Event, body []byte, mime string) error { | ||
if len(body) > 0 { | ||
switch mime { | ||
case mimeJSON: | ||
err := json.Unmarshal(body, &event.Data) | ||
if err != nil { | ||
return errors.New("malformed JSON body") | ||
} | ||
break | ||
default: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two notes:
Combining these two, we'd have something like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alexdebrie About the second note, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. About the first one, we cannot assign There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elsteelbrain Good point on the
I don't understand this one. We're not assigning string to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm gonna second what Alex said, those should be two There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about this:
This also gets the complexity down under the limit without needing an additional function. cc @elsteelbrain @mthenw |
||
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 commentThe 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 commentThe 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: |
||
event.Data = string(byteSlice) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
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?