lib/restclient/request.rb
def process_cookie_args!(uri, headers, args)

      # Avoid ambiguity in whether options from headers or options from
      # Request#initialize should take precedence by raising ArgumentError when
      # both are present. Prior versions of rest-client claimed to give
      # precedence to init options, but actually gave precedence to headers.
      # Avoid that mess by erroring out instead.
      if headers[:cookies] && args[:cookies]
        raise ArgumentError.new(
          "Cannot pass :cookies in Request.new() and in headers hash")
      end

      cookies_data = headers.delete(:cookies) || args[:cookies]

      # return copy of cookie jar as is
      if cookies_data.is_a?(HTTP::CookieJar)
        return cookies_data.dup
      end

      # convert cookies hash into a CookieJar
      jar = HTTP::CookieJar.new

      (cookies_data || []).each do |key, val|

        # Support for Array<HTTP::Cookie> mode:
        # If key is a cookie object, add it to the jar directly and assert that
        # there is no separate val.
        if key.is_a?(HTTP::Cookie)
          if val
            raise ArgumentError.new("extra cookie val: #{val.inspect}")
          end

          jar.add(key)
          next
        end

        if key.is_a?(Symbol)
          key = key.to_s
        end

        # assume implicit domain from the request URI, and set for_domain to
        # permit subdomains
        jar.add(HTTP::Cookie.new(key, val, domain: uri.hostname.downcase,
                                 path: '/', for_domain: true))
      end

      jar
    end
def process_cookie_args!(uri, headers, args)

      # Avoid ambiguity in whether options from headers or options from
      # Request#initialize should take precedence by raising ArgumentError when
      # both are present. Prior versions of rest-client claimed to give
      # precedence to init options, but actually gave precedence to headers.
      # Avoid that mess by erroring out instead.
      if headers[:cookies] && args[:cookies]
        raise ArgumentError.new(
          "Cannot pass :cookies in Request.new() and in headers hash")
      end

      cookies_data = headers.delete(:cookies) || args[:cookies]

      # return copy of cookie jar as is
      if cookies_data.is_a?(HTTP::CookieJar)
        return cookies_data.dup
      end

      # convert cookies hash into a CookieJar
      jar = HTTP::CookieJar.new

      (cookies_data || []).each do |key, val|

        # Support for Array<HTTP::Cookie> mode:
        # If key is a cookie object, add it to the jar directly and assert that
        # there is no separate val.
        if key.is_a?(HTTP::Cookie)
          if val
            raise ArgumentError.new("extra cookie val: #{val.inspect}")
          end

          jar.add(key)
          next
        end

        if key.is_a?(Symbol)
          key = key.to_s
        end

        # assume implicit domain from the request URI, and set for_domain to
        # permit subdomains
        jar.add(HTTP::Cookie.new(key, val, domain: uri.hostname.downcase,
                                 path: '/', for_domain: true))
      end

      jar
    end
Process cookies passed as hash or as HTTP::CookieJar. For backwards
 compatibility, these may be passed as a :cookies option masquerading
 inside the headers hash. To avoid confusion, if :cookies is passed in
 both headers and Request#initialize, raise an error.

 :cookies may be a:
 - Hash{String/Symbol => String}
 - Array<HTTP::Cookie>
 - HTTP::CookieJar

 Passing as a hash:
   Keys may be symbols or strings. Values must be strings.
   Infer the domain name from the request URI and allow subdomains (as
   though '.example.com' had been set in a Set-Cookie header). Assume a
   path of '/'.

     RestClient::Request.new(url: 'http://example.com', method: :get,
       :cookies => {:foo => 'Value', 'bar' => '123'}
     )

 results in cookies as though set from the server by:
     Set-Cookie: foo=Value; Domain=.example.com; Path=/
     Set-Cookie: bar=123; Domain=.example.com; Path=/

 which yields a client cookie header of:
     Cookie: foo=Value; bar=123

 Passing as HTTP::CookieJar, which will be passed through directly:

     jar = HTTP::CookieJar.new
     jar.add(HTTP::Cookie.new('foo', 'Value', domain: 'example.com',
                              path: '/', for_domain: false))

     RestClient::Request.new(..., :cookies => jar)

 @param [URI::HTTP] uri The URI for the request. This will be used to
 infer the domain name for cookies passed as strings in a hash. To avoid
 this implicit behavior, pass a full cookie jar or use HTTP::Cookie hash
 values.
 @param [Hash] headers The headers hash from which to pull the :cookies
   option. MUTATION NOTE: This key will be deleted from the hash if
   present.
 @param [Hash] args The options passed to Request#initialize. This hash
   will be used as another potential source for the :cookies key.
   These args will not be mutated.

 @return [HTTP::CookieJar] A cookie jar containing the parsed cookies.