Sha256: 23dff732962633e54f5a59a486bf597ef43705c258b2e1517ec64065be2d8c77

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

module Rack::AMF
  class Application
    def initialize(app, options = {})
      @app = app
      @options = options
    end

    def call(env)
      # Check if we should handle it
      return @app.call(env) if env['CONTENT_TYPE'] != APPLICATION_AMF

      # Parse request and response
      env['rack.input'].rewind
      request = ::AMF::Request.new.populate_from_stream(env['rack.input'].read)
      env['rack-amf.request'] = request
      
      response = ::AMF::Response.new
      response.amf_version = request.amf_version == 3 ? 3 :0
      env['rack-amf.response'] = response
      
      target_uri = request.message.response_uri
      if request.message.data.is_a?(::AMF::Messages::CommandMessage)
        if request.message.data.operation == ::AMF::Messages::CommandMessage::CLIENT_PING_OPERATION
          data = ::AMF::Messages::AcknowledgeMessage.new(request.message.data)
        else
          data = ::AMF::Messages::ErrorMessage.new(Exception.new("CommandMessage #{request.message.data.operation} not implemented"), 
            request.message.data)
        end
      elsif request.message.data.is_a?(::AMF::Messages::RemotingMessage)
        am = ::AMF::Messages::AcknowledgeMessage.new(request.message.data)

        env["REQUEST_PATH"] = env["REQUEST_URI"] = request.message.data.source
        env["REQUEST_METHOD"] = request.message.data.operation

        status, headers, app_response = @app.call(env)
        
        if !app_response.body.is_a?(String)
          am.body = app_response.body.to_amf
        else
          am.body = app_response.body
        end
        data = am
      end
                  
      target_uri += data.is_a?(::AMF::Messages::ErrorMessage) ? '/onStatus' : '/onResult'

      response.message = ::AMF::Message.new(target_uri, '', data.to_amf({:amf_version => response.amf_version}))
        
      # Calculate length and return result
      result = response.to_s
      [200, {"Content-Type" => APPLICATION_AMF, 'Content-Length' => result.length.to_s}, [result]]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
amfora-0.0.2 lib/rack/amf/application.rb
amfora-0.0.1 lib/rack/amf/application.rb