From mboxrd@z Thu Jan 1 00:00:00 1970 Delivered-To: chneukirchen@gmail.com Received: by 10.229.188.139 with SMTP id da11cs3958qcb; Wed, 22 Feb 2012 00:41:20 -0800 (PST) Return-Path: Received-SPF: pass (google.com: domain of rack-devel+bncCNq3rdOKCBCs1JL6BBoEEdzL0g@googlegroups.com designates 10.180.99.169 as permitted sender) client-ip=10.180.99.169; Authentication-Results: mr.google.com; spf=pass (google.com: domain of rack-devel+bncCNq3rdOKCBCs1JL6BBoEEdzL0g@googlegroups.com designates 10.180.99.169 as permitted sender) smtp.mail=rack-devel+bncCNq3rdOKCBCs1JL6BBoEEdzL0g@googlegroups.com; dkim=pass header.i=rack-devel+bncCNq3rdOKCBCs1JL6BBoEEdzL0g@googlegroups.com Received: from mr.google.com ([10.180.99.169]) by 10.180.99.169 with SMTP id er9mr16870727wib.1.1329900079263 (num_hops = 1); Wed, 22 Feb 2012 00:41:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=beta; h=x-beenthere:received-spf:received-spf:date:from:to:message-id :in-reply-to:references:subject:x-mailer:mime-version :x-original-sender:x-original-authentication-results:reply-to :precedence:mailing-list:list-id:x-google-group-id:list-post :list-help:list-archive:sender:list-subscribe:list-unsubscribe :content-type; bh=nvLzHofJ+UBXI4qfZdLu30BsYYMt+q/d0n1rpRsETNE=; b=QKxZJ3bLsMMaMFslixI0cktxsO5vvR+wjVLQPdh9dmQOH6TNvgNc26a+N4TMOuI6Yo AXyrnzbvpHIP0m1t6owiUhgcnjVAtkIH9lxrlbH5+/H+UsJCUUeyBt/b6unWN1Ud7YFN Dp0xy1RC+dwxAX13tpUnQ29Tv7Ehd2XVMGVzo= Received: by 10.180.99.169 with SMTP id er9mr4890063wib.1.1329900076962; Wed, 22 Feb 2012 00:41:16 -0800 (PST) X-BeenThere: rack-devel@googlegroups.com Received: by 10.213.21.11 with SMTP id h11ls82964ebb.2.gmail; Wed, 22 Feb 2012 00:41:15 -0800 (PST) Received: by 10.14.121.19 with SMTP id q19mr2318422eeh.6.1329900075108; Wed, 22 Feb 2012 00:41:15 -0800 (PST) Received: by 10.14.121.19 with SMTP id q19mr2318420eeh.6.1329900075084; Wed, 22 Feb 2012 00:41:15 -0800 (PST) Received: from mail-ee0-f52.google.com (mail-ee0-f52.google.com [74.125.83.52]) by gmr-mx.google.com with ESMTPS id p49si19530266eef.0.2012.02.22.00.41.14 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 22 Feb 2012 00:41:14 -0800 (PST) Received-SPF: neutral (google.com: 74.125.83.52 is neither permitted nor denied by best guess record for domain of k.haase@finn.de) client-ip=74.125.83.52; Received: by eekc50 with SMTP id c50so2709531eek.25 for ; Wed, 22 Feb 2012 00:41:14 -0800 (PST) Received-SPF: pass (google.com: domain of k.haase@finn.de designates 10.14.28.137 as permitted sender) client-ip=10.14.28.137; Received: from mr.google.com ([10.14.28.137]) by 10.14.28.137 with SMTP id g9mr14756378eea.94.1329900074933 (num_hops = 1); Wed, 22 Feb 2012 00:41:14 -0800 (PST) Received: by 10.14.28.137 with SMTP id g9mr11804054eea.94.1329900074712; Wed, 22 Feb 2012 00:41:14 -0800 (PST) Received: from Mathiass-MacBook-Pro.local (p57BD6BD3.dip0.t-ipconnect.de. [87.189.107.211]) by mx.google.com with ESMTPS id n17sm100257942eei.3.2012.02.22.00.41.12 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 22 Feb 2012 00:41:13 -0800 (PST) Date: Wed, 22 Feb 2012 09:41:11 +0100 From: Konstantin Haase To: rack-devel@googlegroups.com Message-ID: <06457E10529542CD89CDFD831BFFCEF5@finn.de> In-Reply-To: References: Subject: Re: Rack streamed proxy causing Net::HTTPOK#read_body called twice X-Mailer: sparrow 1.5 (build 1043) MIME-Version: 1.0 X-Gm-Message-State: ALoCoQkbHnLOHaqCn0W3eHtFRi5ED1mjH/KO28QqIw1/Pbw5Dvsoc6QXDxWuVEQEu++z7moHJWXH X-Original-Sender: k.haase@finn.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 74.125.83.52 is neither permitted nor denied by best guess record for domain of k.haase@finn.de) smtp.mail=k.haase@finn.de Reply-To: rack-devel@googlegroups.com Precedence: list Mailing-list: list rack-devel@googlegroups.com; contact rack-devel+owners@googlegroups.com List-ID: X-Google-Group-Id: 486215384060 List-Post: , List-Help: , List-Archive: Sender: rack-devel@googlegroups.com List-Subscribe: , List-Unsubscribe: , Content-Type: multipart/alternative; boundary="4f44aa27_1dbabf00_13d" --4f44aa27_1dbabf00_13d Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 On Wednesday, February 22, 2012 at 12:23 AM, Fabio Kreusch wrote: > 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. --4f44aa27_1dbabf00_13d Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Unfortunately, the Rack specification does not make any assumptions o= n 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
=20

On Wednesday, =46ebrua= ry 22, 2012 at 12:23 AM, =46abio Kreusch wrote:

So, I have a Rack application wh= ich 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=5Fmethod, rack=5Frequest,= options).response
.response returns a valid rack response =5Bs= tatus, headers, body=5D.

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

def initialize
...
= =40proxy=5Fconnection =3D Net::HTTP.start(proxied=5Furi.host,
proxied=5Furi.port)
=40proxy=5Fconnection.request(=40proxy=5F= request) do =7Cresponse=7C
=40proxy=5Fresponse =3D response=
end
end

def each(&blo= ck)
=40proxy=5Fresponse.read=5Fbody(&block)
end=

def to=5Fs
=40proxy=5Fresponse.read= =5Fbody
end
The rack app can then call the proxied se= rvice with a full response
with to=5Fs, or as a streaming and r= eceive 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

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

The Rack ap= p is running under rails 3.2.1.
=20 =20 =20 =20
=20

--4f44aa27_1dbabf00_13d--