Skip to content

Commit 0231b0d

Browse files
authored
transport/grpcframer: create grpcframer package (#7397)
1 parent 2bcbcab commit 0231b0d

File tree

3 files changed

+456
-0
lines changed

3 files changed

+456
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
*
3+
* Copyright 2024 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+
package grpchttp2
20+
21+
import "fmt"
22+
23+
// ErrCode represents an HTTP/2 Error Code. Error codes are 32-bit fields
24+
// that are used in [RST_STREAM] and [GOAWAY] frames to convey the reasons for
25+
// the stream or connection error. See [HTTP/2 Error Code] for definitions of
26+
// each of the following error codes.
27+
//
28+
// [HTTP/2 Error Code]: https://httpwg.org/specs/rfc7540.html#ErrorCodes
29+
// [RST_STREAM]: https://httpwg.org/specs/rfc7540.html#RST_STREAM
30+
// [GOAWAY]: https://httpwg.org/specs/rfc7540.html#GOAWAY
31+
type ErrCode uint32
32+
33+
const (
34+
ErrCodeNoError ErrCode = 0x0
35+
ErrCodeProtocol ErrCode = 0x1
36+
ErrCodeInternal ErrCode = 0x2
37+
ErrCodeFlowControl ErrCode = 0x3
38+
ErrCodeSettingsTimeout ErrCode = 0x4
39+
ErrCodeStreamClosed ErrCode = 0x5
40+
ErrCodeFrameSize ErrCode = 0x6
41+
ErrCodeRefusedStream ErrCode = 0x7
42+
ErrCodeCancel ErrCode = 0x8
43+
ErrCodeCompression ErrCode = 0x9
44+
ErrCodeConnect ErrCode = 0xa
45+
ErrCodeEnhanceYourCalm ErrCode = 0xb
46+
ErrCodeIndaequateSecurity ErrCode = 0xc
47+
ErrCodeHTTP11Required ErrCode = 0xd
48+
)
49+
50+
var errorCodeNames = map[ErrCode]string{
51+
ErrCodeNoError: "NO_ERROR",
52+
ErrCodeProtocol: "PROTOCOL_ERROR",
53+
ErrCodeInternal: "INTERNAL_ERROR",
54+
ErrCodeFlowControl: "FLOW_CONTROL_ERROR",
55+
ErrCodeSettingsTimeout: "SETTINGS_TIMEOUT",
56+
ErrCodeStreamClosed: "STREAM_CLOSED",
57+
ErrCodeFrameSize: "FRAME_SIZE_ERROR",
58+
ErrCodeRefusedStream: "REFUSED_STREAM",
59+
ErrCodeCancel: "CANCEL",
60+
ErrCodeCompression: "COMPRESSION_ERROR",
61+
ErrCodeConnect: "CONNECT_ERROR",
62+
ErrCodeEnhanceYourCalm: "ENHANCE_YOUR_CALM",
63+
ErrCodeIndaequateSecurity: "INADEQUATE_SECURITY",
64+
ErrCodeHTTP11Required: "HTTP_1_1_REQUIRED",
65+
}
66+
67+
func (err ErrCode) String() string {
68+
if v, ok := errorCodeNames[err]; ok {
69+
return v
70+
}
71+
return fmt.Sprintf("unknown error code %#x", uint32(err))
72+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
*
3+
* Copyright 2024 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+
package grpchttp2
20+
21+
import (
22+
"testing"
23+
24+
"google.golang.org/grpc/internal/grpctest"
25+
)
26+
27+
type s struct {
28+
grpctest.Tester
29+
}
30+
31+
func Test(t *testing.T) {
32+
grpctest.RunSubTests(t, s{})
33+
}
34+
35+
func (s) TestErrorCodeString(t *testing.T) {
36+
tests := []struct {
37+
err ErrCode
38+
want string
39+
}{
40+
// Known error cases
41+
{err: ErrCodeNoError, want: "NO_ERROR"},
42+
{err: ErrCodeProtocol, want: "PROTOCOL_ERROR"},
43+
{err: ErrCodeInternal, want: "INTERNAL_ERROR"},
44+
{err: ErrCodeFlowControl, want: "FLOW_CONTROL_ERROR"},
45+
{err: ErrCodeSettingsTimeout, want: "SETTINGS_TIMEOUT"},
46+
{err: ErrCodeStreamClosed, want: "STREAM_CLOSED"},
47+
{err: ErrCodeFrameSize, want: "FRAME_SIZE_ERROR"},
48+
{err: ErrCodeRefusedStream, want: "REFUSED_STREAM"},
49+
{err: ErrCodeCancel, want: "CANCEL"},
50+
{err: ErrCodeCompression, want: "COMPRESSION_ERROR"},
51+
{err: ErrCodeConnect, want: "CONNECT_ERROR"},
52+
{err: ErrCodeEnhanceYourCalm, want: "ENHANCE_YOUR_CALM"},
53+
{err: ErrCodeIndaequateSecurity, want: "INADEQUATE_SECURITY"},
54+
{err: ErrCodeHTTP11Required, want: "HTTP_1_1_REQUIRED"},
55+
// Type casting known error case
56+
{err: ErrCode(0x1), want: "PROTOCOL_ERROR"},
57+
// Unknown error case
58+
{err: ErrCode(0xf), want: "unknown error code 0xf"},
59+
}
60+
61+
for _, test := range tests {
62+
got := test.err.String()
63+
if got != test.want {
64+
t.Errorf("ErrCode.String(%#x) = %q, want %q", int(test.err), got, test.want)
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)