danbooru::http: support automatically following redirects.

Replace http.rb's builtin redirect following option with our own
redirect follower. This fixes an issue with http.rb losing cookies after
following a redirect.
This commit is contained in:
evazion
2020-06-21 03:57:14 -05:00
parent 71b0bc6c0f
commit 05d7355ebb
3 changed files with 57 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
require "danbooru/http/html_adapter"
require "danbooru/http/xml_adapter"
require "danbooru/http/redirector"
require "danbooru/http/retriable"
require "danbooru/http/session"
@@ -11,12 +12,24 @@ module Danbooru
DEFAULT_TIMEOUT = 10
MAX_REDIRECTS = 5
attr_writer :cache, :max_size, :http
attr_accessor :cache, :max_size, :http
class << self
delegate :get, :head, :put, :post, :delete, :cache, :follow, :max_size, :timeout, :auth, :basic_auth, :headers, :cookies, :use, :public_only, :download_media, to: :new
end
def initialize
@http ||=
::Danbooru::Http::ApplicationClient.new
.timeout(DEFAULT_TIMEOUT)
.headers(Danbooru.config.http_headers)
.headers("Accept-Encoding" => "gzip")
.use(:auto_inflate)
.use(:retriable)
.use(redirector: { max_redirects: MAX_REDIRECTS })
.use(:session)
end
def get(url, **options)
request(:get, url, **options)
end
@@ -147,16 +160,5 @@ module Danbooru
def fake_response(status, body)
::HTTP::Response.new(status: status, version: "1.1", body: ::HTTP::Response::Body.new(body))
end
def http
@http ||=
::Danbooru::Http::ApplicationClient.new
.follow(strict: false, max_hops: MAX_REDIRECTS)
.use(:session)
.timeout(DEFAULT_TIMEOUT)
.use(:auto_inflate)
.headers(Danbooru.config.http_headers)
.headers("Accept-Encoding" => "gzip")
end
end
end