Class: Protocol::Rack::Rewindable

Inherits:
HTTP::Middleware
  • Object
show all
Defined in:
lib/protocol/rack/rewindable.rb

Overview

Content-type driven input buffering, specific to the needs of ‘rack`.

Constant Summary collapse

BUFFERED_MEDIA_TYPES =

Media types that require buffering.

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rewindable

Initialize the rewindable middleware.



25
26
27
# File 'lib/protocol/rack/rewindable.rb', line 25

def initialize(app)
	super(app)
end

Instance Method Details

#call(request) ⇒ Object

Wrap the request body in a rewindable buffer if required.



53
54
55
56
57
58
59
# File 'lib/protocol/rack/rewindable.rb', line 53

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

#make_environment(request) ⇒ Object



46
47
48
# File 'lib/protocol/rack/rewindable.rb', line 46

def make_environment(request)
	@delegate.make_environment(request)
end

#needs_rewind?(request) ⇒ Boolean

Determine whether the request needs a rewindable body.

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/protocol/rack/rewindable.rb', line 32

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