|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module HTTParty |
| 4 | + class Request |
| 5 | + class StreamingMultipartBody |
| 6 | + NEWLINE = "\r\n" |
| 7 | + CHUNK_SIZE = 64 * 1024 # 64 KB chunks |
| 8 | + |
| 9 | + def initialize(parts, boundary) |
| 10 | + @parts = parts |
| 11 | + @boundary = boundary |
| 12 | + @part_index = 0 |
| 13 | + @state = :header |
| 14 | + @current_file = nil |
| 15 | + @header_buffer = nil |
| 16 | + @header_offset = 0 |
| 17 | + @footer_sent = false |
| 18 | + end |
| 19 | + |
| 20 | + def size |
| 21 | + @size ||= calculate_size |
| 22 | + end |
| 23 | + |
| 24 | + def read(length = nil, outbuf = nil) |
| 25 | + outbuf = outbuf ? outbuf.replace(''.b) : ''.b |
| 26 | + |
| 27 | + return read_all(outbuf) if length.nil? |
| 28 | + |
| 29 | + while outbuf.bytesize < length |
| 30 | + chunk = read_chunk(length - outbuf.bytesize) |
| 31 | + break if chunk.nil? |
| 32 | + outbuf << chunk |
| 33 | + end |
| 34 | + |
| 35 | + outbuf.empty? ? nil : outbuf |
| 36 | + end |
| 37 | + |
| 38 | + def rewind |
| 39 | + @part_index = 0 |
| 40 | + @state = :header |
| 41 | + @current_file = nil |
| 42 | + @header_buffer = nil |
| 43 | + @header_offset = 0 |
| 44 | + @footer_sent = false |
| 45 | + @parts.each do |_key, value, _is_file| |
| 46 | + value.rewind if value.respond_to?(:rewind) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + private |
| 51 | + |
| 52 | + def read_all(outbuf) |
| 53 | + while (chunk = read_chunk(CHUNK_SIZE)) |
| 54 | + outbuf << chunk |
| 55 | + end |
| 56 | + outbuf.empty? ? nil : outbuf |
| 57 | + end |
| 58 | + |
| 59 | + def read_chunk(max_length) |
| 60 | + loop do |
| 61 | + return nil if @part_index >= @parts.size && @footer_sent |
| 62 | + |
| 63 | + if @part_index >= @parts.size |
| 64 | + @footer_sent = true |
| 65 | + return "--#{@boundary}--#{NEWLINE}".b |
| 66 | + end |
| 67 | + |
| 68 | + key, value, is_file = @parts[@part_index] |
| 69 | + |
| 70 | + case @state |
| 71 | + when :header |
| 72 | + chunk = read_header_chunk(key, value, is_file, max_length) |
| 73 | + return chunk if chunk |
| 74 | + |
| 75 | + when :body |
| 76 | + chunk = read_body_chunk(value, is_file, max_length) |
| 77 | + return chunk if chunk |
| 78 | + |
| 79 | + when :newline |
| 80 | + @state = :header |
| 81 | + @part_index += 1 |
| 82 | + return NEWLINE.b |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + def read_header_chunk(key, value, is_file, max_length) |
| 88 | + if @header_buffer.nil? |
| 89 | + @header_buffer = build_part_header(key, value, is_file) |
| 90 | + @header_offset = 0 |
| 91 | + end |
| 92 | + |
| 93 | + remaining = @header_buffer.bytesize - @header_offset |
| 94 | + if remaining > 0 |
| 95 | + chunk_size = [remaining, max_length].min |
| 96 | + chunk = @header_buffer.byteslice(@header_offset, chunk_size) |
| 97 | + @header_offset += chunk_size |
| 98 | + return chunk |
| 99 | + end |
| 100 | + |
| 101 | + @header_buffer = nil |
| 102 | + @header_offset = 0 |
| 103 | + @state = :body |
| 104 | + nil |
| 105 | + end |
| 106 | + |
| 107 | + def read_body_chunk(value, is_file, max_length) |
| 108 | + if is_file |
| 109 | + chunk = read_file_chunk(value, max_length) |
| 110 | + if chunk |
| 111 | + return chunk |
| 112 | + else |
| 113 | + @current_file = nil |
| 114 | + @state = :newline |
| 115 | + return nil |
| 116 | + end |
| 117 | + else |
| 118 | + @state = :newline |
| 119 | + return value.to_s.b |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + def read_file_chunk(file, max_length) |
| 124 | + chunk_size = [max_length, CHUNK_SIZE].min |
| 125 | + chunk = file.read(chunk_size) |
| 126 | + return nil if chunk.nil? |
| 127 | + chunk.force_encoding(Encoding::BINARY) if chunk.respond_to?(:force_encoding) |
| 128 | + chunk |
| 129 | + end |
| 130 | + |
| 131 | + def build_part_header(key, value, is_file) |
| 132 | + header = "--#{@boundary}#{NEWLINE}".b |
| 133 | + header << %(Content-Disposition: form-data; name="#{key}").b |
| 134 | + if is_file |
| 135 | + header << %(; filename="#{file_name(value).gsub(/["\r\n]/, replacement_table)}").b |
| 136 | + header << NEWLINE.b |
| 137 | + header << "Content-Type: #{content_type(value)}#{NEWLINE}".b |
| 138 | + end |
| 139 | + header << NEWLINE.b |
| 140 | + header |
| 141 | + end |
| 142 | + |
| 143 | + def calculate_size |
| 144 | + total = 0 |
| 145 | + @parts.each do |key, value, is_file| |
| 146 | + total += build_part_header(key, value, is_file).bytesize |
| 147 | + total += content_size(value, is_file) |
| 148 | + total += NEWLINE.bytesize |
| 149 | + end |
| 150 | + total += "--#{@boundary}--#{NEWLINE}".bytesize |
| 151 | + total |
| 152 | + end |
| 153 | + |
| 154 | + def content_size(value, is_file) |
| 155 | + if is_file |
| 156 | + if value.respond_to?(:size) |
| 157 | + value.size |
| 158 | + elsif value.respond_to?(:stat) |
| 159 | + value.stat.size |
| 160 | + else |
| 161 | + value.read.bytesize.tap { value.rewind } |
| 162 | + end |
| 163 | + else |
| 164 | + value.to_s.b.bytesize |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + def content_type(object) |
| 169 | + return object.content_type if object.respond_to?(:content_type) |
| 170 | + require 'mini_mime' |
| 171 | + mime = MiniMime.lookup_by_filename(object.path) |
| 172 | + mime ? mime.content_type : 'application/octet-stream' |
| 173 | + end |
| 174 | + |
| 175 | + def file_name(object) |
| 176 | + object.respond_to?(:original_filename) ? object.original_filename : File.basename(object.path) |
| 177 | + end |
| 178 | + |
| 179 | + def replacement_table |
| 180 | + @replacement_table ||= { |
| 181 | + '"' => '%22', |
| 182 | + "\r" => '%0D', |
| 183 | + "\n" => '%0A' |
| 184 | + }.freeze |
| 185 | + end |
| 186 | + end |
| 187 | + end |
| 188 | +end |
0 commit comments