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

CP-1966 Handle request with no encoding and no charset #161

Merged
merged 1 commit into from
Jun 23, 2016

Conversation

evanweible-wf
Copy link
Contributor

Fixes #159.

Issue

#159 reported an issue when setting a request's content-type manually. If that content-type has no charset parameter, and the request's encoding was not set, you will hit an RTE in HttpBody when trying to read the body and a conversion is required (bytes->string or vice versa). This is because the HttpBody constructors have an optional fallbackEncoding parameter that has no default value. For responses, we pass in a fallback encoding of LATIN1 per RFC 2616, but we aren't passing in a fallback encoding at all for request bodies.

This issue hadn't manifested itself before because most requests set the content-type automatically, including the charset, but #153 introduced the ability to manually set the content-type.

Solution

  • Update HttpBody.fromString() and HttpBody.fromBytes() to give the optional fallbackEncoding parameter a default value of UTF8.
  • Add a null check that will throw an ArgumentError if fallbackEncoding happens to be null
  • Additionally, a null check for encoding was added before trying to read the name in ResponseFormatException.
  • Bonus: When attempting to decode/encode a request/response body, we now only catch the expected error instead of any exception (ArgumentError for encode, FormatException for decode). This will prevent us from swallowing unexpected exceptions erroneously wrapping them in a ResponseFormatException, which is what happened in the stack trace from Handle request with no encoding and no charset #159.

Testing

Code Review

@trentgrover-wf
@maxwellpeterson-wf
@dustinlessard-wf
@jayudey-wf
@sebastianmalysa-wf
@srinivasdhanwada-wf

fyi: @stevenosborne-wf

@evanweible-wf evanweible-wf added this to the 2.6.1 milestone Jun 22, 2016
{Encoding fallbackEncoding}) {
{Encoding fallbackEncoding: UTF8}) {
if (fallbackEncoding == null) {
throw new ArgumentError.notNull('fallbackEncoding');
Copy link
Member

@maxwellpeterson-wf maxwellpeterson-wf Jun 22, 2016

Choose a reason for hiding this comment

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

Would it be okay to set it to UTF8 if fallbackEncoding is null here instead of throwing an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's better.

@evanweible-wf
Copy link
Contributor Author

evanweible-wf commented Jun 22, 2016

Sorry, hold off on this briefly, just thought of one improvement.

@evanweible-wf evanweible-wf force-pushed the default-request-encoding branch 2 times, most recently from e3e58bf to ef80a24 Compare June 22, 2016 20:08
@evanweible-wf evanweible-wf changed the title Handle request with no encoding and no charset CP-1966 Handle request with no encoding and no charset Jun 22, 2016
@evanweible-wf
Copy link
Contributor Author

@codecov-io
Copy link

codecov-io commented Jun 22, 2016

Current coverage is 99.45%

Merging #161 into master will decrease coverage by 0.07%

Powered by Codecov. Last updated by ec30c7f...1d8a58a

@maxwellpeterson-wf
Copy link
Member

+1

It looks like there's one branch that isn't covered... specifying the optional encoding to HttpBody.fromBytes().

@evanweible-wf evanweible-wf force-pushed the default-request-encoding branch from ef80a24 to d05d754 Compare June 22, 2016 20:23
@evanweible-wf
Copy link
Contributor Author

@maxwellpeterson-wf good catch, my new HttpBody tests were supposed to test both constructors but I copy pasted and didn't change fromString to fromBytes. Should be fixed now.

@maxwellpeterson-wf
Copy link
Member

+1

1 similar comment
@sebastianmalysa-wf
Copy link

+1

{Encoding fallbackEncoding}) {
_encoding = http_utils.parseEncodingFromContentType(contentType,
fallback: fallbackEncoding);
{Encoding encoding, Encoding fallbackEncoding: UTF8}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

there's some inconsistency in this fallbackEncoding code. why does this method signature have a default value but the other does not? if there's a default value, no need to the null check to set UTF8

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I had it this way at first but switched to Max's suggestion of just populating fallbackEncoding to UTF8 if it's null instead of throwing an ArgumentError. I'll fix this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@evanweible-wf evanweible-wf force-pushed the default-request-encoding branch from d05d754 to 1d8a58a Compare June 23, 2016 17:14
- Throw ArgumentError if a request's encoding is set to null
- Null check for encoding before reading name in ResponseFormatException
- HttpBody constructors now accept encoding param and will use if given
- HttpBody constructors only parse content-type for charset if encoding
  not given
- HttpBody constructors now default fallbackEncoding to UTF8
- Only catch ArgumentException when attempting to encode a body
- Only catch FormatException when attempting to decode body bytes
@evanweible-wf
Copy link
Contributor Author

@trentgrover-wf @maxwellpeterson-wf @sebastianmalysa-wf

Addressed Trent's comment.

@maxwellpeterson-wf
Copy link
Member

+1

@trentgrover-wf
Copy link
Contributor

+1
@jayudey-wf ready for merge

@jayudey-wf
Copy link
Contributor

jayudey-wf commented Jun 23, 2016

QA Resource Approval: +10

  • Testing instruction
  • Dev +1's
  • Dev/QA +10 with detail of what was tested
    • passing CI with tests utilizing new functionality
  • Unit test created/updated
  • All unit tests pass

Merging into master.

@jayudey-wf jayudey-wf merged commit 52b6714 into master Jun 23, 2016
@evanweible-wf evanweible-wf modified the milestones: 2.7.0, 2.6.1 Jun 23, 2016
@evanweible-wf evanweible-wf deleted the default-request-encoding branch July 22, 2016 22:54
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.

Handle request with no encoding and no charset
6 participants