def set_cookies_from_headers(request_uri, http_headers)
      set_cookie_key = http_headers.keys.detect { |k| /\ASet-Cookie\Z/i.match k }
      cookies = gather_header_values http_headers[set_cookie_key] do |value|
        begin
          Cookie.from_set_cookie request_uri, value
        rescue InvalidCookieError
        end
      end

      set_cookie2_key = http_headers.keys.detect { |k| /\ASet-Cookie2\Z/i.match k }
      cookies += gather_header_values(http_headers[set_cookie2_key]) do |value|
        begin
          Cookie.from_set_cookie2 request_uri, value
        rescue InvalidCookieError
        end
      end

      # build the list of cookies, using a Jar. Since Set-Cookie2 values
      # come second, they will replace the Set-Cookie versions.
      jar = Jar.new
      cookies.each do |cookie|
        jar.add_cookie cookie
      end
      cookies = jar.to_a

      # now add them all to our own store.
      cookies.each do |cookie|
        add_cookie cookie
      end
      cookies
    end
def set_cookies_from_headers(request_uri, http_headers)
      set_cookie_key = http_headers.keys.detect { |k| /\ASet-Cookie\Z/i.match k }
      cookies = gather_header_values http_headers[set_cookie_key] do |value|
        begin
          Cookie.from_set_cookie request_uri, value
        rescue InvalidCookieError
        end
      end

      set_cookie2_key = http_headers.keys.detect { |k| /\ASet-Cookie2\Z/i.match k }
      cookies += gather_header_values(http_headers[set_cookie2_key]) do |value|
        begin
          Cookie.from_set_cookie2 request_uri, value
        rescue InvalidCookieError
        end
      end

      # build the list of cookies, using a Jar. Since Set-Cookie2 values
      # come second, they will replace the Set-Cookie versions.
      jar = Jar.new
      cookies.each do |cookie|
        jar.add_cookie cookie
      end
      cookies = jar.to_a

      # now add them all to our own store.
      cookies.each do |cookie|
        add_cookie cookie
      end
      cookies
    end
Given a request URI and some HTTP headers, attempt to add the cookie(s)
 (from Set-Cookie or Set-Cookie2 headers) to the cookie store. If a
 cookie is defined (by equivalent name, domain, and path) via Set-Cookie
 and Set-Cookie2, the Set-Cookie version is ignored.

 @param [String, URI] request_uri the resource returning the header
 @param [Hash<String,[String,Array<String>]>] http_headers a Hash
   which may have a key of "Set-Cookie" or "Set-Cookie2", and values of
   either strings or arrays of strings
 @return [Array<Cookie>,nil] the cookies created, or nil if none found.
 @raise [InvalidCookieError] if one of the cookie headers contained
   invalid formatting or data