| 1 |
require 'action_controller/cgi_ext' |
|---|
| 2 |
require 'action_controller/session/cookie_store' |
|---|
| 3 |
|
|---|
| 4 |
module ActionController |
|---|
| 5 |
class Base |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
def self.process_cgi(cgi = CGI.new, session_options = {}) |
|---|
| 27 |
new.process_cgi(cgi, session_options) |
|---|
| 28 |
end |
|---|
| 29 |
|
|---|
| 30 |
def process_cgi(cgi, session_options = {}) |
|---|
| 31 |
process(CgiRequest.new(cgi, session_options), CgiResponse.new(cgi)).out |
|---|
| 32 |
end |
|---|
| 33 |
end |
|---|
| 34 |
|
|---|
| 35 |
class CgiRequest < AbstractRequest |
|---|
| 36 |
attr_accessor :cgi, :session_options |
|---|
| 37 |
class SessionFixationAttempt < StandardError |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
DEFAULT_SESSION_OPTIONS = { |
|---|
| 41 |
:database_manager => CGI::Session::CookieStore, |
|---|
| 42 |
:prefix => "ruby_sess.", |
|---|
| 43 |
:session_path => "/", |
|---|
| 44 |
:session_key => "_session_id", |
|---|
| 45 |
:cookie_only => true |
|---|
| 46 |
} unless const_defined?(:DEFAULT_SESSION_OPTIONS) |
|---|
| 47 |
|
|---|
| 48 |
def initialize(cgi, session_options = {}) |
|---|
| 49 |
@cgi = cgi |
|---|
| 50 |
@session_options = session_options |
|---|
| 51 |
@env = @cgi.send!(:env_table) |
|---|
| 52 |
super() |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
def query_string |
|---|
| 56 |
qs = @cgi.query_string if @cgi.respond_to?(:query_string) |
|---|
| 57 |
if !qs.blank? |
|---|
| 58 |
qs |
|---|
| 59 |
else |
|---|
| 60 |
super |
|---|
| 61 |
end |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
def body |
|---|
| 67 |
if raw_post = env['RAW_POST_DATA'] |
|---|
| 68 |
StringIO.new(raw_post) |
|---|
| 69 |
else |
|---|
| 70 |
@cgi.stdinput |
|---|
| 71 |
end |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
def query_parameters |
|---|
| 75 |
@query_parameters ||= self.class.parse_query_parameters(query_string) |
|---|
| 76 |
end |
|---|
| 77 |
|
|---|
| 78 |
def request_parameters |
|---|
| 79 |
@request_parameters ||= parse_formatted_request_parameters |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
def cookies |
|---|
| 83 |
@cgi.cookies.freeze |
|---|
| 84 |
end |
|---|
| 85 |
|
|---|
| 86 |
def host_with_port_without_standard_port_handling |
|---|
| 87 |
if forwarded = env["HTTP_X_FORWARDED_HOST"] |
|---|
| 88 |
forwarded.split(/,\s?/).last |
|---|
| 89 |
elsif http_host = env['HTTP_HOST'] |
|---|
| 90 |
http_host |
|---|
| 91 |
elsif server_name = env['SERVER_NAME'] |
|---|
| 92 |
server_name |
|---|
| 93 |
else |
|---|
| 94 |
"#{env['SERVER_ADDR']}:#{env['SERVER_PORT']}" |
|---|
| 95 |
end |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
def host |
|---|
| 99 |
host_with_port_without_standard_port_handling.sub(/:\d+$/, '') |
|---|
| 100 |
end |
|---|
| 101 |
|
|---|
| 102 |
def port |
|---|
| 103 |
if host_with_port_without_standard_port_handling =~ /:(\d+)$/ |
|---|
| 104 |
$1.to_i |
|---|
| 105 |
else |
|---|
| 106 |
standard_port |
|---|
| 107 |
end |
|---|
| 108 |
end |
|---|
| 109 |
|
|---|
| 110 |
def session |
|---|
| 111 |
unless defined?(@session) |
|---|
| 112 |
if @session_options == false |
|---|
| 113 |
@session = Hash.new |
|---|
| 114 |
else |
|---|
| 115 |
stale_session_check! do |
|---|
| 116 |
if cookie_only? && query_parameters[session_options_with_string_keys['session_key']] |
|---|
| 117 |
raise SessionFixationAttempt |
|---|
| 118 |
end |
|---|
| 119 |
case value = session_options_with_string_keys['new_session'] |
|---|
| 120 |
when true |
|---|
| 121 |
@session = new_session |
|---|
| 122 |
when false |
|---|
| 123 |
begin |
|---|
| 124 |
@session = CGI::Session.new(@cgi, session_options_with_string_keys) |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
rescue ArgumentError |
|---|
| 128 |
@session = Hash.new |
|---|
| 129 |
end |
|---|
| 130 |
when nil |
|---|
| 131 |
@session = CGI::Session.new(@cgi, session_options_with_string_keys) |
|---|
| 132 |
else |
|---|
| 133 |
raise ArgumentError, "Invalid new_session option: #{value}" |
|---|
| 134 |
end |
|---|
| 135 |
@session['__valid_session'] |
|---|
| 136 |
end |
|---|
| 137 |
end |
|---|
| 138 |
end |
|---|
| 139 |
@session |
|---|
| 140 |
end |
|---|
| 141 |
|
|---|
| 142 |
def reset_session |
|---|
| 143 |
@session.delete if defined?(@session) && @session.is_a?(CGI::Session) |
|---|
| 144 |
@session = new_session |
|---|
| 145 |
end |
|---|
| 146 |
|
|---|
| 147 |
def method_missing(method_id, *arguments) |
|---|
| 148 |
@cgi.send!(method_id, *arguments) rescue super |
|---|
| 149 |
end |
|---|
| 150 |
|
|---|
| 151 |
private |
|---|
| 152 |
|
|---|
| 153 |
def new_session |
|---|
| 154 |
if @session_options == false |
|---|
| 155 |
Hash.new |
|---|
| 156 |
else |
|---|
| 157 |
CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => false)).delete rescue nil |
|---|
| 158 |
CGI::Session.new(@cgi, session_options_with_string_keys.merge("new_session" => true)) |
|---|
| 159 |
end |
|---|
| 160 |
end |
|---|
| 161 |
|
|---|
| 162 |
def cookie_only? |
|---|
| 163 |
session_options_with_string_keys['cookie_only'] |
|---|
| 164 |
end |
|---|
| 165 |
|
|---|
| 166 |
def stale_session_check! |
|---|
| 167 |
yield |
|---|
| 168 |
rescue ArgumentError => argument_error |
|---|
| 169 |
if argument_error.message =~ %r{undefined class/module ([\w:]*\w)} |
|---|
| 170 |
begin |
|---|
| 171 |
|
|---|
| 172 |
$1.constantize |
|---|
| 173 |
rescue LoadError, NameError => const_error |
|---|
| 174 |
raise ActionController::SessionRestoreError, <<-end_msg |
|---|
| 175 |
Session contains objects whose class definition isn\'t available. |
|---|
| 176 |
Remember to require the classes for all objects kept in the session. |
|---|
| 177 |
(Original exception: |
|---|
| 178 |
end_msg |
|---|
| 179 |
end |
|---|
| 180 |
|
|---|
| 181 |
retry |
|---|
| 182 |
else |
|---|
| 183 |
raise |
|---|
| 184 |
end |
|---|
| 185 |
end |
|---|
| 186 |
|
|---|
| 187 |
def session_options_with_string_keys |
|---|
| 188 |
@session_options_with_string_keys ||= DEFAULT_SESSION_OPTIONS.merge(@session_options).stringify_keys |
|---|
| 189 |
end |
|---|
| 190 |
end |
|---|
| 191 |
|
|---|
| 192 |
class CgiResponse < AbstractResponse |
|---|
| 193 |
def initialize(cgi) |
|---|
| 194 |
@cgi = cgi |
|---|
| 195 |
super() |
|---|
| 196 |
end |
|---|
| 197 |
|
|---|
| 198 |
def out(output = $stdout) |
|---|
| 199 |
output.binmode if output.respond_to?(:binmode) |
|---|
| 200 |
output.sync = false if output.respond_to?(:sync=) |
|---|
| 201 |
|
|---|
| 202 |
begin |
|---|
| 203 |
output.write(@cgi.header(@headers)) |
|---|
| 204 |
|
|---|
| 205 |
if @cgi.send!(:env_table)['REQUEST_METHOD'] == 'HEAD' |
|---|
| 206 |
return |
|---|
| 207 |
elsif @body.respond_to?(:call) |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
output.flush if output.respond_to?(:flush) |
|---|
| 211 |
@body.call(self, output) |
|---|
| 212 |
else |
|---|
| 213 |
output.write(@body) |
|---|
| 214 |
end |
|---|
| 215 |
|
|---|
| 216 |
output.flush if output.respond_to?(:flush) |
|---|
| 217 |
rescue Errno::EPIPE, Errno::ECONNRESET |
|---|
| 218 |
|
|---|
| 219 |
end |
|---|
| 220 |
end |
|---|
| 221 |
end |
|---|
| 222 |
end |
|---|