-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstatus_codes.go
135 lines (113 loc) · 3.25 KB
/
status_codes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2021-2024 antlabs. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package quickws
import (
"encoding/binary"
"strconv"
"strings"
)
// https://datatracker.ietf.org/doc/html/rfc6455#section-7.4.1
// 这里记录了各种状态码的含义
type StatusCode int16
const (
// NormalClosure 正常关闭
NormalClosure StatusCode = 1000
// EndpointGoingAway 对端正在消失
EndpointGoingAway StatusCode = 1001
// ProtocolError 表示对端由于协议错误正在终止连接
ProtocolError StatusCode = 1002
// DataCannotAccept 收到一个不能接受的数据类型
DataCannotAccept StatusCode = 1003
// NotConsistentMessageType 表示对端正在终止连接, 消息类型不一致
NotConsistentMessageType StatusCode = 1007
// TerminatingConnection 表示对端正在终止连接, 没有好用的错误, 可以用这个错误码表示
TerminatingConnection StatusCode = 1008
// TooBigMessage 消息太大, 不能处理, 关闭连接
TooBigMessage StatusCode = 1009
// NoExtensions 只用于客户端, 服务端返回扩展消息
NoExtensions StatusCode = 1010
// ServerTerminating 服务端遇到意外情况, 中止请求
ServerTerminating StatusCode = 1011
)
func (s StatusCode) String() string {
switch s {
case NormalClosure:
return "NormalClosure"
case EndpointGoingAway:
return "EndpointGoingAway"
case ProtocolError:
return "ProtocolError"
case DataCannotAccept:
return "DataCannotAccept"
case NotConsistentMessageType:
return "NotConsistentMessageType"
case TerminatingConnection:
return "TerminatingConnection"
case TooBigMessage:
return "TooBigMessage"
case NoExtensions:
return "NoExtensions"
case ServerTerminating:
return "ServerTerminating"
}
return "unknown"
}
func (s StatusCode) Error() string {
return s.String()
}
func (s StatusCode) toBytes() (rv []byte) {
rv = make([]byte, 2+len(s.String()))
binary.BigEndian.PutUint16(rv, uint16(s))
copy(rv[2:], s.String())
return
}
type CloseErrMsg struct {
Code StatusCode
Msg string
}
func (c CloseErrMsg) Error() string {
var out strings.Builder
out.WriteString("<quickws close: code:")
out.WriteString(strconv.Itoa(int(c.Code)))
out.WriteString(" msg:")
out.WriteString(c.Code.String())
if len(c.Msg) > 0 {
out.WriteString(c.Msg)
}
out.WriteString(">")
return out.String()
}
func bytesToCloseErrMsg(payload []byte) *CloseErrMsg {
var ce CloseErrMsg
if len(payload) >= 2 {
ce.Code = StatusCode(binary.BigEndian.Uint16(payload))
}
if len(payload) >= 3 {
ce.Msg = string(payload[3:])
}
return &ce
}
func validCode(code uint16) bool {
switch code {
case 1004, 1005, 1006, 1015:
return false
}
if code >= 1000 && code <= 1015 {
return true
}
if code >= 3000 && code <= 4999 {
return true
}
return false
}