File: webrick/cookie.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: WEBrick#14
  class: Cookie#15
inherits from
  Object ( Builtin-Module )
has properties
attribute: name [R] #17
attribute: value [RW] #18
attribute: version [RW] #18
attribute: domain [RW] #19
attribute: path [RW] #19
attribute: secure [RW] #19
attribute: comment [RW] #20
attribute: max_age [RW] #20
method: initialize / 2 #23
method: expires= / 1 #32
method: expires #36
method: to_s #40
class method: parse / 1 #55
class method: parse_set_cookie / 1 #79
class method: parse_set_cookies / 1 #104

Class Hierarchy

Object ( Builtin-Module )
  Cookie ( WEBrick ) #15

Code

   1  #
   2  # cookie.rb -- Cookie class
   3  #
   4  # Author: IPR -- Internet Programming with Ruby -- writers
   5  # Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
   6  # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
   7  # reserved.
   8  #
   9  # $IPR: cookie.rb,v 1.16 2002/09/21 12:23:35 gotoyuzo Exp $
  10 
  11  require 'time'
  12  require 'webrick/httputils'
  13 
  14  module WEBrick
  15    class Cookie
  16 
  17      attr_reader :name
  18      attr_accessor :value, :version
  19      attr_accessor :domain, :path, :secure
  20      attr_accessor :comment, :max_age
  21      #attr_accessor :comment_url, :discard, :port
  22 
  23      def initialize(name, value)
  24        @name = name
  25        @value = value
  26        @version = 0     # Netscape Cookie
  27 
  28        @domain = @path = @secure = @comment = @max_age =
  29        @expires = @comment_url = @discard = @port = nil
  30      end
  31 
  32      def expires=(t)
  33        @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
  34      end
  35 
  36      def expires
  37        @expires && Time.parse(@expires)
  38      end
  39 
  40      def to_s
  41        ret = ""
  42        ret << @name << "=" << @value
  43        ret << "; " << "Version=" << @version.to_s if @version > 0
  44        ret << "; " << "Domain="  << @domain  if @domain
  45        ret << "; " << "Expires=" << @expires if @expires
  46        ret << "; " << "Max-Age=" << @max_age.to_s if @max_age
  47        ret << "; " << "Comment=" << @comment if @comment
  48        ret << "; " << "Path="    << @path if @path
  49        ret << "; " << "Secure"   if @secure
  50        ret
  51      end
  52 
  53      # Cookie::parse()
  54      #   It parses Cookie field sent from the user agent.
  55      def self.parse(str)
  56        if str
  57          ret = []
  58          cookie = nil
  59          ver = 0
  60          str.split(/[;,]\s+/).each{|x|
  61            key, val = x.split(/=/,2)
  62            val = val ? HTTPUtils::dequote(val) : ""
  63            case key
  64            when "$Version"; ver = val.to_i
  65            when "$Path";    cookie.path = val
  66            when "$Domain";  cookie.domain = val
  67            when "$Port";    cookie.port = val
  68            else
  69              ret << cookie if cookie
  70              cookie = self.new(key, val)
  71              cookie.version = ver
  72            end
  73          }
  74          ret << cookie if cookie
  75          ret
  76        end
  77      end
  78 
  79      def self.parse_set_cookie(str)
  80        cookie_elem = str.split(/;/)
  81        first_elem = cookie_elem.shift
  82        first_elem.strip!
  83        key, value = first_elem.split(/=/, 2)
  84        cookie = new(key, HTTPUtils.dequote(value))
  85        cookie_elem.each{|pair|
  86          pair.strip!
  87          key, value = pair.split(/=/, 2)
  88          if value
  89            value = HTTPUtils.dequote(value.strip)
  90          end
  91          case key.downcase
  92          when "domain"  then cookie.domain  = value
  93          when "path"    then cookie.path    = value
  94          when "expires" then cookie.expires = value
  95          when "max-age" then cookie.max_age = Integer(value)
  96          when "comment" then cookie.comment = value
  97          when "version" then cookie.version = Integer(value)
  98          when "secure"  then cookie.secure = true
  99          end
 100        }
 101        return cookie
 102      end
 103 
 104      def self.parse_set_cookies(str)
 105        return str.split(/,(?=[^;,]*=)|,$/).collect{|c|
 106          parse_set_cookie(c)
 107        }
 108      end
 109    end
 110  end