Skip to content

Commit

Permalink
refactor the request body reader so it's easier to understand. Small …
Browse files Browse the repository at this point in the history
…performance boost. Fixes #1825 (#1826)
  • Loading branch information
jwoertink authored Sep 4, 2023
1 parent 2e92959 commit 3f15bc8
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/lucky/request_body_reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
class Lucky::RequestBodyReader
getter request : HTTP::Request

def initialize(@request)
def initialize(@request : HTTP::Request)
end

# Returns the body of the `request` and
# reassigns the value back to the request body
# to allow for re-reading
def body : String
(request.body || IO::Memory.new).gets_to_end.tap do |request_body|
request.body = IO::Memory.new(request_body)
end
contents = request.body.try(&.gets_to_end) || ""
request.body = IO::Memory.new(contents)
contents
end
end

0 comments on commit 3f15bc8

Please sign in to comment.