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

336 views
Skip to first unread message

Fabio Kreusch

unread,
Feb 21, 2012, 6:23:42 PM2/21/12
to Rack Development
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.

Konstantin Haase

unread,
Feb 22, 2012, 3:41:11 AM2/22/12
to rack-...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages