Class: WebAgent::CookieManager

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

Defined Under Namespace

Classes: Error, ErrorOverrideOK, NoDotError, SpecialError

Constant Summary collapse

SPECIAL_DOMAIN =
[".com",".edu",".gov",".mil",".net",".org",".int"]

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



218
219
220
221
222
223
224
225
226
# File 'lib/httpclient/cookie.rb', line 218

def initialize(file=nil)
  @cookies = Array.new()
  @cookies_file = file
  @is_saved = true
  @reject_domains = Array.new()
  @accept_domains = Array.new()
  # for conformance to http://wp.netscape.com/newsref/std/cookie_spec.html
  @netscape_rule = false
end

Instance Attribute Details

#accept_domainsObject

Returns the value of attribute accept_domains.



215
216
217
# File 'lib/httpclient/cookie.rb', line 215

def accept_domains
  @accept_domains
end

#cookiesObject

Returns the value of attribute cookies.



213
214
215
# File 'lib/httpclient/cookie.rb', line 213

def cookies
  @cookies
end

#cookies_fileObject

Returns the value of attribute cookies_file.



214
215
216
# File 'lib/httpclient/cookie.rb', line 214

def cookies_file
  @cookies_file
end

#netscape_ruleObject

Returns the value of attribute netscape_rule.



216
217
218
# File 'lib/httpclient/cookie.rb', line 216

def netscape_rule
  @netscape_rule
end

#reject_domainsObject

Returns the value of attribute reject_domains.



215
216
217
# File 'lib/httpclient/cookie.rb', line 215

def reject_domains
  @reject_domains
end

Instance Method Details

#add(cookie) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
# File 'lib/httpclient/cookie.rb', line 314

def add(cookie)
  url = cookie.url
  name, value = cookie.name, cookie.value
  expires, domain, path = 
	cookie.expires, cookie.domain, cookie.path
  secure, domain_orig, path_orig = 
	cookie.secure?, cookie.domain_orig?, cookie.path_orig?
  discard, override = 
	cookie.discard?, cookie.override?

  domainname = url.host
  domain_orig, path_orig = domain, path
  use_security = override

  if !domainname
	cookie_error(NodotError.new(), override)
  end

  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

    if @netscape_rule
      n = total_dot_num(domain)
      if n < 2
        cookie_error(SpecialError.new(), override)
      elsif n == 2
        ## [NETSCAPE] rule
        ok = SPECIAL_DOMAIN.select{|sdomain|
          sdomain == domain[-(sdomain.length)..-1]
        }
        if ok.empty?
          cookie_error(SpecialError.new(), override)
        end
      end
    end

  end

  path ||= url.path.sub!(%r|/[^/]*|, '')
  domain ||= domainname
  cookie = find_cookie_info(domain, path, name)

  if !cookie
	cookie = WebAgent::Cookie.new()
	cookie.use = true
	@cookies << cookie
  end

  cookie.url = url
  cookie.name = name
  cookie.value = value
  cookie.expires = expires
  cookie.domain = domain
  cookie.path = path

  ## for flag
  cookie.secure = secure
  cookie.domain_orig = domain_orig
  cookie.path_orig = path_orig
  if discard || cookie.expires == nil
	cookie.discard = true
  else
	cookie.discard = false
	@is_saved = false
  end

  check_expired_cookies()
  return false
end


410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/httpclient/cookie.rb', line 410

def check_cookie_accept_domain(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_cookiesObject



252
253
254
255
256
257
258
259
260
# File 'lib/httpclient/cookie.rb', line 252

def check_expired_cookies()
  @cookies.reject!{|cookie|
	is_expired = (cookie.expires && (cookie.expires < Time.now.gmtime))
	if is_expired && !cookie.discard?
	  @is_saved = false
	end
	is_expired
  }
end

#find(url) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/httpclient/cookie.rb', line 284

def find(url)

  check_expired_cookies()

  cookie_list = Array.new()

  @cookies.each{|cookie|
	if cookie.use? && cookie.match?(url)
	  if cookie_list.select{|c1| c1.name == cookie.name}.empty?
 cookie_list << cookie
	  end
	end
  }
  return make_cookie_str(cookie_list)
end

#load_cookiesObject



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/httpclient/cookie.rb', line 392

def load_cookies()
  return if !File.readable?(@cookies_file)
  File.open(@cookies_file,'r'){|f|
	while line = f.gets
	  cookie = WebAgent::Cookie.new()
	  @cookies << cookie
	  col = line.chomp.split(/\t/)
	  cookie.url = URI.parse(col[0])
	  cookie.name = col[1]
	  cookie.value = col[2]
	  cookie.expires = Time.at(col[3].to_i)
	  cookie.domain = col[4]
	  cookie.path = col[5]
	  cookie.set_flag(col[6])
	end
  }
end

#parse(str, url) ⇒ Object



262
263
264
265
266
# File 'lib/httpclient/cookie.rb', line 262

def parse(str, url)
  cookie = WebAgent::Cookie.new()
  cookie.parse(str, url)
  add(cookie)
end

#save_all_cookies(force = nil, save_unused = true, save_discarded = true) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/httpclient/cookie.rb', line 228

def save_all_cookies(force = nil, save_unused = true, save_discarded = true)
  if @is_saved and !force
	return
  end
  File.open(@cookies_file, 'w') do |f|
	@cookies.each do |cookie|
      if (cookie.use? or save_unused) and
          (!cookie.discard? or save_discarded)
 f.print(cookie.url.to_s,"\t",
  cookie.name,"\t",
  cookie.value,"\t",
  cookie.expires.to_i,"\t",
  cookie.domain,"\t",
  cookie.path,"\t",
  cookie.flag,"\n")
	  end
    end
  end
end

#save_cookies(force = nil) ⇒ Object



248
249
250
# File 'lib/httpclient/cookie.rb', line 248

def save_cookies(force = nil)
  save_all_cookies(force, false, false)
end