Class: DAV4RackExt::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/dav4rack_ext/handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Handler

include DAV4Rack::HTTPStatus



7
8
9
10
11
# File 'lib/dav4rack_ext/handler.rb', line 7

def initialize(options = {})
  @options          = options.dup
  @logger           = options.delete(:logger)
  @controller_class = options.delete(:controller_class) || DAV4Rack::Controller
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dav4rack_ext/handler.rb', line 13

def call(env)
  request  = Rack::Request.new(env)
  response = Rack::Response.new

  begin
    controller = @controller_class.new(request, response, @options.dup, env)
    res = controller.send(request.request_method.downcase)
    response.status = res.code if res.respond_to?(:code)

  rescue DAV4Rack::HTTPStatus::Status => status
    response.status = status.code
  end

  response['Content-Length'] = response.body.to_s.bytesize unless response['Content-Length'] || !response.body.is_a?(String)
  response.body = [response.body] unless response.body.respond_to? :each
  response.status = response.status ? response.status.to_i : 200
  response.headers.keys.each do |k|
    response.headers[k] = response[k].to_s
  end

  while request.body.read(8192)
    # Apache wants the body dealt with, so just read it and junk it
  end

  response.finish
rescue Exception => e
  @logger.error "DAV Error: #{e}\n#{e.backtrace.join("\n")}"
  raise e
end