Rack streamed proxy causing Net::HTTPOK#read_body called twice

Showing 1-2 of 2 messages
Rack streamed proxy causing Net::HTTPOK#read_body called twice Fabio Kreusch 2/21/12 3:23 PM
So, I have a Rack application which acts as a proxy. I have
implemented a net/http api to deal with the communication, so the rack
app basically calls it like this:

api.new(request_method, rack_request, options).response
.response returns a valid rack response [status, headers, body].

Body is an instance of the api, and it its implementation is like this
(simplified version):

def initialize
  ...
  @proxy_connection = Net::HTTP.start(proxied_uri.host,
proxied_uri.port)
  @proxy_connection.request(@proxy_request) do |response|
    @proxy_response = response
  end
end

def each(&block)
  @proxy_response.read_body(&block)
end

def to_s
  @proxy_response.read_body
end
The rack app can then call the proxied service with a full response
with to_s, or as a streaming and receive the response in chunks with
each.

The thing is, when I return the response in rack, I'm getting the
following stack trace: https://gist.github.com/1879724

Apparently, the server or rack is calling 'each' more than once, but
I'm not sure what might be causing it.

The Rack app is running under rails 3.2.1.
Re: Rack streamed proxy causing Net::HTTPOK#read_body called twice Konstantin Haase 2/22/12 12:41 AM
Unfortunately, the Rack specification does not make any assumptions on whether each will be called once or twice or how to reflect upon that. So you always have to assume that each might be called more than once.

Konstantin