From 215df4905017ebc2d5c6b9050af40ccc62c18ee0 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 12 Nov 2022 02:14:47 -0600 Subject: [PATCH] config: rework http proxy options. Replace the http_proxy_host, http_proxy_port, http_proxy_username, http_proxy_password options with a single `Danbooru.config.http_proxy` option. --- app/logical/danbooru/http.rb | 7 ++++--- app/logical/danbooru/url.rb | 7 ++++++- config/danbooru_default_config.rb | 13 +++---------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/app/logical/danbooru/http.rb b/app/logical/danbooru/http.rb index 8aa81c5a6..a089aca8d 100644 --- a/app/logical/danbooru/http.rb +++ b/app/logical/danbooru/http.rb @@ -131,11 +131,12 @@ module Danbooru use(cache: { expires_in: expires_in }) end - def proxy(host: Danbooru.config.http_proxy_host, port: Danbooru.config.http_proxy_port.to_i, username: Danbooru.config.http_proxy_username, password: Danbooru.config.http_proxy_password) - return self if host.blank? + def proxy(url: Danbooru.config.http_proxy) + return self if url.blank? + parsed_url = Danbooru::URL.parse!(url) dup.tap do |o| - o.http = o.http.via(host, port, username, password) + o.http = o.http.via(parsed_url.host, parsed_url.port, parsed_url.http_user, parsed_url.password) end end diff --git a/app/logical/danbooru/url.rb b/app/logical/danbooru/url.rb index d421c7037..d719598e1 100644 --- a/app/logical/danbooru/url.rb +++ b/app/logical/danbooru/url.rb @@ -34,7 +34,7 @@ module Danbooru # @return [Addressable:URI] The parsed and normalized URL. attr_reader :url - delegate :domain, :host, :site, :path, :query, to: :url + delegate :domain, :host, :port, :site, :path, :query, :password, to: :url # Parse a string into a URL, or raise an exception if the string is not a valid HTTP or HTTPS URL. # @@ -111,6 +111,11 @@ module Danbooru parsed_domain.trd end + # @return [String, nil] The username in a `http://username:password@example.com` URL. + def http_user + url.user + end + # @return [PublicSuffix::Domain] def parsed_domain @parsed_domain ||= PublicSuffix.parse(host) diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index f73b760b5..326dd9844 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -578,16 +578,9 @@ module Danbooru "https://twitter.com/#{Danbooru.config.twitter_username}" end - def http_proxy_host - end - - def http_proxy_port - end - - def http_proxy_username - end - - def http_proxy_password + # The proxy to use for outgoing HTTP requests. + def http_proxy + # "http://username:password@proxy.example.com:1080" end # The URL for the Reportbooru server (https://github.com/evazion/reportbooru).