diff options
Diffstat (limited to 'src/core/transport/chttp2/frame_data.c')
-rw-r--r-- | src/core/transport/chttp2/frame_data.c | 215 |
1 files changed, 101 insertions, 114 deletions
diff --git a/src/core/transport/chttp2/frame_data.c b/src/core/transport/chttp2/frame_data.c index a806e357a8..99414523d5 100644 --- a/src/core/transport/chttp2/frame_data.c +++ b/src/core/transport/chttp2/frame_data.c @@ -42,146 +42,133 @@ #include <grpc/support/useful.h> #include "src/core/transport/transport.h" -grpc_chttp2_parse_error -grpc_chttp2_data_parser_init (grpc_chttp2_data_parser * parser) -{ +grpc_chttp2_parse_error grpc_chttp2_data_parser_init( + grpc_chttp2_data_parser *parser) { parser->state = GRPC_CHTTP2_DATA_FH_0; - grpc_sopb_init (&parser->incoming_sopb); + grpc_sopb_init(&parser->incoming_sopb); return GRPC_CHTTP2_PARSE_OK; } -void -grpc_chttp2_data_parser_destroy (grpc_chttp2_data_parser * parser) -{ - grpc_sopb_destroy (&parser->incoming_sopb); +void grpc_chttp2_data_parser_destroy(grpc_chttp2_data_parser *parser) { + grpc_sopb_destroy(&parser->incoming_sopb); } -grpc_chttp2_parse_error -grpc_chttp2_data_parser_begin_frame (grpc_chttp2_data_parser * parser, gpr_uint8 flags) -{ - if (flags & ~GRPC_CHTTP2_DATA_FLAG_END_STREAM) - { - gpr_log (GPR_ERROR, "unsupported data flags: 0x%02x", flags); - return GRPC_CHTTP2_STREAM_ERROR; - } +grpc_chttp2_parse_error grpc_chttp2_data_parser_begin_frame( + grpc_chttp2_data_parser *parser, gpr_uint8 flags) { + if (flags & ~GRPC_CHTTP2_DATA_FLAG_END_STREAM) { + gpr_log(GPR_ERROR, "unsupported data flags: 0x%02x", flags); + return GRPC_CHTTP2_STREAM_ERROR; + } - if (flags & GRPC_CHTTP2_DATA_FLAG_END_STREAM) - { - parser->is_last_frame = 1; - } - else - { - parser->is_last_frame = 0; - } + if (flags & GRPC_CHTTP2_DATA_FLAG_END_STREAM) { + parser->is_last_frame = 1; + } else { + parser->is_last_frame = 0; + } return GRPC_CHTTP2_PARSE_OK; } -grpc_chttp2_parse_error -grpc_chttp2_data_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last) -{ - gpr_uint8 *const beg = GPR_SLICE_START_PTR (slice); - gpr_uint8 *const end = GPR_SLICE_END_PTR (slice); +grpc_chttp2_parse_error grpc_chttp2_data_parser_parse( + grpc_exec_ctx *exec_ctx, void *parser, + grpc_chttp2_transport_parsing *transport_parsing, + grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) { + gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice); + gpr_uint8 *const end = GPR_SLICE_END_PTR(slice); gpr_uint8 *cur = beg; grpc_chttp2_data_parser *p = parser; gpr_uint32 message_flags = 0; - if (is_last && p->is_last_frame) - { - stream_parsing->received_close = 1; - } + if (is_last && p->is_last_frame) { + stream_parsing->received_close = 1; + } - if (cur == end) - { - return GRPC_CHTTP2_PARSE_OK; - } + if (cur == end) { + return GRPC_CHTTP2_PARSE_OK; + } - switch (p->state) - { - fh_0: + switch (p->state) { + fh_0: case GRPC_CHTTP2_DATA_FH_0: p->frame_type = *cur; - switch (p->frame_type) - { - case 0: - p->is_frame_compressed = 0; /* GPR_FALSE */ - break; - case 1: - p->is_frame_compressed = 1; /* GPR_TRUE */ - break; - default: - gpr_log (GPR_ERROR, "Bad GRPC frame type 0x%02x", p->frame_type); - return GRPC_CHTTP2_STREAM_ERROR; - } - if (++cur == end) - { - p->state = GRPC_CHTTP2_DATA_FH_1; - return GRPC_CHTTP2_PARSE_OK; - } - /* fallthrough */ + switch (p->frame_type) { + case 0: + p->is_frame_compressed = 0; /* GPR_FALSE */ + break; + case 1: + p->is_frame_compressed = 1; /* GPR_TRUE */ + break; + default: + gpr_log(GPR_ERROR, "Bad GRPC frame type 0x%02x", p->frame_type); + return GRPC_CHTTP2_STREAM_ERROR; + } + if (++cur == end) { + p->state = GRPC_CHTTP2_DATA_FH_1; + return GRPC_CHTTP2_PARSE_OK; + } + /* fallthrough */ case GRPC_CHTTP2_DATA_FH_1: - p->frame_size = ((gpr_uint32) * cur) << 24; - if (++cur == end) - { - p->state = GRPC_CHTTP2_DATA_FH_2; - return GRPC_CHTTP2_PARSE_OK; - } - /* fallthrough */ + p->frame_size = ((gpr_uint32)*cur) << 24; + if (++cur == end) { + p->state = GRPC_CHTTP2_DATA_FH_2; + return GRPC_CHTTP2_PARSE_OK; + } + /* fallthrough */ case GRPC_CHTTP2_DATA_FH_2: - p->frame_size |= ((gpr_uint32) * cur) << 16; - if (++cur == end) - { - p->state = GRPC_CHTTP2_DATA_FH_3; - return GRPC_CHTTP2_PARSE_OK; - } - /* fallthrough */ + p->frame_size |= ((gpr_uint32)*cur) << 16; + if (++cur == end) { + p->state = GRPC_CHTTP2_DATA_FH_3; + return GRPC_CHTTP2_PARSE_OK; + } + /* fallthrough */ case GRPC_CHTTP2_DATA_FH_3: - p->frame_size |= ((gpr_uint32) * cur) << 8; - if (++cur == end) - { - p->state = GRPC_CHTTP2_DATA_FH_4; - return GRPC_CHTTP2_PARSE_OK; - } - /* fallthrough */ + p->frame_size |= ((gpr_uint32)*cur) << 8; + if (++cur == end) { + p->state = GRPC_CHTTP2_DATA_FH_4; + return GRPC_CHTTP2_PARSE_OK; + } + /* fallthrough */ case GRPC_CHTTP2_DATA_FH_4: - p->frame_size |= ((gpr_uint32) * cur); + p->frame_size |= ((gpr_uint32)*cur); p->state = GRPC_CHTTP2_DATA_FRAME; ++cur; - if (p->is_frame_compressed) - { - message_flags |= GRPC_WRITE_INTERNAL_COMPRESS; - } - grpc_sopb_add_begin_message (&p->incoming_sopb, p->frame_size, message_flags); - /* fallthrough */ + if (p->is_frame_compressed) { + message_flags |= GRPC_WRITE_INTERNAL_COMPRESS; + } + grpc_sopb_add_begin_message(&p->incoming_sopb, p->frame_size, + message_flags); + /* fallthrough */ case GRPC_CHTTP2_DATA_FRAME: - if (cur == end) - { - grpc_chttp2_list_add_parsing_seen_stream (transport_parsing, stream_parsing); - return GRPC_CHTTP2_PARSE_OK; - } - grpc_chttp2_list_add_parsing_seen_stream (transport_parsing, stream_parsing); - if ((gpr_uint32) (end - cur) == p->frame_size) - { - grpc_sopb_add_slice (&p->incoming_sopb, gpr_slice_sub (slice, (size_t) (cur - beg), (size_t) (end - beg))); - p->state = GRPC_CHTTP2_DATA_FH_0; - return GRPC_CHTTP2_PARSE_OK; - } - else if ((gpr_uint32) (end - cur) > p->frame_size) - { - grpc_sopb_add_slice (&p->incoming_sopb, gpr_slice_sub (slice, (size_t) (cur - beg), (size_t) (cur + p->frame_size - beg))); - cur += p->frame_size; - goto fh_0; /* loop */ - } - else - { - grpc_sopb_add_slice (&p->incoming_sopb, gpr_slice_sub (slice, (size_t) (cur - beg), (size_t) (end - beg))); - GPR_ASSERT (end - cur <= p->frame_size); - p->frame_size -= (gpr_uint32) (end - cur); - return GRPC_CHTTP2_PARSE_OK; - } - } + if (cur == end) { + grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, + stream_parsing); + return GRPC_CHTTP2_PARSE_OK; + } + grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, + stream_parsing); + if ((gpr_uint32)(end - cur) == p->frame_size) { + grpc_sopb_add_slice( + &p->incoming_sopb, + gpr_slice_sub(slice, (size_t)(cur - beg), (size_t)(end - beg))); + p->state = GRPC_CHTTP2_DATA_FH_0; + return GRPC_CHTTP2_PARSE_OK; + } else if ((gpr_uint32)(end - cur) > p->frame_size) { + grpc_sopb_add_slice(&p->incoming_sopb, + gpr_slice_sub(slice, (size_t)(cur - beg), + (size_t)(cur + p->frame_size - beg))); + cur += p->frame_size; + goto fh_0; /* loop */ + } else { + grpc_sopb_add_slice( + &p->incoming_sopb, + gpr_slice_sub(slice, (size_t)(cur - beg), (size_t)(end - beg))); + GPR_ASSERT(end - cur <= p->frame_size); + p->frame_size -= (gpr_uint32)(end - cur); + return GRPC_CHTTP2_PARSE_OK; + } + } - gpr_log (GPR_ERROR, "should never reach here"); - abort (); + gpr_log(GPR_ERROR, "should never reach here"); + abort(); return GRPC_CHTTP2_CONNECTION_ERROR; } |