Class: Rhoconnect::Middleware::BodyContentTypeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rhoconnect/middleware/body_content_type_parser.rb

Constant Summary collapse

CONTENT_TYPE =

Constants

'CONTENT_TYPE'.freeze
POST_BODY =
'rack.input'.freeze
FORM_INPUT =
'rack.request.form_input'.freeze
FORM_HASH =
'rack.request.form_hash'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ BodyContentTypeParser

Returns a new instance of BodyContentTypeParser.



13
14
15
# File 'lib/rhoconnect/middleware/body_content_type_parser.rb', line 13

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rhoconnect/middleware/body_content_type_parser.rb', line 17

def call(env)
  if env[CONTENT_TYPE] && env[CONTENT_TYPE].match(/^application\/json/) 
    begin
      if (body = env[POST_BODY].read).length != 0
        # for some reason , if we do not do this
        # Ruby 1.9 will fail
        env[POST_BODY] = StringIO.new(body)
        env.update(FORM_HASH => JSON.parse(body), FORM_INPUT => env[POST_BODY])
      end
    rescue JSON::ParserError => jpe
      log jpe.message + jpe.backtrace.join("\n")
      return [500, {'Content-Type' => 'text/plain'}, ["Server error while processing client data"]]
    end
  end
  @app.call(env)
end