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

http client: Fallback to default mime type 'application/octet-stream' #7371

Merged
merged 1 commit into from
Feb 4, 2019

Conversation

bew
Copy link
Contributor

@bew bew commented Feb 4, 2019

Fixes #7370

Uses a default mime type as a fallback when the Content-Type header cannot be parsed.

I'm not sure about the DEFAULT_MIME_TYPE as a constant, should the user be able to customize the default mime type?

@WJWH
Copy link

WJWH commented Feb 4, 2019

Whoa, I had just cloned the repo and opened up my editor but you already made a fix! That is less than 45 minutes from issue creation to pr submission ❤️

private def self.check_content_type_charset(body, headers)
return unless body

if content_type = headers["Content-Type"]?
mime_type = MIME::MediaType.parse(content_type)
mime_type = MIME::MediaType.parse?(content_type) || DEFAULT_MIME_TYPE
Copy link
Member

@straight-shoota straight-shoota Feb 4, 2019

Choose a reason for hiding this comment

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

mime_type is not used anywhere except for querying the charset parameter, which isn't set in application/octet-stream anyway. So the following condition should simply be skipped when parse? returns nil.

Copy link
Contributor Author

@bew bew Feb 4, 2019

Choose a reason for hiding this comment

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

ok, so something like that?

    if (content_type = headers["Content-Type"]?) &&
       (mime_type = MIME::MediaType.parse?(content_type)) &&
       (charset = mime_type["charset"]?)
      body.set_encoding(charset, invalid: :skip)
    end

or

    if content_type = headers["Content-Type"]?
      if MIME::MediaType.parse?(content_type).try &.["charset"]
        body.set_encoding(charset, invalid: :skip)
      end
    end

Copy link
Contributor

@Sija Sija Feb 4, 2019

Choose a reason for hiding this comment

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

@bew why not just return unless mime_type?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. In fact we should return unless a lot more here, to avoid nested code.

@bcardiff bcardiff added this to the 0.27.2 milestone Feb 4, 2019
@bew bew force-pushed the fallback-to-default-mime-type branch from e0fd7ac to e7c085e Compare February 4, 2019 17:57
@bew
Copy link
Contributor Author

bew commented Feb 4, 2019

Squashed!

@bcardiff bcardiff added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:networking labels Feb 4, 2019
@bcardiff bcardiff merged commit 4968163 into crystal-lang:master Feb 4, 2019
@bcardiff
Copy link
Member

bcardiff commented Feb 4, 2019

Thanks @bew

@bew bew deleted the fallback-to-default-mime-type branch February 4, 2019 19:53
@straight-shoota
Copy link
Member

Honestly, I really don't like the change to unnest all conditions from if to return unless. IMHO that makes it harder to follow control flow. I'm definitely in favour of using return unless more often, but having it in every second line is simply confusing.

Obviously, this is a matter of stylistic preference. But the agreement is not to change from one style to another for no semantic reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:networking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants