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 2 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
20 changes: 18 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 @@ -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
kkerezsi marked this conversation as resolved.
Show resolved Hide resolved

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()}
Expand Down