From 0e2e9f3bf6d5059f8af983ee38da94d7e5e47fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 3 Aug 2018 08:17:50 +0200 Subject: Refactor some code to fix a Dialyzer warning --- src/cow_ws.erl | 58 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/cow_ws.erl b/src/cow_ws.erl index 48e161d..3d3807f 100644 --- a/src/cow_ws.erl +++ b/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 -- cgit v1.2.3