Debian Codegraph @

crt/aws-c-http/include/aws/http/private/websocket_decoder.h

raw symbols SCIP
1 #ifndef AWS_HTTP_WEBSOCKET_DECODER_H
2 #define AWS_HTTP_WEBSOCKET_DECODER_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/http/private/websocket_impl.h>
10
11 /* Called when the non-payload portion of a frame has been decoded. */
12 typedef int(aws_websocket_decoder_frame_fn)(const struct aws_websocket_frame *frame, void *user_data);
13
14 /* Called repeatedly as the payload is decoded. If a mask was used, the data has been unmasked. */
15 typedef int(aws_websocket_decoder_payload_fn)(struct aws_byte_cursor data, void *user_data);
16
17 /**
18 * Each state consumes data and/or moves decoder to a subsequent state.
19 */
20 enum aws_websocket_decoder_state {
21 AWS_WEBSOCKET_DECODER_STATE_INIT,
22 AWS_WEBSOCKET_DECODER_STATE_OPCODE_BYTE,
23 AWS_WEBSOCKET_DECODER_STATE_LENGTH_BYTE,
24 AWS_WEBSOCKET_DECODER_STATE_EXTENDED_LENGTH,
25 AWS_WEBSOCKET_DECODER_STATE_MASKING_KEY_CHECK,
26 AWS_WEBSOCKET_DECODER_STATE_MASKING_KEY,
27 AWS_WEBSOCKET_DECODER_STATE_PAYLOAD_CHECK,
28 AWS_WEBSOCKET_DECODER_STATE_PAYLOAD,
29 AWS_WEBSOCKET_DECODER_STATE_FRAME_END,
30 AWS_WEBSOCKET_DECODER_STATE_DONE,
31 };
32
33 struct aws_websocket_decoder {
34 enum aws_websocket_decoder_state state;
35 uint64_t state_bytes_processed; /* For multi-byte states, the number of bytes processed so far */
36 uint8_t state_cache[8]; /* For multi-byte states to cache data that might be split across packets */
37
38 struct aws_websocket_frame current_frame; /* Data about current frame being decoded */
39
40 bool expecting_continuation_data_frame; /* True when the next data frame must be CONTINUATION frame */
41
42 /* True while processing a TEXT "message" (from the start of a TEXT frame,
43 * until the end of the TEXT or CONTINUATION frame with the FIN bit set). */
44 bool processing_text_message;
45 struct aws_utf8_decoder *text_message_validator;
46
47 void *user_data;
48 aws_websocket_decoder_frame_fn *on_frame;
49 aws_websocket_decoder_payload_fn *on_payload;
50 };
51
52 AWS_EXTERN_C_BEGIN
53
54 AWS_HTTP_API
55 void aws_websocket_decoder_init(
56 struct aws_websocket_decoder *decoder,
57 struct aws_allocator *alloc,
58 aws_websocket_decoder_frame_fn *on_frame,
59 aws_websocket_decoder_payload_fn *on_payload,
60 void *user_data);
61
62 AWS_HTTP_API
63 void aws_websocket_decoder_clean_up(struct aws_websocket_decoder *decoder);
64
65 /**
66 * Returns when all data is processed, or a frame and its payload have completed.
67 * `data` will be advanced to reflect the amount of data processed by this call.
68 * `frame_complete` will be set true if this call returned due to completion of a frame.
69 * The `on_frame` and `on_payload` callbacks may each be invoked once as a result of this call.
70 * If an error occurs, the decoder is invalid forevermore.
71 */
72 AWS_HTTP_API int aws_websocket_decoder_process(
73 struct aws_websocket_decoder *decoder,
74 struct aws_byte_cursor *data,
75 bool *frame_complete);
76
77 AWS_EXTERN_C_END
78
79 #endif /* AWS_HTTP_WEBSOCKET_DECODER_H */