Class: Proxy::Omaha::HttpVerify
- Inherits:
-
Object
- Object
- Proxy::Omaha::HttpVerify
- Includes:
- Log
- Defined in:
- lib/smart_proxy_omaha/http_verify.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#http_request ⇒ Object
Returns the value of attribute http_request.
-
#local_file ⇒ Object
Returns the value of attribute local_file.
Instance Method Summary collapse
- #content_length ⇒ Object
- #file_size_valid? ⇒ Boolean
- #headers ⇒ Object
-
#initialize(opts = {}) ⇒ HttpVerify
constructor
A new instance of HttpVerify.
- #local_md5 ⇒ Object
- #local_size ⇒ Object
- #md5_hash_valid? ⇒ Boolean
- #remote_hashes ⇒ Object
- #remote_md5 ⇒ Object
- #remote_size ⇒ Object
- #valid? ⇒ Boolean
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
#filename ⇒ Object
Returns the value of attribute filename.
5 6 7 |
# File 'lib/smart_proxy_omaha/http_verify.rb', line 5 def filename @filename end |
#http_request ⇒ Object
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_file ⇒ Object
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_length ⇒ Object
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
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 |
#headers ⇒ Object
69 70 71 |
# File 'lib/smart_proxy_omaha/http_verify.rb', line 69 def headers @headers ||= http_request.to_hash end |
#local_md5 ⇒ Object
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_size ⇒ Object
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
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_hashes ⇒ Object
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_md5 ⇒ Object
60 61 62 |
# File 'lib/smart_proxy_omaha/http_verify.rb', line 60 def remote_md5 remote_hashes['md5'] end |
#remote_size ⇒ Object
36 37 38 |
# File 'lib/smart_proxy_omaha/http_verify.rb', line 36 def remote_size @remote_size ||= content_length end |
#valid? ⇒ 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 |