File: auto_inflate.rb

package info (click to toggle)
ruby-http 4.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704 kB
  • sloc: ruby: 5,388; makefile: 9
file content (42 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

require "set"

module HTTP
  module Features
    class AutoInflate < Feature
      SUPPORTED_ENCODING = Set.new(%w[deflate gzip x-gzip]).freeze
      private_constant :SUPPORTED_ENCODING

      def wrap_response(response)
        return response unless supported_encoding?(response)

        options = {
          :status => response.status,
          :version => response.version,
          :headers => response.headers,
          :proxy_headers => response.proxy_headers,
          :connection => response.connection,
          :body => stream_for(response.connection)
        }

        options[:uri] = response.uri if response.uri

        Response.new(options)
      end

      def stream_for(connection)
        Response::Body.new(Response::Inflater.new(connection))
      end

      private

      def supported_encoding?(response)
        content_encoding = response.headers.get(Headers::CONTENT_ENCODING).first
        content_encoding && SUPPORTED_ENCODING.include?(content_encoding)
      end

      HTTP::Options.register_feature(:auto_inflate, self)
    end
  end
end