Class: WebAgent::Cookie

Inherits:
Object
  • Object
show all
Includes:
CookieUtils
Defined in:
lib/httpclient/webagent-cookie.rb

Constant Summary collapse

USE =
1
SECURE =
2
DOMAIN =
4
PATH =
8
DISCARD =
16
OVERRIDE =
32
OVERRIDE_OK =
32
HTTP_ONLY =
64

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CookieUtils

#domain_match, #head_match?, #tail_match?

Constructor Details

#initializeCookie

Returns a new instance of Cookie.



76
77
78
79
80
81
# File 'lib/httpclient/webagent-cookie.rb', line 76

def initialize
  @name = @value = @domain = @path = nil
  @expires = nil
  @url = nil
  @use = @secure = @http_only = @discard = @domain_orig = @path_orig = @override = nil
end

Instance Attribute Details

#discard=(value) ⇒ Object (writeonly)

Sets the attribute discard

Parameters:

  • value

    the value to set the attribute discard to.



59
60
61
# File 'lib/httpclient/webagent-cookie.rb', line 59

def discard=(value)
  @discard = value
end

#domainObject

Returns the value of attribute domain.



56
57
58
# File 'lib/httpclient/webagent-cookie.rb', line 56

def domain
  @domain
end

#domain_orig=(value) ⇒ Object (writeonly)

Sets the attribute domain_orig

Parameters:

  • value

    the value to set the attribute domain_orig to.



59
60
61
# File 'lib/httpclient/webagent-cookie.rb', line 59

def domain_orig=(value)
  @domain_orig = value
end

#expiresObject

for Netscape Cookie



57
58
59
# File 'lib/httpclient/webagent-cookie.rb', line 57

def expires
  @expires
end

#http_only=(value) ⇒ Object (writeonly)

Sets the attribute http_only

Parameters:

  • value

    the value to set the attribute http_only to.



59
60
61
# File 'lib/httpclient/webagent-cookie.rb', line 59

def http_only=(value)
  @http_only = value
end

#nameObject

Returns the value of attribute name.



55
56
57
# File 'lib/httpclient/webagent-cookie.rb', line 55

def name
  @name
end

#override=(value) ⇒ Object (writeonly)

Sets the attribute override

Parameters:

  • value

    the value to set the attribute override to.



59
60
61
# File 'lib/httpclient/webagent-cookie.rb', line 59

def override=(value)
  @override = value
end

#pathObject

Returns the value of attribute path.



56
57
58
# File 'lib/httpclient/webagent-cookie.rb', line 56

def path
  @path
end

#path_orig=(value) ⇒ Object (writeonly)

Sets the attribute path_orig

Parameters:

  • value

    the value to set the attribute path_orig to.



59
60
61
# File 'lib/httpclient/webagent-cookie.rb', line 59

def path_orig=(value)
  @path_orig = value
end

#secure=(value) ⇒ Object (writeonly)

Sets the attribute secure

Parameters:

  • value

    the value to set the attribute secure to.



59
60
61
# File 'lib/httpclient/webagent-cookie.rb', line 59

def secure=(value)
  @secure = value
end

#urlObject

Returns the value of attribute url.



58
59
60
# File 'lib/httpclient/webagent-cookie.rb', line 58

def url
  @url
end

#use=(value) ⇒ Object (writeonly)

Sets the attribute use

Parameters:

  • value

    the value to set the attribute use to.



59
60
61
# File 'lib/httpclient/webagent-cookie.rb', line 59

def use=(value)
  @use = value
end

#valueObject

Returns the value of attribute value.



55
56
57
# File 'lib/httpclient/webagent-cookie.rb', line 55

def value
  @value
end

Class Method Details

.parse(str, url) ⇒ Object



70
71
72
73
74
# File 'lib/httpclient/webagent-cookie.rb', line 70

def self.parse(str, url)
  cookie = new
  cookie.parse(str, url)
  cookie
end

Instance Method Details

#discard?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/httpclient/webagent-cookie.rb', line 83

def discard?
  @discard
end

#domain_orig?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/httpclient/webagent-cookie.rb', line 99

def domain_orig?
  @domain_orig
end

#flagObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/httpclient/webagent-cookie.rb', line 111

def flag
  flg = 0
  flg += USE  if @use
  flg += SECURE  if @secure
  flg += HTTP_ONLY  if @http_only
  flg += DOMAIN  if @domain_orig
  flg += PATH  if @path_orig
  flg += DISCARD if @discard
  flg += OVERRIDE if @override
  flg
end

#http_only?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/httpclient/webagent-cookie.rb', line 95

def http_only?
  @http_only
end

#join_quotedstr(array, sep) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/httpclient/webagent-cookie.rb', line 146

def join_quotedstr(array, sep)
  ret = Array.new
  old_elem = nil
  array.each{|elem|
    if (elem.scan(/"/).length % 2) == 0
      if old_elem
        old_elem << sep << elem
      else
        ret << elem
        old_elem = nil
      end
    else
      if old_elem
        old_elem << sep << elem
        ret << old_elem
        old_elem = nil
      else
        old_elem = elem.dup
      end
    end
  }
  ret
end

#match?(url) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/httpclient/webagent-cookie.rb', line 134

def match?(url)
  domainname = url.host
  if (!domainname ||
      !domain_match(domainname, @domain) ||
      (@path && !head_match?(@path, url.path.empty? ? '/' : url.path)) ||
      (@secure && (url.scheme != 'https')) )
    return false
  else
    return true
  end
end

#override?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/httpclient/webagent-cookie.rb', line 107

def override?
  @override
end

#parse(str, url) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/httpclient/webagent-cookie.rb', line 170

def parse(str, url)
  @url = url
  # TODO: should not depend on join_quotedstr. scan with escape like CSV.
  cookie_elem = str.split(/;/)
  cookie_elem = join_quotedstr(cookie_elem, ';')
  cookie_elem -= [""] # del empty elements, a cookie might included ";;"
  first_elem = cookie_elem.shift
  if first_elem !~ /([^=]*)(\=(.*))?/
    return
    ## raise ArgumentError 'invalid cookie value'
  end
  @name = $1.strip
  @value = normalize_cookie_value($3)
  cookie_elem.each{|pair|
    key, value = pair.split(/=/, 2)  ## value may nil
    key.strip!
    value = normalize_cookie_value(value)
    case key.downcase
    when 'domain'
      @domain = value
    when 'expires'
      @expires = nil
      begin
        @expires = Time.parse(value).gmtime if value
      rescue ArgumentError
      end
    when 'path'
      @path = value
    when 'secure'
      @secure = true  ## value may nil, but must 'true'.
    when 'httponly'
      @http_only = true  ## value may nil, but must 'true'.
    else
      warn("Unknown key: #{key} = #{value}")
    end
  }
end

#path_orig?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/httpclient/webagent-cookie.rb', line 103

def path_orig?
  @path_orig
end

#secure?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/httpclient/webagent-cookie.rb', line 91

def secure?
  @secure
end

#set_flag(flag) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/httpclient/webagent-cookie.rb', line 123

def set_flag(flag)
  flag = flag.to_i
  @use = true      if flag & USE > 0
  @secure = true   if flag & SECURE > 0
  @http_only = true   if flag & HTTP_ONLY > 0
  @domain_orig = true if flag & DOMAIN > 0
  @path_orig = true if flag & PATH > 0
  @discard  = true if flag & DISCARD > 0
  @override = true if flag & OVERRIDE > 0
end

#use?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/httpclient/webagent-cookie.rb', line 87

def use?
  @use
end