Class: Hanami::Middleware::BodyParser

Inherits:
Object
  • Object
show all
Extended by:
ClassInterface
Defined in:
lib/hanami/middleware/body_parser.rb,
lib/hanami/middleware/body_parser/errors.rb,
lib/hanami/middleware/body_parser/parser.rb,
lib/hanami/middleware/body_parser/form_parser.rb,
lib/hanami/middleware/body_parser/json_parser.rb,
lib/hanami/middleware/body_parser/class_interface.rb

Overview

HTTP request body parser

Since:

  • 1.3.0

Defined Under Namespace

Modules: ClassInterface Classes: BodyParsingError, FormParser, InvalidParserError, JsonParser, Parser, UnknownParserError

Constant Summary collapse

CONTENT_TYPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 1.3.0

"CONTENT_TYPE"
MEDIA_TYPE_MATCHER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 1.3.0

/\s*[;,]\s*/
RACK_INPUT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 1.3.0

"rack.input"
ROUTER_PARAMS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 1.3.0

"router.params"
FALLBACK_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 1.3.0

"_"

Instance Method Summary collapse

Methods included from ClassInterface

build, build_parsers, new

Constructor Details

#initialize(app, parsers) ⇒ BodyParser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of BodyParser.

Since:

  • 1.3.0



36
37
38
39
# File 'lib/hanami/middleware/body_parser.rb', line 36

def initialize(app, parsers)
  @app = app
  @parsers = parsers
end

Instance Method Details

#call(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.3.0



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hanami/middleware/body_parser.rb', line 41

def call(env)
  return @app.call(env) if env.key?(Router::ROUTER_PARSED_BODY)

  input = env[RACK_INPUT]
  return @app.call(env) unless input

  parser = @parsers[media_type(env)]
  return @app.call(env) unless parser

  # The input in Rack 3 is not rewindable. For compatbility with Rack 2, make the input
  # rewindable, rewind it for reading, then rewind once more before returning to the app.
  input = env[RACK_INPUT] = Rack::RewindableInput.new(input) unless input.respond_to?(:rewind)
  input.rewind
  body = input.read
  input.rewind

  return @app.call(env) if body.nil? || body.empty?

  env[Router::ROUTER_PARSED_BODY] = parser.parse(body, env)
  env[ROUTER_PARAMS] = _symbolize(env[Router::ROUTER_PARSED_BODY])

  @app.call(env)
end