Class | RubyAMF::RequestParser |
In: |
lib/rubyamf/request_parser.rb
|
Parent: | Object |
RubyAMF uses a two-stage middleware strategy. RequestParser is the first stage, and is responsible for parsing the stream and setting env [‘rubyamf.request’] and env [‘rubyamf.response’] to an instance of RubyAMF::Envelope. If the response envelope is marked as constructed, it will send back the serialized response. The second stage is RubyAMF::Rails::RequestProcessor.
Determine how to handle the request and pass off to handle_amf, handle_image, or handle_html. If the show_html_gateway config is set to false, it will not serve an HTML page for non-amf requests to the gateway.
# File lib/rubyamf/request_parser.rb, line 17 17: def call env 18: # Show HTML gateway page if it's enabled for non-amf requests 19: if RubyAMF.configuration.show_html_gateway 20: if env['PATH_INFO'] == gateway_image_path 21: return handle_image 22: elsif env['PATH_INFO'] == gateway_path && env['CONTENT_TYPE'] != RubyAMF::MIME_TYPE 23: return handle_html 24: end 25: end 26: 27: # Handle if it's AMF or pass it up the chain 28: if env['PATH_INFO'] == gateway_path && env['CONTENT_TYPE'] == RubyAMF::MIME_TYPE 29: return handle_amf(env) 30: else 31: return @app.call(env) 32: end 33: end
It parses the request, creates a response object, and forwards the call to the next middleware. If the amf response is constructed, then it serializes the response and returns it as the response.
# File lib/rubyamf/request_parser.rb, line 38 38: def handle_amf env 39: # Wrap request and response 40: env['rack.input'].rewind 41: env['rubyamf.request'] = RubyAMF::Envelope.new.populate_from_stream(env['rack.input'].read) 42: env['rubyamf.response'] = RubyAMF::Envelope.new 43: 44: # Pass up the chain to the request processor, or whatever is layered in between 45: result = @app.call(env) 46: 47: # Calculate length and return response 48: if env['rubyamf.response'].constructed? 49: RubyAMF.logger.info "Sending back AMF" 50: response = env['rubyamf.response'].to_s 51: return [200, {"Content-Type" => RubyAMF::MIME_TYPE, 'Content-Length' => response.length.to_s}, [response]] 52: else 53: return result 54: end 55: end
It returns a simple HTML page confirming that the gateway is properly running
# File lib/rubyamf/request_parser.rb, line 58 58: def handle_html 59: info_url = "https://github.com/rubyamf/rubyamf" 60: html = "<html>\n <head>\n <title>RubyAMF Gateway</title>\n <style>body{margin:0;padding:0;color:#c8c8c8;background-color:#222222;}</style>\n </head>\n <body>\n <table width=\"100%\" height=\"100%\" align=\"center\" valign=\"middle\">\n <tr><td align=\"center\"><a href=\"\#{info_url}\"><img border=\"0\" src=\"\#{gateway_image_path}\" /></a></td></tr>\n </table>\n </body>\n</html>\n" 61: return [200, {'Content-Type' => 'text/html', 'Content-Length' => html.length.to_s}, [html]] 62: end
Serve rubyamf logo for html page
# File lib/rubyamf/request_parser.rb, line 78 78: def handle_image 79: path = File.join(File.dirname(__FILE__), 'gateway.png') 80: content = File.read(path) 81: size = content.respond_to?(:bytesize) ? content.bytesize : content.size 82: return [200, {'Content-Type' => 'image/png', 'Content-Length' => size.to_s}, [content]] 83: end