@@ -57,15 +57,16 @@ func parseClosePayload(p []byte) (code StatusCode, reason string, err error) {
5757 if ! utf8 .ValidString (reason ) {
5858 return 0 , "" , xerrors .Errorf ("invalid utf-8: %q" , reason )
5959 }
60- if ! isValidReceivedCloseCode (code ) {
60+ if ! validCloseCode (code ) {
6161 return 0 , "" , xerrors .Errorf ("invalid code %v" , code )
6262 }
6363
6464 return code , reason , nil
6565}
6666
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
6769var validReceivedCloseCodes = map [StatusCode ]bool {
68- // see http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
6970 StatusNormalClosure : true ,
7071 StatusGoingAway : true ,
7172 StatusProtocolError : true ,
@@ -82,7 +83,7 @@ var validReceivedCloseCodes = map[StatusCode]bool{
8283 StatusTLSHandshake : false ,
8384}
8485
85- func isValidReceivedCloseCode (code StatusCode ) bool {
86+ func validCloseCode (code StatusCode ) bool {
8687 return validReceivedCloseCodes [code ] || (code >= 3000 && code <= 4999 )
8788}
8889
@@ -95,8 +96,8 @@ func closePayload(code StatusCode, reason string) ([]byte, error) {
9596 if bits .Len (uint (code )) > 16 {
9697 return nil , errors .New ("status code is larger than 2 bytes" )
9798 }
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 )
100101 }
101102
102103 buf := make ([]byte , 2 + len (reason ))
0 commit comments