Class: HTTP::Session::Features::AutoInflate

Inherits:
Feature
  • Object
show all
Defined in:
lib/http/session/features/auto_inflate.rb

Instance Method Summary collapse

Constructor Details

#initialize(br: false) ⇒ AutoInflate

Returns a new instance of AutoInflate.



4
5
6
7
8
9
10
# File 'lib/http/session/features/auto_inflate.rb', line 4

def initialize(br: false)
  load_dependencies if br

  @supported_encoding = Set.new(%w[deflate gzip x-gzip])
  @supported_encoding.add("br") if br
  @supported_encoding.freeze
end

Instance Method Details

#wrap_response(response) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/http/session/features/auto_inflate.rb', line 12

def wrap_response(response)
  content_encoding = response.headers.get(HTTP::Headers::CONTENT_ENCODING).first
  return response unless content_encoding && @supported_encoding.include?(content_encoding)

  content =
    case content_encoding
    when "br" then brotli_inflate(response.body)
    else inflate(response.body)
    end
  response.headers.delete(HTTP::Headers::CONTENT_ENCODING)
  response.headers[HTTP::Headers::CONTENT_LENGTH] = content.length

  options = {
    status: response.status,
    version: response.version,
    headers: response.headers,
    proxy_headers: response.proxy_headers,
    body: HTTP::Session::Response::StringBody.new(content),
    request: response.request
  }
  HTTP::Response.new(options)
end