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

Net::HTTPResponse has bad nil handling in read_body and stream_check #70

Closed
BrianHawley opened this issue Sep 22, 2022 · 2 comments · Fixed by #71
Closed

Net::HTTPResponse has bad nil handling in read_body and stream_check #70

BrianHawley opened this issue Sep 22, 2022 · 2 comments · Fixed by #71

Comments

@BrianHawley
Copy link
Contributor

BrianHawley commented Sep 22, 2022

I was getting a weird error from Net::HTTPResponse, and noticed that a couple methods had some bad nil checking.

In read_body, it sets @body = nil and then later on calls string methods on it like @body.force_encoding. Right after you set @read = true you should add this code: return if @body.nil?.

Then, the weird error was from stream_check:

undefined method `closed?' for nil:NilClass (NoMethodError)

    raise IOError, 'attempt to read body out of block' if @socket.closed?

Since you set @socket = nil in reading_body, the stream_check method should be:

def stream_check
  raise IOError, 'attempt to read body out of block' if @socket.nil? || @socket.closed?
end
BrianHawley added a commit to BrianHawley/net-http that referenced this issue Sep 22, 2022
Fix nil handling in read_body and stream_check.

Fixes: ruby#70
@kalashnikovisme
Copy link

Having this issue too. Waiting for PR to be merged or another solution.

Using

gem 'net-http', git: 'https://github.com/BrianHawley/net-http', branch: 'fixes_70'

@BrianHawley
Copy link
Contributor Author

Here it is in patch form, for those who prefer that:

# frozen_string_literal: true

require "net/http"

begin
  response = Net::HTTPResponse.new("", "", "")
  response.body
rescue IOError
  # We're good
rescue
  # Bad exception
  Net::HTTPResponse.class_exec do
    private

    def stream_check
      raise IOError, "attempt to read body out of block" if @socket.nil? || @socket.closed?
    end
  end
end

@nurse nurse closed this as completed in #71 Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants