rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / Atom feed
* How to use common code in each "map"
@ 2009-09-10 15:53 Iñaki Baz Castillo
  2009-09-10 16:56 ` Magnus Holm
  0 siblings, 1 reply; 3+ messages in thread
From: Iñaki Baz Castillo @ 2009-09-10 15:53 UTC (permalink / raw)
  To: rack-devel


Hi, I'm trying to use common HTTP request validation code  in each "map" block:

----------------------------------------------------------------------
Rack::Builder.new do
					
  map "/" do
    run Proc.new { |env| [ 404, {"Content-Type" => "text/plain"}, [""] ] }
  end

  map "/service1" do
    ___VALIDATION_CODE___
    run MyApp1
  end

  map "/service2" do
    ___VALIDATION_CODE___
    run MyApp2
  end

end
----------------------------------------------------------------------


Unfortunatelly I don't know how to fill ___VALIDATION_CODE___.
There I want to inspect env["SERVER_NAME"] and some other headers. In
case the validation is not passed, it should, *by itself*, return 403
code and MyApp1 or MyApp2 execution shouldn't take place.

IMHO it means that it cannot be done with a middleware since it would
execute MyApp1 or MyApp2.

Which is the appropriate approach to achieve it? Thanks for any help. Regards.


-- 
Iñaki Baz Castillo
<ibc@aliax.net>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to use common code in each "map"
  2009-09-10 15:53 How to use common code in each "map" Iñaki Baz Castillo
@ 2009-09-10 16:56 ` Magnus Holm
  2009-09-10 17:30   ` Iñaki Baz Castillo
  0 siblings, 1 reply; 3+ messages in thread
From: Magnus Holm @ 2009-09-10 16:56 UTC (permalink / raw)
  To: rack-devel


It is possible with a middleware:

class Restricter
  def initialize(app, headers = {})
    @app = app
    @headers = headers
  end

  def call(env)
    @headers.each do |key, value|
      unless env[key] == value
        return [403, {}, ['Something']]
      end
    end

    @app.call(env)
  end
end

map "/service1" do
  use Restricter, "SERVER_NAME" => "testing"
  run App1
end

//Magnus Holm



On Thu, Sep 10, 2009 at 17:53, Iñaki Baz Castillo<ibc@aliax.net> wrote:
>
> Hi, I'm trying to use common HTTP request validation code  in each "map" block:
>
> ----------------------------------------------------------------------
> Rack::Builder.new do
>
>  map "/" do
>    run Proc.new { |env| [ 404, {"Content-Type" => "text/plain"}, [""] ] }
>  end
>
>  map "/service1" do
>    ___VALIDATION_CODE___
>    run MyApp1
>  end
>
>  map "/service2" do
>    ___VALIDATION_CODE___
>    run MyApp2
>  end
>
> end
> ----------------------------------------------------------------------
>
>
> Unfortunatelly I don't know how to fill ___VALIDATION_CODE___.
> There I want to inspect env["SERVER_NAME"] and some other headers. In
> case the validation is not passed, it should, *by itself*, return 403
> code and MyApp1 or MyApp2 execution shouldn't take place.
>
> IMHO it means that it cannot be done with a middleware since it would
> execute MyApp1 or MyApp2.
>
> Which is the appropriate approach to achieve it? Thanks for any help. Regards.
>
>
> --
> Iñaki Baz Castillo
> <ibc@aliax.net>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to use common code in each "map"
  2009-09-10 16:56 ` Magnus Holm
@ 2009-09-10 17:30   ` Iñaki Baz Castillo
  0 siblings, 0 replies; 3+ messages in thread
From: Iñaki Baz Castillo @ 2009-09-10 17:30 UTC (permalink / raw)
  To: rack-devel


2009/9/10 Magnus Holm <judofyr@gmail.com>:
>
> It is possible with a middleware:
>
> class Restricter
>  def initialize(app, headers = {})
>    @app = app
>    @headers = headers
>  end
>
>  def call(env)
>    @headers.each do |key, value|
>      unless env[key] == value
>        return [403, {}, ['Something']]
>      end
>    end
>
>    @app.call(env)
>  end
> end
>
> map "/service1" do
>  use Restricter, "SERVER_NAME" => "testing"
>  run App1
> end


Great!

I was trying to get it but couldn't do it. Thanks a lot.



-- 
Iñaki Baz Castillo
<ibc@aliax.net>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-09-10 17:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-10 15:53 How to use common code in each "map" Iñaki Baz Castillo
2009-09-10 16:56 ` Magnus Holm
2009-09-10 17:30   ` Iñaki Baz Castillo

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).