Module: Skooma::BodyParsers::MultipartParser

Defined in:
lib/skooma/body_parsers.rb

Overview

Parses multipart bodies with Rack’s multipart parser (the boundary is taken from the request’s own Content-Type header). File parts are replaced by their content read as a binary string, so they validate against ‘type: string` / `format: binary` schemas.

Class Method Summary collapse

Class Method Details

.call(body, headers: nil, **_options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/skooma/body_parsers.rb', line 72

def self.call(body, headers: nil, **_options)
  content_type = headers && headers["Content-Type"]&.value
  return body if content_type.nil? || body.nil?

  input = StringIO.new(body.to_s.dup.force_encoding(Encoding::BINARY))
  params = Rack::Multipart.parse_multipart(
    "CONTENT_TYPE" => content_type,
    "CONTENT_LENGTH" => input.size.to_s,
    "rack.input" => input
  )
  return body if params.nil?

  simplify(params)
# Rack 2/3 raise different errors on malformed multipart (Multipart::Error,
# EOFError, ...); fall back to the raw string so validation rejects it.
rescue
  body
end