Class: HTTPX::Transcoder::Multipart::Decoder

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/httpx/transcoder/multipart/decoder.rb

Constant Summary collapse

CRLF =
"\r\n"
BOUNDARY_RE =
/;\s*boundary=([^;]+)/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
WINDOW_SIZE =
2 << 14

Constants included from Utils

Utils::FILENAME_EXTENSION_REGEX, Utils::FILENAME_REGEX, Utils::TOKEN, Utils::URIParser, Utils::VALUE

Instance Method Summary collapse

Methods included from Utils

elapsed_time, get_filename, now, parse_retry_after, to_uri

Constructor Details

#initialize(response) ⇒ Decoder

Returns a new instance of Decoder.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/httpx/transcoder/multipart/decoder.rb', line 30

def initialize(response)
  @boundary = begin
    m = response.headers["content-type"].to_s[BOUNDARY_RE, 1]
    raise Error, "no boundary declared in content-type header" unless m

    m.strip
  end
  @buffer = "".b
  @parts = {}
  @intermediate_boundary = "--#{@boundary}"
  @state = :idle
end

Instance Method Details

#call(response) ⇒ Object

Raises:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/httpx/transcoder/multipart/decoder.rb', line 43

def call(response, *)
  response.body.each do |chunk|
    @buffer << chunk

    parse
  end

  raise Error, "invalid or unsupported multipart format" unless @buffer.empty?

  @parts
end