@@ -57,15 +57,16 @@ func parseClosePayload(p []byte) (code StatusCode, reason string, err error) {
57
57
if ! utf8 .ValidString (reason ) {
58
58
return 0 , "" , xerrors .Errorf ("invalid utf-8: %q" , reason )
59
59
}
60
- if ! isValidReceivedCloseCode (code ) {
60
+ if ! validCloseCode (code ) {
61
61
return 0 , "" , xerrors .Errorf ("invalid code %v" , code )
62
62
}
63
63
64
64
return code , reason , nil
65
65
}
66
66
67
+ // See http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
68
+ // and https://tools.ietf.org/html/rfc6455#section-7.4.1
67
69
var validReceivedCloseCodes = map [StatusCode ]bool {
68
- // see http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
69
70
StatusNormalClosure : true ,
70
71
StatusGoingAway : true ,
71
72
StatusProtocolError : true ,
@@ -82,7 +83,7 @@ var validReceivedCloseCodes = map[StatusCode]bool{
82
83
StatusTLSHandshake : false ,
83
84
}
84
85
85
- func isValidReceivedCloseCode (code StatusCode ) bool {
86
+ func validCloseCode (code StatusCode ) bool {
86
87
return validReceivedCloseCodes [code ] || (code >= 3000 && code <= 4999 )
87
88
}
88
89
@@ -95,8 +96,8 @@ func closePayload(code StatusCode, reason string) ([]byte, error) {
95
96
if bits .Len (uint (code )) > 16 {
96
97
return nil , errors .New ("status code is larger than 2 bytes" )
97
98
}
98
- if code == StatusNoStatusRcvd || code == StatusAbnormalClosure {
99
- return nil , fmt .Errorf ("status code %v cannot be set by applications " , code )
99
+ if ! validCloseCode ( code ) {
100
+ return nil , fmt .Errorf ("status code %v cannot be set" , code )
100
101
}
101
102
102
103
buf := make ([]byte , 2 + len (reason ))
0 commit comments