Froze rails gems
[depot.git] / vendor / rails / railties / lib / webrick_server.rb
1 # Donated by Florian Gross
2
3 require 'webrick'
4 require 'cgi'
5 require 'stringio'
6 require 'dispatcher'
7
8 include WEBrick
9
10 class CGI #:nodoc:
11 def stdinput
12 @stdin || $stdin
13 end
14
15 def env_table
16 @env_table || ENV
17 end
18
19 def initialize(type = "query", table = nil, stdin = nil)
20 @env_table, @stdin = table, stdin
21
22 if defined?(MOD_RUBY) && !ENV.key?("GATEWAY_INTERFACE")
23 Apache.request.setup_cgi_env
24 end
25
26 extend QueryExtension
27 @multipart = false
28 if defined?(CGI_PARAMS)
29 warn "do not use CGI_PARAMS and CGI_COOKIES"
30 @params = CGI_PARAMS.dup
31 @cookies = CGI_COOKIES.dup
32 else
33 initialize_query() # set @params, @cookies
34 end
35 @output_cookies = nil
36 @output_hidden = nil
37 end
38 end
39
40 # A custom dispatch servlet for use with WEBrick. It dispatches requests
41 # (using the Rails Dispatcher) to the appropriate controller/action. By default,
42 # it restricts WEBrick to a managing a single Rails request at a time, but you
43 # can change this behavior by setting ActionController::Base.allow_concurrency
44 # to true.
45 class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet
46 # Start the WEBrick server with the given options, mounting the
47 # DispatchServlet at <tt>/</tt>.
48 def self.dispatch(options = {})
49 Socket.do_not_reverse_lookup = true # patch for OS X
50
51 params = { :Port => options[:port].to_i,
52 :ServerType => options[:server_type],
53 :BindAddress => options[:ip] }
54 params[:MimeTypes] = options[:mime_types] if options[:mime_types]
55
56 server = WEBrick::HTTPServer.new(params)
57 server.mount('/', DispatchServlet, options)
58
59 trap("INT") { server.shutdown }
60 server.start
61 end
62
63 def initialize(server, options) #:nodoc:
64 @server_options = options
65 @file_handler = WEBrick::HTTPServlet::FileHandler.new(server, options[:server_root])
66 # Change to the RAILS_ROOT, since Webrick::Daemon.start does a Dir::cwd("/")
67 # OPTIONS['working_directory'] is an absolute path of the RAILS_ROOT, set in railties/lib/commands/servers/webrick.rb
68 Dir.chdir(OPTIONS['working_directory']) if defined?(OPTIONS) && File.directory?(OPTIONS['working_directory'])
69 super
70 end
71
72 def service(req, res) #:nodoc:
73 unless handle_file(req, res)
74 unless handle_dispatch(req, res)
75 raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found."
76 end
77 end
78 end
79
80 def handle_file(req, res) #:nodoc:
81 begin
82 req = req.dup
83 path = req.path.dup
84
85 # Add .html if the last path piece has no . in it
86 path << '.html' if path != '/' && (%r{(^|/)[^./]+$} =~ path)
87 path.gsub!('+', ' ') # Unescape + since FileHandler doesn't do so.
88
89 req.instance_variable_set(:@path_info, path) # Set the modified path...
90
91 @file_handler.send(:service, req, res)
92 return true
93 rescue HTTPStatus::PartialContent, HTTPStatus::NotModified => err
94 res.set_error(err)
95 return true
96 rescue => err
97 return false
98 end
99 end
100
101 def handle_dispatch(req, res, origin = nil) #:nodoc:
102 data = StringIO.new
103 Dispatcher.dispatch(
104 CGI.new("query", create_env_table(req, origin), StringIO.new(req.body || "")),
105 ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS,
106 data
107 )
108
109 header, body = extract_header_and_body(data)
110
111 set_charset(header)
112 assign_status(res, header)
113 res.cookies.concat(header.delete('set-cookie') || [])
114 header.each { |key, val| res[key] = val.join(", ") }
115
116 res.body = body
117 return true
118 rescue => err
119 p err, err.backtrace
120 return false
121 end
122
123 private
124 def create_env_table(req, origin)
125 env = req.meta_vars.clone
126 env.delete "SCRIPT_NAME"
127 env["QUERY_STRING"] = req.request_uri.query
128 env["REQUEST_URI"] = origin if origin
129 return env
130 end
131
132 def extract_header_and_body(data)
133 data.rewind
134 data = data.read
135
136 raw_header, body = *data.split(/^[\xd\xa]{2}/on, 2)
137 header = WEBrick::HTTPUtils::parse_header(raw_header)
138
139 return header, body
140 end
141
142 def set_charset(header)
143 ct = header["content-type"]
144 if ct.any? { |x| x =~ /^text\// } && ! ct.any? { |x| x =~ /charset=/ }
145 ch = @server_options[:charset] || "UTF-8"
146 ct.find { |x| x =~ /^text\// } << ("; charset=" + ch)
147 end
148 end
149
150 def assign_status(res, header)
151 if /^(\d+)/ =~ header['status'][0]
152 res.status = $1.to_i
153 header.delete('status')
154 end
155 end
156 end