Class: WebAgent::CookieManager
- Inherits:
-
Object
- Object
- WebAgent::CookieManager
- Includes:
- CookieUtils
- Defined in:
- lib/httpclient/cookie.rb
Defined Under Namespace
Classes: Error, ErrorOverrideOK, SpecialError
Constant Summary collapse
- SPECIAL_DOMAIN =
[".com",".edu",".gov",".mil",".net",".org",".int"]
Instance Attribute Summary collapse
-
#accept_domains ⇒ Object
Returns the value of attribute accept_domains.
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#cookies_file ⇒ Object
Returns the value of attribute cookies_file.
-
#netscape_rule ⇒ Object
for conformance to wp.netscape.com/newsref/std/cookie_spec.html.
-
#reject_domains ⇒ Object
Returns the value of attribute reject_domains.
Instance Method Summary collapse
- #add(cookie) ⇒ Object
- #check_cookie_accept_domain(domain) ⇒ Object
- #check_expired_cookies ⇒ Object
- #find(url) ⇒ Object
-
#initialize(file = nil) ⇒ CookieManager
constructor
A new instance of CookieManager.
- #load_cookies ⇒ Object
- #parse(str, url) ⇒ Object
- #save_all_cookies(force = nil, save_unused = true, save_discarded = true) ⇒ Object
- #save_cookies(force = nil) ⇒ Object
Methods included from CookieUtils
#domain_match, #head_match?, #tail_match?, #total_dot_num
Constructor Details
#initialize(file = nil) ⇒ CookieManager
Returns a new instance of CookieManager.
232 233 234 235 236 237 238 239 240 |
# File 'lib/httpclient/cookie.rb', line 232 def initialize(file=nil) @cookies = Array.new() @cookies.extend(MonitorMixin) @cookies_file = file @is_saved = true @reject_domains = Array.new() @accept_domains = Array.new() @netscape_rule = false end |
Instance Attribute Details
#accept_domains ⇒ Object
Returns the value of attribute accept_domains.
226 227 228 |
# File 'lib/httpclient/cookie.rb', line 226 def accept_domains @accept_domains end |
#cookies ⇒ Object
Returns the value of attribute cookies.
224 225 226 |
# File 'lib/httpclient/cookie.rb', line 224 def @cookies end |
#cookies_file ⇒ Object
Returns the value of attribute cookies_file.
225 226 227 |
# File 'lib/httpclient/cookie.rb', line 225 def @cookies_file end |
#netscape_rule ⇒ Object
for conformance to wp.netscape.com/newsref/std/cookie_spec.html
229 230 231 |
# File 'lib/httpclient/cookie.rb', line 229 def netscape_rule @netscape_rule end |
#reject_domains ⇒ Object
Returns the value of attribute reject_domains.
226 227 228 |
# File 'lib/httpclient/cookie.rb', line 226 def reject_domains @reject_domains end |
Instance Method Details
#add(cookie) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/httpclient/cookie.rb', line 337 def add() url = .url name, value = .name, .value expires, domain, path = .expires, .domain, .path secure, http_only, domain_orig, path_orig = .secure?, .http_only?, .domain_orig?, .path_orig? discard, override = .discard?, .override? domainname = url.host domain_orig, path_orig = domain, path if domain # [DRAFT 12] s. 4.2.2 (does not apply in the case that # host name is the same as domain attribute for version 0 # cookie) # I think that this rule has almost the same effect as the # tail match of [NETSCAPE]. if domain !~ /^\./ && domainname != domain domain = '.'+domain end # [NETSCAPE] rule if @netscape_rule n = total_dot_num(domain) if n < 2 (SpecialError.new(), override) elsif n == 2 ## [NETSCAPE] rule ok = SPECIAL_DOMAIN.select{|sdomain| sdomain == domain[-(sdomain.length)..-1] } if ok.empty? (SpecialError.new(), override) end end end # this implementation does not check RFC2109 4.3.2 case 2; # the portion of host not in domain does not contain a dot. # according to nsCookieService.cpp in Firefox 3.0.4, Firefox 3.0.4 # and IE does not check, too. end path ||= url.path.sub(%r|/[^/]*\z|, '') domain ||= domainname @cookies.synchronize do = (domain, path, name) if ! = WebAgent::Cookie.new() .use = true @cookies << end () end .url = url .name = name .value = value .expires = expires .domain = domain .path = path ## for flag .secure = secure .http_only = http_only .domain_orig = domain_orig .path_orig = path_orig if discard || .expires == nil .discard = true else .discard = false @is_saved = false end end |
#check_cookie_accept_domain(domain) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/httpclient/cookie.rb', line 440 def (domain) unless domain return false end @accept_domains.each{|dom| if domain_match(domain, dom) return true end } @reject_domains.each{|dom| if domain_match(domain, dom) return false end } return true end |
#check_expired_cookies ⇒ Object
275 276 277 278 279 280 281 282 283 |
# File 'lib/httpclient/cookie.rb', line 275 def () @cookies.reject!{|| is_expired = (.expires && (.expires < Time.now.gmtime)) if is_expired && !.discard? @is_saved = false end is_expired } end |
#find(url) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/httpclient/cookie.rb', line 307 def find(url) return nil if @cookies.empty? = Array.new() @cookies.each{|| is_expired = (.expires && (.expires < Time.now.gmtime)) if .use? && !is_expired && .match?(url) if .select{|c1| c1.name == .name}.empty? << end end } return () end |
#load_cookies ⇒ Object
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/httpclient/cookie.rb', line 415 def () return if !File.readable?(@cookies_file) @cookies.synchronize do @cookies.clear File.open(@cookies_file,'r'){|f| while line = f.gets = WebAgent::Cookie.new() @cookies << col = line.chomp.split(/\t/) .url = URI.parse(col[0]) .name = col[1] .value = col[2] if col[3].empty? or col[3] == '0' .expires = nil else .expires = Time.at(col[3].to_i).gmtime end .domain = col[4] .path = col[5] .set_flag(col[6]) end } end end |
#parse(str, url) ⇒ Object
285 286 287 288 289 |
# File 'lib/httpclient/cookie.rb', line 285 def parse(str, url) = WebAgent::Cookie.new() .parse(str, url) add() end |
#save_all_cookies(force = nil, save_unused = true, save_discarded = true) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/httpclient/cookie.rb', line 247 def (force = nil, save_unused = true, save_discarded = true) @cookies.synchronize do () if @is_saved and !force return end File.open(@cookies_file, 'w') do |f| @cookies.each do || if (.use? or save_unused) and (!.discard? or save_discarded) f.print(.url.to_s,"\t", .name,"\t", .value,"\t", .expires.to_i,"\t", .domain,"\t", .path,"\t", .flag,"\n") end end end end @is_saved = true end |
#save_cookies(force = nil) ⇒ Object
271 272 273 |
# File 'lib/httpclient/cookie.rb', line 271 def (force = nil) (force, false, false) end |