| 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 |
class Dispatcher |
|---|
| 28 |
|
|---|
| 29 |
class << self |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
def dispatch(cgi = nil, session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) |
|---|
| 36 |
controller = nil |
|---|
| 37 |
if cgi ||= new_cgi(output) |
|---|
| 38 |
request, response = ActionController::CgiRequest.new(cgi, session_options), ActionController::CgiResponse.new(cgi) |
|---|
| 39 |
prepare_application |
|---|
| 40 |
controller = ActionController::Routing::Routes.recognize(request) |
|---|
| 41 |
controller.process(request, response).out(output) |
|---|
| 42 |
end |
|---|
| 43 |
rescue Exception => exception |
|---|
| 44 |
failsafe_response(output, '500 Internal Server Error', exception) do |
|---|
| 45 |
controller ||= ApplicationController rescue LoadError nil |
|---|
| 46 |
controller ||= ActionController::Base |
|---|
| 47 |
controller.process_with_exception(request, response, exception).out(output) |
|---|
| 48 |
end |
|---|
| 49 |
ensure |
|---|
| 50 |
|
|---|
| 51 |
reset_after_dispatch |
|---|
| 52 |
end |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
def reset_application! |
|---|
| 58 |
ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord) |
|---|
| 59 |
|
|---|
| 60 |
Dependencies.clear |
|---|
| 61 |
ActiveSupport::Deprecation.silence do |
|---|
| 62 |
Class.remove_class(*Reloadable.reloadable_classes) |
|---|
| 63 |
end |
|---|
| 64 |
|
|---|
| 65 |
ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
def to_prepare(identifier = nil, &block) |
|---|
| 77 |
unless identifier.nil? |
|---|
| 78 |
callback = preparation_callbacks.detect { |ident, _| ident == identifier } |
|---|
| 79 |
|
|---|
| 80 |
if callback |
|---|
| 81 |
callback[-1] = block |
|---|
| 82 |
return |
|---|
| 83 |
end |
|---|
| 84 |
end |
|---|
| 85 |
|
|---|
| 86 |
preparation_callbacks << [identifier, block] |
|---|
| 87 |
|
|---|
| 88 |
return |
|---|
| 89 |
end |
|---|
| 90 |
|
|---|
| 91 |
private |
|---|
| 92 |
|
|---|
| 93 |
attr_accessor :preparation_callbacks, :preparation_callbacks_run |
|---|
| 94 |
alias_method :preparation_callbacks_run?, :preparation_callbacks_run |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
def new_cgi(output) |
|---|
| 100 |
failsafe_response(output, '400 Bad Request') { CGI.new } |
|---|
| 101 |
end |
|---|
| 102 |
|
|---|
| 103 |
def prepare_application |
|---|
| 104 |
if Dependencies.load? |
|---|
| 105 |
ActionController::Routing::Routes.reload |
|---|
| 106 |
self.preparation_callbacks_run = false |
|---|
| 107 |
end |
|---|
| 108 |
|
|---|
| 109 |
prepare_breakpoint |
|---|
| 110 |
require_dependency 'application' unless Object.const_defined?(:ApplicationController) |
|---|
| 111 |
ActiveRecord::Base.verify_active_connections! if defined?(ActiveRecord) |
|---|
| 112 |
run_preparation_callbacks |
|---|
| 113 |
end |
|---|
| 114 |
|
|---|
| 115 |
def reset_after_dispatch |
|---|
| 116 |
reset_application! if Dependencies.load? |
|---|
| 117 |
Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT) |
|---|
| 118 |
end |
|---|
| 119 |
|
|---|
| 120 |
def prepare_breakpoint |
|---|
| 121 |
return unless defined?(BREAKPOINT_SERVER_PORT) |
|---|
| 122 |
require 'breakpoint' |
|---|
| 123 |
Breakpoint.activate_drb("druby://localhost:#{BREAKPOINT_SERVER_PORT}", nil, !defined?(FastCGI)) |
|---|
| 124 |
true |
|---|
| 125 |
rescue |
|---|
| 126 |
nil |
|---|
| 127 |
end |
|---|
| 128 |
|
|---|
| 129 |
def run_preparation_callbacks |
|---|
| 130 |
return if preparation_callbacks_run? |
|---|
| 131 |
preparation_callbacks.each { |_, callback| callback.call } |
|---|
| 132 |
self.preparation_callbacks_run = true |
|---|
| 133 |
end |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
def failsafe_response(output, status, exception = nil) |
|---|
| 137 |
yield |
|---|
| 138 |
rescue Exception |
|---|
| 139 |
begin |
|---|
| 140 |
output.write "Status: #{status}\r\n" |
|---|
| 141 |
|
|---|
| 142 |
if exception |
|---|
| 143 |
message = exception.to_s + "\r\n" + exception.backtrace.join("\r\n") |
|---|
| 144 |
error_path = File.join(RAILS_ROOT, 'public', '500.html') |
|---|
| 145 |
|
|---|
| 146 |
if defined?(RAILS_DEFAULT_LOGGER) && !RAILS_DEFAULT_LOGGER.nil? |
|---|
| 147 |
RAILS_DEFAULT_LOGGER.fatal(message) |
|---|
| 148 |
|
|---|
| 149 |
output.write "Content-Type: text/html\r\n\r\n" |
|---|
| 150 |
|
|---|
| 151 |
if File.exists?(error_path) |
|---|
| 152 |
output.write(IO.read(error_path)) |
|---|
| 153 |
else |
|---|
| 154 |
output.write("<html><body><h1>Application error (Rails)</h1></body></html>") |
|---|
| 155 |
end |
|---|
| 156 |
else |
|---|
| 157 |
output.write "Content-Type: text/plain\r\n\r\n" |
|---|
| 158 |
output.write(message) |
|---|
| 159 |
end |
|---|
| 160 |
end |
|---|
| 161 |
rescue Exception |
|---|
| 162 |
end |
|---|
| 163 |
end |
|---|
| 164 |
end |
|---|
| 165 |
|
|---|
| 166 |
self.preparation_callbacks = [] |
|---|
| 167 |
self.preparation_callbacks_run = false |
|---|
| 168 |
|
|---|
| 169 |
end |
|---|
| 170 |
|
|---|
| 171 |
Dispatcher.to_prepare :activerecord_instantiate_observers do |
|---|
| 172 |
ActiveRecord::Base.instantiate_observers |
|---|
| 173 |
end if defined?(ActiveRecord) |
|---|