Sha256: cb2c02b1f23803321cbaacbb8381d9cb155fe7abb7da165547be8cb8729d8f9a

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module Rack
  module JSONP
  
    class Callback
      def initialize(app, callback_param = "_callback")
        @app, @callback_param = app, callback_param
      end

      def call(env)
        request = Rack::Request.new(env)
      
        if callback = request.params[@callback_param]
          # Save for underlying middlewares
          env["jsonp.callback"] = callback

          # Call original app
          status, headers, @body = @app.call(env)

          # Fix content length if present
          content_length = headers["Content-Length"].to_i

          if (200...300).include?(status) && content_length > 0
            @pre, @post = "#{callback}(", ")" 
            headers["Content-Length"] = (@pre.size + content_length + @post.size).to_s
            headers["Content-Type"]   = "application/javascript" # Set proper content type as per RFC4329
            [200, headers, self] # Override status
          else
            [status, headers, @body]
          end
        else
          @app.call(env)
        end
      end
      
      def each(&block)
        block.call(@pre)
        @body.each(&block)
        block.call(@post)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-jsonp-tools-0.3.3 lib/rack/jsonp/callback.rb