lib/grape/middleware/formatter.rb in grape-0.4.1 vs lib/grape/middleware/formatter.rb in grape-0.5.0
- old
+ new
@@ -21,56 +21,67 @@
read_body_input
end
def after
status, headers, bodies = *@app_response
- formatter = Grape::Formatter::Base.formatter_for env['api.format'], options
+ # allow content-type to be explicitly overwritten
+ api_format = mime_types[headers["Content-Type"]] || env['api.format']
+ formatter = Grape::Formatter::Base.formatter_for api_format, options
begin
bodymap = bodies.collect do |body|
formatter.call body, env
end
- rescue Exception => e
+ rescue Grape::Exceptions::InvalidFormatter => e
throw :error, :status => 500, :message => e.message
end
headers['Content-Type'] = content_type_for(env['api.format']) unless headers['Content-Type']
Rack::Response.new(bodymap, status, headers).to_a
end
private
# store read input in env['api.request.input']
def read_body_input
- if (request.post? || request.put? || request.patch?) && (! request.form_data?) && (! request.parseable_data?) && (request.content_length.to_i > 0)
- if env['rack.input'] && (body = (env['api.request.input'] = env['rack.input'].read)).length > 0
- read_rack_input body
+ if (request.post? || request.put? || request.patch?) &&
+ (! request.form_data? || ! request.media_type) &&
+ (! request.parseable_data?) &&
+ (request.content_length.to_i > 0 || request.env['HTTP_TRANSFER_ENCODING'] == 'chunked')
+
+ if (input = env['rack.input'])
+ input.rewind
+ body = env['api.request.input'] = input.read
+ begin
+ read_rack_input(body) if body && body.length > 0
+ ensure
+ input.rewind
+ end
end
end
end
# store parsed input in env['api.request.body']
def read_rack_input(body)
- begin
- fmt = mime_types[request.media_type] if request.media_type
- if content_type_for(fmt)
- parser = Grape::Parser::Base.parser_for fmt, options
- if parser
- begin
- body = (env['api.request.body'] = parser.call(body, env))
- if body.is_a?(Hash)
- env['rack.request.form_hash'] = env['rack.request.form_hash'] ?
- env['rack.request.form_hash'].merge(body) :
- body
- end
+ fmt = mime_types[request.media_type] if request.media_type
+ fmt ||= options[:default_format]
+ if content_type_for(fmt)
+ parser = Grape::Parser::Base.parser_for fmt, options
+ if parser
+ begin
+ body = (env['api.request.body'] = parser.call(body, env))
+ if body.is_a?(Hash)
+ env['rack.request.form_hash'] = env['rack.request.form_hash'] ?
+ env['rack.request.form_hash'].merge(body) :
+ body
env['rack.request.form_input'] = env['rack.input']
- rescue Exception => e
- throw :error, :status => 400, :message => e.message
end
+ rescue Exception => e
+ throw :error, :status => 400, :message => e.message
end
else
- throw :error, :status => 406, :message => "The requested content-type '#{request.media_type}' is not supported."
+ env['api.request.body'] = body
end
- ensure
- env['rack.input'].rewind
+ else
+ throw :error, :status => 406, :message => "The requested content-type '#{request.media_type}' is not supported."
end
end
def negotiate_content_type
fmt = format_from_extension || format_from_params || options[:format] || format_from_header || options[:default_format]