Module: HTTPX::Plugins::Multipart

Defined in:
lib/httpx/plugins/multipart.rb,
lib/httpx/plugins/multipart/part.rb,
lib/httpx/plugins/multipart/decoder.rb,
lib/httpx/plugins/multipart/encoder.rb,
lib/httpx/plugins/multipart/mime_type_detector.rb

Overview

This plugin adds support for passing ‘http-form_data` objects (like file objects) as “multipart/form-data”;

HTTPX.post(URL, form: form: { image: HTTP::FormData::File.new("path/to/file")})

gitlab.com/honeyryderchuck/httpx/wikis/Multipart-Uploads

Defined Under Namespace

Modules: FormTranscoder, MimeTypeDetector, Part Classes: Decoder, Encoder, FilePart

Constant Summary collapse

MULTIPART_VALUE_COND =
lambda do |value|
  value.respond_to?(:read) ||
    (value.respond_to?(:to_hash) &&
      value.key?(:body) &&
      (value.key?(:filename) || value.key?(:content_type)))
end
CRLF =
"\r\n"
TOKEN =
%r{[^\s()<>,;:\\"/\[\]?=]+}.freeze
VALUE =
/"(?:\\"|[^"])*"|#{TOKEN}/.freeze
CONDISP =
/Content-Disposition:\s*#{TOKEN}\s*/i.freeze
BROKEN_QUOTED =
/^#{CONDISP}.*;\s*filename="(.*?)"(?:\s*$|\s*;\s*#{TOKEN}=)/i.freeze
BROKEN_UNQUOTED =
/^#{CONDISP}.*;\s*filename=(#{TOKEN})/i.freeze
MULTIPART_CONTENT_TYPE =
/Content-Type: (.*)#{CRLF}/ni.freeze
MULTIPART_CONTENT_DISPOSITION =
/Content-Disposition:.*;\s*name=(#{VALUE})/ni.freeze
MULTIPART_CONTENT_ID =
/Content-ID:\s*([^#{CRLF}]*)/ni.freeze
ATTRIBUTE_CHAR =

Updated definitions from RFC 2231

%r{[^ \t\v\n\r)(><@,;:\\"/\[\]?='*%]}.freeze
ATTRIBUTE =
/#{ATTRIBUTE_CHAR}+/.freeze
SECTION =
/\*[0-9]+/.freeze
REGULAR_PARAMETER_NAME =
/#{ATTRIBUTE}#{SECTION}?/.freeze
REGULAR_PARAMETER =
/(#{REGULAR_PARAMETER_NAME})=(#{VALUE})/.freeze
EXTENDED_OTHER_NAME =
/#{ATTRIBUTE}\*[1-9][0-9]*\*/.freeze
EXTENDED_OTHER_VALUE =
/%[0-9a-fA-F]{2}|#{ATTRIBUTE_CHAR}/.freeze
EXTENDED_OTHER_PARAMETER =
/(#{EXTENDED_OTHER_NAME})=(#{EXTENDED_OTHER_VALUE}*)/.freeze
EXTENDED_INITIAL_NAME =
/#{ATTRIBUTE}(?:\*0)?\*/.freeze
EXTENDED_INITIAL_VALUE =
/[a-zA-Z0-9\-]*'[a-zA-Z0-9\-]*'#{EXTENDED_OTHER_VALUE}*/.freeze
EXTENDED_INITIAL_PARAMETER =
/(#{EXTENDED_INITIAL_NAME})=(#{EXTENDED_INITIAL_VALUE})/.freeze
EXTENDED_PARAMETER =
/#{EXTENDED_INITIAL_PARAMETER}|#{EXTENDED_OTHER_PARAMETER}/.freeze
DISPPARM =
/;\s*(?:#{REGULAR_PARAMETER}|#{EXTENDED_PARAMETER})\s*/.freeze
RFC2183 =
/^#{CONDISP}(#{DISPPARM})+$/i.freeze

Class Method Summary collapse

Class Method Details

.configureObject



44
45
46
# File 'lib/httpx/plugins/multipart.rb', line 44

def configure(*)
  Transcoder.register("form", FormTranscoder)
end

.load_dependenciesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/httpx/plugins/multipart.rb', line 25

def load_dependencies(*)
  # :nocov:
  begin
    unless defined?(HTTP::FormData)
      # in order not to break legacy code, we'll keep loading http/form_data for them.
      require "http/form_data"
      warn "httpx: http/form_data is no longer a requirement to use HTTPX :multipart plugin. See migration instructions under" \
           "https://honeyryderchuck.gitlab.io/httpx/wiki/Multipart-Uploads.html#notes. \n\n" \
           "If you'd like to stop seeing this message, require 'http/form_data' yourself."
    end
  rescue LoadError
  end
  # :nocov:
  require "httpx/plugins/multipart/encoder"
  require "httpx/plugins/multipart/decoder"
  require "httpx/plugins/multipart/part"
  require "httpx/plugins/multipart/mime_type_detector"
end

.normalize_keys(key, value, &block) ⇒ Object



21
22
23
# File 'lib/httpx/plugins/multipart.rb', line 21

def normalize_keys(key, value, &block)
  Transcoder.normalize_keys(key, value, MULTIPART_VALUE_COND, &block)
end