Browse Source

Override mode, packet and active TCP options (#148)

A user could provide TCP options that would make a connection unusable,
namely `list` or `{mode, list}`, `packet` with a value other than `0` or
`raw`, and `active` with a value other than `false`.

The changes in this commit override the aforementioned options with the
correct values, and makes `true` the default for the `nodelay` option
but allows a user to set it to something else if wanted.
Jan Uhlig 5 years ago
parent
commit
6f033d6947
1 changed files with 19 additions and 2 deletions
  1. 19 2
      src/mysql_conn.erl

+ 19 - 2
src/mysql_conn.erl

@@ -149,8 +149,7 @@ connect(#state{connect_timeout = ConnectTimeout} = State) ->
 
 connect_socket(#state{tcp_opts = TcpOpts, host = Host, port = Port} = State) ->
     %% Connect socket
-    SockOpts = [binary, {packet, raw}, {active, false}, {nodelay, true}
-                | TcpOpts],
+    SockOpts = sanitize_tcp_opts(TcpOpts),
     {ok, Socket} = gen_tcp:connect(Host, Port, SockOpts),
 
     %% If buffer wasn't specifically defined make it at least as
@@ -166,6 +165,24 @@ connect_socket(#state{tcp_opts = TcpOpts, host = Host, port = Port} = State) ->
 
     {ok, State#state{socket = Socket}}.
 
+sanitize_tcp_opts(TcpOpts0) ->
+    TcpOpts1 = lists:filter(
+        fun
+            ({mode, _}) -> false;
+            (binary) -> false;
+            (list) -> false;
+            ({packet, _}) -> false;
+            ({active, _}) -> false;
+            (_) -> true
+        end,
+        TcpOpts0
+    ),
+    TcpOpts2 = case lists:keymember(nodelay, 1, TcpOpts1) of
+        true -> TcpOpts1;
+        false -> [{nodelay, true} | TcpOpts1]
+    end,
+    [binary, {packet, raw}, {active, false} | TcpOpts2].
+
 handshake(#state{socket = Socket0, ssl_opts = SSLOpts,
         user = User, password = Password, database = Database,
         cap_found_rows = SetFoundRows} = State0) ->