do not return Sec-WebSocket-Accept in response if upgrade error (not read yet from Request) + StatusCode getter

This commit is contained in:
xdrm-brackets 2018-04-28 16:23:07 +02:00
parent db2b24ae51
commit 5fcdc9ca70
1 changed files with 17 additions and 1 deletions

View File

@ -23,6 +23,12 @@ func (r *T) SetProtocol(p []byte) {
// to the rfc from the Sec-WebSocket-Key // to the rfc from the Sec-WebSocket-Key
func (r *T) ProcessKey(k []byte) { func (r *T) ProcessKey(k []byte) {
// do nothing for empty key
if k == nil || len(k) == 0 {
r.accept = nil
return
}
/* (1) Concat with constant salt */ /* (1) Concat with constant salt */
mix := append(k, WSSalt...) mix := append(k, WSSalt...)
@ -48,7 +54,11 @@ func (r T) Send(w io.Writer) (int, error) {
optionalProtocol = fmt.Sprintf("Sec-WebSocket-Protocol: %s\r\n", r.protocol) optionalProtocol = fmt.Sprintf("Sec-WebSocket-Protocol: %s\r\n", r.protocol)
} }
headers := fmt.Sprintf("Upgrade: websocket\t\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\nSec-WebSocket-Version: %d\r\n%s", r.accept, WSVersion, optionalProtocol) headers := fmt.Sprintf("Upgrade: websocket\t\nConnection: Upgrade\r\nSec-WebSocket-Version: %d\r\n%s", WSVersion, optionalProtocol)
if r.accept != nil {
headers = fmt.Sprintf("%sSec-WebSocket-Accept: %s\r\n", headers, r.accept)
}
headers = fmt.Sprintf("%s\r\n", headers)
/* (3) Build all */ /* (3) Build all */
raw := []byte(fmt.Sprintf("%s%s\r\n", responseLine, headers)) raw := []byte(fmt.Sprintf("%s%s\r\n", responseLine, headers))
@ -64,3 +74,9 @@ func (r T) Send(w io.Writer) (int, error) {
func (r T) GetProtocol() []byte { func (r T) GetProtocol() []byte {
return r.protocol return r.protocol
} }
// GetStatusCode returns the response status code
func (r T) GetStatusCode() StatusCode {
return r.code
}