2008-06-01 14:25:11 -04:00
|
|
|
require 'action_controller/cgi_ext'
|
|
|
|
|
|
|
|
module ActionController #:nodoc:
|
|
|
|
class RackRequest < AbstractRequest #:nodoc:
|
2008-08-08 02:34:36 -04:00
|
|
|
attr_accessor :session_options
|
2008-06-01 14:25:11 -04:00
|
|
|
|
|
|
|
class SessionFixationAttempt < StandardError #:nodoc:
|
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def initialize(env)
|
2008-06-01 14:25:11 -04:00
|
|
|
@env = env
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2008-07-15 22:18:09 -04:00
|
|
|
%w[ AUTH_TYPE GATEWAY_INTERFACE PATH_INFO
|
2008-08-19 21:22:27 -04:00
|
|
|
PATH_TRANSLATED REMOTE_HOST
|
2008-07-01 22:52:28 -04:00
|
|
|
REMOTE_IDENT REMOTE_USER SCRIPT_NAME
|
|
|
|
SERVER_NAME SERVER_PROTOCOL
|
|
|
|
|
|
|
|
HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
|
2008-08-08 05:29:37 -04:00
|
|
|
HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM
|
2008-07-01 22:52:28 -04:00
|
|
|
HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT ].each do |env|
|
|
|
|
define_method(env.sub(/^HTTP_/n, '').downcase) do
|
|
|
|
@env[env]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-19 21:22:27 -04:00
|
|
|
def query_string
|
|
|
|
qs = super
|
|
|
|
if !qs.blank?
|
|
|
|
qs
|
|
|
|
else
|
|
|
|
@env['QUERY_STRING']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-08 02:34:36 -04:00
|
|
|
def body_stream #:nodoc:
|
|
|
|
@env['rack.input']
|
2008-06-01 14:25:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def key?(key)
|
2008-07-01 22:52:28 -04:00
|
|
|
@env.key?(key)
|
2008-06-01 14:25:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cookies
|
2008-12-07 17:37:48 -05:00
|
|
|
Rack::Request.new(@env).cookies
|
2008-06-01 14:25:11 -04:00
|
|
|
end
|
|
|
|
|
2008-07-01 22:52:28 -04:00
|
|
|
def server_port
|
|
|
|
@env['SERVER_PORT'].to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def server_software
|
|
|
|
@env['SERVER_SOFTWARE'].split("/").first
|
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def session_options
|
|
|
|
@env['rack.session.options'] ||= {}
|
2008-06-01 14:25:11 -04:00
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def session_options=(options)
|
|
|
|
@env['rack.session.options'] = options
|
2008-06-01 14:25:11 -04:00
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def session
|
|
|
|
@env['rack.session'] ||= {}
|
|
|
|
end
|
2008-06-01 14:25:11 -04:00
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def reset_session
|
|
|
|
@env['rack.session'] = {}
|
|
|
|
end
|
2008-06-01 14:25:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class RackResponse < AbstractResponse #:nodoc:
|
2008-12-15 17:33:31 -05:00
|
|
|
def initialize
|
2008-06-01 14:25:11 -04:00
|
|
|
@writer = lambda { |x| @body << x }
|
|
|
|
@block = nil
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
# Retrieve status from instance variable if has already been delete
|
|
|
|
def status
|
|
|
|
@status || super
|
|
|
|
end
|
|
|
|
|
2008-12-18 14:14:09 -05:00
|
|
|
def to_a(&block)
|
2008-06-01 14:25:11 -04:00
|
|
|
@block = block
|
2008-08-17 20:09:38 -04:00
|
|
|
@status = headers.delete("Status")
|
|
|
|
if [204, 304].include?(status.to_i)
|
|
|
|
headers.delete("Content-Type")
|
|
|
|
[status, headers.to_hash, []]
|
2008-06-01 14:25:11 -04:00
|
|
|
else
|
2008-08-17 20:09:38 -04:00
|
|
|
[status, headers.to_hash, self]
|
2008-06-01 14:25:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&callback)
|
|
|
|
if @body.respond_to?(:call)
|
|
|
|
@writer = lambda { |x| callback.call(x) }
|
|
|
|
@body.call(self, self)
|
2008-06-07 01:02:23 -04:00
|
|
|
elsif @body.is_a?(String)
|
|
|
|
@body.each_line(&callback)
|
2008-06-01 14:25:11 -04:00
|
|
|
else
|
|
|
|
@body.each(&callback)
|
|
|
|
end
|
|
|
|
|
|
|
|
@writer = callback
|
|
|
|
@block.call(self) if @block
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(str)
|
|
|
|
@writer.call str.to_s
|
|
|
|
str
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@body.close if @body.respond_to?(:close)
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
@block == nil && @body.empty?
|
|
|
|
end
|
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
def prepare!
|
|
|
|
super
|
2008-06-01 14:25:11 -04:00
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
convert_language!
|
|
|
|
convert_expires!
|
|
|
|
set_status!
|
2008-12-16 21:21:27 -05:00
|
|
|
set_cookies!
|
2008-08-17 20:09:38 -04:00
|
|
|
end
|
2008-06-01 14:25:11 -04:00
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
private
|
|
|
|
def convert_language!
|
|
|
|
headers["Content-Language"] = headers.delete("language") if headers["language"]
|
|
|
|
end
|
2008-06-01 14:25:11 -04:00
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
def convert_expires!
|
|
|
|
headers["Expires"] = headers.delete("") if headers["expires"]
|
|
|
|
end
|
2008-07-01 22:52:28 -04:00
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
def convert_content_type!
|
|
|
|
super
|
|
|
|
headers['Content-Type'] = headers.delete('type') || "text/html"
|
|
|
|
headers['Content-Type'] += "; charset=" + headers.delete('charset') if headers['charset']
|
|
|
|
end
|
2008-06-01 14:25:11 -04:00
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
def set_content_length!
|
|
|
|
super
|
|
|
|
headers["Content-Length"] = headers["Content-Length"].to_s if headers["Content-Length"]
|
|
|
|
end
|
2008-06-01 14:25:11 -04:00
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
def set_status!
|
|
|
|
self.status ||= "200 OK"
|
|
|
|
end
|
2008-06-01 14:25:11 -04:00
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
def set_cookies!
|
|
|
|
# Convert 'cookie' header to 'Set-Cookie' headers.
|
|
|
|
# Because Set-Cookie header can appear more the once in the response body,
|
|
|
|
# we store it in a line break separated string that will be translated to
|
|
|
|
# multiple Set-Cookie header by the handler.
|
|
|
|
if cookie = headers.delete('cookie')
|
|
|
|
cookies = []
|
|
|
|
|
|
|
|
case cookie
|
|
|
|
when Array then cookie.each { |c| cookies << c.to_s }
|
|
|
|
when Hash then cookie.each { |_, c| cookies << c.to_s }
|
|
|
|
else cookies << cookie.to_s
|
2008-06-01 14:25:11 -04:00
|
|
|
end
|
|
|
|
|
2008-08-17 20:09:38 -04:00
|
|
|
headers['Set-Cookie'] = [headers['Set-Cookie'], cookies].flatten.compact
|
|
|
|
end
|
2008-06-01 14:25:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|