GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
Hongli Lai (Phusion) (author)
Fri May 09 10:53:45 -0700 2008
commit  ad9d1365ddc9271244c127cd9c1dbcc462514b29
tree    b8d327b3f1153271017cc05162db0f7ce8503c53
parent  5d0fb07a71c1f77fb80e2ac9644936f1cefef700
passenger / lib / passenger / rack / request_handler.rb
100644 82 lines (75 sloc) 2.607 kb
1
2
3
4
5
6
7
8
9
10
11
12
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
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
76
77
78
79
80
81
82
# Phusion Passenger - http://www.modrails.com/
# Copyright (C) 2008 Phusion
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
require 'passenger/passenger'
module Passenger
module Rack
 
# A request handler for Rack applications.
class RequestHandler < AbstractRequestHandler
  # Constants which exist to relieve Ruby's garbage collector.
  RACK_VERSION = "rack.version" # :nodoc:
  RACK_VERSION_VALUE = [0, 1] # :nodoc:
  RACK_INPUT = "rack.input" # :nodoc:
  RACK_ERRORS = "rack.errors" # :nodoc:
  RACK_MULTITHREAD = "rack.multithread" # :nodoc:
  RACK_MULTIPROCESS = "rack.multiprocess" # :nodoc:
  RACK_RUN_ONCE = "rack.run_once" # :nodoc:
  RACK_URL_SCHEME = "rack.url_scheme" # :nodoc:
  HTTPS = "HTTPS" # :nodoc:
  HTTPS_DOWNCASE = "https" # :nodoc:
  HTTP = "http" # :nodoc:
  YES = "yes" # :nodoc:
  ON = "on" # :nodoc:
  ONE = "one" # :nodoc:
  CRLF = "\r\n" # :nodoc:
 
  # +app+ is the Rack application object.
  def initialize(owner_pipe, app)
    super(owner_pipe)
    @app = app
  end
 
protected
  # Overrided method.
  def process_request(env, input, output)
    env[RACK_VERSION] = RACK_VERSION_VALUE
    env[RACK_INPUT] = input
    env[RACK_ERRORS] = STDERR
    env[RACK_MULTITHREAD] = false
    env[RACK_MULTIPROCESS] = true
    env[RACK_RUN_ONCE] = false
    if env[HTTPS] == YES || env[HTTPS] == ON || env[HTTPS] == ONE
      env[RACK_URL_SCHEME] = HTTPS_DOWNCASE
    else
      env[RACK_URL_SCHEME] = HTTP
    end
    
    status, headers, body = @app.call(env)
    begin
      output.write("Status: #{status}\r\n")
      headers[X_POWERED_BY] = PASSENGER_HEADER
      headers.each do |k, vs|
        vs.each do |v|
          output.write("#{k}: #{v}\r\n")
        end
      end
      output.write(CRLF)
      body.each do |s|
        output.write(s)
      end
    ensure
      body.close if body.respond_to?(:close)
    end
  end
end
 
end # module Rack
end # module Passenger