123456789_123456789_123456789_123456789_123456789_

Class: WEBrick::Cookie

Relationships & Source Files
Inherits: Object
Defined in: lib/webrick/cookie.rb

Overview

Processes HTTP cookies

Class Method Summary

Instance Attribute Summary

Instance Method Summary

  • #to_s

    The cookie string suitable for use in an HTTP header.

Constructor Details

.new(name, value) ⇒ Cookie

Creates a new cookie with the given #name and #value

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 66

def initialize(name, value)
  @name = name
  @value = value
  @version = 0     # Netscape Cookie

  @domain = @path = @secure = @comment = @max_age =
  @expires = @comment_url = @discard = @port = nil
end

Class Method Details

.parse(str)

Parses a Cookie field sent from the user-agent. Returns an array of cookies.

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 111

def self.parse(str)
  if str
    ret = []
    cookie = nil
    ver = 0
    str.split(/[;,]\s+/).each{|x|
      key, val = x.split(/=/,2)
      val = val ? HTTPUtils::dequote(val) : ""
      case key
      when "$Version"; ver = val.to_i
      when "$Path";    cookie.path = val
      when "$Domain";  cookie.domain = val
      when "$Port";    cookie.port = val
      else
        ret << cookie if cookie
        cookie = self.new(key, val)
        cookie.version = ver
      end
    }
    ret << cookie if cookie
    ret
  end
end

.parse_set_cookies(str)

Parses the cookies in str

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 166

def self.parse_set_cookies(str)
  return str.split(/,(?=[^;,]*=)|,$/).collect{|c|
    parse_set_cookie(c)
  }
end

Instance Attribute Details

#comment (rw)

The cookie comment

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 54

attr_accessor :comment

#domain (rw)

The cookie domain

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 39

attr_accessor :domain

#expires (rw)

Retrieves the expiration time as a Time

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 87

def expires
  @expires && Time.parse(@expires)
end

#expires=(t) (rw)

Sets the cookie expiration to the time t. The expiration time may be a false value to disable expiration or a Time or HTTP format time string to set the expiration date.

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 80

def expires=(t)
  @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
end

#max_age (rw)

The maximum age of the cookie

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 59

attr_accessor :max_age

#name (readonly)

The cookie name

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 25

attr_reader :name

#path (rw)

The cookie path

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 44

attr_accessor :path

#secure (rw)

Is this a secure cookie?

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 49

attr_accessor :secure

#value (rw)

The cookie value

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 30

attr_accessor :value

#version (rw)

The cookie version

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 35

attr_accessor :version

Instance Method Details

#to_s

The cookie string suitable for use in an HTTP header

[ GitHub ]

  
# File 'lib/webrick/cookie.rb', line 94

def to_s
  ret = ""
  ret << @name << "=" << @value
  ret << "; " << "Version=" << @version.to_s if @version > 0
  ret << "; " << "Domain="  << @domain  if @domain
  ret << "; " << "Expires=" << @expires if @expires
  ret << "; " << "Max-Age=" << @max_age.to_s if @max_age
  ret << "; " << "Comment=" << @comment if @comment
  ret << "; " << "Path="    << @path if @path
  ret << "; " << "Secure"   if @secure
  ret
end