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/transport/http2_errors.h"
30
grpc_chttp2_rst_stream_create(uint32_t id,uint32_t code,grpc_transport_one_way_stats * stats)31 grpc_slice grpc_chttp2_rst_stream_create(uint32_t id, uint32_t code,
32 grpc_transport_one_way_stats* stats) {
33 static const size_t frame_size = 13;
34 grpc_slice slice = GRPC_SLICE_MALLOC(frame_size);
35 stats->framing_bytes += frame_size;
36 uint8_t* p = GRPC_SLICE_START_PTR(slice);
37
38 // Frame size.
39 *p++ = 0;
40 *p++ = 0;
41 *p++ = 4;
42 // Frame type.
43 *p++ = GRPC_CHTTP2_FRAME_RST_STREAM;
44 // Flags.
45 *p++ = 0;
46 // Stream ID.
47 *p++ = static_cast<uint8_t>(id >> 24);
48 *p++ = static_cast<uint8_t>(id >> 16);
49 *p++ = static_cast<uint8_t>(id >> 8);
50 *p++ = static_cast<uint8_t>(id);
51 // Error code.
52 *p++ = static_cast<uint8_t>(code >> 24);
53 *p++ = static_cast<uint8_t>(code >> 16);
54 *p++ = static_cast<uint8_t>(code >> 8);
55 *p++ = static_cast<uint8_t>(code);
56
57 return slice;
58 }
59
grpc_chttp2_rst_stream_parser_begin_frame(grpc_chttp2_rst_stream_parser * parser,uint32_t length,uint8_t flags)60 grpc_error* grpc_chttp2_rst_stream_parser_begin_frame(
61 grpc_chttp2_rst_stream_parser* parser, uint32_t length, uint8_t flags) {
62 if (length != 4) {
63 char* msg;
64 gpr_asprintf(&msg, "invalid rst_stream: length=%d, flags=%02x", length,
65 flags);
66 grpc_error* err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
67 gpr_free(msg);
68 return err;
69 }
70 parser->byte = 0;
71 return GRPC_ERROR_NONE;
72 }
73
grpc_chttp2_rst_stream_parser_parse(void * parser,grpc_chttp2_transport * t,grpc_chttp2_stream * s,grpc_slice slice,int is_last)74 grpc_error* grpc_chttp2_rst_stream_parser_parse(void* parser,
75 grpc_chttp2_transport* t,
76 grpc_chttp2_stream* s,
77 grpc_slice slice, int is_last) {
78 uint8_t* const beg = GRPC_SLICE_START_PTR(slice);
79 uint8_t* const end = GRPC_SLICE_END_PTR(slice);
80 uint8_t* cur = beg;
81 grpc_chttp2_rst_stream_parser* p =
82 static_cast<grpc_chttp2_rst_stream_parser*>(parser);
83
84 while (p->byte != 4 && cur != end) {
85 p->reason_bytes[p->byte] = *cur;
86 cur++;
87 p->byte++;
88 }
89 s->stats.incoming.framing_bytes += static_cast<uint64_t>(end - cur);
90
91 if (p->byte == 4) {
92 GPR_ASSERT(is_last);
93 uint32_t reason = ((static_cast<uint32_t>(p->reason_bytes[0])) << 24) |
94 ((static_cast<uint32_t>(p->reason_bytes[1])) << 16) |
95 ((static_cast<uint32_t>(p->reason_bytes[2])) << 8) |
96 ((static_cast<uint32_t>(p->reason_bytes[3])));
97 grpc_error* error = GRPC_ERROR_NONE;
98 if (reason != GRPC_HTTP2_NO_ERROR || s->metadata_buffer[1].size == 0) {
99 char* message;
100 gpr_asprintf(&message, "Received RST_STREAM with error code %d", reason);
101 error = grpc_error_set_int(
102 grpc_error_set_str(GRPC_ERROR_CREATE_FROM_STATIC_STRING("RST_STREAM"),
103 GRPC_ERROR_STR_GRPC_MESSAGE,
104 grpc_slice_from_copied_string(message)),
105 GRPC_ERROR_INT_HTTP2_ERROR, static_cast<intptr_t>(reason));
106 gpr_free(message);
107 }
108 grpc_chttp2_mark_stream_closed(t, s, true, true, error);
109 }
110
111 return GRPC_ERROR_NONE;
112 }
113