A subclass of Rack::Request providing CloudKit-specific features.
Methods
public class
public instance
Included modules
Public class methods
# File lib/cloudkit/request.rb, line 8 8: def initialize(env) 9: super(env) 10: end
Public instance methods
Report to downstream middleware that authentication is in use.
# File lib/cloudkit/request.rb, line 135 135: def announce_auth(via) 136: inject_via(via) 137: @env[CLOUDKIT_AUTH_PRESENCE] = 1 138: end
Return the current user URI.
# File lib/cloudkit/request.rb, line 119 119: def current_user 120: return nil unless @env[CLOUDKIT_AUTH_KEY] && @env[CLOUDKIT_AUTH_KEY] != '' 121: @env[CLOUDKIT_AUTH_KEY] 122: end
Set the current user URI.
# File lib/cloudkit/request.rb, line 125 125: def current_user=(user) 126: @env[CLOUDKIT_AUTH_KEY] = user 127: end
Return the host and scheme
# File lib/cloudkit/request.rb, line 173 173: def domain_root 174: "#{scheme}://#{@env['HTTP_HOST']}" 175: end
Return the flash session for this request.
# File lib/cloudkit/request.rb, line 168 168: def flash 169: session[CLOUDKIT_FLASH] ||= CloudKit::FlashSession.new 170: end
Return parsed contents of an If-Match header.
Note: Only a single ETag is useful in the context of CloudKit, so a list is treated as one ETag; the result of using the wrong ETag or a list of ETags is the same in the context of PUT and DELETE where If-Match headers are required.
# File lib/cloudkit/request.rb, line 103 103: def if_match 104: etag = @env['HTTP_IF_MATCH'] 105: return nil unless etag 106: etag.strip! 107: etag = unquote(etag) 108: return nil if etag == '*' 109: etag 110: end
Add a via entry to the Rack environment.
# File lib/cloudkit/request.rb, line 113 113: def inject_via(key) 114: items = via << key 115: @env[CLOUDKIT_VIA] = items.join(', ') 116: end
Return the JSON content from the request body
# File lib/cloudkit/request.rb, line 18 18: def json 19: self.body.rewind 20: raw = self.body.read 21: # extract the json from the body to avoid tunneled _method param from being parsed as json 22: (matches = raw.match(/(\{.*\})/)) ? matches[1] : raw 23: end
Return the last path element in the request URI.
# File lib/cloudkit/request.rb, line 80 80: def last_path_element 81: path_element(-1) 82: end
Return the login URL for this request. This is stashed in the Rack environment so the OpenID and OAuth middleware can cooperate during the token authorization step in the OAuth flow.
# File lib/cloudkit/request.rb, line 148 148: def login_url 149: @env[CLOUDKIT_LOGIN_URL] || '/login' 150: end
Set the login url for this request.
# File lib/cloudkit/request.rb, line 153 153: def login_url=(url) 154: @env[CLOUDKIT_LOGIN_URL] = url 155: end
Return the logout URL for this request.
# File lib/cloudkit/request.rb, line 158 158: def logout_url 159: @env[CLOUDKIT_LOGOUT_URL] || '/logout' 160: end
Set the logout URL for this request.
# File lib/cloudkit/request.rb, line 163 163: def logout_url=(url) 164: @env[CLOUDKIT_LOGOUT_URL] = url 165: end
Return true if method, path, and required_params match.
# File lib/cloudkit/request.rb, line 31 31: def match?(method, path, required_params=[]) 32: (request_method == method) && 33: path_info.match(path.gsub(':id', '*')) && # just enough to work for now 34: param_match?(required_params) 35: end
Return OAuth header params in a hash.
# File lib/cloudkit/request.rb, line 56 56: def oauth_header_params 57: # This is a copy of the same method from the OAuth gem. 58: # TODO: Refactor the OAuth gem so that this method is available via a 59: # mixin, outside of the request proxy context. 60: %w( X-HTTP_AUTHORIZATION Authorization HTTP_AUTHORIZATION ).each do |header| 61: next unless @env.include?(header) 62: header = @env[header] 63: next unless header[0,6] == 'OAuth ' 64: oauth_param_string = header[6,header.length].split(/[,=]/) 65: oauth_param_string.map!{|v| unescape(v.strip)} 66: oauth_param_string.map!{|v| v =~ /^\".*\"$/ ? v[1..-2] : v} 67: oauth_params = Hash[*oauth_param_string.flatten] 68: oauth_params.reject!{|k,v| k !~ /^oauth_/} 69: return oauth_params 70: end 71: return {} 72: end
Return true of the array of required params match the request params. If a hash in passed in for a param, its value is also used in the match.
# File lib/cloudkit/request.rb, line 39 39: def param_match?(required_params) 40: required_params.all? do |required_param| 41: case required_param 42: when Hash 43: key = required_param.keys.first 44: return false unless params.has_key? key 45: return false unless params[key] == required_param[key] 46: when String 47: return false unless params.has_key? required_param 48: else 49: false 50: end 51: true 52: end 53: end
Return a merged set of both standard params and OAuth header params.
# File lib/cloudkit/request.rb, line 13 13: def params 14: @cloudkit_params ||= cloudkit_params.merge(oauth_header_params) 15: end
Return a specific path element
# File lib/cloudkit/request.rb, line 85 85: def path_element(index) 86: path_info.split('/')[index] rescue nil 87: end
Return the session associated with this request.
# File lib/cloudkit/request.rb, line 141 141: def session 142: @env['rack.session'] 143: end
Unescape a value according to the OAuth spec.
# File lib/cloudkit/request.rb, line 75 75: def unescape(value) 76: ::URI.unescape(value.gsub('+', '%2B')) 77: end
Return a CloudKit::URI instance representing the rack request’s path info.
# File lib/cloudkit/request.rb, line 26 26: def uri 27: @uri ||= CloudKit::URI.new(self.path_info) 28: end
Return true if authentication is being used.
# File lib/cloudkit/request.rb, line 130 130: def using_auth? 131: @env[CLOUDKIT_AUTH_PRESENCE] != nil 132: end
Return an array containing one entry for each piece of upstream middleware. This is in the same spirit as Via headers in HTTP, but does not use the header because the transition from one piece of middleware to the next does not use HTTP.
# File lib/cloudkit/request.rb, line 93 93: def via 94: @env[CLOUDKIT_VIA].split(', ') rescue [] 95: end