summaryrefslogtreecommitdiff
path: root/lib/rack/multipart
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rack/multipart')
-rw-r--r--lib/rack/multipart/generator.rb6
-rw-r--r--lib/rack/multipart/parser.rb76
-rw-r--r--lib/rack/multipart/uploaded_file.rb3
3 files changed, 35 insertions, 50 deletions
diff --git a/lib/rack/multipart/generator.rb b/lib/rack/multipart/generator.rb
index 1c586b75..edf11669 100644
--- a/lib/rack/multipart/generator.rb
+++ b/lib/rack/multipart/generator.rb
@@ -15,8 +15,8 @@ module Rack
flattened_params.map do |name, file|
if file.respond_to?(:original_filename)
- ::File.open(file.path, "rb") do |f|
- f.set_encoding(Encoding::BINARY) if f.respond_to?(:set_encoding)
+ ::File.open(file.path, 'rb') do |f|
+ f.set_encoding(Encoding::BINARY)
content_for_tempfile(f, file, name)
end
else
@@ -90,4 +90,4 @@ EOF
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/rack/multipart/parser.rb b/lib/rack/multipart/parser.rb
index 3be3a207..c47c5f37 100644
--- a/lib/rack/multipart/parser.rb
+++ b/lib/rack/multipart/parser.rb
@@ -27,11 +27,7 @@ module Rack
end
def initialize(boundary, io, content_length, env, tempfile, bufsize)
- @buf = ""
-
- if @buf.respond_to? :force_encoding
- @buf.force_encoding Encoding::ASCII_8BIT
- end
+ @buf = "".force_encoding(Encoding::ASCII_8BIT)
@params = Utils::KeySpaceConstrainedParams.new
@boundary = "--#{boundary}"
@@ -110,11 +106,7 @@ module Rack
def get_current_head_and_filename_and_content_type_and_name_and_body
head = nil
- body = ''
-
- if body.respond_to? :force_encoding
- body.force_encoding Encoding::ASCII_8BIT
- end
+ body = ''.force_encoding(Encoding::ASCII_8BIT)
filename = content_type = name = nil
@@ -172,7 +164,7 @@ module Rack
filename = Utils.unescape(filename)
end
- scrub_filename filename
+ scrub_filename(filename)
if filename !~ /\\[^\\"]/
filename = filename.gsub(/\\(.)/, '\1')
@@ -180,51 +172,45 @@ module Rack
filename
end
- if "<3".respond_to? :valid_encoding?
- def scrub_filename(filename)
- unless filename.valid_encoding?
- # FIXME: this force_encoding is for Ruby 2.0 and 1.9 support.
- # We can remove it after they are dropped
- filename.force_encoding(Encoding::ASCII_8BIT)
- filename.encode!(:invalid => :replace, :undef => :replace)
- end
+ def scrub_filename(filename)
+ unless filename.valid_encoding?
+ # FIXME: this force_encoding is for Ruby 2.0 and 1.9 support.
+ # We can remove it after they are dropped
+ filename.force_encoding(Encoding::ASCII_8BIT)
+ filename.encode!(:invalid => :replace, :undef => :replace)
end
+ end
- CHARSET = "charset"
+ CHARSET = "charset"
- def tag_multipart_encoding(filename, content_type, name, body)
- name = name.to_s
- name.force_encoding Encoding::UTF_8
+ def tag_multipart_encoding(filename, content_type, name, body)
+ name = name.to_s
+ encoding = Encoding::UTF_8
- return if filename
+ name.force_encoding(encoding)
- encoding = Encoding::UTF_8
+ return if filename
- if content_type
- list = content_type.split(';')
- type_subtype = list.first
- type_subtype.strip!
- if TEXT_PLAIN == type_subtype
- rest = list.drop 1
- rest.each do |param|
- k,v = param.split('=', 2)
- k.strip!
- v.strip!
- encoding = Encoding.find v if k == CHARSET
- end
+ if content_type
+ list = content_type.split(';')
+ type_subtype = list.first
+ type_subtype.strip!
+ if TEXT_PLAIN == type_subtype
+ rest = list.drop 1
+ rest.each do |param|
+ k,v = param.split('=', 2)
+ k.strip!
+ v.strip!
+ encoding = Encoding.find v if k == CHARSET
end
end
-
- name.force_encoding encoding
- body.force_encoding encoding
- end
- else
- def scrub_filename(filename)
- end
- def tag_multipart_encoding(filename, content_type, name, body)
end
+
+ name.force_encoding(encoding)
+ body.force_encoding(encoding)
end
+
def get_data(filename, body, content_type, name, head)
data = body
if filename == ""
diff --git a/lib/rack/multipart/uploaded_file.rb b/lib/rack/multipart/uploaded_file.rb
index 1b56ad75..924b1f08 100644
--- a/lib/rack/multipart/uploaded_file.rb
+++ b/lib/rack/multipart/uploaded_file.rb
@@ -11,8 +11,7 @@ module Rack
raise "#{path} file does not exist" unless ::File.exist?(path)
@content_type = content_type
@original_filename = ::File.basename(path)
- @tempfile = Tempfile.new([@original_filename, ::File.extname(path)])
- @tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)
+ @tempfile = Tempfile.new([@original_filename, ::File.extname(path)], encoding: Encoding::BINARY)
@tempfile.binmode if binary
FileUtils.copy_file(path, @tempfile.path)
end