Skip to content

Commit

Permalink
gracefully handling gzip decompression failure
Browse files Browse the repository at this point in the history
  • Loading branch information
trusche committed Apr 26, 2018
1 parent 29f4b58 commit 6adcfe9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/httplog/http_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ def log_body(body, encoding = nil, content_type = nil)
end

if encoding =~ /gzip/ && body && !body.empty?
sio = StringIO.new(body.to_s)
gz = Zlib::GzipReader.new(sio)
body = gz.read
begin
sio = StringIO.new(body.to_s)
gz = Zlib::GzipReader.new(sio)
body = gz.read
rescue Zlib::GzipFile::Error => e
log("(gzip decompression failed)")
end
end

data = utf_encoded(body.to_s, content_type)
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/http_client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'spec_helper'

describe HTTPClient do
let(:client) { HTTPClient.new }

it 'works with transparent_gzip_decompression' do
client.transparent_gzip_decompression = true
expect { client.get("http://localhost:9292/index.html.gz") }.to_not(raise_error)
expect(log).to include(HttpLog::LOG_PREFIX + 'Status: 200')
expect(log).to include(HttpLog::LOG_PREFIX + 'Data:')
expect(log).to include(HttpLog::LOG_PREFIX + "Response:\n<html>")
end
end
2 changes: 1 addition & 1 deletion spec/http_log_spec.rb → spec/lib/http_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
expect(res).to be_a adapter.response if adapter.respond_to? :response
end

context 'with gzipped response body' do
context 'with gzip encoding' do
let(:path) { '/index.html.gz' }
let(:data) { nil }

Expand Down
8 changes: 8 additions & 0 deletions spec/support/not_gzipped.html.gz
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Test Page</title>
</head>
<body>
<h1>This is the test page.</h1>
</body>
</html>

0 comments on commit 6adcfe9

Please sign in to comment.