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: don't turn body_io into nil if content-length is zero #8503

Merged
merged 2 commits into from
Apr 1, 2020

Conversation

asterite
Copy link
Member

Fixes #8461

@@ -38,7 +38,7 @@ module HTTP
if body_type.prohibited?
body = nil
elsif content_length = content_length(headers)
if content_length != 0
if content_length >= 0
Copy link
Member

Choose a reason for hiding this comment

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

The type is UInt64, so this condition will always be true.

Copy link
Contributor

Choose a reason for hiding this comment

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

Regardless of the type, what would it even mean to have a negative length?

Copy link
Member Author

Choose a reason for hiding this comment

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

the content_length method seems to be calling to_u64 on the string, so a negative length will raise overflow, I think.

I guess negative content length doesn't make sense, I was just checking because I don't know how to handle negative values. But since I now know it's handled (kind of badly) in the content_length method, it's probably good now.

@@ -38,7 +38,7 @@ module HTTP
if body_type.prohibited?
body = nil
elsif content_length = content_length(headers)
if content_length != 0
if content_length >= 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Regardless of the type, what would it even mean to have a negative length?

@@ -38,7 +38,7 @@ module HTTP
if body_type.prohibited?
body = nil
elsif content_length = content_length(headers)
if content_length != 0
if content_length >= 0
# Don't create IO for Content-Length == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment needs touchup too.

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

This is a valid improvement, but we need to figure out if this is really the intended solution for #8461. See #8461 (comment)

Copy link
Member

@bcardiff bcardiff left a comment

Choose a reason for hiding this comment

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

I think this is a valid fix. And is better to return an empty body_io rather than a nil.

I stumble on this while trying to log the responses from an api that sometimes return a response with content-length 0. In that scenario is hard to find it up front.

I suggest merging as is to keep the current api and improve the handling of content-length 0 responses.

@asterite
Copy link
Member Author

asterite commented Apr 1, 2020

Cool! I can't merge because of the requested changes, but if someone else can, please do.

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

Sure, it's an improvement.

@asterite asterite added this to the 0.34.0 milestone Apr 1, 2020
@asterite asterite merged commit 3ed17e1 into master Apr 1, 2020
@asterite asterite deleted the bug/http-client-response-empty branch April 1, 2020 10:56
carlhoerberg pushed a commit to carlhoerberg/crystal that referenced this pull request Apr 29, 2020
…rystal-lang#8503)

* HTTP::Client: don't turn body_io into nil if content-length is zero

* fixup! HTTP::Client: don't turn body_io into nil if content-length is zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

error reading body_io of a zero length http client response
5 participants