Skip to content

Commit

Permalink
typhoeus enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nulldef authored and trusche committed Sep 17, 2022
1 parent bc392a1 commit 1be99c9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -159,6 +161,7 @@ DEPENDENCIES
rspec (~> 3.7)
simplecov (~> 0.15)
thin (~> 1.7)
typhoeus (~> 1.4)

BUNDLED WITH
2.2.32
1 change: 1 addition & 0 deletions httplog.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
43 changes: 29 additions & 14 deletions lib/httplog/adapters/ethon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,47 @@ 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],
url: @http_log[:url],
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
Expand Down
8 changes: 4 additions & 4 deletions spec/adapters/typhoeus_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/http_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def configure
EthonAdapter,
PatronAdapter,
HTTPAdapter,
RestClientAdapter
RestClientAdapter,
TyphoeusAdapter
].freeze

ADAPTERS.each do |adapter_class|
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'rspec'
require 'httpclient'
require 'excon'
# require 'typhoeus'
require 'typhoeus'
require 'ethon'
require 'patron'
require 'restclient'
Expand All @@ -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.
Expand Down

0 comments on commit 1be99c9

Please sign in to comment.