-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
http client: Fallback to default mime type 'application/octet-stream' #7371
Conversation
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 ❤️ |
src/http/common.cr
Outdated
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 |
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.
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
.
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.
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
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.
@bew why not just return unless mime_type
?
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.
Yes. In fact we should return unless
a lot more here, to avoid nested code.
e0fd7ac
to
e7c085e
Compare
Squashed! |
Thanks @bew |
Honestly, I really don't like the change to unnest all conditions from Obviously, this is a matter of stylistic preference. But the agreement is not to change from one style to another for no semantic reason. |
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?