Class: WebAgent::CookieManager

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

Defined Under Namespace

Classes: Error, ErrorOverrideOK, SpecialError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CookieUtils

#domain_match, #head_match?, #tail_match?

Constructor Details

#initialize(file = nil) ⇒ CookieManager

Returns a new instance of CookieManager.



224
225
226
227
228
229
230
231
232
# File 'lib/httpclient/cookie.rb', line 224

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_domainsObject

Returns the value of attribute accept_domains.



222
223
224
# File 'lib/httpclient/cookie.rb', line 222

def accept_domains
  @accept_domains
end

#cookiesObject

Returns the value of attribute cookies.



220
221
222
# File 'lib/httpclient/cookie.rb', line 220

def cookies
  @cookies
end

#cookies_fileObject

Returns the value of attribute cookies_file.



221
222
223
# File 'lib/httpclient/cookie.rb', line 221

def cookies_file
  @cookies_file
end

#reject_domainsObject

Returns the value of attribute reject_domains.



222
223
224
# File 'lib/httpclient/cookie.rb', line 222

def reject_domains
  @reject_domains
end

Instance Method Details

#add(given) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/httpclient/cookie.rb', line 298

def add(given)
  check_domain(given.domain, given.url.host, given.override?)

  domain = given.domain || given.url.host
  path = given.path || given.url.path.sub(%r|/[^/]*\z|, '')

  cookie = nil
  @cookies.synchronize do
    cookie = @cookies.find { |c|
      c.domain == domain && c.path == path && c.name == given.name
    }
    if !cookie
      cookie = WebAgent::Cookie.new
      cookie.use = true
      @cookies << cookie
    end
    check_expired_cookies
  end

  cookie.domain = domain
  cookie.path = path
  cookie.url = given.url
  cookie.name = given.name
  cookie.value = given.value
  cookie.expires = given.expires
  cookie.secure = given.secure?
  cookie.http_only = given.http_only?
  cookie.domain_orig = given.domain
  cookie.path_orig = given.path

  if cookie.discard? || cookie.expires == nil
	cookie.discard = true
  else
	cookie.discard = false
	@is_saved = false
  end
end

Who use it?



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/httpclient/cookie.rb', line 362

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



267
268
269
270
271
272
273
274
275
# File 'lib/httpclient/cookie.rb', line 267

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



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

def find(url)
  return nil if @cookies.empty?

  cookie_list = Array.new
  @cookies.each{|cookie|
    is_expired = (cookie.expires && (cookie.expires < Time.now.gmtime))
    if cookie.use? && !is_expired && 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



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/httpclient/cookie.rb', line 336

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

#parse(str, url) ⇒ Object



277
278
279
280
281
# File 'lib/httpclient/cookie.rb', line 277

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



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/httpclient/cookie.rb', line 239

def save_all_cookies(force = nil, save_unused = true, save_discarded = true)
  @cookies.synchronize do
    check_expired_cookies
    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
  @is_saved = true
end

#save_cookies(force = nil) ⇒ Object



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

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