Module: Puppet::Network::HTTP::Compression::Active

Defined in:
lib/puppet/network/http/compression.rb

Defined Under Namespace

Classes: ZlibAdapter

Instance Method Summary collapse

Instance Method Details

#add_accept_encoding(headers = {}) ⇒ Object



53
54
55
56
# File 'lib/puppet/network/http/compression.rb', line 53

def add_accept_encoding(headers={})
  headers['accept-encoding'] = Puppet::Network::HTTP::Compression::ACCEPT_ENCODING
  headers
end

#uncompress(response) ⇒ Object

Raises:

  • (Net::HTTPError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet/network/http/compression.rb', line 34

def uncompress(response)
  raise Net::HTTPError.new("No block passed") unless block_given?

  case response['content-encoding']
  when 'gzip','deflate'
    uncompressor = ZlibAdapter.new
  when nil, 'identity'
    uncompressor = IdentityAdapter.new
  else
    raise Net::HTTPError.new(_("Unknown content encoding - %{encoding}") % { encoding: response['content-encoding'] }, response)
  end

  begin
    yield uncompressor
  ensure
    uncompressor.close
  end
end

#uncompress_body(response) ⇒ Object

return an uncompressed body if the response has been compressed



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet/network/http/compression.rb', line 20

def uncompress_body(response)
  case response['content-encoding']
  when 'gzip'
    # ZLib::GzipReader has an associated encoding, by default Encoding.default_external
    return Zlib::GzipReader.new(StringIO.new(response.body), :encoding => Encoding::BINARY).read
  when 'deflate'
    return Zlib::Inflate.new.inflate(response.body)
  when nil, 'identity'
    return response.body
  else
    raise Net::HTTPError.new(_("Unknown content encoding - %{encoding}") % { encoding: response['content-encoding'] }, response)
  end
end