Revert to URI.parse/1 (#770)

This commit is contained in:
Jonatan Kłosko 2021-12-08 16:11:04 +01:00 committed by GitHub
parent c9b21f221d
commit fef322706f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 11 deletions

View file

@ -18,10 +18,10 @@ defmodule Livebook.ContentLoader do
""" """
@spec rewrite_url(String.t()) :: String.t() @spec rewrite_url(String.t()) :: String.t()
def rewrite_url(url) do def rewrite_url(url) do
case URI.new(url) do url
{:ok, uri} -> uri |> do_rewrite_url() |> URI.to_string() |> URI.parse()
{:error, _} -> url |> do_rewrite_url()
end |> URI.to_string()
end end
defp do_rewrite_url(%URI{host: "github.com"} = uri) do defp do_rewrite_url(%URI{host: "github.com"} = uri) do

View file

@ -366,7 +366,7 @@ defimpl Livebook.FileSystem, for: Livebook.FileSystem.S3 do
headers = opts[:headers] || [] headers = opts[:headers] || []
body = opts[:body] body = opts[:body]
%{host: host} = URI.new!(file_system.bucket_url) %{host: host} = URI.parse(file_system.bucket_url)
url = file_system.bucket_url <> path <> "?" <> URI.encode_query(query) url = file_system.bucket_url <> path <> "?" <> URI.encode_query(query)
@ -394,7 +394,7 @@ defimpl Livebook.FileSystem, for: Livebook.FileSystem.S3 do
defp region_from_uri(uri) do defp region_from_uri(uri) do
# For many services the API host is of the form *.[region].[rootdomain].com # For many services the API host is of the form *.[region].[rootdomain].com
%{host: host} = URI.new!(uri) %{host: host} = URI.parse(uri)
host |> String.split(".") |> Enum.reverse() |> Enum.at(2, "auto") host |> String.split(".") |> Enum.reverse() |> Enum.at(2, "auto")
end end

View file

@ -167,10 +167,8 @@ defmodule Livebook.Utils do
""" """
@spec valid_url?(String.t()) :: boolean() @spec valid_url?(String.t()) :: boolean()
def valid_url?(url) do def valid_url?(url) do
case URI.new(url) do uri = URI.parse(url)
{:ok, uri} -> uri.scheme != nil and uri.host != nil uri.scheme != nil and uri.host != nil
_ -> false
end
end end
@doc """ @doc """
@ -247,7 +245,7 @@ defmodule Livebook.Utils do
@spec expand_url(String.t(), String.t()) :: String.t() @spec expand_url(String.t(), String.t()) :: String.t()
def expand_url(url, relative_path) do def expand_url(url, relative_path) do
url url
|> URI.new!() |> URI.parse()
|> Map.update!(:path, fn path -> |> Map.update!(:path, fn path ->
path |> Path.dirname() |> Path.join(relative_path) |> Path.expand() path |> Path.dirname() |> Path.join(relative_path) |> Path.expand()
end) end)