Sha256: 6ea3ed1881b635487e2ccc16068313d71c1c54bc5c5a72e7b1a292d223e43a61

Contents?: true

Size: 1.04 KB

Versions: 36

Compression:

Stored size: 1.04 KB

Contents

require 'rhoconnect/middleware/helpers'

module Rhoconnect
  module Middleware
    class BodyContentTypeParser
      # Constants
      #
      CONTENT_TYPE = 'CONTENT_TYPE'.freeze
      POST_BODY = 'rack.input'.freeze
      FORM_INPUT = 'rack.request.form_input'.freeze
      FORM_HASH = 'rack.request.form_hash'.freeze

      def initialize(app)
        @app = app
      end

      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
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
rhoconnect-7.6.0 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-7.5.1 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-7.4.1 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-7.1.17 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-6.2.0 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-6.0.11 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-5.5.18 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-5.5.17 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-5.5.15 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-5.5.0.22 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-5.5.2 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-5.5.0.7 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-5.5.0.3 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-5.5.0 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-5.1.1 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-4.0.4 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-4.0.3 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-4.0.2 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-4.0.1 lib/rhoconnect/middleware/body_content_type_parser.rb
rhoconnect-4.0.0 lib/rhoconnect/middleware/body_content_type_parser.rb