Class: Falcon::Adapters::Rewindable

Inherits:
Async::HTTP::Middleware
  • Object
show all
Defined in:
lib/falcon/adapters/rewindable.rb

Overview

Content type driven input buffering.

Constant Summary collapse

BUFFERED_MEDIA_TYPES =
%r{
	application/x-www-form-urlencoded|
	multipart/form-data|
	multipart/related|
	multipart/mixed
}x
POST =
'POST'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rewindable

Returns a new instance of Rewindable.



36
37
38
# File 'lib/falcon/adapters/rewindable.rb', line 36

def initialize(app)
	super(app)
end

Instance Method Details

#call(request) ⇒ Async::HTTP::Response

Wrap the request body in a rewindable buffer.

Returns:

  • (Async::HTTP::Response)

    the response.



56
57
58
59
60
61
62
# File 'lib/falcon/adapters/rewindable.rb', line 56

def call(request)
	if body = request.body and needs_rewind?(request)
		request.body = Async::HTTP::Body::Rewindable.new(body)
	end
	
	return super
end

#needs_rewind?(request) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/falcon/adapters/rewindable.rb', line 40

def needs_rewind?(request)
	content_type = request.headers['content-type']
	
	if request.method == POST and content_type.nil?
		return true
	end
	
	if BUFFERED_MEDIA_TYPES =~ content_type
		return true
	end
	
	return false
end