Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(master): ensure url_parser does not fail on missing txt record #261

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/mongo/url_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -167,6 +167,24 @@ 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"
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

@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)
Expand Down
Loading