From 306287115f5f4bfd495a5ef662e820f39b4646e6 Mon Sep 17 00:00:00 2001 From: "NI\\akerezsi" Date: Tue, 10 Dec 2024 15:30:25 +0200 Subject: [PATCH 1/4] fix url parser to avid failing on missing txt record --- lib/mongo/url_parser.ex | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/mongo/url_parser.ex b/lib/mongo/url_parser.ex index 05454a3..4b7ca58 100644 --- a/lib/mongo/url_parser.ex +++ b/lib/mongo/url_parser.ex @@ -155,8 +155,8 @@ defmodule Mongo.UrlParser do {:ok, {_, _, _, _, _, srv_record}} <- :inet_res.getbyname(~c"_mongodb._tcp." ++ url_char, :srv), {:ok, host} <- get_host_srv(srv_record), - {:ok, {_, _, _, _, _, txt_record}} <- :inet_res.getbyname(url_char, :txt), - txt <- "#{orig_options}&#{txt_record}&ssl=true" do + {:ok, txt_record} <- resolve_txt_record(url_char), + txt <- build_params(orig_options, txt_record) do frags |> Map.put("seeds", host) |> Map.put("options", txt) @@ -165,6 +165,22 @@ defmodule Mongo.UrlParser do end end + defp build_params(orig_options, txt_record) do + [orig_options, txt_record, "ssl=true"] + |> Enum.filter(&(&1 != nil)) + |> Enum.join("&") + end + + defp resolve_txt_record(url_char) do + case :inet_res.lookup(url_char, :in, :txt) do + [txt_record | _] -> + {:ok, txt_record} + + _other -> + {:ok, nil} + end + end + defp resolve_srv_url(frags), do: frags @spec get_host_srv([{term, term, term, term}]) :: {:ok, String.t()} From 2450eeca949306673e65e37a2dbcbbf781827302 Mon Sep 17 00:00:00 2001 From: "NI\\akerezsi" Date: Tue, 10 Dec 2024 16:07:46 +0200 Subject: [PATCH 2/4] Fix lookup value parsing --- lib/mongo/url_parser.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo/url_parser.ex b/lib/mongo/url_parser.ex index 4b7ca58..fed4c35 100644 --- a/lib/mongo/url_parser.ex +++ b/lib/mongo/url_parser.ex @@ -173,7 +173,7 @@ defmodule Mongo.UrlParser do defp resolve_txt_record(url_char) do case :inet_res.lookup(url_char, :in, :txt) do - [txt_record | _] -> + [[txt_record] | _] -> {:ok, txt_record} _other -> From 931470742a85714c4ffd58558fd39c2f353bd8fa Mon Sep 17 00:00:00 2001 From: "NI\\akerezsi" Date: Wed, 11 Dec 2024 08:04:51 +0200 Subject: [PATCH 3/4] Fix warning --- lib/mongo/url_parser.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mongo/url_parser.ex b/lib/mongo/url_parser.ex index fed4c35..fc446b0 100644 --- a/lib/mongo/url_parser.ex +++ b/lib/mongo/url_parser.ex @@ -165,6 +165,8 @@ defmodule Mongo.UrlParser do end end + defp resolve_srv_url(frags), do: frags + defp build_params(orig_options, txt_record) do [orig_options, txt_record, "ssl=true"] |> Enum.filter(&(&1 != nil)) @@ -181,8 +183,6 @@ defmodule Mongo.UrlParser do end end - defp resolve_srv_url(frags), do: frags - @spec get_host_srv([{term, term, term, term}]) :: {:ok, String.t()} defp get_host_srv(srv) when is_list(srv) do hosts = Enum.map_join(srv, ",", fn {_, _, port, host} -> "#{host}:#{port}" end) From fa4b0b1c76ebb3d81c18facdc16044d5ee5478c5 Mon Sep 17 00:00:00 2001 From: "NI\\akerezsi" Date: Wed, 11 Dec 2024 08:16:36 +0200 Subject: [PATCH 4/4] fix comments --- lib/mongo/url_parser.ex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/mongo/url_parser.ex b/lib/mongo/url_parser.ex index fc446b0..661469f 100644 --- a/lib/mongo/url_parser.ex +++ b/lib/mongo/url_parser.ex @@ -167,10 +167,12 @@ defmodule Mongo.UrlParser do defp resolve_srv_url(frags), do: frags + defp build_params(orig_options, nil) do + "#{orig_options}&ssl=true" + end + defp build_params(orig_options, txt_record) do - [orig_options, txt_record, "ssl=true"] - |> Enum.filter(&(&1 != nil)) - |> Enum.join("&") + "#{orig_options}&#{txt_record}&ssl=true" end defp resolve_txt_record(url_char) do