rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / Atom feed
From: Ryan Tomayko <r@tomayko.com>
To: rack-devel@googlegroups.com
Subject: Re: Why env.object_id is different in each middleware?
Date: Tue, 5 Jan 2010 12:57:58 -0800	[thread overview]
Message-ID: <f732822d1001051257w5b1e174bl6de748ed48c020e0@mail.gmail.com> (raw)
In-Reply-To: <A361F143-9409-4464-8DFA-33131C5E76D7@spork.in>

On Sun, Jan 3, 2010 at 4:50 PM, Tim Carey-Smith <g@spork.in> wrote:
> On 10/12/2009, at 12:15 PM, Iñaki Baz Castillo wrote:
>
>> El Miércoles, 9 de Diciembre de 2009, Iñaki Baz Castillo escribió:
>>>
>>> Since env is a hash I cannot understand why its object_id changes. Any
>>> explanation for it?
>>> This explains that when I change env[XXX] into my final appplication
>>> MyApp.handle_request the change doesn't exist after calling
>>> resp = @app.call(env)
>>> in the first middleware.
>>
>> If I add a env["LALA"] in the first middleware then it's visible for
>> following
>> middlewares.
>> However if I add env["LOLO"] in the second middleware this is not visible
>> for
>> first middleware after calling "@app.call(env)".
>>
>> Is it the expected behavior?
>>
>> Thanks.
>
> Hi there,
>
> I've hit this before. This is because you are using Rack::URLMap (via
> Builder#map).
> The inner app is called with a new env hash.
>
>> diff --git a/lib/rack/urlmap.rb b/lib/rack/urlmap.rb
>> index b699d35..3374535 100644
>> --- a/lib/rack/urlmap.rb
>> +++ b/lib/rack/urlmap.rb
>> @@ -45,7 +45,7 @@ module Rack
>>         next unless rest.empty? || rest[0] == ?/
>>
>>         return app.call(
>> -          env.merge(
>> +          env.merge!(
>>             'SCRIPT_NAME' => (script_name + location),
>>             'PATH_INFO'   => rest))
>>       }
>
> Is this patch useful?
> Is it useful to assume that a request will only have a single env hash?
> Will it make Rack::Cascade and friends behave incorrectly?
>
> Should URLMap revert the change in an ensure to allow subsequent requests to
> function?

This is something I've always thought should be included in the rack
spec. Should a rack component, A, that calls another component, B, be
able to assume that env modifications made by B (or downstream) will
be visible in the env passed by A when B returns? As of right now,
there's a number of core and contrib middleware that assume: no. The
URLMap example above is a good one. A lot of middleware pass a copy of
the env downstream instead of modifying and relaying the env provided.

I see this come up fairly often but I'm not sure there's ever been a
good use case for it. It's usually a sign that you're using the env
where you should be using the response tuple. e.g., instead of putting
something in the env, put it in the response headers or convey it via
status code.

Or maybe this really is something the env could be useful for and I've
just never run into a good case. Can anyone provide real examples of
where it would be necessary?

Thanks,
Ryan

  parent reply	other threads:[~2010-01-05 20:58 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-09 22:59 Why env.object_id is different in each middleware? Iñaki Baz Castillo
2009-12-09 23:15 ` Iñaki Baz Castillo
2010-01-04  0:50   ` Tim Carey-Smith
2010-01-04  0:58     ` Iñaki Baz Castillo
2010-01-04 19:44     ` Iñaki Baz Castillo
2010-01-05 20:57     ` Ryan Tomayko [this message]
2010-01-05 23:47       ` Iñaki Baz Castillo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://groups.google.com/group/rack-devel

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f732822d1001051257w5b1e174bl6de748ed48c020e0@mail.gmail.com \
    --to=rack-devel@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).