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.



42
43
44
# File 'lib/protocol/rack/rewindable.rb', line 42

def initialize(app)
	super(app)
end

Instance Method Details

#call(request) ⇒ Object

Wrap the request body in a rewindable buffer if required.



70
71
72
73
74
75
76
# File 'lib/protocol/rack/rewindable.rb', line 70

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



63
64
65
# File 'lib/protocol/rack/rewindable.rb', line 63

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

#needs_rewind?(request) ⇒ Boolean

Determine whether the request needs a rewindable body.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/protocol/rack/rewindable.rb', line 49

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