Skip to content

Commit

Permalink
Pass headers to body parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Apr 6, 2024
1 parent bcfc890 commit ebeda47
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning].

## [Unreleased]

### Changed

- BREAKING CHANGE: Pass `headers` parameter to registered `BodyParsers`. ([@skryukov])

```ruby
# Before:
Skooma::BodyParsers.register("application/xml", ->(body) { Hash.from_xml(body) })
# After:
Skooma::BodyParsers.register("application/xml", ->(body, headers:) { Hash.from_xml(body) })
```

## [0.2.3] - 2024-01-18

### Added
Expand Down
4 changes: 2 additions & 2 deletions lib/skooma/body_parsers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Skooma
module BodyParsers
class << self
DEFAULT_PARSER = ->(body) { body }
DEFAULT_PARSER = ->(body, **_options) { body }

def [](media_type)
parsers[media_type.to_s.strip.downcase] || DEFAULT_PARSER
Expand All @@ -20,7 +20,7 @@ def register(*media_types, parser)
self.parsers = {}

module JSONParser
def self.call(body)
def self.call(body, **_options)
JSON.parse(body)
rescue JSON::ParserError
body
Expand Down
11 changes: 10 additions & 1 deletion lib/skooma/env_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ def map_request(env)
{
"query" => env["rack.request.query_string"] || env["QUERY_STRING"],
"headers" => env.select { |k, _| k.start_with?("HTTP_") || PLAIN_HEADERS.include?(k) }.transform_keys { |k| k.sub(REGEXP_HTTP, "").split("_").map(&:capitalize).join("-") },
"body" => env["RAW_POST_DATA"]
"body" => env["RAW_POST_DATA"] || read_rack_input(env["rack.input"])
}
end

def read_rack_input(input)
return nil unless input.respond_to?(:rewind)

input.rewind
raw_input = input.read
input.rewind
raw_input
end

def map_response(response)
status, headers, body = response.to_a
full_body = +""
Expand Down
16 changes: 8 additions & 8 deletions lib/skooma/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ def parse_value(value)
data = {}
data["status"] = JSONSkooma::JSONNode.new(value.fetch("status"), key: "status", parent: self)
data["headers"] = Headers.new(value.fetch("headers", {}), key: "headers", parent: self)
body_value = parse_body(value["body"], data["headers"]&.[]("Content-Type"))
body_value = parse_body(value["body"], data["headers"])
data["body"] = Attribute.new(body_value, key: "body", parent: self)
["object", data]
end

def parse_body(body, content_type)
def parse_body(body, headers)
return nil unless body

parser = BodyParsers[content_type&.split(";")&.first]
parser ? parser.call(body) : body
parser = BodyParsers[headers["Content-Type"]&.value&.split(";")&.first]
parser ? parser.call(body, headers: headers) : body
end
end

Expand All @@ -83,16 +83,16 @@ def parse_value(value)
data = {}
data["query"] = Attribute.new(value.fetch("query", ""), key: "query", parent: self)
data["headers"] = Headers.new(value.fetch("headers", {}), key: "headers", parent: self)
body_value = parse_body(value["body"], data["headers"]&.[]("Content-Type"))
body_value = parse_body(value["body"], data["headers"])
data["body"] = Attribute.new(body_value, key: "body", parent: self)
["object", data]
end

def parse_body(body, content_type)
def parse_body(body, headers)
return nil unless body

parser = BodyParsers[content_type&.split(";")&.first]
parser ? parser.call(body) : body
parser = BodyParsers[headers["Content-Type"]&.value&.split(";")&.first]
parser ? parser.call(body, headers: headers) : body
end
end

Expand Down

0 comments on commit ebeda47

Please sign in to comment.