Class: Camo::Request
- Inherits:
-
Object
- Object
- Camo::Request
- Includes:
- HeadersUtils, Rack::Utils
- Defined in:
- lib/camo/request.rb
Constant Summary collapse
- SUPPORTED_PROTOCOLS =
%w[http https]
Constants included from HeadersUtils
HeadersUtils::HOSTNAME, HeadersUtils::REQUEST_SECURITY_HEADERS, HeadersUtils::RESPONSE_SECURITY_HEADERS, HeadersUtils::TIMING_ALLOW_ORIGIN
Instance Attribute Summary collapse
-
#destination_url ⇒ Object
readonly
Returns the value of attribute destination_url.
-
#digest ⇒ Object
readonly
Returns the value of attribute digest.
-
#digest_type ⇒ Object
readonly
Returns the value of attribute digest_type.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#query_string ⇒ Object
readonly
Returns the value of attribute query_string.
Instance Method Summary collapse
-
#initialize(env, key) ⇒ Request
constructor
A new instance of Request.
- #url ⇒ Object
- #valid_digest? ⇒ Boolean
- #valid_request? ⇒ Boolean
Methods included from HeadersUtils
#default_request_headers, #default_response_headers, user_agent, #user_agent
Constructor Details
#initialize(env, key) ⇒ Request
Returns a new instance of Request.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/camo/request.rb', line 13 def initialize(env, key) @method = env["REQUEST_METHOD"] @query_string = env["QUERY_STRING"] @params = parse_query(@query_string) @protocol = env["rack.url_scheme"] || "http" @host = env["HTTP_HOST"] @path = env["PATH_INFO"] @headers = build_headers(env) @key = key @digest, encoded_url = path[1..].split("/", 2).map { |part| String(part) } if encoded_url @digest_type = "path" @destination_url = Addressable::URI.parse(String(decode_hex(encoded_url))) else @digest_type = "query" @destination_url = Addressable::URI.parse(String(params["url"])) end @errors = [] end |
Instance Attribute Details
#destination_url ⇒ Object (readonly)
Returns the value of attribute destination_url.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def destination_url @destination_url end |
#digest ⇒ Object (readonly)
Returns the value of attribute digest.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def digest @digest end |
#digest_type ⇒ Object (readonly)
Returns the value of attribute digest_type.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def digest_type @digest_type end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def errors @errors end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def headers @headers end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def host @host end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def key @key end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def path @path end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def protocol @protocol end |
#query_string ⇒ Object (readonly)
Returns the value of attribute query_string.
11 12 13 |
# File 'lib/camo/request.rb', line 11 def query_string @query_string end |
Instance Method Details
#url ⇒ Object
36 37 38 |
# File 'lib/camo/request.rb', line 36 def url "#{protocol}://#{host}#{path}#{query_string.empty? ? nil : "?#{query_string}"}" end |
#valid_digest? ⇒ Boolean
45 46 47 |
# File 'lib/camo/request.rb', line 45 def valid_digest? OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha1"), key, destination_url) == digest end |
#valid_request? ⇒ Boolean
40 41 42 43 |
# File 'lib/camo/request.rb', line 40 def valid_request? validate_request Array(errors).empty? end |