class Rack::Radar::RackRadarSession
Attributes
header[R]
headers[R]
last_request[R]
last_response[R]
Public Class Methods
new(app)
click to toggle source
# File lib/rack/radar/session.rb, line 9 def initialize app @app = assert_valid_app(app) @headers, @cookies = {}, RackRadarCookies.new end
Public Instance Methods
__r__session()
click to toggle source
# File lib/rack/radar/session.rb, line 71 def __r__session self end
invoke_request(scheme, request_method, base_url, path, params, env)
click to toggle source
# File lib/rack/radar/session.rb, line 48 def invoke_request scheme, request_method, base_url, path, params, env uri = uri(scheme, base_url, path) env = RACK_RADAR__DEFAULT_ENV.merge(env) env.update :method => request_method env.update :params => normalize_params(params) env.update 'HTTP_COOKIE' => cookies.to_s(uri) env.update basic_auth_header env.update token_auth_header process_request(@app, uri, env) if @digest_auth && @last_response.status == 401 && (challenge = @last_response['WWW-Authenticate']) env.update(digest_auth_header(challenge, uri.path, request_method)) process_request(@app, uri, env) end cookies.persist(@last_response.header['Set-Cookie'], uri) @last_response.respond_to?(:finish) && @last_response.finish @last_response end
reset_auth!()
click to toggle source
# File lib/rack/radar/session.rb, line 42 def reset_auth! reset_basic_auth! reset_digest_auth! reset_token_auth! end
reset_basic_auth!()
click to toggle source
# File lib/rack/radar/session.rb, line 30 def reset_basic_auth! @basic_auth = nil end
reset_digest_auth!()
click to toggle source
# File lib/rack/radar/session.rb, line 34 def reset_digest_auth! @digest_auth = nil end
reset_token_auth!()
click to toggle source
# File lib/rack/radar/session.rb, line 38 def reset_token_auth! @token_auth = nil end
Private Instance Methods
assert_valid_app(app)
click to toggle source
# File lib/rack/radar/session.rb, line 85 def assert_valid_app app return app if app.respond_to?(:call) raise(ArgumentError, 'app should be a valid Rack app') end
basic_auth_header()
click to toggle source
# File lib/rack/radar/session.rb, line 138 def basic_auth_header return {} unless auth = @basic_auth {'HTTP_AUTHORIZATION' => 'Basic %s' % ["#{auth.first}:#{auth.last}"].pack("m*")} end
digest_auth_header(challenge, uri, request_method)
click to toggle source
# File lib/rack/radar/session.rb, line 143 def digest_auth_header challenge, uri, request_method params = ::Rack::Auth::Digest::Params.parse(challenge.split(" ", 2).last) params.update({ "username" => @digest_auth.first, "nc" => "00000001", "cnonce" => "nonsensenonce", "uri" => uri, "method" => request_method, }) params["response"] = MockDigestRequest.new(params).response(@digest_auth.last) {'HTTP_AUTHORIZATION' => 'Digest ' << params.map {|p| '%s="%s"' % p}.join(', ')} end
headers_to_env()
click to toggle source
# File lib/rack/radar/session.rb, line 128 def headers_to_env headers.keys.inject({}) do |headers, key| value = self.headers[key] if (key =~ /\A[[:upper:]].*\-?[[:upper:]]?.*?/) && (key !~ /\AHTTP_|\ACONTENT_TYPE\Z/) key = (key == 'Content-Type' ? '' : 'HTTP_') << key.upcase.gsub('-', '_') end headers.merge key => value end end
normalize_params(params)
click to toggle source
# File lib/rack/radar/session.rb, line 77 def normalize_params params params.inject({}) do |params, (k, v)| k = k.to_s if k.is_a?(Numeric) || k.is_a?(Symbol) v = v.to_s if v.is_a?(Numeric) || v.is_a?(Symbol) params.merge k => v end end
process_request(app, path, env)
click to toggle source
# File lib/rack/radar/session.rb, line 111 def process_request app, path, env env = ::Rack::MockRequest.env_for(path.to_s, Hash[env]) explicit_env = headers_to_env explicit_env['rack.input'] && env['REQUEST_METHOD'] == 'POST' && env.delete('CONTENT_TYPE') env.update explicit_env @last_request = ::Rack::Request.new(env) # initializing params. do not remove! needed for nested params to work @last_request.params status, headers, body = app.call(@last_request.env) @last_response = ::Rack::MockResponse.new(status, headers, body, env['rack.errors'].flush) body.respond_to?(:close) && body.close end
token_auth_header()
click to toggle source
# File lib/rack/radar/session.rb, line 156 def token_auth_header return {} unless auth = @token_auth chunks = auth[1].each_with_object(['token=%s' % auth[0].to_s.inspect]) do |(k,v),o| o << [key, value.to_s.inspect]*'=' end {'HTTP_AUTHORIZATION' => 'Token %s' % chunks.join(', ')} end
uri(scheme, base_url, path)
click to toggle source
# File lib/rack/radar/session.rb, line 90 def uri scheme, base_url, path path = path*'/' # base_url ignored if given path starting with a protocol or a slash path = [base_url, path].compact.map(&:to_s)*'/' unless path =~ %r[\A/|\A(\w+)?://] # removing leading slash(es) path.sub!(/\A\/+/, '') uri = ::URI.parse(path) # adding leading slash unless path starts with a protocol uri.path = '/' << uri.path unless path =~ %r[\A(\w+)?://] uri.host ||= RACK_RADAR__DEFAULT_HOST uri.scheme ||= scheme.to_s # now that we have a full URI, eg. with scheme, port etc. # lets build a new one based on it ::URI.parse(uri.to_s) end