diff --git a/Gemfile.lock b/Gemfile.lock index a035e83..f22f8f8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -135,6 +135,8 @@ GEM eventmachine (~> 1.0, >= 1.0.4) rack (>= 1, < 3) thor (1.2.1) + typhoeus (1.4.0) + ethon (>= 0.9.0) unf (0.1.4) unf_ext unf_ext (0.0.8.1) @@ -159,6 +161,7 @@ DEPENDENCIES rspec (~> 3.7) simplecov (~> 0.15) thin (~> 1.7) + typhoeus (~> 1.4) BUNDLED WITH 2.2.32 diff --git a/httplog.gemspec b/httplog.gemspec index 819e853..b40e6bf 100644 --- a/httplog.gemspec +++ b/httplog.gemspec @@ -32,6 +32,7 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'httparty', ['~> 0.16'] gem.add_development_dependency 'httpclient', ['~> 2.8'] gem.add_development_dependency 'rest-client', ['~> 2.0'] + gem.add_development_dependency 'typhoeus', ['~> 1.4'] gem.add_development_dependency 'listen', ['~> 3.0'] gem.add_development_dependency 'patron', ['~> 0.12'] gem.add_development_dependency 'rake', ['~> 13.0'] diff --git a/lib/httplog/adapters/ethon.rb b/lib/httplog/adapters/ethon.rb index 4415e89..fada010 100644 --- a/lib/httplog/adapters/ethon.rb +++ b/lib/httplog/adapters/ethon.rb @@ -18,16 +18,9 @@ module Operations def perform return orig_perform unless HttpLog.url_approved?(url) - bm = Benchmark.realtime { orig_perform } - - # Not sure where the actual status code is stored - so let's - # extract it from the response header. - encoding = response_headers.scan(/Content-Encoding: (\S+)/).flatten.first - content_type = response_headers.scan(/Content-Type: (\S+(; charset=\S+)?)/).flatten.first + httplog_add_callback - # Hard to believe that Ethon wouldn't parse out the headers into - # an array; probably overlooked it. Anyway, let's do it ourselves: - headers = response_headers.split(/\r?\n/).drop(1) + bm = Benchmark.realtime { orig_perform } HttpLog.call( method: @http_log[:method], @@ -35,15 +28,37 @@ def perform request_body: @http_log[:body], request_headers: @http_log[:headers], response_code: @return_code, - response_body: response_body, - response_headers: headers.map { |header| header.split(/:\s/) }.to_h, + response_body: @http_log[:response_body], + response_headers: @http_log[:response_headers].map { |header| header.split(/:\s/) }.to_h, benchmark: bm, - encoding: encoding, - content_type: content_type, - mask_body: HttpLog.masked_body_url?(url) + encoding: @http_log[:encoding], + content_type: @http_log[:content_type], + mask_body: HttpLog.masked_body_url?(@http_log[:url]) ) return_code end + + def httplog_add_callback + # Hack to perform this callback before the cleanup + @on_complete ||= [] + @on_complete.unshift -> (*) do + # Not sure where the actual status code is stored - so let's + # extract it from the response header. + encoding = response_headers.scan(/Content-Encoding: (\S+)/).flatten.first + content_type = response_headers.scan(/Content-Type: (\S+(; charset=\S+)?)/).flatten.first + + # Hard to believe that Ethon wouldn't parse out the headers into + # an array; probably overlooked it. Anyway, let's do it ourselves: + headers = response_headers.split(/\r?\n/).drop(1) + + @http_log.merge!( + encoding: encoding, + content_type: content_type, + response_headers: headers, + response_body: response_body + ) + end + end end end end diff --git a/spec/adapters/typhoeus_adapter.rb b/spec/adapters/typhoeus_adapter.rb index 0f42f0a..d8f76e2 100644 --- a/spec/adapters/typhoeus_adapter.rb +++ b/spec/adapters/typhoeus_adapter.rb @@ -3,19 +3,19 @@ require 'excon' class TyphoeusAdapter < HTTPBaseAdapter def send_get_request - Typhoeus.get(parse_uri(true).to_s, headers: @headers) + Typhoeus.get(parse_uri(true).to_s, headers: @headers).body end def send_head_request - Typhoeus.head(parse_uri.to_s, headers: @headers) + Typhoeus.head(parse_uri.to_s, headers: @headers).body end def send_post_request - Typhoeus.post(parse_uri.to_s, body: @data, headers: @headers) + Typhoeus.post(parse_uri.to_s, body: @data, headers: @headers).body end def send_post_form_request - Typhoeus.post(parse_uri.to_s, body: @params, headers: @headers) + Typhoeus.post(parse_uri.to_s, body: @params, headers: @headers).body end def send_multipart_post_request diff --git a/spec/lib/http_log_spec.rb b/spec/lib/http_log_spec.rb index 2bab144..87ef99c 100644 --- a/spec/lib/http_log_spec.rb +++ b/spec/lib/http_log_spec.rb @@ -77,7 +77,8 @@ def configure EthonAdapter, PatronAdapter, HTTPAdapter, - RestClientAdapter + RestClientAdapter, + TyphoeusAdapter ].freeze ADAPTERS.each do |adapter_class| diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b8d38d6..3f11deb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,7 +5,7 @@ require 'rspec' require 'httpclient' require 'excon' -# require 'typhoeus' +require 'typhoeus' require 'ethon' require 'patron' require 'restclient' @@ -25,7 +25,7 @@ # Start a local rack server to serve up test pages. Thread.new do - Rack::Handler::Thin.run Httplog::Test::Server.new, Port: 9292 + Rack::Handler::Thin.run Httplog::Test::Server.new, Host: '127.0.0.1', Port: 9292 end # Wait for the server to be booted.