Compare commits

..

No commits in common. "outbound-proxy" and "develop" have entirely different histories.

4 changed files with 40 additions and 28 deletions

View file

@ -128,16 +128,6 @@ 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,8 +20,7 @@ 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,9 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require "typhoeus" require "faraday"
require "faraday/follow_redirects"
module DiasporaFederation module DiasporaFederation
# HTTP GET wrapper # A wrapper for {https://github.com/lostisland/faraday Faraday}
# #
# @see Discovery::Discovery # @see Discovery::Discovery
# @see Federation::Fetcher # @see Federation::Fetcher
@ -11,21 +12,32 @@ module DiasporaFederation
# Perform a GET request # Perform a GET request
# #
# @param [String] uri the URI # @param [String] uri the URI
# @return [Typhoeus::Response] the response # @return [Faraday::Response] the response
def self.get(uri) def self.get(uri)
opts = { connection.get(uri)
followlocation: false, end
timeout: DiasporaFederation.http_timeout,
method: :get, # Gets the Faraday connection
verbose: DiasporaFederation.http_verbose, #
cainfo: DiasporaFederation.certificate_authorities, # @return [Faraday::Connection] the response
forbid_reuse: true, def self.connection
proxy: DiasporaFederation.http_proxy, create_default_connection unless @connection
headers: {"User-Agent" => DiasporaFederation.http_user_agent} @connection.dup
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)
req.run @connection = Faraday::Connection.new(options) do |builder|
req.response builder.use Faraday::FollowRedirects::Middleware, limit: DiasporaFederation.http_redirect_limit
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_return(success: false, return_code: :too_many_redirects) .to raise_error Faraday::FollowRedirects::RedirectLimitReached
end end
it "uses the gem name as User-Agent" do it "uses the gem name as User-Agent" do
@ -44,5 +44,16 @@ 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