Class: Proxy::Omaha::HttpVerify

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/smart_proxy_omaha/http_verify.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ HttpVerify

Returns a new instance of HttpVerify.



7
8
9
10
11
# File 'lib/smart_proxy_omaha/http_verify.rb', line 7

def initialize(opts = {})
  self.http_request = opts.fetch(:http_request)
  self.local_file = opts.fetch(:local_file)
  self.filename = opts.fetch(:filename, File.basename(local_file))
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



5
6
7
# File 'lib/smart_proxy_omaha/http_verify.rb', line 5

def filename
  @filename
end

#http_requestObject

Returns the value of attribute http_request.



5
6
7
# File 'lib/smart_proxy_omaha/http_verify.rb', line 5

def http_request
  @http_request
end

#local_fileObject

Returns the value of attribute local_file.



5
6
7
# File 'lib/smart_proxy_omaha/http_verify.rb', line 5

def local_file
  @local_file
end

Instance Method Details

#content_lengthObject



64
65
66
67
# File 'lib/smart_proxy_omaha/http_verify.rb', line 64

def content_length
  length = headers['content-length'] || headers['x-goog-stored-content-length']
  length.first.to_i if length
end

#file_size_valid?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/smart_proxy_omaha/http_verify.rb', line 20

def file_size_valid?
  unless remote_size
    logger.error "#{filename}: Cannot determine remote file size."
    return false
  end
  unless local_size == remote_size
    logger.debug "#{filename}: File sizes do not match. Remote: #{remote_size}, Local: #{local_size}"
    return false
  end
  true
end

#headersObject



69
70
71
# File 'lib/smart_proxy_omaha/http_verify.rb', line 69

def headers
  @headers ||= http_request.to_hash
end

#local_md5Object



56
57
58
# File 'lib/smart_proxy_omaha/http_verify.rb', line 56

def local_md5
  @local_md5 ||= Digest::MD5.file(local_file).base64digest
end

#local_sizeObject



32
33
34
# File 'lib/smart_proxy_omaha/http_verify.rb', line 32

def local_size
  File.size(local_file)
end

#md5_hash_valid?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/smart_proxy_omaha/http_verify.rb', line 40

def md5_hash_valid?
  unless local_md5 == remote_md5
    logger.debug "#{filename}: MD5 checksums do not match. Remote: #{remote_md5}, Local: #{local_md5}"
    return false
  end
  true
end

#remote_hashesObject



48
49
50
51
52
53
54
# File 'lib/smart_proxy_omaha/http_verify.rb', line 48

def remote_hashes
  headers['x-goog-hash'].inject({}) do |hsh, header|
    key, value = header.split('=', 2)
    hsh[key] = value
    hsh
  end
end

#remote_md5Object



60
61
62
# File 'lib/smart_proxy_omaha/http_verify.rb', line 60

def remote_md5
  remote_hashes['md5']
end

#remote_sizeObject



36
37
38
# File 'lib/smart_proxy_omaha/http_verify.rb', line 36

def remote_size
  @remote_size ||= content_length
end

#valid?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
# File 'lib/smart_proxy_omaha/http_verify.rb', line 13

def valid?
  logger.debug "#{filename}: Verifying if file is valid."
  return false unless file_size_valid?
  return false if headers['x-goog-hash'] && !md5_hash_valid?
  true
end