|
| 1 | +#-- |
| 2 | +# Copyright (c) 2007-2013 Nick Sieger. |
| 3 | +# See the file README.txt included with the distribution for |
| 4 | +# software license details. |
| 5 | +#++ |
| 6 | + |
| 7 | +module Datadog |
| 8 | + module Vendor |
| 9 | + module Multipart |
| 10 | + module Post |
| 11 | + module Parts |
| 12 | + module Part |
| 13 | + def self.new(boundary, name, value, headers = {}) |
| 14 | + headers ||= {} # avoid nil values |
| 15 | + if file?(value) |
| 16 | + FilePart.new(boundary, name, value, headers) |
| 17 | + else |
| 18 | + ParamPart.new(boundary, name, value, headers) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + def self.file?(value) |
| 23 | + value.respond_to?(:content_type) && value.respond_to?(:original_filename) |
| 24 | + end |
| 25 | + |
| 26 | + def length |
| 27 | + @part.length |
| 28 | + end |
| 29 | + |
| 30 | + def to_io |
| 31 | + @io |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + # Represents a parametric part to be filled with given value. |
| 36 | + class ParamPart |
| 37 | + include Part |
| 38 | + |
| 39 | + # @param boundary [String] |
| 40 | + # @param name [#to_s] |
| 41 | + # @param value [String] |
| 42 | + # @param headers [Hash] Content-Type and Content-ID are used, if present. |
| 43 | + def initialize(boundary, name, value, headers = {}) |
| 44 | + @part = build_part(boundary, name, value, headers) |
| 45 | + @io = StringIO.new(@part) |
| 46 | + end |
| 47 | + |
| 48 | + def length |
| 49 | + @part.bytesize |
| 50 | + end |
| 51 | + |
| 52 | + # @param boundary [String] |
| 53 | + # @param name [#to_s] |
| 54 | + # @param value [String] |
| 55 | + # @param headers [Hash] Content-Type is used, if present. |
| 56 | + def build_part(boundary, name, value, headers = {}) |
| 57 | + part = '' |
| 58 | + part << "--#{boundary}\r\n" |
| 59 | + part << "Content-ID: #{headers["Content-ID"]}\r\n" if headers["Content-ID"] |
| 60 | + part << "Content-Disposition: form-data; name=\"#{name.to_s}\"\r\n" |
| 61 | + part << "Content-Type: #{headers["Content-Type"]}\r\n" if headers["Content-Type"] |
| 62 | + part << "\r\n" |
| 63 | + part << "#{value}\r\n" |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + # Represents a part to be filled from file IO. |
| 68 | + class FilePart |
| 69 | + include Part |
| 70 | + |
| 71 | + attr_reader :length |
| 72 | + |
| 73 | + # @param boundary [String] |
| 74 | + # @param name [#to_s] |
| 75 | + # @param io [IO] |
| 76 | + # @param headers [Hash] |
| 77 | + def initialize(boundary, name, io, headers = {}) |
| 78 | + file_length = io.respond_to?(:length) ? io.length : File.size(io.local_path) |
| 79 | + @head = build_head(boundary, name, io.original_filename, io.content_type, file_length, |
| 80 | + io.respond_to?(:opts) ? io.opts.merge(headers) : headers) |
| 81 | + @foot = "\r\n" |
| 82 | + @length = @head.bytesize + file_length + @foot.length |
| 83 | + @io = CompositeReadIO.new(StringIO.new(@head), io, StringIO.new(@foot)) |
| 84 | + end |
| 85 | + |
| 86 | + # @param boundary [String] |
| 87 | + # @param name [#to_s] |
| 88 | + # @param filename [String] |
| 89 | + # @param type [String] |
| 90 | + # @param content_len [Integer] |
| 91 | + # @param opts [Hash] |
| 92 | + def build_head(boundary, name, filename, type, content_len, opts = {}) |
| 93 | + opts = opts.clone |
| 94 | + |
| 95 | + trans_encoding = opts.delete("Content-Transfer-Encoding") || "binary" |
| 96 | + content_disposition = opts.delete("Content-Disposition") || "form-data" |
| 97 | + |
| 98 | + part = '' |
| 99 | + part << "--#{boundary}\r\n" |
| 100 | + part << "Content-Disposition: #{content_disposition}; name=\"#{name.to_s}\"; filename=\"#{filename}\"\r\n" |
| 101 | + part << "Content-Length: #{content_len}\r\n" |
| 102 | + if content_id = opts.delete("Content-ID") |
| 103 | + part << "Content-ID: #{content_id}\r\n" |
| 104 | + end |
| 105 | + |
| 106 | + if opts["Content-Type"] != nil |
| 107 | + part << "Content-Type: " + opts["Content-Type"] + "\r\n" |
| 108 | + else |
| 109 | + part << "Content-Type: #{type}\r\n" |
| 110 | + end |
| 111 | + |
| 112 | + part << "Content-Transfer-Encoding: #{trans_encoding}\r\n" |
| 113 | + |
| 114 | + opts.each do |k, v| |
| 115 | + part << "#{k}: #{v}\r\n" |
| 116 | + end |
| 117 | + |
| 118 | + part << "\r\n" |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + # Represents the epilogue or closing boundary. |
| 123 | + class EpiloguePart |
| 124 | + include Part |
| 125 | + |
| 126 | + def initialize(boundary) |
| 127 | + @part = "--#{boundary}--\r\n" |
| 128 | + @io = StringIO.new(@part) |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | +end |
0 commit comments