Skip to content

Commit

Permalink
Add support for redirect with cookie authentication
Browse files Browse the repository at this point in the history
In relation to feedreader/pluto#39, the request cookie gets saved for each redirect. This should support cookie based authentication.
  • Loading branch information
pacbard committed Aug 5, 2021
1 parent 11e142f commit 5143a1a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fetcher/lib/fetcher/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def get_response( src )

redirect_limit = 6
response = nil
cookie = nil

until false
raise ArgumentError, 'HTTP redirect too deep' if redirect_limit == 0
Expand All @@ -164,6 +165,7 @@ def get_response( src )
logger.debug "GET #{uri.request_uri} uri=#{uri}, redirect_limit=#{redirect_limit}"

headers = { 'User-Agent' => "fetcher gem v#{VERSION}" }
headers['Cookie'] = cookie unless cookie.nil?

if use_cache?
## check for existing cache entry in cache store (lookup by uri)
Expand Down Expand Up @@ -214,6 +216,7 @@ def get_response( src )
newuri = uri + response.header['location']
end
uri = newuri
cookie = response['Set-Cookie']
else
puts "*** error - fetch HTTP - #{response.code} #{response.message}"
break # will return response
Expand Down
12 changes: 12 additions & 0 deletions fetcher/test/test_get.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ def test_get_not_found
assert_equal '404', res.code
end

def test_get_cookie_redirect
url = 'https://link.springer.com/search.rss?search-within=Journal&facet-journal-id=10827'
worker = Fetcher::Worker.new
res = worker.get( url )
pp res

assert_equal '200', res.code # note: returned code is a string e.g. '200' not 200
assert_equal 'OK', res.message
assert_equal 'text/xml', res.content_type

end

end # class TestGet

0 comments on commit 5143a1a

Please sign in to comment.