Method: Mechanize::HTTP::Agent#content_encoding_brotli

Defined in:
lib/mechanize/http/agent.rb

#content_encoding_brotli(body_io) ⇒ Object

Decodes a Brotli-encoded body_io

(Experimental, CRuby only) Although Mechanize will never request a Brotli-encoded response via accept-encoding, buggy servers may return brotli-encoded responses anyway. Let’s try to handle that case if the Brotli gem is loaded.

If you need to handle Brotli-encoded responses, install the ‘brotli’ gem and require it in your application. If the Brotli constant is defined, Mechanize will attempt to use it to inflate the response.



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/mechanize/http/agent.rb', line 516

def content_encoding_brotli(body_io)
  log.debug('deflate brotli body') if log

  unless defined?(::Brotli)
    raise Mechanize::Error, "cannot deflate brotli-encoded response. Please install and require the 'brotli' gem."
  end

  begin
    return StringIO.new(Brotli.inflate(body_io.read))
  rescue Brotli::Error
    log.error("unable to brotli-inflate response") if log
    raise Mechanize::Error, "error inflating brotli-encoded response."
  end
ensure
  body_io.close
end