Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "src/core/transport/chttp2/frame_rst_stream.h"
35 : #include "src/core/transport/chttp2/internal.h"
36 :
37 : #include <grpc/support/log.h>
38 :
39 : #include "src/core/transport/chttp2/frame.h"
40 :
41 278996 : gpr_slice grpc_chttp2_rst_stream_create(gpr_uint32 id, gpr_uint32 code) {
42 278996 : gpr_slice slice = gpr_slice_malloc(13);
43 279001 : gpr_uint8 *p = GPR_SLICE_START_PTR(slice);
44 :
45 279001 : *p++ = 0;
46 279001 : *p++ = 0;
47 279001 : *p++ = 4;
48 279001 : *p++ = GRPC_CHTTP2_FRAME_RST_STREAM;
49 279001 : *p++ = 0;
50 279001 : *p++ = (gpr_uint8)(id >> 24);
51 279001 : *p++ = (gpr_uint8)(id >> 16);
52 279001 : *p++ = (gpr_uint8)(id >> 8);
53 279001 : *p++ = (gpr_uint8)(id);
54 279001 : *p++ = (gpr_uint8)(code >> 24);
55 279001 : *p++ = (gpr_uint8)(code >> 16);
56 279001 : *p++ = (gpr_uint8)(code >> 8);
57 279001 : *p++ = (gpr_uint8)(code);
58 :
59 279001 : return slice;
60 : }
61 :
62 278025 : grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_begin_frame(
63 : grpc_chttp2_rst_stream_parser *parser, gpr_uint32 length, gpr_uint8 flags) {
64 278025 : if (length != 4) {
65 0 : gpr_log(GPR_ERROR, "invalid rst_stream: length=%d, flags=%02x", length,
66 : flags);
67 0 : return GRPC_CHTTP2_CONNECTION_ERROR;
68 : }
69 278025 : parser->byte = 0;
70 278025 : return GRPC_CHTTP2_PARSE_OK;
71 : }
72 :
73 5764 : grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_parse(
74 : grpc_exec_ctx *exec_ctx, void *parser,
75 : grpc_chttp2_transport_parsing *transport_parsing,
76 : grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
77 5764 : gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice);
78 5764 : gpr_uint8 *const end = GPR_SLICE_END_PTR(slice);
79 5764 : gpr_uint8 *cur = beg;
80 5764 : grpc_chttp2_rst_stream_parser *p = parser;
81 :
82 34454 : while (p->byte != 4 && cur != end) {
83 22926 : p->reason_bytes[p->byte] = *cur;
84 22926 : cur++;
85 22926 : p->byte++;
86 : }
87 :
88 5764 : if (p->byte == 4) {
89 5731 : GPR_ASSERT(is_last);
90 5731 : stream_parsing->received_close = 1;
91 5731 : stream_parsing->saw_rst_stream = 1;
92 5731 : stream_parsing->rst_stream_reason =
93 11462 : (((gpr_uint32)p->reason_bytes[0]) << 24) |
94 11462 : (((gpr_uint32)p->reason_bytes[1]) << 16) |
95 11462 : (((gpr_uint32)p->reason_bytes[2]) << 8) |
96 5731 : (((gpr_uint32)p->reason_bytes[3]));
97 : }
98 :
99 5764 : return GRPC_CHTTP2_PARSE_OK;
100 : }
|