Browse Source

Refactor some code to fix a Dialyzer warning

Loïc Hoguin 6 years ago
parent
commit
0e2e9f3bf6
1 changed files with 35 additions and 23 deletions
  1. 35 23
      src/cow_ws.erl

+ 35 - 23
src/cow_ws.erl

@@ -151,32 +151,44 @@ init_permessage_deflate(InflateWindowBits, DeflateWindowBits, Opts) ->
 		maps:get(mem_level, Opts, 8),
 		maps:get(strategy, Opts, default)),
 	%% Set the owner pid of the zlib contexts if requested.
-	%%
-	%% The zlib port became a reference in OTP 20.1+. There
-	%% was however no way to change the controlling process
-	%% until the OTP 20.1.3 patch version. Since we can't
-	%% enable compression for 20.1, 20.1.1 and 20.1.2 we
-	%% explicitly crash. The caller should ignore this extension.
-	_ = case Opts of
-		#{owner := Pid} when is_port(Inflate) ->
-			true = erlang:port_connect(Inflate, Pid),
-			true = unlink(Inflate),
-			true = erlang:port_connect(Deflate, Pid),
-			unlink(Deflate);
-		#{owner := Pid} ->
-			case erlang:function_exported(zlib, set_controlling_process, 2) of
-				true ->
-					zlib:set_controlling_process(Inflate, Pid),
-					zlib:set_controlling_process(Deflate, Pid);
-				false ->
-					exit({error, incompatible_zlib_version,
-						'OTP 20.1, 20.1.1 and 20.1.2 are missing required functionality.'})
-			end;
-		_ ->
-			true
+	case Opts of
+		#{owner := Pid} -> set_owner(Pid, Inflate, Deflate);
+		_ -> ok
 	end,
 	{Inflate, Deflate}.
 
+-ifdef(OTP_RELEASE).
+%% Using is_port/1 on a zlib context results in a Dialyzer warning in OTP 21.
+%% This function helps silence that warning while staying compatible
+%% with all supported versions.
+
+set_owner(Pid, Inflate, Deflate) ->
+	zlib:set_controlling_process(Inflate, Pid),
+	zlib:set_controlling_process(Deflate, Pid).
+-else.
+%% The zlib port became a reference in OTP 20.1+. There
+%% was however no way to change the controlling process
+%% until the OTP 20.1.3 patch version. Since we can't
+%% enable compression for 20.1, 20.1.1 and 20.1.2 we
+%% explicitly crash. The caller should ignore this extension.
+
+set_owner(Pid, Inflate, Deflate) when is_port(Inflate) ->
+	true = erlang:port_connect(Inflate, Pid),
+	true = unlink(Inflate),
+	true = erlang:port_connect(Deflate, Pid),
+	true = unlink(Deflate),
+	ok;
+set_owner(Pid, Inflate, Deflate) ->
+	case erlang:function_exported(zlib, set_controlling_process, 2) of
+		true ->
+			zlib:set_controlling_process(Inflate, Pid),
+			zlib:set_controlling_process(Deflate, Pid);
+		false ->
+			exit({error, incompatible_zlib_version,
+				'OTP 20.1, 20.1.1 and 20.1.2 are missing required functionality.'})
+	end.
+-endif.
+
 %% @doc Negotiate the x-webkit-deflate-frame extension.
 %%
 %% The implementation is very basic and none of the parameters