@@ -11,7 +11,7 @@ use self::{
1111 coding:: { CloseCode , Control as OpCtl , Data as OpData , OpCode } ,
1212 Frame , FrameCodec ,
1313 } ,
14- message:: { IncompleteMessage , IncompleteMessageType } ,
14+ message:: { IncompleteMessage , MessageType } ,
1515} ;
1616use crate :: {
1717 error:: { CapacityError , Error , ProtocolError , Result } ,
@@ -679,44 +679,35 @@ impl WebSocketContext {
679679
680680 OpCode :: Data ( data) => {
681681 let fin = frame. header ( ) . is_final ;
682- match data {
683- OpData :: Continue => {
684- let msg = self
685- . incomplete
686- . as_mut ( )
687- . ok_or ( Error :: Protocol ( ProtocolError :: UnexpectedContinueFrame ) ) ?;
688- msg. extend ( frame. into_payload ( ) , self . config . max_message_size ) ?;
689- if fin {
690- Ok ( Some ( self . incomplete . take ( ) . unwrap ( ) . complete ( ) ?) )
691- } else {
692- Ok ( None )
693- }
694- }
695- c if self . incomplete . is_some ( ) => {
696- Err ( Error :: Protocol ( ProtocolError :: ExpectedFragment ( c) ) )
697- }
698- OpData :: Text if fin => {
699- check_max_size ( frame. payload ( ) . len ( ) , self . config . max_message_size ) ?;
700- Ok ( Some ( Message :: Text ( frame. into_text ( ) ?) ) )
682+
683+ let payload = match ( data, self . incomplete . as_mut ( ) ) {
684+ ( OpData :: Continue , None ) => Err ( ProtocolError :: UnexpectedContinueFrame ) ,
685+ ( OpData :: Continue , Some ( incomplete) ) => {
686+ incomplete. extend ( frame. into_payload ( ) , self . config . max_message_size ) ?;
687+ Ok ( None )
701688 }
702- OpData :: Binary if fin => {
703- check_max_size ( frame. payload ( ) . len ( ) , self . config . max_message_size ) ?;
704- Ok ( Some ( Message :: Binary ( frame. into_payload ( ) ) ) )
689+ ( _, Some ( _) ) => Err ( ProtocolError :: ExpectedFragment ( data) ) ,
690+ ( OpData :: Text , _) => Ok ( Some ( ( frame. into_payload ( ) , MessageType :: Text ) ) ) ,
691+ ( OpData :: Binary , _) => Ok ( Some ( ( frame. into_payload ( ) , MessageType :: Binary ) ) ) ,
692+ ( OpData :: Reserved ( i) , _) => Err ( ProtocolError :: UnknownDataFrameType ( i) ) ,
693+ } ?;
694+
695+ match ( payload, fin) {
696+ ( None , true ) => Ok ( Some ( self . incomplete . take ( ) . unwrap ( ) . complete ( ) ?) ) ,
697+ ( None , false ) => Ok ( None ) ,
698+ ( Some ( ( payload, t) ) , true ) => {
699+ check_max_size ( payload. len ( ) , self . config . max_message_size ) ?;
700+ match t {
701+ MessageType :: Text => Ok ( Some ( Message :: Text ( payload. try_into ( ) ?) ) ) ,
702+ MessageType :: Binary => Ok ( Some ( Message :: Binary ( payload) ) ) ,
703+ }
705704 }
706- OpData :: Text | OpData :: Binary => {
707- let message_type = match data {
708- OpData :: Text => IncompleteMessageType :: Text ,
709- OpData :: Binary => IncompleteMessageType :: Binary ,
710- _ => panic ! ( "Bug: message is not text nor binary" ) ,
711- } ;
712- let mut incomplete = IncompleteMessage :: new ( message_type) ;
713- incomplete. extend ( frame. into_payload ( ) , self . config . max_message_size ) ?;
705+ ( Some ( ( payload, t) ) , false ) => {
706+ let mut incomplete = IncompleteMessage :: new ( t) ;
707+ incomplete. extend ( payload, self . config . max_message_size ) ?;
714708 self . incomplete = Some ( incomplete) ;
715709 Ok ( None )
716710 }
717- OpData :: Reserved ( i) => {
718- Err ( Error :: Protocol ( ProtocolError :: UnknownDataFrameType ( i) ) )
719- }
720711 }
721712 }
722713 } // match opcode
0 commit comments