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.

Constant Summary collapse

@@file_only_methods =
["GET","HEAD"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ CampingHandler

Returns a new instance of CampingHandler.



40
41
42
43
44
# File 'lib/mongrel/camping.rb', line 40

def initialize(klass)
  @files = Mongrel::DirHandler.new(nil, false)
  @guard = Mutex.new
  @klass = klass
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



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

def files
  @files
end

#guardObject (readonly)

Returns the value of attribute guard.



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

def guard
  @guard
end

Instance Method Details

#process(request, response) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mongrel/camping.rb', line 46

def process(request, response)
  if response.socket.closed?
    return
  end

  controller = nil
  @guard.synchronize {
    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
    request.params[Mongrel::Const::PATH_INFO] = sendfile
    @files.process(request, response)
  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