From ff9bf3d0213266aaff5579ef5cc25a1f0b063061 Mon Sep 17 00:00:00 2001 From: Alexey Zapparov Date: Wed, 18 Jan 2017 13:27:48 +0100 Subject: [PATCH] Set Content-Type on non-file parts Resolves #5 --- lib/http/form_data.rb | 3 +++ lib/http/form_data/file.rb | 3 --- lib/http/form_data/multipart/param.rb | 31 +++++++++-------------- spec/lib/http/form_data/multipart_spec.rb | 1 + 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index 73749af..2738e31 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -27,6 +27,9 @@ module HTTP # socket << "\r\n" # socket << form.to_s module FormData + # Default MIME type + DEFAULT_MIME = "application/octet-stream" + # CRLF CRLF = "\r\n" diff --git a/lib/http/form_data/file.rb b/lib/http/form_data/file.rb index adcf5de..da5b67e 100644 --- a/lib/http/form_data/file.rb +++ b/lib/http/form_data/file.rb @@ -19,9 +19,6 @@ module FormData # # FormData::File.new "/home/ixti/avatar.png" class File - # Default MIME type - DEFAULT_MIME = "application/octet-stream" - attr_reader :mime_type, :filename # @see DEFAULT_MIME diff --git a/lib/http/form_data/multipart/param.rb b/lib/http/form_data/multipart/param.rb index 32b9c90..5562784 100644 --- a/lib/http/form_data/multipart/param.rb +++ b/lib/http/form_data/multipart/param.rb @@ -5,19 +5,21 @@ module FormData class Multipart # Utility class to represent multi-part chunks class Param - CONTENT_DISPOSITION = "" - # @param [#to_s] name # @param [FormData::File, #to_s] value def initialize(name, value) - @name = name.to_s - @value = value - @header = "Content-Disposition: form-data; name=#{@name.inspect}" + mime = value.mime_type if value.respond_to?(:mime_type) + params = ["name=#{name.to_s.inspect}"] - return unless file? + if value.is_a? FormData::File + @value = value + params << "filename=#{value.filename.inspect}" + else + @value = value.to_s + end - @header = "#{@header}; filename=#{value.filename.inspect}#{CRLF}" \ - "Content-Type: #{value.mime_type}" + @header = "Content-Disposition: form-data; #{params.join '; '}" \ + "#{CRLF}Content-Type: #{mime || DEFAULT_MIME}" end # Returns body part with headers and data. @@ -46,10 +48,10 @@ def to_s def size size = @header.bytesize + (CRLF.bytesize * 2) - if file? + if @value.is_a? FormData::File size + @value.size else - size + @value.to_s.bytesize + size + @value.bytesize end end @@ -70,15 +72,6 @@ def self.coerce(data) params end - - private - - # Tells whenever value is a {FormData::File} or not. - # - # @return [Boolean] - def file? - @value.is_a? FormData::File - end end end end diff --git a/spec/lib/http/form_data/multipart_spec.rb b/spec/lib/http/form_data/multipart_spec.rb index 8571fab..b058c17 100644 --- a/spec/lib/http/form_data/multipart_spec.rb +++ b/spec/lib/http/form_data/multipart_spec.rb @@ -33,6 +33,7 @@ def disposition(params) expect(form_data.to_s).to eq [ "--#{boundary_value}#{crlf}", "#{disposition 'name' => 'foo'}#{crlf}", + "Content-Type: application/octet-stream#{crlf}", "#{crlf}bar#{crlf}", "--#{boundary_value}#{crlf}", "#{disposition 'name' => 'baz', 'filename' => file.filename}#{crlf}",