rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / Atom feed
From: Stephen Bannasch <stephen.bannasch@deanbrook.org>
To: rack-devel@googlegroups.com
Subject: rack middleware, best way to replace request body?
Date: Tue, 4 Aug 2009 10:42:23 -0400	[thread overview]
Message-ID: <p06240801c69de5d66ebe@[68.244.251.104]> (raw)


I like to create a rack middleware filter that will automatically 
decompress a request with a content encoding of 'b64gzip' before 
passing it on.

What's the best way for a rack middleware filter to replace the 
request bosy with an uncompressed version?

FYI, this is what my code looks like now -- it works except for the 
part of actualyy replacing the request body ;-)

module Rack

   class ExpandB64Gzip
     def initialize(app)
       @app = app
     end

     def call(env)
       @compressed = env['HTTP_CONTENT_ENCODING'] == "b64gzip"
       @app.call(env)
     end

     def each(&block)
       if @compressed
         req = Rack::Request.new(env)
         decompressed_body = b64gzip_decompress_body(req.body)
         debugger
         req.body = decompressed_body
       end

     end

     def b64gzip_decompress_body(body)
       Zlib::GzipReader.new(StringIO.new(B64::B64.decode(body.read))).read
     end
   end
end

module B64
   class B64
     def self.folding_encode(str, eol = "\n", limit = 60)
       [str].pack('m')
     end

     def self.encode(str)
       [str].pack('m').tr( "\r\n", '')
     end

     def self.decode(str, strict = false)
       str.unpack('m').first
     end
   end
end

                 reply	other threads:[~2009-08-04 14:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='p06240801c69de5d66ebe@[68.244.251.104]' \
    --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).