module CookieJar::CookieValidation
Contains logic to parse and validate cookie headers
Constants
- BASE_HOSTNAME
- BASE_PATH
- HDN
- IPADDR
- PARAM1
- PARAM2
- TOKEN
Public Class Methods
Source
# File lib/cookiejar/cookie_validation.rb, line 148 def self.compute_search_domains(request_uri) uri = to_uri request_uri return nil unless uri.is_a? URI::HTTP host = uri.host compute_search_domains_for_host host end
Given a URI, compute the relevant search domains for pre-existing cookies. This includes all the valid dotted forms for a named or IP domains.
@param [String, URI] request_uri requested uri @return [Array<String>] all cookie domain values which would match the
requested uri
Source
# File lib/cookiejar/cookie_validation.rb, line 161 def self.compute_search_domains_for_host(host) host = effective_host host result = [host] unless host =~ IPADDR result << ".#{host}" base = hostname_reach host result << ".#{base}" if base end result end
Given a host, compute the relevant search domains for pre-existing cookies
@param [String] host host being requested @return [Array<String>] all cookie domain values which would match the
requested uri
Source
# File lib/cookiejar/cookie_validation.rb, line 346 def self.decode_value(value) if /\A"(.*)"\Z/ =~ value value_to_string value else CGI.unescape value end end
Attempt to decipher a partially decoded version of text cookie values
Source
# File lib/cookiejar/cookie_validation.rb, line 93 def self.domains_match(tested_domain, base_domain) base = effective_host base_domain search_domains = compute_search_domains_for_host base search_domains.find do |domain| domain == tested_domain end end
Compare a tested domain against the base domain to see if they match, or if the base domain is reachable.
@param [String] tested_domain domain to be tested against @param [String] base_domain new domain being tested @return [String,nil] matching domain on success, nil on failure
Source
# File lib/cookiejar/cookie_validation.rb, line 209 def self.effective_host(host_or_uri) hostname = to_domain host_or_uri hostname = hostname.downcase if /.[\.:]./.match(hostname) || hostname == '.local' hostname else hostname + '.local' end end
Compute the effective host (RFC 2965, section 1)
Has the added additional logic of searching for interior dots specifically, and matches colons to prevent .local being suffixed on IPv6 addresses
@param [String, URI] host_or_uridomain name, or absolute URI @return [String] effective host per RFC rules
Source
# File lib/cookiejar/cookie_validation.rb, line 106 def self.hostname_reach(hostname) host = to_domain hostname host = host.downcase match = BASE_HOSTNAME.match host match[1] if match end
Compute the reach of a hostname (RFC 2965, section 1) Determines the next highest superdomain
@param [String,URI,Cookie] hostname hostname, or object holding hostname @return [String,nil] next highest hostname, or nil if none
Source
# File lib/cookiejar/cookie_validation.rb, line 77 def self.to_domain(uri_or_domain) if uri_or_domain.is_a? URI uri_or_domain.host elsif uri_or_domain.is_a? Cookie uri_or_domain.domain else uri_or_domain end end
Converts an input cookie or uri to a string representing the domain. Assume strings are already domains. Value may not be an effective host.
@param [String, URI, Cookie] object containing the domain @return [String] domain information.
Source
# File lib/cookiejar/cookie_validation.rb, line 64 def self.to_path(uri_or_path) if (uri_or_path.is_a? URI) || (uri_or_path.is_a? Cookie) uri_or_path.path else uri_or_path end end
Converts an input cookie or uri to a string representing the path. Assume strings are already paths
@param [String, URI, Cookie] object containing the path @return [String] path information
Source
# File lib/cookiejar/cookie_validation.rb, line 55 def self.to_uri(request_uri) (request_uri.is_a? URI) ? request_uri : (URI.parse request_uri) end
Converts the input object to a URI (if not already a URI)
@param [String, URI] request_uri URI we are normalizing @param [URI] URI representation of input string, or original URI
Source
# File lib/cookiejar/cookie_validation.rb, line 336 def self.value_to_string(value) if /\A"(.*)"\Z/ =~ value value = Regexp.last_match(1) value.gsub(/\\(.)/, '\1') else value end end
Parse a RFC 2965 value and convert to a literal string