Using Rack Middlware to filter parameters

552 views
Skip to first unread message

RobL

unread,
Nov 5, 2009, 6:02:58 AM11/5/09
to Rack Development
Hi there,

I wanted to ask if anyone could point me in the direction of some code
examples where you can use Rack Middleware to filter incoming
parameters.

Of course the classic example of middleware that does not a lot is

module Rack
class Myfilter
def initialize(app)
@app = app
end

def call(env)
@app.call(env)
end
end
end

But as a very tivial example I want be able to do something like

module Rack
class Myfilter
def initialize(app)
@app = app
end

def call(env)
request = Rack::Request.new(env)
request.params.delete('someparam')
# but at this point I want/need to modify env
@app.call(env)
end
end
end

I'm not sure how I'd then get the modified env back, do you have to
literally modify something like env['query_string'], also at this
point the content length would be different so I'd have to update that
also.

Most of the applications I've seen are an endpoint and an app in
itself than sitting in the middle of the incoming request, and I'm yet
to find an example of modifying the incoming params.

Any help on this would be greatly appreciated

All the best.

RobL

Iñaki Baz Castillo

unread,
Nov 5, 2009, 5:32:39 PM11/5/09
to rack-...@googlegroups.com
El Jueves, 5 de Noviembre de 2009, RobL escribió:
> Hi there,
>
> I wanted to ask if anyone could point me in the direction of some code
> examples where you can use Rack Middleware to filter incoming
> parameters.

> But as a very tivial example I want be able to do something like


>
> module Rack
> class Myfilter
> def initialize(app)
> @app = app
> end
>
> def call(env)
> request = Rack::Request.new(env)
> request.params.delete('someparam')
> # but at this point I want/need to modify env
> @app.call(env)
> end
> end
> end
>
> I'm not sure how I'd then get the modified env back, do you have to
> literally modify something like env['query_string'], also at this
> point the content length would be different so I'd have to update that
> also.

Do you mean parameters in the request URI? or in the body? in the first case
the content-length should not be modified.


> Most of the applications I've seen are an endpoint and an app in
> itself than sitting in the middle of the incoming request, and I'm yet
> to find an example of modifying the incoming params.

I expect that Rack is not designed for that, as changing the request is not a
very common usage (IMHO).

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

Martin

unread,
Nov 5, 2009, 6:24:41 PM11/5/09
to rack-...@googlegroups.com
An example of rack middleware that modifies the request:
http://github.com/mynyml/rack-abstract-format/blob/master/lib/rack/abstract_format.rb
Reply all
Reply to author
Forward
0 new messages