Compare commits

...

1 commit

Author SHA1 Message Date
931dbdb655 replace faraday with typhoeus in http_client 2024-10-08 21:01:17 -04:00
4 changed files with 28 additions and 40 deletions

View file

@ -128,6 +128,16 @@ module DiasporaFederation
# @return [String] user agent # @return [String] user agent
attr_reader :http_user_agent attr_reader :http_user_agent
# Outbound HTTP proxy
#
# @overload http_proxy
# @return [String] http proxy
# @overload http_proxy=
# @example
# config.http_proxy = AppConfig.environment.http_proxy
# @param [String] value http proxy
attr_accessor :http_proxy
# Configure the federation library # Configure the federation library
# #
# @example # @example

View file

@ -20,7 +20,8 @@ module DiasporaFederation
method: :post, method: :post,
verbose: DiasporaFederation.http_verbose, verbose: DiasporaFederation.http_verbose,
cainfo: DiasporaFederation.certificate_authorities, cainfo: DiasporaFederation.certificate_authorities,
forbid_reuse: true forbid_reuse: true,
proxy: DiasporaFederation.http_proxy
} }
end end

View file

@ -1,10 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require "faraday" require "typhoeus"
require "faraday/follow_redirects"
module DiasporaFederation module DiasporaFederation
# A wrapper for {https://github.com/lostisland/faraday Faraday} # HTTP GET wrapper
# #
# @see Discovery::Discovery # @see Discovery::Discovery
# @see Federation::Fetcher # @see Federation::Fetcher
@ -12,32 +11,21 @@ module DiasporaFederation
# Perform a GET request # Perform a GET request
# #
# @param [String] uri the URI # @param [String] uri the URI
# @return [Faraday::Response] the response # @return [Typhoeus::Response] the response
def self.get(uri) def self.get(uri)
connection.get(uri) opts = {
end followlocation: false,
timeout: DiasporaFederation.http_timeout,
# Gets the Faraday connection method: :get,
# verbose: DiasporaFederation.http_verbose,
# @return [Faraday::Connection] the response cainfo: DiasporaFederation.certificate_authorities,
def self.connection forbid_reuse: true,
create_default_connection unless @connection proxy: DiasporaFederation.http_proxy,
@connection.dup headers: {"User-Agent" => DiasporaFederation.http_user_agent}
end
private_class_method def self.create_default_connection
options = {
request: {timeout: DiasporaFederation.http_timeout},
ssl: {ca_file: DiasporaFederation.certificate_authorities}
} }
req = Typhoeus::Request.new(uri, opts)
@connection = Faraday::Connection.new(options) do |builder| req.run
builder.use Faraday::FollowRedirects::Middleware, limit: DiasporaFederation.http_redirect_limit req.response
builder.adapter Faraday.default_adapter
end
@connection.headers["User-Agent"] = DiasporaFederation.http_user_agent
end end
end end
end end

View file

@ -34,7 +34,7 @@ module DiasporaFederation
.to_return(status: 302, headers: {"Location" => "http://www.example.com"}) .to_return(status: 302, headers: {"Location" => "http://www.example.com"})
expect { HttpClient.get("http://www.example.com") } expect { HttpClient.get("http://www.example.com") }
.to raise_error Faraday::FollowRedirects::RedirectLimitReached .to_return(success: false, return_code: :too_many_redirects)
end end
it "uses the gem name as User-Agent" do it "uses the gem name as User-Agent" do
@ -44,16 +44,5 @@ module DiasporaFederation
HttpClient.get("http://www.example.com") HttpClient.get("http://www.example.com")
end end
end end
describe ".connection" do
it "returns a new connection every time" do
expect(HttpClient.connection).to be_a Faraday::Connection
end
it "returns a new connection every time" do
connection1 = HttpClient.connection
expect(HttpClient.connection).to_not be(connection1)
end
end
end end
end end