Fix HTTP not handling relative reidrects correctly

This commit is contained in:
Shadowfacts 2019-11-10 15:15:54 -05:00
parent 2a5bfb22db
commit 8d790b8af0
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 12 additions and 1 deletions

View File

@ -2,7 +2,6 @@ defmodule Frenzy.HTTP do
require Logger require Logger
@redirect_codes [301, 302] @redirect_codes [301, 302]
# @spec get(url :: String.t()) :: {:ok, HTTPoison.Response.t()} | {:error, String.()}
def get(url) do def get(url) do
case HTTPoison.get(url) do case HTTPoison.get(url) do
{:ok, %HTTPoison.Response{status_code: 200} = response} -> {:ok, %HTTPoison.Response{status_code: 200} = response} ->
@ -14,6 +13,16 @@ defmodule Frenzy.HTTP do
|> Enum.find(fn {name, _value} -> name == "Location" end) |> Enum.find(fn {name, _value} -> name == "Location" end)
|> case do |> case do
{"Location", new_url} -> {"Location", new_url} ->
new_url =
case URI.parse(new_url) do
%URI{host: nil, path: path} ->
# relative path
%URI{URI.parse(url) | path: path} |> URI.to_string()
uri ->
uri
end
Logger.debug("Got 301 redirect from #{url} to #{new_url}") Logger.debug("Got 301 redirect from #{url} to #{new_url}")
get(new_url) get(new_url)

View File

@ -19,6 +19,8 @@ defmodule Frenzy.Task.FetchFavicon do
%URI{URI.parse(feed.feed_url) | path: nil, query: nil, fragment: nil} |> URI.to_string() %URI{URI.parse(feed.feed_url) | path: nil, query: nil, fragment: nil} |> URI.to_string()
end end
Logger.debug("Fetching favicon for #{site_url}")
case fetch_favicon_from_webpage(site_url) do case fetch_favicon_from_webpage(site_url) do
{:ok, favicon_data} -> {:ok, favicon_data} ->
changeset = Feed.changeset(feed, %{favicon: favicon_data}) changeset = Feed.changeset(feed, %{favicon: favicon_data})