class Rewindable
Content-type driven input buffering, specific to the needs of rack
.
Definitions
BUFFERED_MEDIA_TYPES
Media types that require buffering.
Implementation
BUFFERED_MEDIA_TYPES = %r{
application/x-www-form-urlencoded|
multipart/form-data|
multipart/related|
multipart/mixed
}x
def initialize(app)
Initialize the rewindable middleware.
Signature
-
parameter
app
Protocol::HTTP::Middleware
The middleware to wrap.
Implementation
def initialize(app)
super(app)
end
def needs_rewind?(request)
Determine whether the request needs a rewindable body.
Signature
-
parameter
request
Protocol::HTTP::Request
-
returns
Boolean
Implementation
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
def call(request)
Wrap the request body in a rewindable buffer if required.
Signature
-
parameter
request
Protocol::HTTP::Request
-
returns
Protocol::HTTP::Response
the response.
Implementation
def call(request)
if body = request.body and needs_rewind?(request)
request.body = Protocol::HTTP::Body::Rewindable.new(body)
end
return super
end