summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-05-19 19:27:00 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2015-05-21 10:12:58 -0500
commit6ba819dd88383a9ad7ef7af5b013cbf6a4da2c84 (patch)
tree55eff88af3c3a71a6ba78fcc9f9c8554dd4bc4f3
parent605552d20d7dee84484f9d197d11a42c05d0ee1f (diff)
downloadchef-6ba819dd88383a9ad7ef7af5b013cbf6a4da2c84.tar.gz
Allow spaces in files for remote_file
The following example now works ```ruby remote_file "c:\\nospaces" do source "file:///c:/foo bar" end ```
-rw-r--r--lib/chef/mixin/uris.rb11
-rw-r--r--lib/chef/provider/remote_file/content.rb5
-rw-r--r--lib/chef/provider/remote_file/local_file.rb12
-rw-r--r--lib/chef/resource/remote_file.rb5
4 files changed, 27 insertions, 6 deletions
diff --git a/lib/chef/mixin/uris.rb b/lib/chef/mixin/uris.rb
index 6c1ae793e2..0136b55f6a 100644
--- a/lib/chef/mixin/uris.rb
+++ b/lib/chef/mixin/uris.rb
@@ -28,6 +28,17 @@ class Chef
# From open-uri
!!(%r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ source)
end
+
+
+ def as_uri(source)
+ begin
+ URI.parse(source)
+ rescue URI::InvalidURIError
+ Chef::Log.warn("#{source} was an invalid URI. Trying to escape invalid characters")
+ URI.parse(URI.escape(source))
+ end
+ end
+
end
end
end
diff --git a/lib/chef/provider/remote_file/content.rb b/lib/chef/provider/remote_file/content.rb
index 938a7e58f4..4f450ce333 100644
--- a/lib/chef/provider/remote_file/content.rb
+++ b/lib/chef/provider/remote_file/content.rb
@@ -20,6 +20,7 @@
require 'uri'
require 'tempfile'
require 'chef/file_content_management/content_base'
+require 'chef/mixin/uris'
class Chef
class Provider
@@ -28,6 +29,8 @@ class Chef
private
+ include Chef::Mixin::Uris
+
def file_for_provider
Chef::Log.debug("#{@new_resource} checking for changes")
@@ -48,7 +51,7 @@ class Chef
uri = if Chef::Provider::RemoteFile::Fetcher.network_share?(source)
source
else
- URI.parse(source)
+ as_uri(source)
end
raw_file = grab_file_from_uri(uri)
rescue SocketError, Errno::ECONNREFUSED, Errno::ENOENT, Errno::EACCES, Timeout::Error, Net::HTTPServerException, Net::HTTPFatalError, Net::FTPError => e
diff --git a/lib/chef/provider/remote_file/local_file.rb b/lib/chef/provider/remote_file/local_file.rb
index e78311f2c3..d3fdcab24a 100644
--- a/lib/chef/provider/remote_file/local_file.rb
+++ b/lib/chef/provider/remote_file/local_file.rb
@@ -32,15 +32,19 @@ class Chef
@new_resource = new_resource
@uri = uri
end
-
+
# CHEF-4472: Remove the leading slash from windows paths that we receive from a file:// URI
- def fix_windows_path(path)
- path.gsub(/^\/([a-zA-Z]:)/,'\1')
+ def fix_windows_path(path)
+ path.gsub(/^\/([a-zA-Z]:)/,'\1')
+ end
+
+ def uri_path
+ URI.unescape(uri.path)
end
# Fetches the file at uri, returning a Tempfile-like File handle
def fetch
- source_path = Chef::Platform.windows? ? fix_windows_path(uri.path) : uri.path
+ source_path = Chef::Platform.windows? ? fix_windows_path(uri_path) : uri_path
tempfile = Chef::FileContentManagement::Tempfile.new(new_resource).tempfile
Chef::Log.debug("#{new_resource} staging #{source_path} to #{tempfile.path}")
FileUtils.cp(source_path, tempfile.path)
diff --git a/lib/chef/resource/remote_file.rb b/lib/chef/resource/remote_file.rb
index 69614f0298..df9fb7ad76 100644
--- a/lib/chef/resource/remote_file.rb
+++ b/lib/chef/resource/remote_file.rb
@@ -21,6 +21,7 @@ require 'uri'
require 'chef/resource/file'
require 'chef/provider/remote_file'
require 'chef/mixin/securable'
+require 'chef/mixin/uris'
class Chef
class Resource
@@ -127,6 +128,8 @@ class Chef
private
+ include Chef::Mixin::Uris
+
def validate_source(source)
source = Array(source).flatten
raise ArgumentError, "#{resource_name} has an empty source" if source.empty?
@@ -140,7 +143,7 @@ class Chef
end
def absolute_uri?(source)
- Chef::Provider::RemoteFile::Fetcher.network_share?(source) or (source.kind_of?(String) and URI.parse(source).absolute?)
+ Chef::Provider::RemoteFile::Fetcher.network_share?(source) or (source.kind_of?(String) and as_uri(source).absolute?)
rescue URI::InvalidURIError
false
end