From faa57b16eba29a2609c23bbe01d42f6d37a1d511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sat, 5 Oct 2019 16:40:29 +0200 Subject: Allow disabling the UTF-8 Websocket text frames validation --- src/cow_ws.erl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/cow_ws.erl b/src/cow_ws.erl index d24dc12..3bb46c5 100644 --- a/src/cow_ws.erl +++ b/src/cow_ws.erl @@ -69,7 +69,7 @@ -type rsv() :: <<_:3>>. -export_type([rsv/0]). --type utf8_state() :: 0..8. +-type utf8_state() :: 0..8 | undefined. -export_type([utf8_state/0]). %% @doc Generate a key for the Websocket handshake request. @@ -466,7 +466,8 @@ parse_payload(Data, MaskKey, Utf8State, ParsedLen, Type, Len, FragState, Payload = inflate_frame(unmask(Data2, MaskKey, ParsedLen), Inflate, TakeOver, FragState, Eof), validate_payload(Payload, Rest, Utf8State, ParsedLen, Type, FragState, Eof); %% Empty frame. -parse_payload(Data, _, Utf8State = 0, 0, _, 0, _, _, _) -> +parse_payload(Data, _, Utf8State, 0, _, 0, _, _, _) + when Utf8State =:= 0; Utf8State =:= undefined -> {ok, <<>>, Utf8State, Data}; %% Start of close frame. parse_payload(Data, MaskKey, Utf8State, 0, Type = close, Len, FragState, _, << 0:3 >>) -> @@ -551,6 +552,11 @@ inflate_frame(Data, Inflate, TakeOver, FragState, true) inflate_frame(Data, Inflate, _T, _F, _E) -> iolist_to_binary(zlib:inflate(Inflate, Data)). +%% The Utf8State variable can be set to 'undefined' to disable the validation. +validate_payload(Payload, _, undefined, _, _, _, false) -> + {more, Payload, undefined}; +validate_payload(Payload, Rest, undefined, _, _, _, true) -> + {ok, Payload, undefined, Rest}; %% Text frames and close control frames MUST have a payload that is valid UTF-8. validate_payload(Payload, Rest, Utf8State, _, Type, _, Eof) when Type =:= text; Type =:= close -> case validate_utf8(Payload, Utf8State) of -- cgit v1.2.3