Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / 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 #include "src/core/ext/transport/chttp2/transport/internal.h"
23
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/ext/transport/chttp2/transport/frame.h"
29 #include "src/core/lib/gprpp/memory.h"
30 #include "src/core/lib/transport/http2_errors.h"
31
32 grpc_slice grpc_chttp2_rst_stream_create(uint32_t id, uint32_t code,
33                                          grpc_transport_one_way_stats* stats) {
34   static const size_t frame_size = 13;
35   grpc_slice slice = GRPC_SLICE_MALLOC(frame_size);
36   if (stats != nullptr) stats->framing_bytes += frame_size;
37   uint8_t* p = GRPC_SLICE_START_PTR(slice);
38
39   // Frame size.
40   *p++ = 0;
41   *p++ = 0;
42   *p++ = 4;
43   // Frame type.
44   *p++ = GRPC_CHTTP2_FRAME_RST_STREAM;
45   // Flags.
46   *p++ = 0;
47   // Stream ID.
48   *p++ = static_cast<uint8_t>(id >> 24);
49   *p++ = static_cast<uint8_t>(id >> 16);
50   *p++ = static_cast<uint8_t>(id >> 8);
51   *p++ = static_cast<uint8_t>(id);
52   // Error code.
53   *p++ = static_cast<uint8_t>(code >> 24);
54   *p++ = static_cast<uint8_t>(code >> 16);
55   *p++ = static_cast<uint8_t>(code >> 8);
56   *p++ = static_cast<uint8_t>(code);
57
58   return slice;
59 }
60
61 void grpc_chttp2_add_rst_stream_to_next_write(
62     grpc_chttp2_transport* t, uint32_t id, uint32_t code,
63     grpc_transport_one_way_stats* stats) {
64   t->num_pending_induced_frames++;
65   grpc_slice_buffer_add(&t->qbuf,
66                         grpc_chttp2_rst_stream_create(id, code, stats));
67 }
68
69 grpc_error* grpc_chttp2_rst_stream_parser_begin_frame(
70     grpc_chttp2_rst_stream_parser* parser, uint32_t length, uint8_t flags) {
71   if (length != 4) {
72     char* msg;
73     gpr_asprintf(&msg, "invalid rst_stream: length=%d, flags=%02x", length,
74                  flags);
75     grpc_error* err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
76     gpr_free(msg);
77     return err;
78   }
79   parser->byte = 0;
80   return GRPC_ERROR_NONE;
81 }
82
83 grpc_error* grpc_chttp2_rst_stream_parser_parse(void* parser,
84                                                 grpc_chttp2_transport* t,
85                                                 grpc_chttp2_stream* s,
86                                                 const grpc_slice& slice,
87                                                 int is_last) {
88   const uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
89   const uint8_t* const end = GRPC_SLICE_END_PTR(slice);
90   const uint8_t* cur = beg;
91   grpc_chttp2_rst_stream_parser* p =
92       static_cast<grpc_chttp2_rst_stream_parser*>(parser);
93
94   while (p->byte != 4 && cur != end) {
95     p->reason_bytes[p->byte] = *cur;
96     cur++;
97     p->byte++;
98   }
99   s->stats.incoming.framing_bytes += static_cast<uint64_t>(end - cur);
100
101   if (p->byte == 4) {
102     GPR_ASSERT(is_last);
103     uint32_t reason = ((static_cast<uint32_t>(p->reason_bytes[0])) << 24) |
104                       ((static_cast<uint32_t>(p->reason_bytes[1])) << 16) |
105                       ((static_cast<uint32_t>(p->reason_bytes[2])) << 8) |
106                       ((static_cast<uint32_t>(p->reason_bytes[3])));
107     grpc_error* error = GRPC_ERROR_NONE;
108     if (reason != GRPC_HTTP2_NO_ERROR || s->metadata_buffer[1].size == 0) {
109       char* message;
110       gpr_asprintf(&message, "Received RST_STREAM with error code %d", reason);
111       error = grpc_error_set_int(
112           grpc_error_set_str(GRPC_ERROR_CREATE_FROM_STATIC_STRING("RST_STREAM"),
113                              GRPC_ERROR_STR_GRPC_MESSAGE,
114                              grpc_slice_from_moved_string(
115                                  grpc_core::UniquePtr<char>(message))),
116           GRPC_ERROR_INT_HTTP2_ERROR, static_cast<intptr_t>(reason));
117     }
118     grpc_chttp2_mark_stream_closed(t, s, true, true, error);
119   }
120
121   return GRPC_ERROR_NONE;
122 }