class Rack::Events

def call(env)

def call(env)
  request = make_request env
  on_start request, nil
  begin
    status, headers, body = @app.call request.env
    response = make_response status, headers, body
    on_commit request, response
  rescue StandardError => e
    on_error request, response, e
    on_finish request, response
    raise
  end
  body = EventedBodyProxy.new(body, request, response, @handlers) do
    on_finish request, response
  end
  [response.status, response.headers, body]
end

def initialize(app, handlers)

def initialize(app, handlers)
  @app      = app
  @handlers = handlers
end

def make_request(env)

def make_request(env)
  Rack::Request.new env
end

def make_response(status, headers, body)

def make_response(status, headers, body)
  BufferedResponse.new status, headers, body
end

def on_commit(request, response)

def on_commit(request, response)
  @handlers.reverse_each { |handler| handler.on_commit request, response }
end

def on_error(request, response, e)

def on_error(request, response, e)
  @handlers.reverse_each { |handler| handler.on_error request, response, e }
end

def on_finish(request, response)

def on_finish(request, response)
  @handlers.reverse_each { |handler| handler.on_finish request, response }
end

def on_start(request, response)

def on_start(request, response)
  @handlers.each { |handler| handler.on_start request, nil }
end