File: cookie.rb

package info (click to toggle)
libnora-ruby 1%3A0.0.20041021-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 412 kB
  • ctags: 711
  • sloc: ruby: 5,186; ansic: 669; makefile: 260; sql: 10
file content (122 lines) | stat: -rwxr-xr-x 3,051 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Web::Cookie
# Copyright(c) 2002 MoonWolf <moonwolf@moonwolf.com>
require "web/common"
require 'time'

module Web
  class Cookie
    def self.parse(header,req)
      cookies = []
      cookie = nil
      until header.empty?
        case header
        when /\A\s*;\s*/
          header = $'
        when /\A\s*,\s*/
          header = $'
          if cookie
            if cookie.domain == nil
              cookie.domain = req.server_name.downcase
            end
            cookies << cookie
          end
          cookie = nil
        when /\Aexpires\s*=\s*(?:"(\w+, \d\d-\w+-\d+ \d+:\d+:\d+ GMT)"|(\w+, \d\d-\w+-\d+ \d+:\d+:\d+ GMT))/i
          header = $'
          cookie.expires = $1 || $2
        when /\A([^\000-\040\042\073\075\177]+)(?:\s*=\s*(?:([^ ,;]*)|"(.*?)"))?/
          header = $'
          value = $2 || $3
          name  = $1
          case name
          when 'comment'
            cookie.comment = value
          when 'domain'
            cookie.domain  = value.sub(/\A\./,'')
          when 'path'
            cookie.path    = value
          when 'secure'
            cookie.secure  = true
          when 'version'
            cookie.version = value
          when /^\$/
            #
          else
            if cookie
              if cookie.domain == nil
                cookie.domain = req.server_name.downcase
              end
              cookies << cookie
            end
            cookie = Cookie.new(name, value)
          end
        when /\A[\000-\377]/n
          header = $'
        end
      end
      if cookie
        if cookie.domain == nil
          cookie.domain = req.server_name.downcase
        end
        cookies << cookie
      end
      cookies
    end

    def initialize(name, value='')
      @name    = name
      @value   = value
      @comment = nil
      @domain  = nil
      @expires = nil
      @path    = nil
      @secure  = nil
      @version = nil
    end
    attr_accessor :name, :value, :comment, :domain, :path, :secure, :version
    attr_reader :expires
    
    def expires=(tm)
      if Time === tm
        @expires = tm
      elsif tm==nil
        @expires = nil
      else
        @expires = Time.parse(tm)
      end
    end
    
    def to_request_header
      "#{@name}=#{@value}"
    end
    
    def to_response_header
      value = @value.to_s
      ary = []
      ary << "#{@name}=#{value}"
      ary << "comment=#{@comment}" if @comment
      ary << "domain=#{@domain}" if @domain
      if @expires
        ary << "expires=#{Common::rfc1123date(@expires)}"
      end
      ary << "path=#{@path}" if @path
      ary << "secure" if @secure
      ary << "version=#{@version}" if @version
      ary.join("; ")
    end
    
    class Cookies < Web::Common::ParamHash
      def initialize(header=nil)
        super()
        parse(header) if header
      end
      
      def parse(header, req)
        Web::Cookie::parse(header, req).each {|cookie|
          self.add(cookie.name, cookie)
        }
        self
      end
    end # Cookies
  end # Cookie
end # Web