开发者

Deal with gzipped body in PUT requests to heroku

I'm working on a rails app that communicates with an iphone app via a restful xml interface. The iphone app developer wants to gzip the body of his requests since he's sending up various media. I thought that heroku would automatically deal with gzipped requests (http://adam.heroku.com/past/2009/4/22/gzip_makes_a_happy_web/) but it doesn't seem to: i get a load of garbled text through which i don't know how to deal with.

Is there a setting i need to set with my heroku app to 开发者_运维知识库deal with this? Or a particular header he (iphone dev) needs to send with his requests to tell heroku how to deal with it?

Bit stuck, any advice appreciated! thanks, max


I've used the method described in this gist to transparently decompress gzipped request bodies. The important part with Rails was to insert it before ActionDispatch::ParamsParser. In config/environments/development.rb or config/environments/production.rb:

config.middleware.insert_before ActionDispatch::ParamsParser, "CompressedRequests"

Place the content of the gist into the file at lib/middleware/compressed_requests.rb and be sure to add lib/middleware/ to the autoload path in config/application.rb:

config.autoload_paths += %W(#{config.root}/lib #{config.root}/lib/middleware)

Using this your application will never know the request was gzipped, and it should be independent of any rack capable webserver. I've used it with both webrick and thin without any issue.

EDIT: Any HTTP request using a gzipped request body should set the HTTP header Content-Encoding: gzip. Here is a sample test with curl:

gzip my_file.txt
curl --header 'Content-Encoding: gzip' --data-binary @my_file.txt.gz http://example.com/path


From the link you posted:

All apps deployed to Heroku automatically compress pages they serve

In your case they request body is gzipped, which is not automatically handled by any webserver, you will need to inflate that garbled string in your code to get the body:

def inflate(body)
  zstream = Zlib::Inflate.new
  buf = zstream.inflate(body)
  zstream.finish
  zstream.close
  buf # inflated body returned
end

(Example taken from How to decompress Gzip string in ruby?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜