diff options
author | Jesse Campbell <hikeit@gmail.com> | 2013-02-26 13:41:21 -0500 |
---|---|---|
committer | Jesse Campbell <hikeit@gmail.com> | 2013-02-26 13:41:21 -0500 |
commit | 3685a5ed995d74a90ccf6a2af0615a4f92e02bce (patch) | |
tree | 3817a8570532517b7661e9e02ca0a51dc0de9002 /lib/chef/provider/remote_file.rb | |
parent | 691e0195fdbea22c35f2b472c7c2330a432942ac (diff) | |
download | chef-3685a5ed995d74a90ccf6a2af0615a4f92e02bce.tar.gz |
refactor http out of fetch
Diffstat (limited to 'lib/chef/provider/remote_file.rb')
-rw-r--r-- | lib/chef/provider/remote_file.rb | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/lib/chef/provider/remote_file.rb b/lib/chef/provider/remote_file.rb index e979f5a4ba..d7a009475d 100644 --- a/lib/chef/provider/remote_file.rb +++ b/lib/chef/provider/remote_file.rb @@ -138,50 +138,23 @@ class Chef if_modified_since ||= @current_resource.last_modified if_none_match ||= @current_resource.etag end - target_matched = false - raw_file = nil - last_modified = nil - etag = nil if URI::HTTP === uri #HTTP or HTTPS - begin - headers = Hash.new - if if_none_match - headers[:if_none_match] = "\"#{if_none_match}\"" - elsif if_modified_since - headers[:if_modified_since] = if_modified_since.strftime("%a, %d %b %Y %H:%M:%S %Z") - end - rest = RestClient::Request.execute(:method => :get, :url => uri.to_s, :headers => headers, :raw_response => true) - raw_file = rest.file - if rest.headers.include?(:last_modified) - last_modified = Time.parse(rest.headers[:last_modified]) - end - if rest.headers.include?(:etag) - etag = rest.headers[:etag] - end - rescue RestClient::Exception => e - if e.http_code == 304 - target_matched = true - else - raise e - end - end + raw_file, last_modified, etag, target_matched = http_fetch(uri, if_modified_since, if_none_match) elsif URI::FTP === uri #FTP raw_file, last_modified = FTP::fetch_if_modified(uri, @new_resource.ftp_active_mode, if_modified_since) - if last_modified && if_modified_since && last_modified <= if_modified_since - target_matched = true - end + etag = nil + target_matched = last_modified && if_modified_since && last_modified.to_i <= if_modified_since.to_i elsif uri.scheme == "file" #local/network file last_modified = ::File.mtime(uri.path) + etag = nil raw_file = ::File.new(uri.path, "r") def raw_file.close! self.close end - if last_modified && if_modified_since && last_modified.to_i <= if_modified_since.to_i - target_matched = true - end + target_mathed = last_modified && if_modified_since && last_modified.to_i <= if_modified_since.to_i else raise ArgumentError, "Invalid uri. Only http(s), ftp, and file are currently supported" end @@ -193,6 +166,35 @@ class Chef return raw_file, target_matched end + def http_fetch(uri, if_modified_since, if_none_match) + last_modified = nil + etag = nil + target_matched = false + begin + headers = Hash.new + if if_none_match + headers[:if_none_match] = "\"#{if_none_match}\"" + elsif if_modified_since + headers[:if_modified_since] = if_modified_since.strftime("%a, %d %b %Y %H:%M:%S %Z") + end + rest = RestClient::Request.execute(:method => :get, :url => uri.to_s, :headers => headers, :raw_response => true) + raw_file = rest.file + if rest.headers.include?(:last_modified) + last_modified = Time.parse(rest.headers[:last_modified]) + end + if rest.headers.include?(:etag) + etag = rest.headers[:etag] + end + rescue RestClient::Exception => e + if e.http_code == 304 + target_matched = true + else + raise e + end + end + return raw_file, last_modified, etag, target_matched + end + def load_fileinfo begin Chef::JSONCompat.from_json(Chef::FileCache.load("remote_file/#{new_resource.name}")) |