class ActionDispatch::Cookies::CookieJar
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/action_dispatch/middleware/cookies.rbs class ActionDispatch::Cookies::CookieJar def self.build: (ActionDispatch::Request req, Hash cookies) -> ActionDispatch::Cookies::CookieJar def initialize: (ActionDispatch::Request request) -> void def update: (Hash other_hash) -> ActionDispatch::Cookies::CookieJar end
:nodoc:
def self.build(req, cookies)
Experimental RBS support (using type sampling data from the type_fusion
project).
def self.build: (ActionDispatch::Request req, _gemsh_session | String | _skillit_session | String | token | String cookies) -> ActionDispatch::Cookies::CookieJar
This signature was generated using 1 sample from 1 application.
def self.build(req, cookies) jar = new(req) jar.update(cookies) jar end
def [](name)
def [](name) @cookies[name.to_s] end
def []=(name, options)
Sets the cookie named +name+. The second argument may be the cookie's
def []=(name, options) if options.is_a?(Hash) options.symbolize_keys! value = options[:value] else value = options options = { value: value } end handle_options(options) if @cookies[name.to_s] != value || options[:expires] @cookies[name.to_s] = value @set_cookies[name.to_s] = options @delete_cookies.delete(name.to_s) end value end
def clear(options = {})
def clear(options = {}) @cookies.each_key { |k| delete(k, options) } end
def commit!
def commit! @committed = true @set_cookies.freeze @delete_cookies.freeze end
def committed?; @committed; end
def committed?; @committed; end
def delete(name, options = {})
and the expiration date in the past. Like []=, you can pass in
Removes the cookie on the client machine by setting the value to an empty string
def delete(name, options = {}) return unless @cookies.has_key? name.to_s options.symbolize_keys! handle_options(options) value = @cookies.delete(name.to_s) @delete_cookies[name.to_s] = options value end
def deleted?(name, options = {})
Like []=, you can pass in an options hash to test if a
Whether the given cookie is to be deleted by this CookieJar.
def deleted?(name, options = {}) options.symbolize_keys! handle_options(options) @delete_cookies[name.to_s] == options end
def each(&block)
def each(&block) @cookies.each(&block) end
def escape(string)
def escape(string) ::Rack::Utils.escape(string) end
def fetch(name, *args, &block)
def fetch(name, *args, &block) @cookies.fetch(name.to_s, *args, &block) end
def handle_options(options)
def handle_options(options) if options[:expires].respond_to?(:from_now) options[:expires] = options[:expires].from_now end options[:path] ||= "/" cookies_same_site_protection = request.cookies_same_site_protection options[:same_site] ||= cookies_same_site_protection.call(request) if options[:domain] == :all || options[:domain] == "all" cookie_domain = "" dot_splitted_host = request.host.split(".", -1) # Case where request.host is not an IP address or it's an invalid domain # (ip confirms to the domain structure we expect so we explicitly check for ip) if request.host.match?(/^[\d.]+$/) || dot_splitted_host.include?("") || dot_splitted_host.length == 1 options[:domain] = nil return end # If there is a provided tld length then we use it otherwise default domain. if options[:tld_length].present? # Case where the tld_length provided is valid if dot_splitted_host.length >= options[:tld_length] cookie_domain = dot_splitted_host.last(options[:tld_length]).join(".") end # Case where tld_length is not provided else # Regular TLDs if !(/\.[^.]{2,3}\.[^.]{2}\z/.match?(request.host)) cookie_domain = dot_splitted_host.last(2).join(".") # **.**, ***.** style TLDs like co.uk and com.au else cookie_domain = dot_splitted_host.last(3).join(".") end end options[:domain] = if cookie_domain.present? ".#{cookie_domain}" end elsif options[:domain].is_a? Array # If host matches one of the supplied domains. options[:domain] = options[:domain].find do |domain| domain = domain.delete_prefix(".") request.host == domain || request.host.end_with?(".#{domain}") end end end
def initialize(request)
Experimental RBS support (using type sampling data from the type_fusion
project).
def initialize: (ActionDispatch::Request request) -> void
This signature was generated using 2 samples from 1 application.
def initialize(request) @set_cookies = {} @delete_cookies = {} @request = request @cookies = {} @committed = false end
def key?(name)
def key?(name) @cookies.key?(name.to_s) end
def make_set_cookie_header(header)
def make_set_cookie_header(header) header = @set_cookies.inject(header) { |m, (k, v)| if write_cookie?(v) ::Rack::Utils.add_cookie_to_header(m, k, v) else m end } @delete_cookies.inject(header) { |m, (k, v)| ::Rack::Utils.add_remove_cookie_to_header(m, k, v) } end
def to_header
def to_header @cookies.map { |k, v| "#{escape(k)}=#{escape(v)}" }.join "; " end
def update(other_hash)
Experimental RBS support (using type sampling data from the type_fusion
project).
def update: ((_gemsh_session | String | _skillit_session | String | token | String | _phlexing_session | String | _web_session | String | remember_token | String | _gemsh_session | String | _skillit_session | String | token | String) other_hash) -> ActionDispatch::Cookies::CookieJar
This signature was generated using 2 samples from 1 application.
def update(other_hash) @cookies.update other_hash.stringify_keys self end
def update_cookies_from_jar
def update_cookies_from_jar request_jar = @request.cookie_jar.instance_variable_get(:@cookies) set_cookies = request_jar.reject { |k, _| @delete_cookies.key?(k) || @set_cookies.key?(k) } @cookies.update set_cookies if set_cookies end
def write(headers)
def write(headers) if header = make_set_cookie_header(headers[HTTP_HEADER]) headers[HTTP_HEADER] = header end end
def write_cookie?(cookie)
def write_cookie?(cookie) request.ssl? || !cookie[:secure] || always_write_cookie || request.host.end_with?(".onion") end