Class: Mongrel::Camping::CampingHandler

Inherits:
HttpHandler
  • Object
show all
Defined in:
lib/mongrel/camping.rb

Overview

This is a specialized handler for Camping applications that has them process the request and then translates the results into something the Mongrel::HttpResponse needs.

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ CampingHandler

Returns a new instance of CampingHandler.



36
37
38
# File 'lib/mongrel/camping.rb', line 36

def initialize(klass)
  @klass = klass
end

Instance Method Details

#process(request, response) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mongrel/camping.rb', line 40

def process(request, response)
  controller = @klass.run(request.body, request.params)
  sendfile, clength = nil
  response.status = controller.status
  controller.headers.each do |k, v|
    if k =~ /^X-SENDFILE$/i
      sendfile = v
    elsif k =~ /^CONTENT-LENGTH$/i
      clength = v.to_i
    else
      [*v].each do |vi|
        response.header[k] = vi
      end
    end
  end

  if sendfile
    response.send_status(File.size(sendfile))
    response.send_header
    response.send_file(sendfile)
  elsif controller.body.respond_to? :read
    response.send_status(clength)
    response.send_header
    while chunk = controller.body.read(16384)
      response.write(chunk)
    end
    if controller.body.respond_to? :close
      controller.body.close
    end
  else
    body = controller.body.to_s
    response.send_status(body.length)
    response.send_header
    response.write(body)
  end
end