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

Filter client capabilities that can't be met by server #174

Merged
Merged
Show file tree
Hide file tree
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
19 changes: 15 additions & 4 deletions lib/myxql/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ defmodule MyXQL.Protocol do
decode_connect_err_packet_body(rest)
end

defp filter_capabilities(allowed_flags, requested_flags) do
requested_capabilities = list_capability_flags(requested_flags)

Enum.reduce(requested_capabilities, requested_flags, fn name, acc ->
if has_capability_flag?(allowed_flags, name) do
acc
else
remove_capability_flag(acc, name)
end
end)
end

defp ensure_capabilities(capability_flags, names) do
Enum.reduce_while(names, :ok, fn name, _acc ->
if has_capability_flag?(capability_flags, name) do
Expand Down Expand Up @@ -172,11 +184,10 @@ defmodule MyXQL.Protocol do
if config.ssl? && !has_capability_flag?(server_capability_flags, :client_ssl) do
{:error, :server_does_not_support_ssl}
else
client_capabilities = list_capability_flags(client_capability_flags)
client_capability_flags =
filter_capabilities(server_capability_flags, client_capability_flags)

with :ok <- ensure_capabilities(server_capability_flags, client_capabilities) do
{:ok, client_capability_flags}
end
{:ok, client_capability_flags}
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/myxql/protocol/flags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ defmodule MyXQL.Protocol.Flags do

def has_capability_flag?(flags, name), do: has_flag?(@capability_flags, flags, name)

def remove_capability_flag(flags, name), do: remove_flag(@capability_flags, flags, name)

def put_capability_flags(flags \\ 0, names), do: put_flags(@capability_flags, flags, names)

def list_capability_flags(flags), do: list_flags(@capability_flags, flags)
Expand Down Expand Up @@ -93,6 +95,11 @@ defmodule MyXQL.Protocol.Flags do
Enum.reduce(names, flags, &(&2 ||| Keyword.fetch!(all_flags, &1)))
end

defp remove_flag(all_flags, flags, name) do
value = Keyword.fetch!(all_flags, name)
flags &&& ~~~value
end

def list_flags(all_flags, flags) do
all_flags
|> Keyword.keys()
Expand Down
Loading