/usr/lib/ruby/vendor_ruby/rack/test/cookie_jar.rb is in ruby-rack-test 0.6.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | require "uri"
require "time"
module Rack
module Test
class Cookie # :nodoc:
include Rack::Utils
# :api: private
attr_reader :name, :value
# :api: private
def initialize(raw, uri = nil, default_host = DEFAULT_HOST)
@default_host = default_host
uri ||= default_uri
# separate the name / value pair from the cookie options
@name_value_raw, options = raw.split(/[;,] */n, 2)
@name, @value = parse_query(@name_value_raw, ';').to_a.first
@options = parse_query(options, ';')
@options["domain"] ||= (uri.host || default_host)
@options["path"] ||= uri.path.sub(/\/[^\/]*\Z/, "")
end
def replaces?(other)
[name.downcase, domain, path] == [other.name.downcase, other.domain, other.path]
end
# :api: private
def raw
@name_value_raw
end
# :api: private
def empty?
@value.nil? || @value.empty?
end
# :api: private
def domain
@options["domain"]
end
def secure?
@options.has_key?("secure")
end
# :api: private
def path
@options["path"].strip || "/"
end
# :api: private
def expires
Time.parse(@options["expires"]) if @options["expires"]
end
# :api: private
def expired?
expires && expires < Time.now
end
# :api: private
def valid?(uri)
uri ||= default_uri
if uri.host.nil?
uri.host = @default_host
end
real_domain = domain =~ /^\./ ? domain[1..-1] : domain
(!secure? || (secure? && uri.scheme == "https")) &&
uri.host =~ Regexp.new("#{Regexp.escape(real_domain)}$", Regexp::IGNORECASE) &&
uri.path =~ Regexp.new("^#{Regexp.escape(path)}")
end
# :api: private
def matches?(uri)
! expired? && valid?(uri)
end
# :api: private
def <=>(other)
# Orders the cookies from least specific to most
[name, path, domain.reverse] <=> [other.name, other.path, other.domain.reverse]
end
protected
def default_uri
URI.parse("//" + @default_host + "/")
end
end
class CookieJar # :nodoc:
# :api: private
def initialize(cookies = [], default_host = DEFAULT_HOST)
@default_host = default_host
@cookies = cookies
@cookies.sort!
end
def [](name)
cookies = hash_for(nil)
# TODO: Should be case insensitive
cookies[name] && cookies[name].value
end
def []=(name, value)
merge("#{name}=#{Rack::Utils.escape(value)}")
end
def delete(name)
@cookies.reject! do |cookie|
cookie.name == name
end
end
def merge(raw_cookies, uri = nil)
return unless raw_cookies
if raw_cookies.is_a? String
raw_cookies = raw_cookies.split("\n")
raw_cookies.reject!{|c| c.empty? }
end
raw_cookies.each do |raw_cookie|
cookie = Cookie.new(raw_cookie, uri, @default_host)
self << cookie if cookie.valid?(uri)
end
end
def <<(new_cookie)
@cookies.reject! do |existing_cookie|
new_cookie.replaces?(existing_cookie)
end
@cookies << new_cookie
@cookies.sort!
end
# :api: private
def for(uri)
hash_for(uri).values.map { |c| c.raw }.join(';')
end
def to_hash
cookies = {}
hash_for(nil).each do |name, cookie|
cookies[name] = cookie.value
end
return cookies
end
protected
def hash_for(uri = nil)
cookies = {}
# The cookies are sorted by most specific first. So, we loop through
# all the cookies in order and add it to a hash by cookie name if
# the cookie can be sent to the current URI. It's added to the hash
# so that when we are done, the cookies will be unique by name and
# we'll have grabbed the most specific to the URI.
@cookies.each do |cookie|
cookies[cookie.name] = cookie if !uri || cookie.matches?(uri)
end
return cookies
end
end
end
end
|