Skip to content
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

Merged
merged 17 commits into from
Mar 26, 2018
Merged

Make sure event body is a string #393

merged 17 commits into from
Mar 26, 2018

Conversation

RaeesBhatti
Copy link
Contributor

No description provided.

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)
Copy link
Contributor

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

Copy link
Contributor

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.

@RaeesBhatti
Copy link
Contributor Author

@mthenw @alexdebrie Changed as requested.

@RaeesBhatti
Copy link
Contributor Author

@alexdebrie @mthenw Please take a look now

router/event.go Outdated
return errors.New("malformed JSON body")
}
break
default:
Copy link
Contributor

@alexdebrie alexdebrie Mar 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two notes:

  1. We check properties of the body at the beginning of this block, but then check the properties of event.Data within the default statement, though those should be the same thing since body is assigned to event.Data earlier.

  2. The first case statement checks the mimetype, and the default statement also checks the mimetype in an if statement. How about we move that if statement into a second case 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)
  }
}

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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).

Copy link
Contributor

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.

Copy link
Contributor

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

mthenw
mthenw previously requested changes Mar 20, 2018
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 {
Copy link
Contributor

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:
Copy link
Contributor

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@RaeesBhatti RaeesBhatti dismissed alexdebrie’s stale review March 20, 2018 16:02

Updated as requested

@RaeesBhatti RaeesBhatti dismissed mthenw’s stale review March 20, 2018 16:16

Updated and explained

@codecov-io
Copy link

codecov-io commented Mar 23, 2018

Codecov Report

Merging #393 into master will increase coverage by 2.41%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
router/event.go 86.56% <100%> (+0.85%) ⬆️
router/router.go 60.92% <0%> (+10.92%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bf66945...9e63be3. Read the comment docs.

router/event.go Outdated
if err != nil {
return nil, "", errors.New("malformed JSON body")
}
break
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elsteelbrain Same as above

Copy link
Contributor

@mthenw mthenw left a 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?

@RaeesBhatti RaeesBhatti merged commit 0da4cd7 into master Mar 26, 2018
@RaeesBhatti RaeesBhatti deleted the fixEncoding branch March 26, 2018 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants