Sha256: 5b8eb41fadd65f086efee6a91a9195c78f28d45e2f0e2b9055388326ab999584
Contents?: true
Size: 1.5 KB
Versions: 2
Compression:
Stored size: 1.5 KB
Contents
module Rack # A Rack middleware for providing JSON-P support. # # Full credit to Flinn Mueller (http://actsasflinn.com/) for this contribution. # class JSONP def initialize(app) @app = app end # Proxies the request to the application, stripping out the JSON-P callback # method and padding the response with the appropriate callback format. # # Changes nothing if no <tt>callback</tt> param is specified. # def call(env) # remove the callback and _ parameters BEFORE calling the backend, # so that caching middleware does not store a copy for each value of the callback parameter request = Rack::Request.new(env) callback = request.params.delete('callback') env['QUERY_STRING'] = env['QUERY_STRING'].split("&").delete_if{|param| param =~ /^(_|callback)/}.join("&") status, headers, response = @app.call(env) if callback response = pad(callback, response) headers['Content-Length'] = response.length.to_s end [status, headers, response] end # Pads the response with the appropriate callback format according to the # JSON-P spec/requirements. # # The Rack response spec indicates that it should be enumerable. The method # of combining all of the data into a single string makes sense since JSON # is returned as a full string. # def pad(callback, response, body = "") response.each{ |s| body << s } "#{callback}(#{body})" end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
cryx-g5k-0.2.10 | lib/g5k/rack/jsonp.rb |
cryx-g5k-0.2.9 | lib/g5k/rack/jsonp.rb |