55
66#include <aws/http/private/websocket_decoder.h>
77
8- /* TODO: decoder logging */
8+ #include <inttypes.h>
99
1010typedef int (state_fn )(struct aws_websocket_decoder * decoder , struct aws_byte_cursor * data );
1111
@@ -46,7 +46,12 @@ static int s_state_opcode_byte(struct aws_websocket_decoder *decoder, struct aws
4646 case AWS_WEBSOCKET_OPCODE_PONG :
4747 break ;
4848 default :
49- return aws_raise_error (AWS_ERROR_HTTP_PROTOCOL_ERROR );
49+ AWS_LOGF_ERROR (
50+ AWS_LS_HTTP_WEBSOCKET ,
51+ "id=%p: Received frame with unknown opcode 0x%" PRIx8 ,
52+ (void * )decoder -> user_data ,
53+ decoder -> current_frame .opcode );
54+ return aws_raise_error (AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR );
5055 }
5156
5257 /* RFC-6455 Section 5.2 Fragmentation
@@ -61,15 +66,23 @@ static int s_state_opcode_byte(struct aws_websocket_decoder *decoder, struct aws
6166 bool is_continuation_frame = AWS_WEBSOCKET_OPCODE_CONTINUATION == decoder -> current_frame .opcode ;
6267
6368 if (decoder -> expecting_continuation_data_frame != is_continuation_frame ) {
64- return aws_raise_error (AWS_ERROR_HTTP_PROTOCOL_ERROR );
69+ AWS_LOGF_ERROR (
70+ AWS_LS_HTTP_WEBSOCKET ,
71+ "id=%p: Fragmentation error. Received start of new message before end of previous message" ,
72+ (void * )decoder -> user_data );
73+ return aws_raise_error (AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR );
6574 }
6675
6776 decoder -> expecting_continuation_data_frame = !decoder -> current_frame .fin ;
6877
6978 } else {
7079 /* Control frames themselves MUST NOT be fragmented. */
7180 if (!decoder -> current_frame .fin ) {
72- return aws_raise_error (AWS_ERROR_HTTP_PROTOCOL_ERROR );
81+ AWS_LOGF_ERROR (
82+ AWS_LS_HTTP_WEBSOCKET ,
83+ "id=%p: Received fragmented control frame. This is illegal" ,
84+ (void * )decoder -> user_data );
85+ return aws_raise_error (AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR );
7386 }
7487 }
7588
@@ -150,21 +163,17 @@ static int s_state_extended_length(struct aws_websocket_decoder *decoder, struct
150163 struct aws_byte_cursor cache_cursor = aws_byte_cursor_from_array (decoder -> state_cache , total_bytes_extended_length );
151164 if (total_bytes_extended_length == 2 ) {
152165 uint16_t val ;
153- if (!aws_byte_cursor_read_be16 (& cache_cursor , & val )) {
154- return aws_raise_error (AWS_ERROR_HTTP_PROTOCOL_ERROR );
155- }
156-
166+ aws_byte_cursor_read_be16 (& cache_cursor , & val );
157167 decoder -> current_frame .payload_length = val ;
158168 } else {
159- if (!aws_byte_cursor_read_be64 (& cache_cursor , & decoder -> current_frame .payload_length )) {
160- return aws_raise_error (AWS_ERROR_HTTP_PROTOCOL_ERROR );
161- }
169+ aws_byte_cursor_read_be64 (& cache_cursor , & decoder -> current_frame .payload_length );
162170 }
163171
164172 if (decoder -> current_frame .payload_length < min_acceptable_value ||
165173 decoder -> current_frame .payload_length > max_acceptable_value ) {
166174
167- return aws_raise_error (AWS_ERROR_HTTP_PROTOCOL_ERROR );
175+ AWS_LOGF_ERROR (AWS_LS_HTTP_WEBSOCKET , "id=%p: Failed to decode payload length" , (void * )decoder -> user_data );
176+ return aws_raise_error (AWS_ERROR_HTTP_WEBSOCKET_PROTOCOL_ERROR );
168177 }
169178
170179 decoder -> state = AWS_WEBSOCKET_DECODER_STATE_MASKING_KEY_CHECK ;
0 commit comments