Sha256: 46f1f6da252c7cf4a139b35cf81741bd32883b05659de5fd717a76ce1bac71d3

Contents?: true

Size: 964 Bytes

Versions: 16

Compression:

Stored size: 964 Bytes

Contents

require 'faraday_middleware/response_middleware'

module FaradayMiddleware
  # Public: Parse a Transfer-Encoding: Chunked response to just the original data
  class Chunked < FaradayMiddleware::ResponseMiddleware
    TRANSFER_ENCODING = 'transfer-encoding'.freeze

    define_parser do |raw_body|
      decoded_body = []
      until raw_body.empty?
        chunk_len, raw_body = raw_body.split("\r\n", 2)
        chunk_len = chunk_len.split(';',2).first.hex
        break if chunk_len == 0
        decoded_body << raw_body[0, chunk_len]
        # The 2 is to strip the extra CRLF at the end of the chunk
        raw_body = raw_body[chunk_len + 2, raw_body.length - chunk_len - 2]
      end
      decoded_body.join('')
    end

    def parse_response?(env)
      super and chunked_encoding?(env[:response_headers])
    end

    def chunked_encoding?(headers)
      encoding = headers[TRANSFER_ENCODING] and encoding.split(',').include?('chunked')
    end
  end
end

Version data entries

16 entries across 16 versions & 4 rubygems

Version Path
faraday_middleware-0.14.0 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.13.1 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.13.0 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.12.2 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.12.0 lib/faraday_middleware/response/chunked.rb
faraday_middleware_safeyaml-0.12.pre.safeyaml lib/faraday_middleware/response/chunked.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/faraday_middleware-0.11.0.1/lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.11.0.1 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.10.1 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.10.0 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.9.2 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.9.1 lib/faraday_middleware/response/chunked.rb
agiley-faraday_middleware-0.9.1 lib/faraday_middleware/response/chunked.rb
agiley-faraday_middleware-0.9.0 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.9.0 lib/faraday_middleware/response/chunked.rb
faraday_middleware-0.8.8 lib/faraday_middleware/response/chunked.rb