From: Magnus Holm <judofyr@gmail.com>
To: rack-devel@googlegroups.com
Subject: Re: How to use common code in each "map"
Date: Thu, 10 Sep 2009 18:56:50 +0200 [thread overview]
Message-ID: <391a49da0909100956u4d0b32cex47dbf74e48560c12@mail.gmail.com> (raw)
In-Reply-To: <cc1f582e0909100853k5ce9a491u92747f05b3ae12c1@mail.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
//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>
>
next prev parent reply other threads:[~2009-09-10 16:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-10 15:53 How to use common code in each "map" Iñaki Baz Castillo
2009-09-10 16:56 ` Magnus Holm [this message]
2009-09-10 17:30 ` 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=391a49da0909100956u4d0b32cex47dbf74e48560c12@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).