Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting Malformed JSON in the request body when request.body is not rewindable #19

Open
senid231 opened this issue Dec 22, 2019 · 2 comments

Comments

@senid231
Copy link

senid231 commented Dec 22, 2019

I'm using Falcon as http server for sinatra app that uses sinja for json:api.
I've encountered a problem that my request.body is always malformed json.
After digging in I've found that Falcon uses Protocol::HTTP1::Body::Fixed as body class which is not rewindable. method #content? reads first 2 bytes to check if request has body. after that #deserialize_request_body always retrieves part of content, because in this case env['rack.input'] is an instance of Falcon::Adapters::Input which silently ignores rewind, because it's underlying body Protocol::HTTP1::Body::Fixed does not respond to it. I want to leave this issue here - maybe someone will looking solution for same problem.

I think it worth for PR - we can use #empty? on body if it responds to #empty?.

my bugfix is following

module SinjaPatch
  def content?
    return request.body.size > 0 if request.body.respond_to?(:size)
    return !request.body.empty? if request.body.respond_to?(:empty?)
    request.body.rewind
    request.body.read(1)
  end
end

module FalconAdaptersInputPatch
  def empty?
    @body.respond_to?(:empty?) ? @body.empty? : @body.size
  end
end

Falcon::Adapters::Input.include FalconAdaptersInputPatch
# must be added after `register Sinatra::JSONAPI` performed.
MyApp.prepend SinjaPatch
@senid231
Copy link
Author

this thread related to this problem
rack/rack#1148

@mwpastore
Copy link
Owner

Hi @senid231, thank you for reporting this issue. I'll take a look at it over the weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants