Class: Raw::MongrelHandler

Inherits:
Mongrel::HttpHandler
  • Object
show all
Includes:
AdapterHandlerMixin
Defined in:
lib/raw/adapter/mongrel.rb

Overview

The Mongrel Handler, handles an HTTP request.

Instance Method Summary collapse

Methods included from AdapterHandlerMixin

#handle_context, #rewrite, #unrewrite

Constructor Details

#initialize(application) ⇒ MongrelHandler

Returns a new instance of MongrelHandler.



67
68
69
# File 'lib/raw/adapter/mongrel.rb', line 67

def initialize(application)
  @application = application
end

Instance Method Details

#handle_file(req, res) ⇒ Object

Handle a static file. Also handles cached pages. Typically not used in production applications.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/raw/adapter/mongrel.rb', line 74

def handle_file(req, res)
  return false unless @application.handle_static_files    

  filename = File.join(@application.public_dir, req.path_info).squeeze("/")

  File.open(filename, "rb") do |f|
    # TODO: check whether path circumvents public_root directory?
    res.status = 200
    res.body << f.read # XXX inefficient for large files, may cause leaks
  end
  
  return true
rescue Errno::ENOENT => ex # TODO: Lookup Win32 error for 'file missing'
  return false
end

#process(req, res) ⇒ Object

Handle the request. – TODO: recode this like the camping mongrel handler. ++



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/raw/adapter/mongrel.rb', line 95

def process(req, res)
  # Perform default rewriting rules.
  
  rewrite(req)

  # First, try to serve a static file from disk.

  return if handle_file(req, res)

  context = Context.new(@application)

  if req.body.is_a? String
    context.in = StringIO.new(req.body)
  else
    context.in =   req.body
  end

  context.headers = {}
  req.params.each { |h, v|
    if h =~ /\AHTTP_(.*)\Z/
      context.headers[$1.gsub("_", "-")] = v
    end
    context.headers[h] = v
  }
  
  # hackfix: make it behave like webrick and fcgi
  context.headers["REQUEST_URI"] << "?#{context.headers['QUERY_STRING']}" if context.headers["QUERY_STRING"]
  context.headers["QUERY_STRING"] ||= ""

  output = handle_context(context)

  # THINK: what is the error code if a request without a handler 
  # is comming?!
  
  res.start(context.status, true) do |head, out|
    context.response_headers.each do |key, value|
      head[key] = value
    end
    
    context.response_cookies.each do |cookie|
      head["Set-Cookie"] = cookie
    end if context.response_cookies
    
    out.write(output)
  end

  context.close
end