Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / ext / transport / chttp2 / transport / frame_rst_stream.cc
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/ext/transport/chttp2/transport/frame_rst_stream.h"
22
23 #include "absl/strings/str_cat.h"
24 #include "absl/strings/str_format.h"
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/ext/transport/chttp2/transport/frame.h"
30 #include "src/core/ext/transport/chttp2/transport/internal.h"
31 #include "src/core/lib/gprpp/memory.h"
32 #include "src/core/lib/transport/http2_errors.h"
33
34 grpc_slice grpc_chttp2_rst_stream_create(uint32_t id, uint32_t code,
35                                          grpc_transport_one_way_stats* stats) {
36   static const size_t frame_size = 13;
37   grpc_slice slice = GRPC_SLICE_MALLOC(frame_size);
38   if (stats != nullptr) stats->framing_bytes += frame_size;
39   uint8_t* p = GRPC_SLICE_START_PTR(slice);
40
41   // Frame size.
42   *p++ = 0;
43   *p++ = 0;
44   *p++ = 4;
45   // Frame type.
46   *p++ = GRPC_CHTTP2_FRAME_RST_STREAM;
47   // Flags.
48   *p++ = 0;
49   // Stream ID.
50   *p++ = static_cast<uint8_t>(id >> 24);
51   *p++ = static_cast<uint8_t>(id >> 16);
52   *p++ = static_cast<uint8_t>(id >> 8);
53   *p++ = static_cast<uint8_t>(id);
54   // Error code.
55   *p++ = static_cast<uint8_t>(code >> 24);
56   *p++ = static_cast<uint8_t>(code >> 16);
57   *p++ = static_cast<uint8_t>(code >> 8);
58   *p++ = static_cast<uint8_t>(code);
59
60   return slice;
61 }
62
63 void grpc_chttp2_add_rst_stream_to_next_write(
64     grpc_chttp2_transport* t, uint32_t id, uint32_t code,
65     grpc_transport_one_way_stats* stats) {
66   t->num_pending_induced_frames++;
67   grpc_slice_buffer_add(&t->qbuf,
68                         grpc_chttp2_rst_stream_create(id, code, stats));
69 }
70
71 grpc_error_handle grpc_chttp2_rst_stream_parser_begin_frame(
72     grpc_chttp2_rst_stream_parser* parser, uint32_t length, uint8_t flags) {
73   if (length != 4) {
74     return GRPC_ERROR_CREATE_FROM_CPP_STRING(absl::StrFormat(
75         "invalid rst_stream: length=%d, flags=%02x", length, flags));
76   }
77   parser->byte = 0;
78   return GRPC_ERROR_NONE;
79 }
80
81 grpc_error_handle grpc_chttp2_rst_stream_parser_parse(void* parser,
82                                                       grpc_chttp2_transport* t,
83                                                       grpc_chttp2_stream* s,
84                                                       const grpc_slice& slice,
85                                                       int is_last) {
86   const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
87   const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
88   const uint8_t* cur = beg;
89   grpc_chttp2_rst_stream_parser* p =
90       static_cast<grpc_chttp2_rst_stream_parser*>(parser);
91
92   while (p->byte != 4 && cur != end) {
93     p->reason_bytes[p->byte] = *cur;
94     cur++;
95     p->byte++;
96   }
97   s->stats.incoming.framing_bytes += static_cast<uint64_t>(end - cur);
98
99   if (p->byte == 4) {
100     GPR_ASSERT(is_last);
101     uint32_t reason = ((static_cast<uint32_t>(p->reason_bytes[0])) << 24) |
102                       ((static_cast<uint32_t>(p->reason_bytes[1])) << 16) |
103                       ((static_cast<uint32_t>(p->reason_bytes[2])) << 8) |
104                       ((static_cast<uint32_t>(p->reason_bytes[3])));
105     grpc_error_handle error = GRPC_ERROR_NONE;
106     if (reason != GRPC_HTTP2_NO_ERROR || s->metadata_buffer[1].size == 0) {
107       error = grpc_error_set_int(
108           grpc_error_set_str(
109               GRPC_ERROR_CREATE_FROM_STATIC_STRING("RST_STREAM"),
110               GRPC_ERROR_STR_GRPC_MESSAGE,
111               grpc_slice_from_cpp_string(absl::StrCat(
112                   "Received RST_STREAM with error code ", reason))),
113           GRPC_ERROR_INT_HTTP2_ERROR, static_cast<intptr_t>(reason));
114     }
115     grpc_chttp2_mark_stream_closed(t, s, true, true, error);
116   }
117
118   return GRPC_ERROR_NONE;
119 }