From 5fcdc9ca7087baa3b5a6a9d86342606c33335a0a Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 28 Apr 2018 16:23:07 +0200 Subject: [PATCH] do not return Sec-WebSocket-Accept in response if upgrade error (not read yet from Request) + StatusCode getter --- internal/http/upgrade/response/public.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/http/upgrade/response/public.go b/internal/http/upgrade/response/public.go index c906077..e644abb 100644 --- a/internal/http/upgrade/response/public.go +++ b/internal/http/upgrade/response/public.go @@ -23,6 +23,12 @@ func (r *T) SetProtocol(p []byte) { // to the rfc from the Sec-WebSocket-Key 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 */ 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) } - 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 */ raw := []byte(fmt.Sprintf("%s%s\r\n", responseLine, headers)) @@ -63,4 +73,10 @@ func (r T) Send(w io.Writer) (int, error) { // GetProtocol returns the choosen protocol if set, else nil func (r T) GetProtocol() []byte { return r.protocol +} + + +// GetStatusCode returns the response status code +func (r T) GetStatusCode() StatusCode { + return r.code } \ No newline at end of file