Skip to content

Commit

Permalink
fix(master): ensure url_parser does not fail on missing txt record (#261
Browse files Browse the repository at this point in the history
)

* fix url parser to avid failing on missing txt record

* Fix lookup value parsing

* Fix warning

* fix comments

---------

Co-authored-by: NI\akerezsi <[email protected]>
  • Loading branch information
kkerezsi and alexkerezsini authored Dec 11, 2024
1 parent c60d7d1 commit e4a3102
Showing 1 changed file with 20 additions and 2 deletions.
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

0 comments on commit e4a3102

Please sign in to comment.