Class: HttpMimic::CookieStore

Inherits:
Object
  • Object
show all
Defined in:
lib/http_mimic/cookie_store.rb

Constant Summary collapse

DEFAULT_TTL =

2 hours

7200

Class Method Summary collapse

Class Method Details

.clear(host = nil) ⇒ Object

Clear stored cookies for a host or all hosts



85
86
87
88
89
90
91
92
# File 'lib/http_mimic/cookie_store.rb', line 85

def clear(host = nil)
  if host
    file = cookie_file_for(host)
    FileUtils.rm_f(file)
  else
    FileUtils.rm_rf(store_dir)
  end
end


16
17
18
19
# File 'lib/http_mimic/cookie_store.rb', line 16

def cookie_file_for(host)
  clean_host = sanitize_host(host)
  File.join(store_dir, "#{clean_host}.json")
end

.extract_host(url_or_host) ⇒ Object

Extract host from URL or host string



95
96
97
98
99
100
101
102
103
104
# File 'lib/http_mimic/cookie_store.rb', line 95

def extract_host(url_or_host)
  str = url_or_host.to_s.strip
  if str =~ %r{^https?://}i
    URI.parse(str).host
  else
    str.split(':').first
  end
rescue StandardError
  str
end

.load(host, max_age: DEFAULT_TTL) ⇒ Object

Load cookies for a given host Returns a Hash of cookies if valid and not expired, or empty Hash



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/http_mimic/cookie_store.rb', line 23

def load(host, max_age: DEFAULT_TTL)
  file = cookie_file_for(host)
  return {} unless File.file?(file)

  data = nil
  File.open(file, File::RDONLY) do |f|
    f.flock(File::LOCK_SH)
    data = JSON.parse(f.read) rescue nil
  end

  return {} unless data.is_a?(Hash)

  updated_at = data['updated_at'].to_i
  if !max_age.nil?
    return {} if (Time.now.to_i - updated_at) > max_age
  end

  data['cookies'] || {}
rescue StandardError => e
  if HttpMimic.configuration.debug
    puts "[HttpMimic::CookieStore] Error loading cookies for #{host}: #{e.message}"
  end
  {}
end

.save(host, cookies, ttl: DEFAULT_TTL) ⇒ Object

Save cookies for a given host with atomic locking



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
# File 'lib/http_mimic/cookie_store.rb', line 49

def save(host, cookies, ttl: DEFAULT_TTL)
  return if cookies.nil? || cookies.empty?

  clean_host = sanitize_host(host)
  dir = store_dir
  FileUtils.mkdir_p(dir)

  file = cookie_file_for(clean_host)
  existing_cookies = load(clean_host, max_age: ttl) || {}

  new_cookies = cookies.is_a?(Cookies) ? cookies.to_h : cookies.dup
  merged = existing_cookies.merge(stringify_keys(new_cookies))

  payload = {
    'host' => clean_host,
    'updated_at' => Time.now.to_i,
    'ttl' => ttl,
    'cookies' => merged
  }

  # Thread & Process safe atomic write
  tmp_file = "#{file}.tmp.#{Process.pid}_#{Time.now.to_f}"
  File.open(tmp_file, File::RDWR | File::CREAT, 0644) do |f|
    f.flock(File::LOCK_EX)
    f.write(JSON.pretty_generate(payload))
    f.flush
  end
  File.rename(tmp_file, file)
rescue StandardError => e
  FileUtils.rm_f(tmp_file) if tmp_file
  if HttpMimic.configuration.debug
    puts "[HttpMimic::CookieStore] Error saving cookies for #{host}: #{e.message}"
  end
end

.store_dirObject



12
13
14
# File 'lib/http_mimic/cookie_store.rb', line 12

def store_dir
  File.expand_path(HttpMimic.configuration.cookie_store_dir || '~/.http_mimic/cookies')
end