X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Faviator%2Flib%2Fpuppet%2Ffeature%2Ffaraday%2Fupload_io.rb;fp=3rdparty%2Fmodules%2Faviator%2Flib%2Fpuppet%2Ffeature%2Ffaraday%2Fupload_io.rb;h=9130d159df06caa012b2f05e02fb6b2325952974;hb=4631045ebb77ee8622f6fa09277a50c372bcc02e;hp=0000000000000000000000000000000000000000;hpb=3d4dc4fd9e59bd0e07646c99f6b356c7d9d859aa;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/aviator/lib/puppet/feature/faraday/upload_io.rb b/3rdparty/modules/aviator/lib/puppet/feature/faraday/upload_io.rb new file mode 100644 index 000000000..9130d159d --- /dev/null +++ b/3rdparty/modules/aviator/lib/puppet/feature/faraday/upload_io.rb @@ -0,0 +1,67 @@ +begin + require 'composite_io' + require 'parts' + require 'stringio' +rescue LoadError + $stderr.puts "Install the multipart-post gem." + raise +end + +module Faraday + # Similar but not compatible with ::CompositeReadIO provided by multipart-post. + class CompositeReadIO + def initialize(*parts) + @parts = parts.flatten + @ios = @parts.map { |part| part.to_io } + @index = 0 + end + + def length + @parts.inject(0) { |sum, part| sum + part.length } + end + + def rewind + @ios.each { |io| io.rewind } + @index = 0 + end + + # Read from IOs in order until `length` bytes have been received. + def read(length = nil, outbuf = nil) + got_result = false + outbuf = outbuf ? outbuf.replace("") : "" + + while io = current_io + if result = io.read(length) + got_result ||= !result.nil? + result.force_encoding("BINARY") if result.respond_to?(:force_encoding) + outbuf << result + length -= result.length if length + break if length == 0 + end + advance_io + end + (!got_result && length) ? nil : outbuf + end + + def close + @ios.each { |io| io.close } + end + + def ensure_open_and_readable + # Rubinius compatibility + end + + private + + def current_io + @ios[@index] + end + + def advance_io + @index += 1 + end + end + + UploadIO = ::UploadIO + Parts = ::Parts +end