From 7868e5045e8b2e9258535fec9f5bfc57c779f2a3 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 15 Jun 2020 22:07:41 -0500 Subject: [PATCH] nicoseiga: fix regression with http redirects. 3cdf67920 changed it so that Danbooru::Http follows redirects by default. This broke some things in the Nico Seiga strategy, so disable following redirects in the Nico Seiga API client for now. Also change it so that Danbooru::Http follows redirects after a POST request (by setting `strict: false`). Nico Seiga needs this because it sends a redirect after we POST the login form. --- app/logical/danbooru/http.rb | 8 ++++++-- app/logical/nico_seiga_api_client.rb | 16 ++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/logical/danbooru/http.rb b/app/logical/danbooru/http.rb index d80c869c9..343d8d886 100644 --- a/app/logical/danbooru/http.rb +++ b/app/logical/danbooru/http.rb @@ -6,7 +6,7 @@ module Danbooru attr_writer :cache, :http class << self - delegate :get, :put, :post, :delete, :cache, :timeout, :auth, :basic_auth, :headers, to: :new + delegate :get, :put, :post, :delete, :cache, :follow, :timeout, :auth, :basic_auth, :headers, to: :new end def get(url, **options) @@ -29,6 +29,10 @@ module Danbooru dup.tap { |o| o.cache = expiry.to_i } end + def follow(*args) + dup.tap { |o| o.http = o.http.follow(*args) } + end + def timeout(*args) dup.tap { |o| o.http = o.http.timeout(*args) } end @@ -77,7 +81,7 @@ module Danbooru def http @http ||= ::HTTP. - follow(max_hops: MAX_REDIRECTS). + follow(strict: false, max_hops: MAX_REDIRECTS). timeout(DEFAULT_TIMEOUT). use(:auto_inflate). headers(Danbooru.config.http_headers). diff --git a/app/logical/nico_seiga_api_client.rb b/app/logical/nico_seiga_api_client.rb index d75aaade3..f5418ac76 100644 --- a/app/logical/nico_seiga_api_client.rb +++ b/app/logical/nico_seiga_api_client.rb @@ -2,9 +2,13 @@ class NicoSeigaApiClient extend Memoist XML_API = "https://seiga.nicovideo.jp/api" - def initialize(work_id:, type:) + attr_reader :http + + # XXX temp disable following redirects. + def initialize(work_id:, type:, http: Danbooru::Http.follow(nil)) @work_id = work_id @work_type = type + @http = http end def image_ids @@ -51,7 +55,7 @@ class NicoSeigaApiClient api_response = JSON.parse(resp)["target_image"] elsif @work_type == "manga" - resp = Danbooru::Http.cache(1.minute).get("#{XML_API}/theme/info?id=#{@work_id}") + resp = http.cache(1.minute).get("#{XML_API}/theme/info?id=#{@work_id}") return {} if resp.blank? || resp.code.to_i == 404 api_response = Hash.from_xml(resp.to_s)["response"]["theme"] end @@ -70,7 +74,7 @@ class NicoSeigaApiClient end def user_api_response(user_id) - resp = Danbooru::Http.cache(1.minute).get("#{XML_API}/user/info?id=#{user_id}") + resp = http.cache(1.minute).get("#{XML_API}/user/info?id=#{user_id}") return {} if resp.blank? || resp.code.to_i == 404 Hash.from_xml(resp.to_s)["response"]["user"] end @@ -78,11 +82,11 @@ class NicoSeigaApiClient def get(url) cookie_header = Cache.get("nicoseiga-cookie-header") || regenerate_cookie_header - resp = Danbooru::Http.headers({Cookie: cookie_header}).cache(1.minute).get(url) + resp = http.headers({Cookie: cookie_header}).cache(1.minute).get(url) if resp.headers["Location"] =~ %r{seiga\.nicovideo\.jp/login/}i cookie_header = regenerate_cookie_header - resp = Danbooru::Http.headers({Cookie: cookie_header}).cache(1.minute).get(url) + resp = http.headers({Cookie: cookie_header}).cache(1.minute).get(url) end resp @@ -93,7 +97,7 @@ class NicoSeigaApiClient mail_tel: Danbooru.config.nico_seiga_login, password: Danbooru.config.nico_seiga_password } - resp = Danbooru::Http.post("https://account.nicovideo.jp/api/v1/login", form: form) + resp = http.post("https://account.nicovideo.jp/api/v1/login", form: form) cookies = resp.cookies.map { |c| c.name + "=" + c.value } cookies << "accept_fetish_warning=2"